feat(labs): add the labd provisioner with clone pool, time to live and limits
This commit is contained in:
1 parent
9fdaa62b9b
commit
9385b2ddd4
53 files changed
+14675
-6
No files matched your search
@@ -0,0 +1,125 @@
|
||||
package lxd_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"os"
|
||||
"slices"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"lincodex/labd/internal/lxd"
|
||||
)
|
||||
|
||||
const liveTimeout = 5 * time.Minute
|
||||
|
||||
// TestLiveLXD creates one lab bridge and one instance copied from a prepared
|
||||
// snapshot on a real LXD, checks that they appear, and deletes them. It runs
|
||||
// only with LABD_LIVE_SOCKET and LABD_LIVE_SOURCE set (make -C labd
|
||||
// test-live) and is meant for a test host, never the production app host.
|
||||
// LABD_LIVE_START=1 also boots the instance, which needs KVM.
|
||||
func TestLiveLXD(t *testing.T) {
|
||||
socket, source := os.Getenv("LABD_LIVE_SOCKET"), os.Getenv("LABD_LIVE_SOURCE")
|
||||
if socket == "" || source == "" {
|
||||
t.Skip("set LABD_LIVE_SOCKET and LABD_LIVE_SOURCE to run against a real LXD")
|
||||
}
|
||||
project := envOr("LABD_LIVE_PROJECT", "default")
|
||||
profile := envOr("LABD_LIVE_PROFILE", "lincodex-lab-small")
|
||||
ctx, cancel := context.WithTimeout(context.Background(), liveTimeout)
|
||||
defer cancel()
|
||||
client := lxd.NewUnix(socket, project)
|
||||
if err := client.Ping(ctx); err != nil {
|
||||
t.Fatalf("Ping() error = %v", err)
|
||||
}
|
||||
|
||||
suffix := randomHex(t, 3)
|
||||
bridge := "lxllive" + suffix
|
||||
instance := "lcxl-live-" + suffix
|
||||
marker := map[string]string{"user.lincodex.lab": "live-test-" + suffix}
|
||||
t.Cleanup(func() {
|
||||
// Best effort if an assertion stopped the test half way.
|
||||
cleanupCtx, cleanupCancel := context.WithTimeout(context.Background(), liveTimeout)
|
||||
defer cleanupCancel()
|
||||
_ = client.StopInstance(cleanupCtx, instance)
|
||||
_ = client.DeleteInstance(cleanupCtx, instance)
|
||||
_ = client.DeleteNetwork(cleanupCtx, bridge)
|
||||
})
|
||||
|
||||
if err := client.CreateNetwork(ctx, lxd.NetworkSpec{
|
||||
Name: bridge, Description: "labd live test",
|
||||
Config: map[string]string{"ipv4.address": "none", "ipv6.address": "none", "user.lincodex.lab": marker["user.lincodex.lab"]},
|
||||
}); err != nil {
|
||||
t.Fatalf("CreateNetwork() error = %v", err)
|
||||
}
|
||||
started := time.Now()
|
||||
if err := client.CreateInstance(ctx, lxd.InstanceSpec{
|
||||
Name: instance, Description: "labd live test", Source: source, Profiles: []string{profile}, Config: marker,
|
||||
Devices: map[string]map[string]string{"eth1": {"type": "nic", "network": bridge}},
|
||||
}); err != nil {
|
||||
t.Fatalf("CreateInstance() error = %v", err)
|
||||
}
|
||||
t.Logf("copy of %s took %s", source, time.Since(started))
|
||||
if !hasInstance(t, client, instance) || !hasNetwork(t, client, bridge) {
|
||||
t.Fatal("the created objects are not listed")
|
||||
}
|
||||
if os.Getenv("LABD_LIVE_START") == "1" {
|
||||
if err := client.StartInstance(ctx, instance); err != nil {
|
||||
t.Fatalf("StartInstance() error = %v", err)
|
||||
}
|
||||
if running, err := client.InstanceRunning(ctx, instance); err != nil || !running {
|
||||
t.Fatalf("InstanceRunning() = %v, %v", running, err)
|
||||
}
|
||||
}
|
||||
|
||||
if err := client.StopInstance(ctx, instance); err != nil {
|
||||
t.Fatalf("StopInstance() error = %v", err)
|
||||
}
|
||||
if err := client.DeleteInstance(ctx, instance); err != nil {
|
||||
t.Fatalf("DeleteInstance() error = %v", err)
|
||||
}
|
||||
if err := client.DeleteNetwork(ctx, bridge); err != nil {
|
||||
t.Fatalf("DeleteNetwork() error = %v", err)
|
||||
}
|
||||
if hasInstance(t, client, instance) || hasNetwork(t, client, bridge) {
|
||||
t.Fatal("deleted objects are still listed")
|
||||
}
|
||||
if err := client.DeleteInstance(ctx, instance); !errors.Is(err, lxd.ErrNotFound) {
|
||||
t.Fatalf("second DeleteInstance() error = %v, want ErrNotFound", err)
|
||||
}
|
||||
}
|
||||
|
||||
func envOr(name, fallback string) string {
|
||||
if value := os.Getenv(name); value != "" {
|
||||
return value
|
||||
}
|
||||
return fallback
|
||||
}
|
||||
|
||||
func randomHex(t *testing.T, size int) string {
|
||||
t.Helper()
|
||||
raw := make([]byte, size)
|
||||
if _, err := rand.Read(raw); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return hex.EncodeToString(raw)
|
||||
}
|
||||
|
||||
func hasInstance(t *testing.T, client *lxd.Client, name string) bool {
|
||||
t.Helper()
|
||||
instances, err := client.ListInstances(context.Background())
|
||||
if err != nil {
|
||||
t.Fatalf("ListInstances() error = %v", err)
|
||||
}
|
||||
return slices.ContainsFunc(instances, func(instance lxd.Instance) bool { return instance.Name == name })
|
||||
}
|
||||
|
||||
func hasNetwork(t *testing.T, client *lxd.Client, name string) bool {
|
||||
t.Helper()
|
||||
networks, err := client.ListNetworks(context.Background())
|
||||
if err != nil {
|
||||
t.Fatalf("ListNetworks() error = %v", err)
|
||||
}
|
||||
return slices.ContainsFunc(networks, func(network lxd.Network) bool { return network.Name == name })
|
||||
}
|
||||
Reference in new issue
Block a user