File: runtime_test.go

package info (click to toggle)
golang-github-containers-common 0.56.0%2Bds1-4
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,852 kB
  • sloc: makefile: 126; sh: 62
file content (100 lines) | stat: -rw-r--r-- 2,450 bytes parent folder | download
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
package libimage

import (
	"os"
	"testing"

	"github.com/containers/common/pkg/config"
	"github.com/containers/image/v5/types"
	"github.com/containers/storage"
	"github.com/containers/storage/pkg/reexec"
	"github.com/stretchr/testify/require"
)

func TestMain(m *testing.M) {
	if reexec.Init() {
		return
	}
	os.Exit(m.Run())
}

type testNewRuntimeOptions struct {
	registriesConfPath string
}

// Create a new Runtime that can be used for testing.  The second return value
// is a clean-up function that should be called by users to make sure all
// temporary test data gets removed.
func testNewRuntime(t *testing.T, options ...testNewRuntimeOptions) (runtime *Runtime, cleanup func()) {
	workdir, err := os.MkdirTemp("", "testStorageRuntime")
	require.NoError(t, err)
	storeOptions := &storage.StoreOptions{
		RunRoot:         workdir,
		GraphRoot:       workdir,
		GraphDriverName: "vfs",
	}

	// Make sure that the tests do not use the host's registries.conf.
	systemContext := &types.SystemContext{
		SystemRegistriesConfPath:    "testdata/registries.conf",
		SystemRegistriesConfDirPath: "/dev/null",
	}

	if len(options) == 1 && options[0].registriesConfPath != "" {
		systemContext.SystemRegistriesConfPath = options[0].registriesConfPath
	}

	runtime, err = RuntimeFromStoreOptions(&RuntimeOptions{SystemContext: systemContext}, storeOptions)
	require.NoError(t, err)
	tmpd, err := tmpdir()
	require.NoError(t, err)
	require.Equal(t, runtime.systemContext.BigFilesTemporaryDir, tmpd)

	cleanup = func() {
		_ = runtime.Shutdown(true)
		_ = os.RemoveAll(workdir)
	}

	sys := runtime.SystemContext()
	require.NotNil(t, sys)
	return runtime, cleanup
}

func TestTmpdir(t *testing.T) {
	tmpStr := "TMPDIR"
	tmp, tmpSet := os.LookupEnv(tmpStr)

	confStr := "CONTAINERS_CONF"
	conf, confSet := os.LookupEnv(confStr)

	os.Setenv(confStr, "testdata/containers.conf")
	_, err := config.Reload()
	require.NoError(t, err)

	tmpd, err := tmpdir()
	require.NoError(t, err)
	require.Equal(t, "/tmp/from/containers.conf", tmpd)

	if confSet {
		os.Setenv(confStr, conf)
	} else {
		os.Unsetenv(confStr)
	}
	_, err = config.Reload()
	require.NoError(t, err)

	os.Unsetenv(tmpStr)
	tmpd, err = tmpdir()
	require.NoError(t, err)
	require.Equal(t, "/var/tmp", tmpd)

	os.Setenv(tmpStr, "/tmp/test")
	tmpd, err = tmpdir()
	require.NoError(t, err)
	require.Equal(t, "/tmp/test", tmpd)
	if tmpSet {
		os.Setenv(tmpStr, tmp)
	} else {
		os.Unsetenv(tmpStr)
	}
}