File: oci_util_test.go

package info (click to toggle)
golang-github-containers-image 5.36.1-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 5,152 kB
  • sloc: sh: 267; makefile: 100
file content (91 lines) | stat: -rw-r--r-- 2,255 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
package internal

import (
	"fmt"
	"testing"

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

type testDataSplitReference struct {
	ref   string
	dir   string
	image string
}

type testDataScopeValidation struct {
	scope      string
	errMessage string
}

type testOCIReference struct {
	ref   string
	image string
	index int
}

func TestSplitReferenceIntoDirAndImageWindows(t *testing.T) {
	tests := []testDataSplitReference{
		{`C:\foo\bar:busybox:latest`, `C:\foo\bar`, "busybox:latest"},
		{`C:\foo\bar:busybox`, `C:\foo\bar`, "busybox"},
		{`C:\foo\bar`, `C:\foo\bar`, ""},
	}
	for _, test := range tests {
		dir, image := splitPathAndImageWindows(test.ref)
		assert.Equal(t, test.dir, dir, "Unexpected OCI directory")
		assert.Equal(t, test.image, image, "Unexpected image")
	}
}

func TestSplitReferenceIntoDirAndImageNonWindows(t *testing.T) {
	tests := []testDataSplitReference{
		{"/foo/bar:busybox:latest", "/foo/bar", "busybox:latest"},
		{"/foo/bar:busybox", "/foo/bar", "busybox"},
		{"/foo/bar", "/foo/bar", ""},
	}
	for _, test := range tests {
		dir, image := splitPathAndImageNonWindows(test.ref)
		assert.Equal(t, test.dir, dir, "Unexpected OCI directory")
		assert.Equal(t, test.image, image, "Unexpected image")
	}
}

func TestValidateScopeWindows(t *testing.T) {
	tests := []testDataScopeValidation{
		{`C:\foo`, ""},
		{`D:\`, ""},
		{"C:", "Invalid scope 'C:'. Must be an absolute path"},
		{"E", "Invalid scope 'E'. Must be an absolute path"},
		{"", "Invalid scope ''. Must be an absolute path"},
	}
	for _, test := range tests {
		err := validateScopeWindows(test.scope)
		if test.errMessage == "" {
			assert.NoError(t, err)
		} else {
			assert.EqualError(t, err, test.errMessage, fmt.Sprintf("No error for scope '%s'", test.scope))
		}
	}
}

func TestParseOCIReferenceName(t *testing.T) {
	validTests := []testOCIReference{
		{"@0", "", 0},
		{"notlatest@1", "notlatest@1", -1},
	}
	for _, test := range validTests {
		img, idx, err := parseOCIReferenceName(test.ref)
		assert.NoError(t, err)
		assert.Equal(t, img, test.image)
		assert.Equal(t, idx, test.index)
	}

	invalidTests := []string{
		"@-5",
		"@invalidIndex",
	}
	for _, test := range invalidTests {
		_, _, err := parseOCIReferenceName(test)
		assert.Error(t, err)
	}
}