inspektor-gadget
222 строки · 5.4 Кб
1// Copyright 2023 The Inspektor Gadget authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package oci
16
17import (
18"testing"
19
20"github.com/stretchr/testify/assert"
21"github.com/stretchr/testify/require"
22)
23
24func TestSplitIGDomain(t *testing.T) {
25t.Parallel()
26
27type testDefinition struct {
28name string
29expectedDomain string
30expectedRemainder string
31}
32
33tests := map[string]testDefinition{
34"no_domain_and_remainder": {
35name: "trace_exec",
36expectedDomain: defaultDomain,
37expectedRemainder: officialRepoPrefix + "trace_exec",
38},
39"no_domain_and_remainder_with_tag": {
40name: "trace_exec:v0.42.0",
41expectedDomain: defaultDomain,
42expectedRemainder: officialRepoPrefix + "trace_exec:v0.42.0",
43},
44"no_domain": {
45name: "xyz/gadget/trace_exec",
46expectedDomain: defaultDomain,
47expectedRemainder: "xyz/gadget/trace_exec",
48},
49"full": {
50name: "foobar.baz/xyz/gadget/trace_exec",
51expectedDomain: "foobar.baz",
52expectedRemainder: "xyz/gadget/trace_exec",
53},
54"full_with_port": {
55name: "foobar.baz:443/xyz/gadget/trace_exec",
56expectedDomain: "foobar.baz:443",
57expectedRemainder: "xyz/gadget/trace_exec",
58},
59"full_with_port_with_tag": {
60name: "foobar.baz:443/xyz/gadget/trace_exec:v0.42.0",
61expectedDomain: "foobar.baz:443",
62expectedRemainder: "xyz/gadget/trace_exec:v0.42.0",
63},
64"localhost": {
65name: "localhost/trace_exec",
66expectedDomain: "localhost",
67expectedRemainder: "trace_exec",
68},
69"localhost_with_long_remainder": {
70name: "localhost/a/b/c/e/d/g/r/trace_exec",
71expectedDomain: "localhost",
72expectedRemainder: "a/b/c/e/d/g/r/trace_exec",
73},
74"localhost_with_port": {
75name: "localhost:5000/trace_exec",
76expectedDomain: "localhost:5000",
77expectedRemainder: "trace_exec",
78},
79"localhost_with_port_with_tag": {
80name: "localhost:5000/trace_exec:v1.0.3",
81expectedDomain: "localhost:5000",
82expectedRemainder: "trace_exec:v1.0.3",
83},
84}
85
86for name, test := range tests {
87test := test
88t.Run(name, func(t *testing.T) {
89t.Parallel()
90
91actualDomain, actualRemainder := splitIGDomain(test.name)
92assert.Equal(t, test.expectedDomain, actualDomain)
93assert.Equal(t, test.expectedRemainder, actualRemainder)
94})
95}
96}
97
98func TestNormalizeImage(t *testing.T) {
99t.Parallel()
100
101type testDefinition struct {
102image string
103imageExpected string
104err bool
105}
106
107tests := map[string]testDefinition{
108"empty": {
109image: "",
110err: true,
111},
112"badtag": {
113image: "inspektor-gadget/ig:~½¬",
114err: true,
115},
116"image": {
117image: "ig",
118imageExpected: "ghcr.io/inspektor-gadget/gadget/ig:latest",
119},
120"image_and_tag": {
121image: "ig:latest",
122imageExpected: "ghcr.io/inspektor-gadget/gadget/ig:latest",
123},
124"image_and_tag_2": {
125image: "ig:latestttt",
126imageExpected: "ghcr.io/inspektor-gadget/gadget/ig:latestttt",
127},
128"host_image_and_tag": {
129image: "inspektor-gadget/ig:foobar",
130imageExpected: "ghcr.io/inspektor-gadget/ig:foobar",
131},
132"schema_host_image_and_tag": {
133image: "https://inspektor-gadget/ig:baz",
134err: true,
135},
136"host_port_image_and_tag": {
137image: "ghcr.io:443/inspektor-gadget/ig:baz",
138imageExpected: "ghcr.io:443/inspektor-gadget/ig:baz",
139},
140"schema_host_port_image_and_tag": {
141image: "https://ghcr.io:443/inspektor-gadget/ig:latest",
142err: true,
143},
144}
145
146for name, test := range tests {
147test := test
148t.Run(name, func(t *testing.T) {
149t.Parallel()
150
151imageRef, err := normalizeImageName(test.image)
152if test.err {
153require.Error(t, err)
154return
155}
156
157require.NoError(t, err)
158require.Equal(t, test.imageExpected, imageRef.String())
159})
160}
161}
162
163func TestGetHostString(t *testing.T) {
164t.Parallel()
165
166type testDefinition struct {
167image string
168host string
169err bool
170}
171
172tests := map[string]testDefinition{
173"empty": {
174image: "",
175err: true,
176},
177"badtag": {
178image: "inspektor-gadget/ig:~½¬",
179err: true,
180},
181"image": {
182image: "ig",
183host: "",
184},
185"host": {
186image: "ghcr.io",
187host: "",
188},
189"host_image_and_tag": {
190image: "inspektor-gadget/ig:latest",
191host: "inspektor-gadget",
192},
193"schema_host_image_and_tag": {
194image: "https://inspektor-gadget/ig:latest",
195err: true,
196},
197"host_port_image_and_tag": {
198image: "ghcr.io:443/inspektor-gadget/ig:latest",
199host: "ghcr.io:443",
200},
201"schema_host_port_image_and_tag": {
202image: "https://ghcr.io:443/inspektor-gadget/ig:latest",
203err: true,
204},
205}
206
207for name, test := range tests {
208test := test
209t.Run(name, func(t *testing.T) {
210t.Parallel()
211
212host, err := getHostString(test.image)
213if test.err {
214require.Error(t, err)
215return
216}
217
218require.NoError(t, err)
219require.Equal(t, test.host, host)
220})
221}
222}
223