File: ostree_transport_test.go

package info (click to toggle)
golang-github-containers-image 5.28.0-4
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 5,104 kB
  • sloc: sh: 194; makefile: 73
file content (305 lines) | stat: -rw-r--r-- 11,271 bytes parent folder | download | duplicates (2)
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
//go:build containers_image_ostree
// +build containers_image_ostree

package ostree

import (
	"context"
	"fmt"
	"os"
	"path/filepath"
	"strings"
	"testing"

	_ "github.com/containers/image/v5/internal/testing/explicitfilepath-tmpdir"
	"github.com/containers/image/v5/types"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

const (
	sha256digestHex = "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
	sha256digest    = "@sha256:" + sha256digestHex
)

func TestTransportName(t *testing.T) {
	assert.Equal(t, "ostree", Transport.Name())
}

// A helper to replace $TMP in a repo path with a real temporary directory
func withTmpDir(repo string, tmpDir string) string {
	return strings.ReplaceAll(repo, "$TMP", tmpDir)
}

// A common list of repo suffixes to test for the various ImageReference methods.
var repoSuffixes = []struct{ repoSuffix, resolvedRepo string }{
	{"", "/ostree/repo"},
	{"@/ostree/repo", "/ostree/repo"}, // /ostree/repo is accepted even if neither /ostree/repo nor /ostree exists, as a special case.
	{"@$TMP/at@sign@repo", "$TMP/at@sign@repo"},
	// Rejected as ambiguous: /repo:with:colons could either be an (/repo, with:colons) policy configuration identity, or a (/repo:with, colons) policy configuration namespace.
	{"@$TMP/repo:with:colons", ""},
}

// A common list of cases for image name parsing and normalization
var imageNameTestcases = []struct{ input, normalized, branchName string }{
	{"busybox:notlatest", "busybox:notlatest", "busybox_3Anotlatest"},                                                  // Explicit tag
	{"busybox", "busybox:latest", "busybox_3Alatest"},                                                                  // Default tag
	{"docker.io/library/busybox:latest", "docker.io/library/busybox:latest", "docker.io_2Flibrary_2Fbusybox_3Alatest"}, // A hierarchical name
	{"127.0.0.1:5000/busybox:latest", "127.0.0.1:5000/busybox:latest", "127.0.0.1_3A5000_2Fbusybox_3Alatest"},          // Port usage
	{"busybox" + sha256digest, "busybox" + sha256digest, "busybox_40sha256_3A0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"},
	{"UPPERCASEISINVALID", "", ""},      // Invalid input
	{"busybox:invalid+tag", "", ""},     // Invalid tag value
	{"busybox:tag:with:colons", "", ""}, // Multiple colons - treated as a tag which contains a colon, which is invalid
	{"", "", ""},                        // Empty input is rejected (invalid repository.Named)
}

func TestTransportParseReference(t *testing.T) {
	tmpDir := t.TempDir()

	for _, c := range imageNameTestcases {
		for _, suffix := range repoSuffixes {
			fullInput := c.input + withTmpDir(suffix.repoSuffix, tmpDir)
			ref, err := Transport.ParseReference(fullInput)
			if c.normalized == "" || suffix.resolvedRepo == "" {
				assert.Error(t, err, fullInput)
			} else {
				require.NoError(t, err, fullInput)
				ostreeRef, ok := ref.(ostreeReference)
				require.True(t, ok, fullInput)
				assert.Equal(t, c.normalized, ostreeRef.image, fullInput)
				assert.Equal(t, c.branchName, ostreeRef.branchName, fullInput)
				assert.Equal(t, withTmpDir(suffix.resolvedRepo, tmpDir), ostreeRef.repo, fullInput)
			}
		}
	}
}

func TestTransportValidatePolicyConfigurationScope(t *testing.T) {
	for _, scope := range []string{
		"/etc:docker.io/library/busybox:notlatest", // This also demonstrates that two colons are interpreted as repo:name:tag.
		"/etc:docker.io/library/busybox",
		"/etc:docker.io/library",
		"/etc:docker.io",
		"/etc:repo",
		"/this/does/not/exist:notlatest",
	} {
		err := Transport.ValidatePolicyConfigurationScope(scope)
		assert.NoError(t, err, scope)
	}

	for _, scope := range []string{
		"/colon missing as a path-reference delimiter",
		"relative/path:busybox",
		"/double//slashes:busybox",
		"/has/./dot:busybox",
		"/has/dot/../dot:busybox",
		"/trailing/slash/:busybox",
	} {
		err := Transport.ValidatePolicyConfigurationScope(scope)
		assert.Error(t, err, scope)
	}
}

func TestNewReference(t *testing.T) {
	tmpDir := t.TempDir()

	for _, c := range imageNameTestcases {
		for _, suffix := range repoSuffixes {
			if suffix.repoSuffix == "" {
				continue
			}
			caseName := c.input + suffix.repoSuffix
			ref, err := NewReference(c.input, withTmpDir(strings.TrimPrefix(suffix.repoSuffix, "@"), tmpDir))
			if c.normalized == "" || suffix.resolvedRepo == "" {
				assert.Error(t, err, caseName)
			} else {
				require.NoError(t, err, caseName)
				ostreeRef, ok := ref.(ostreeReference)
				require.True(t, ok, caseName)
				assert.Equal(t, c.normalized, ostreeRef.image, caseName)
				assert.Equal(t, c.branchName, ostreeRef.branchName, caseName)
				assert.Equal(t, withTmpDir(suffix.resolvedRepo, tmpDir), ostreeRef.repo, caseName)
			}
		}
	}

	for _, path := range []string{
		"/",
		"/etc",
		tmpDir,
		"relativepath",
		tmpDir + "/thisdoesnotexist",
	} {
		_, err := NewReference("busybox", path)
		require.NoError(t, err, path)
	}

	_, err := NewReference("busybox", tmpDir+"/thisparentdoesnotexist/something")
	assert.Error(t, err)
}

// A common list of reference formats to test for the various ImageReference methods.
var validReferenceTestCases = []struct{ input, stringWithinTransport, policyConfigurationIdentity string }{
	{"busybox", "busybox:latest@/ostree/repo", "/ostree/repo:busybox:latest"},                                                         // Everything implied
	{"busybox:latest@/ostree/repo", "busybox:latest@/ostree/repo", "/ostree/repo:busybox:latest"},                                     // All implied values explicitly specified
	{"example.com/ns/foo:bar@$TMP/non-DEFAULT", "example.com/ns/foo:bar@$TMP/non-DEFAULT", "$TMP/non-DEFAULT:example.com/ns/foo:bar"}, // All values explicitly specified, a hierarchical name
	// A non-canonical path. Testing just one, the various other cases are tested in explicitfilepath.ResolvePathToFullyExplicit.
	{"busybox@$TMP/.", "busybox:latest@$TMP", "$TMP:busybox:latest"},
	// "/" as a corner case
	{"busybox@/", "busybox:latest@/", "/:busybox:latest"},
}

func TestReferenceTransport(t *testing.T) {
	ref, err := Transport.ParseReference("busybox")
	require.NoError(t, err)
	assert.Equal(t, Transport, ref.Transport())
}

func TestReferenceStringWithinTransport(t *testing.T) {
	tmpDir := t.TempDir()

	for _, c := range validReferenceTestCases {
		ref, err := Transport.ParseReference(withTmpDir(c.input, tmpDir))
		require.NoError(t, err, c.input)
		stringRef := ref.StringWithinTransport()
		assert.Equal(t, withTmpDir(c.stringWithinTransport, tmpDir), stringRef, c.input)
		// Do one more round to verify that the output can be parsed, to an equal value.
		ref2, err := Transport.ParseReference(stringRef)
		require.NoError(t, err, c.input)
		stringRef2 := ref2.StringWithinTransport()
		assert.Equal(t, stringRef, stringRef2, c.input)
	}
}

func TestReferenceDockerReference(t *testing.T) {
	tmpDir := t.TempDir()

	for _, c := range validReferenceTestCases {
		ref, err := Transport.ParseReference(withTmpDir(c.input, tmpDir))
		require.NoError(t, err, c.input)
		dockerRef := ref.DockerReference()
		assert.Nil(t, dockerRef, c.input)
	}
}

func TestReferencePolicyConfigurationIdentity(t *testing.T) {
	tmpDir := t.TempDir()

	for _, c := range validReferenceTestCases {
		ref, err := Transport.ParseReference(withTmpDir(c.input, tmpDir))
		require.NoError(t, err, c.input)
		assert.Equal(t, withTmpDir(c.policyConfigurationIdentity, tmpDir), ref.PolicyConfigurationIdentity(), c.input)
	}
}

func TestReferencePolicyConfigurationNamespaces(t *testing.T) {
	tmpDir := t.TempDir()

	// Test both that DockerReferenceIdentity returns the expected value (fullName+suffix),
	// and that DockerReferenceNamespaces starts with the expected value (fullName), i.e. that the two functions are
	// consistent.
	for inputName, expectedNS := range map[string][]string{
		"example.com/ns/repo": {"example.com/ns/repo", "example.com/ns", "example.com"},
		"example.com/repo":    {"example.com/repo", "example.com"},
		"localhost/ns/repo":   {"localhost/ns/repo", "localhost/ns", "localhost"},
		"localhost/repo":      {"localhost/repo", "localhost"},
		"ns/repo":             {"ns/repo", "ns"},
		"repo":                {"repo"},
	} {
		// Test with a known path which should exist. Test just one non-canonical
		// path, the various other cases are tested in explicitfilepath.ResolvePathToFullyExplicit.
		for _, repoInput := range []string{tmpDir, tmpDir + "/./."} {
			fullName := inputName + ":notlatest"
			ref, err := NewReference(fullName, repoInput)
			require.NoError(t, err, fullName)

			identity := ref.PolicyConfigurationIdentity()
			assert.Equal(t, tmpDir+":"+expectedNS[0]+":notlatest", identity, fullName)

			ns := ref.PolicyConfigurationNamespaces()
			require.NotNil(t, ns, fullName)
			require.Len(t, ns, len(expectedNS), fullName)
			moreSpecific := identity
			for i := range expectedNS {
				assert.Equal(t, tmpDir+":"+expectedNS[i], ns[i], fmt.Sprintf("%s item %d", fullName, i))
				assert.True(t, strings.HasPrefix(moreSpecific, ns[i]))
				moreSpecific = ns[i]
			}
		}
	}
}

func TestReferenceNewImage(t *testing.T) {
	ref, err := Transport.ParseReference("busybox")
	require.NoError(t, err)
	_, err = ref.NewImage(context.Background(), nil)
	assert.Error(t, err)
}

func TestReferenceNewImageSource(t *testing.T) {
	ref, err := Transport.ParseReference("busybox")
	require.NoError(t, err)
	src, err := ref.NewImageSource(context.Background(), nil)
	require.NoError(t, err)
	defer src.Close()
}

func TestReferenceNewImageDestination(t *testing.T) {
	otherTmpDir := t.TempDir()

	for _, c := range []struct {
		sys    *types.SystemContext
		tmpDir string
	}{
		{nil, os.TempDir()},
		{&types.SystemContext{}, os.TempDir()},
		{&types.SystemContext{OSTreeTmpDirPath: otherTmpDir}, otherTmpDir},
	} {
		ref, err := Transport.ParseReference("busybox")
		require.NoError(t, err)
		dest, err := ref.NewImageDestination(context.Background(), c.sys)
		require.NoError(t, err)
		ostreeDest, ok := dest.(*ostreeImageDestination)
		require.True(t, ok)
		assert.Equal(t, c.tmpDir+"/busybox_3Alatest", ostreeDest.tmpDirPath)
		defer dest.Close()
	}
}

func TestReferenceDeleteImage(t *testing.T) {
	tmpDir := t.TempDir()

	ref, err := Transport.ParseReference(withTmpDir("busybox@$TMP/this-repo-does-not-exist", tmpDir))
	require.NoError(t, err)
	err = ref.DeleteImage(context.Background(), nil)
	assert.Error(t, err)
}

func TestEncodeOSTreeRef(t *testing.T) {
	// Just a smoke test
	assert.Equal(t, "busybox_3Alatest", encodeOStreeRef("busybox:latest"))
}

func TestReferenceManifestPath(t *testing.T) {
	ref, err := Transport.ParseReference("busybox")
	require.NoError(t, err)
	ostreeRef, ok := ref.(ostreeReference)
	require.True(t, ok)
	assert.Equal(t, fmt.Sprintf("manifest%cmanifest.json", filepath.Separator), ostreeRef.manifestPath())
}

func TestReferenceSignaturePath(t *testing.T) {
	ref, err := Transport.ParseReference("busybox")
	require.NoError(t, err)
	ostreeRef, ok := ref.(ostreeReference)
	require.True(t, ok)
	for _, c := range []struct {
		input  int
		suffix string
	}{
		{0, "-1"},
		{42, "-43"},
	} {
		assert.Equal(t, fmt.Sprintf("manifest%csignature%s", filepath.Separator, c.suffix), ostreeRef.signaturePath(c.input), fmt.Sprintf("%d", c.input))
	}
}