290 lines
7.3 KiB
Go
290 lines
7.3 KiB
Go
// Package lxdfake is an in-memory LXD for unit tests of labd. It enforces the
|
|
// rules of LXD that the provisioner depends on: an instance is copied only
|
|
// from a known snapshot, its NICs need existing networks, a running instance
|
|
// cannot be deleted, and a network in use cannot be deleted.
|
|
package lxdfake
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"maps"
|
|
"slices"
|
|
"sync"
|
|
|
|
"lincodex/labd/internal/lxd"
|
|
)
|
|
|
|
// ErrInjected is the error of an injected failure.
|
|
var ErrInjected = errors.New("injected lxd failure")
|
|
|
|
// Instance is the fake state of an instance.
|
|
type Instance struct {
|
|
Spec lxd.InstanceSpec
|
|
Running bool
|
|
}
|
|
|
|
// LXD is the fake. It is safe for concurrent use.
|
|
type LXD struct {
|
|
mu sync.Mutex
|
|
sources map[string]struct{}
|
|
instances map[string]*Instance
|
|
networks map[string]lxd.NetworkSpec
|
|
// external networks exist but were not created through the fake, such
|
|
// as the management bridge.
|
|
external map[string]struct{}
|
|
// failCreateInstance, when set, is called before an instance is created
|
|
// and fails the call with its error.
|
|
failCreateInstance func(name string) error
|
|
failDelete bool
|
|
unreachable bool
|
|
}
|
|
|
|
// New returns a fake with the given prepared snapshots and pre-existing
|
|
// networks.
|
|
func New(sources []string, networks ...string) *LXD {
|
|
fake := &LXD{
|
|
sources: make(map[string]struct{}),
|
|
instances: make(map[string]*Instance),
|
|
networks: make(map[string]lxd.NetworkSpec),
|
|
external: make(map[string]struct{}),
|
|
}
|
|
for _, source := range sources {
|
|
fake.sources[source] = struct{}{}
|
|
}
|
|
for _, network := range networks {
|
|
fake.external[network] = struct{}{}
|
|
}
|
|
return fake
|
|
}
|
|
|
|
// FailCreateInstance makes CreateInstance fail for the names that fail
|
|
// returns an error for.
|
|
func (f *LXD) FailCreateInstance(fail func(name string) error) {
|
|
f.mu.Lock()
|
|
defer f.mu.Unlock()
|
|
f.failCreateInstance = fail
|
|
}
|
|
|
|
// FailDeletes makes every delete fail until it is switched off.
|
|
func (f *LXD) FailDeletes(fail bool) {
|
|
f.mu.Lock()
|
|
defer f.mu.Unlock()
|
|
f.failDelete = fail
|
|
}
|
|
|
|
// SetUnreachable makes every call fail as if the socket were gone.
|
|
func (f *LXD) SetUnreachable(unreachable bool) {
|
|
f.mu.Lock()
|
|
defer f.mu.Unlock()
|
|
f.unreachable = unreachable
|
|
}
|
|
|
|
// AddInstance puts an instance into the fake without labd, as an operator or
|
|
// a crashed labd would leave it.
|
|
func (f *LXD) AddInstance(spec lxd.InstanceSpec, running bool) {
|
|
f.mu.Lock()
|
|
defer f.mu.Unlock()
|
|
f.instances[spec.Name] = &Instance{Spec: spec, Running: running}
|
|
}
|
|
|
|
// AddNetwork puts a managed network into the fake without labd.
|
|
func (f *LXD) AddNetwork(spec lxd.NetworkSpec) {
|
|
f.mu.Lock()
|
|
defer f.mu.Unlock()
|
|
f.networks[spec.Name] = spec
|
|
}
|
|
|
|
// Instances returns a copy of every instance by name.
|
|
func (f *LXD) Instances() map[string]Instance {
|
|
f.mu.Lock()
|
|
defer f.mu.Unlock()
|
|
result := make(map[string]Instance, len(f.instances))
|
|
for name, instance := range f.instances {
|
|
result[name] = *instance
|
|
}
|
|
return result
|
|
}
|
|
|
|
// Networks returns a copy of every network created in the fake by name.
|
|
func (f *LXD) Networks() map[string]lxd.NetworkSpec {
|
|
f.mu.Lock()
|
|
defer f.mu.Unlock()
|
|
return maps.Clone(f.networks)
|
|
}
|
|
|
|
func (f *LXD) check() error {
|
|
if f.unreachable {
|
|
return fmt.Errorf("lxdfake: dial: %w", ErrInjected)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (f *LXD) Ping(context.Context) error {
|
|
f.mu.Lock()
|
|
defer f.mu.Unlock()
|
|
return f.check()
|
|
}
|
|
|
|
func (f *LXD) ListInstances(context.Context) ([]lxd.Instance, error) {
|
|
f.mu.Lock()
|
|
defer f.mu.Unlock()
|
|
if err := f.check(); err != nil {
|
|
return nil, err
|
|
}
|
|
names := slices.Sorted(maps.Keys(f.instances))
|
|
result := make([]lxd.Instance, 0, len(names))
|
|
for _, name := range names {
|
|
instance := f.instances[name]
|
|
status := "Stopped"
|
|
if instance.Running {
|
|
status = "Running"
|
|
}
|
|
result = append(result, lxd.Instance{Name: name, Status: status, Config: maps.Clone(instance.Spec.Config)})
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
func (f *LXD) CreateInstance(_ context.Context, spec lxd.InstanceSpec) error {
|
|
f.mu.Lock()
|
|
defer f.mu.Unlock()
|
|
if err := f.check(); err != nil {
|
|
return err
|
|
}
|
|
if f.failCreateInstance != nil {
|
|
if err := f.failCreateInstance(spec.Name); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
if _, exists := f.instances[spec.Name]; exists {
|
|
return &lxd.APIError{Code: 409, Message: "instance exists"}
|
|
}
|
|
if _, known := f.sources[spec.Source]; !known {
|
|
return fmt.Errorf("lxdfake: source %s: %w", spec.Source, lxd.ErrNotFound)
|
|
}
|
|
for device, config := range spec.Devices {
|
|
if config["type"] != "nic" {
|
|
continue
|
|
}
|
|
if !f.networkExists(config["network"]) {
|
|
return &lxd.APIError{Code: 400, Message: "device " + device + " names an unknown network"}
|
|
}
|
|
}
|
|
f.instances[spec.Name] = &Instance{Spec: spec}
|
|
return nil
|
|
}
|
|
|
|
func (f *LXD) networkExists(name string) bool {
|
|
_, managed := f.networks[name]
|
|
_, external := f.external[name]
|
|
return managed || external
|
|
}
|
|
|
|
func (f *LXD) InstanceRunning(_ context.Context, name string) (bool, error) {
|
|
f.mu.Lock()
|
|
defer f.mu.Unlock()
|
|
if err := f.check(); err != nil {
|
|
return false, err
|
|
}
|
|
instance, ok := f.instances[name]
|
|
if !ok {
|
|
return false, lxd.ErrNotFound
|
|
}
|
|
return instance.Running, nil
|
|
}
|
|
|
|
func (f *LXD) StartInstance(_ context.Context, name string) error {
|
|
return f.setRunning(name, true)
|
|
}
|
|
|
|
func (f *LXD) StopInstance(_ context.Context, name string) error {
|
|
return f.setRunning(name, false)
|
|
}
|
|
|
|
func (f *LXD) setRunning(name string, running bool) error {
|
|
f.mu.Lock()
|
|
defer f.mu.Unlock()
|
|
if err := f.check(); err != nil {
|
|
return err
|
|
}
|
|
instance, ok := f.instances[name]
|
|
if !ok {
|
|
return lxd.ErrNotFound
|
|
}
|
|
instance.Running = running
|
|
return nil
|
|
}
|
|
|
|
func (f *LXD) DeleteInstance(_ context.Context, name string) error {
|
|
f.mu.Lock()
|
|
defer f.mu.Unlock()
|
|
if err := f.check(); err != nil {
|
|
return err
|
|
}
|
|
if f.failDelete {
|
|
return fmt.Errorf("lxdfake: delete %s: %w", name, ErrInjected)
|
|
}
|
|
instance, ok := f.instances[name]
|
|
if !ok {
|
|
return lxd.ErrNotFound
|
|
}
|
|
if instance.Running {
|
|
return &lxd.APIError{Code: 400, Message: "instance is running"}
|
|
}
|
|
delete(f.instances, name)
|
|
return nil
|
|
}
|
|
|
|
func (f *LXD) ListNetworks(context.Context) ([]lxd.Network, error) {
|
|
f.mu.Lock()
|
|
defer f.mu.Unlock()
|
|
if err := f.check(); err != nil {
|
|
return nil, err
|
|
}
|
|
names := slices.Sorted(maps.Keys(f.networks))
|
|
result := make([]lxd.Network, 0, len(names))
|
|
for _, name := range names {
|
|
result = append(result, lxd.Network{Name: name, Managed: true, Config: maps.Clone(f.networks[name].Config)})
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
func (f *LXD) CreateNetwork(_ context.Context, spec lxd.NetworkSpec) error {
|
|
f.mu.Lock()
|
|
defer f.mu.Unlock()
|
|
if err := f.check(); err != nil {
|
|
return err
|
|
}
|
|
if len(spec.Name) > 15 {
|
|
return &lxd.APIError{Code: 400, Message: "network name too long"}
|
|
}
|
|
if f.networkExists(spec.Name) {
|
|
return &lxd.APIError{Code: 409, Message: "network exists"}
|
|
}
|
|
f.networks[spec.Name] = spec
|
|
return nil
|
|
}
|
|
|
|
func (f *LXD) DeleteNetwork(_ context.Context, name string) error {
|
|
f.mu.Lock()
|
|
defer f.mu.Unlock()
|
|
if err := f.check(); err != nil {
|
|
return err
|
|
}
|
|
if f.failDelete {
|
|
return fmt.Errorf("lxdfake: delete %s: %w", name, ErrInjected)
|
|
}
|
|
if _, ok := f.networks[name]; !ok {
|
|
return lxd.ErrNotFound
|
|
}
|
|
for _, instance := range f.instances {
|
|
for _, config := range instance.Spec.Devices {
|
|
if config["network"] == name {
|
|
return &lxd.APIError{Code: 400, Message: "network is in use"}
|
|
}
|
|
}
|
|
}
|
|
delete(f.networks, name)
|
|
return nil
|
|
}
|