File: runtime_test.go

package info (click to toggle)
golang-github-containers-common 0.62.2%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 5,436 kB
  • sloc: makefile: 131; sh: 102
file content (151 lines) | stat: -rw-r--r-- 3,843 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
//go:build !remote

package libimage

import (
	"context"
	"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/assert"
	"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.
func testNewRuntime(t *testing.T, options ...testNewRuntimeOptions) *Runtime {
	workdir := t.TempDir()
	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)

	t.Cleanup(func() { _ = runtime.Shutdown(true) })

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

func testRuntimePullImage(t *testing.T, r *Runtime, ctx context.Context, imageName string) {
	pullOptions := &PullOptions{}
	pullOptions.Writer = os.Stdout
	_, err := r.Pull(ctx, imageName, config.PullPolicyMissing, pullOptions)
	require.NoError(t, err)
}

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)
	}
}

func TestRuntimeListImagesAllImages(t *testing.T) {
	runtime := testNewRuntime(t)
	ctx := context.Background()

	// Prefetch alpine, busybox.
	testRuntimePullImage(t, runtime, ctx, "quay.io/libpod/alpine:latest")
	testRuntimePullImage(t, runtime, ctx, "quay.io/libpod/busybox:latest")

	images, err := runtime.ListImages(ctx, nil)
	require.NoError(t, err)

	require.Len(t, images, 2)
	var image_names []string
	for _, i := range images {
		image_names = append(image_names, i.Names()...)
	}
	assert.ElementsMatch(t,
		image_names,
		[]string{"quay.io/libpod/alpine:latest", "quay.io/libpod/busybox:latest"},
	)
}

func TestRuntimeListImagesByNames(t *testing.T) {
	runtime := testNewRuntime(t)
	ctx := context.Background()

	// Prefetch alpine, busybox.
	testRuntimePullImage(t, runtime, ctx, "quay.io/libpod/alpine:latest")
	testRuntimePullImage(t, runtime, ctx, "quay.io/libpod/busybox:latest")

	for _, test := range []struct {
		name     string
		fullName string
	}{
		{"alpine", "quay.io/libpod/alpine:latest"},
		{"busybox", "quay.io/libpod/busybox:latest"},
	} {
		images, err := runtime.ListImagesByNames([]string{test.name})
		require.NoError(t, err)
		require.Len(t, images, 1)
		require.Contains(t, images[0].Names(), test.fullName)
	}
	_, err := runtime.ListImagesByNames([]string{""})
	require.Error(t, err)
}