1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
|
package main
import (
"context"
"fmt"
"os"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
"golang.org/x/sys/unix"
"github.com/lxc/incus/v6/internal/server/db"
"github.com/lxc/incus/v6/internal/server/db/cluster"
"github.com/lxc/incus/v6/internal/server/sys"
internalUtil "github.com/lxc/incus/v6/internal/util"
"github.com/lxc/incus/v6/shared/idmap"
)
func mockStartDaemon() (*Daemon, error) {
d := defaultDaemon()
d.os.MockMode = true
// Setup test certificates. We reuse the ones already on disk under
// the test/ directory, to avoid generating new ones, which is
// expensive.
err := sys.SetupTestCerts(internalUtil.VarPath())
if err != nil {
return nil, err
}
err = d.Init()
if err != nil {
return nil, err
}
d.os.IdmapSet = &idmap.Set{Entries: []idmap.Entry{
{IsUID: true, HostID: 100000, NSID: 0, MapRange: 500000},
{IsGID: true, HostID: 100000, NSID: 0, MapRange: 500000},
}}
return d, nil
}
type daemonTestSuite struct {
suite.Suite
d *Daemon
Req *require.Assertions
tmpdir string
}
const daemonTestSuiteDefaultStoragePool string = "testrunPool"
func (suite *daemonTestSuite) SetupTest() {
tmpdir, err := os.MkdirTemp("", "incus_testrun_")
if err != nil {
suite.T().Errorf("failed to create temp dir: %v", err)
}
suite.tmpdir = tmpdir
err = os.Setenv("INCUS_DIR", suite.tmpdir)
if err != nil {
suite.T().Errorf("failed to set INCUS_DIR: %v", err)
}
suite.d, err = mockStartDaemon()
if err != nil {
suite.T().Errorf("failed to start daemon: %v", err)
}
// Create default storage pool. Make sure that we don't pass a nil to
// the next function.
poolConfig := map[string]string{}
// Create the database entry for the storage pool.
poolDescription := fmt.Sprintf("%s storage pool", daemonTestSuiteDefaultStoragePool)
_, err = dbStoragePoolCreateAndUpdateCache(context.Background(), suite.d.State(), daemonTestSuiteDefaultStoragePool, poolDescription, "mock", poolConfig)
if err != nil {
suite.T().Errorf("failed to create default storage pool: %v", err)
}
rootDev := map[string]string{}
rootDev["path"] = "/"
rootDev["pool"] = daemonTestSuiteDefaultStoragePool
device := cluster.Device{
Name: "root",
Type: cluster.TypeDisk,
Config: rootDev,
}
err = suite.d.db.Cluster.Transaction(context.TODO(), func(ctx context.Context, tx *db.ClusterTx) error {
profile, err := cluster.GetProfile(ctx, tx.Tx(), "default", "default")
if err != nil {
return err
}
return cluster.UpdateProfileDevices(ctx, tx.Tx(), int64(profile.ID), map[string]cluster.Device{"root": device})
})
if err != nil {
suite.T().Errorf("failed to update default profile: %v", err)
}
suite.Req = require.New(suite.T())
}
func (suite *daemonTestSuite) TearDownTest() {
err := suite.d.Stop(context.Background(), unix.SIGQUIT)
if err != nil {
suite.T().Errorf("failed to stop daemon: %v", err)
}
err = os.RemoveAll(suite.tmpdir)
if err != nil {
suite.T().Errorf("failed to remove temp dir: %v", err)
}
}
|