File: testing.go

package info (click to toggle)
incus 6.0.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 24,392 kB
  • sloc: sh: 16,313; ansic: 3,121; python: 457; makefile: 337; ruby: 51; sql: 50; lisp: 6
file content (59 lines) | stat: -rw-r--r-- 1,482 bytes parent folder | download | duplicates (5)
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
//go:build linux && cgo && !agent

package sys

import (
	"os"
	"path/filepath"
	"runtime"
	"testing"

	"github.com/stretchr/testify/require"
)

// NewTestOS returns a new OS instance initialized with test values.
func NewTestOS(t *testing.T) (*OS, func()) {
	dir, err := os.MkdirTemp("", "incus-sys-os-test-")
	require.NoError(t, err)
	require.NoError(t, SetupTestCerts(dir))

	cleanup := func() {
		require.NoError(t, os.RemoveAll(dir))
	}

	os := &OS{
		// FIXME: setting mock mode can be avoided once daemon tasks
		// are fixed to exit gracefully. See daemon.go.
		MockMode: true,

		VarDir:   dir,
		CacheDir: filepath.Join(dir, "cache"),
		LogDir:   filepath.Join(dir, "log"),
		RunDir:   filepath.Join(dir, "run"),
	}

	_, err = os.Init()
	require.NoError(t, err)

	return os, cleanup
}

// SetupTestCerts populates the given test directory with server certificates.
//
// Since generating certificates is CPU intensive, they will be simply
// symlink'ed from the test/deps/ directory.
//
//	FIXME: this function is exported because some tests use it
//		directly. Eventually we should rework those tests to use NewTestOS
//		instead.
func SetupTestCerts(dir string) error {
	_, filename, _, _ := runtime.Caller(0)
	deps := filepath.Join(filepath.Dir(filename), "..", "..", "..", "test", "deps")
	for _, f := range []string{"server.crt", "server.key"} {
		err := os.Symlink(filepath.Join(deps, f), filepath.Join(dir, f))
		if err != nil {
			return err
		}
	}
	return nil
}