Debian Codegraph @

api/internal/image/image_test.go

raw
1 // Copyright 2020 The Kubernetes Authors.
2 // SPDX-License-Identifier: Apache-2.0
3
4 package image_test
5
6 import (
7 "testing"
8
9 "github.com/stretchr/testify/assert"
10 "sigs.k8s.io/kustomize/api/internal/image"
11 )
12
13 func TestIsImageMatched(t *testing.T) {
14 testCases := []struct {
15 testName string
16 value string
17 name string
18 isMatched bool
19 }{
20 {
21 testName: "identical",
22 value: "nginx",
23 name: "nginx",
24 isMatched: true,
25 },
26 {
27 testName: "name is match with tag",
28 value: "nginx:12345",
29 name: "nginx",
30 isMatched: true,
31 },
32 {
33 testName: "name is match with digest",
34 value: "nginx@sha256:xyz",
35 name: "nginx",
36 isMatched: true,
37 },
38 {
39 testName: "name is match with tag and digest",
40 value: "nginx:12345@sha256:xyz",
41 name: "nginx",
42 isMatched: true,
43 },
44 {
45 testName: "name is not a match",
46 value: "apache:12345",
47 name: "nginx",
48 isMatched: false,
49 },
50 }
51
52 for _, tc := range testCases {
53 t.Run(tc.testName, func(t *testing.T) {
54 assert.Equal(t, tc.isMatched, image.IsImageMatched(tc.value, tc.name))
55 })
56 }
57 }
58
59 func TestSplit(t *testing.T) {
60 testCases := []struct {
61 testName string
62 value string
63 name string
64 tag string
65 digest string
66 }{
67 {
68 testName: "no tag",
69 value: "nginx",
70 name: "nginx",
71 tag: "",
72 digest: "",
73 },
74 {
75 testName: "with tag",
76 value: "nginx:1.2.3",
77 name: "nginx",
78 tag: "1.2.3",
79 digest: "",
80 },
81 {
82 testName: "with digest",
83 value: "nginx@sha256:12345",
84 name: "nginx",
85 tag: "",
86 digest: "sha256:12345",
87 },
88 {
89 testName: "with tag and digest",
90 value: "nginx:1.2.3@sha256:12345",
91 name: "nginx",
92 tag: "1.2.3",
93 digest: "sha256:12345",
94 },
95 {
96 testName: "with domain",
97 value: "docker.io/nginx:1.2.3",
98 name: "docker.io/nginx",
99 tag: "1.2.3",
100 digest: "",
101 },
102 {
103 testName: "with domain and port",
104 value: "foo.com:443/nginx:1.2.3",
105 name: "foo.com:443/nginx",
106 tag: "1.2.3",
107 digest: "",
108 },
109 {
110 testName: "with domain, port, tag and digest",
111 value: "foo.com:443/nginx:1.2.3@sha256:12345",
112 name: "foo.com:443/nginx",
113 tag: "1.2.3",
114 digest: "sha256:12345",
115 },
116 }
117
118 for _, tc := range testCases {
119 t.Run(tc.testName, func(t *testing.T) {
120 name, tag, digest := image.Split(tc.value)
121 assert.Equal(t, tc.name, name)
122 assert.Equal(t, tc.tag, tag)
123 assert.Equal(t, tc.digest, digest)
124 })
125 }
126 }