|
| 1 | +// Copyright 2024 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 | + |
| 15 | +package image |
| 16 | + |
| 17 | +import ( |
| 18 | + "bytes" |
| 19 | + "fmt" |
| 20 | + "os" |
| 21 | + "testing" |
| 22 | + |
| 23 | + "github.com/spf13/cobra" |
| 24 | + "github.com/stretchr/testify/require" |
| 25 | + |
| 26 | + commonImage "github.com/inspektor-gadget/inspektor-gadget/cmd/common/image" |
| 27 | + . "github.com/inspektor-gadget/inspektor-gadget/integration" |
| 28 | +) |
| 29 | + |
| 30 | +func TestImage(t *testing.T) { |
| 31 | + // ensure that experimental is enabled |
| 32 | + os.Setenv("IG_EXPERIMENTAL", "true") |
| 33 | + |
| 34 | + // start registry |
| 35 | + r := StartRegistry(t, "test-image-registry") |
| 36 | + t.Cleanup(func() { |
| 37 | + r.Stop(t) |
| 38 | + }) |
| 39 | + if len(r.PortBindings()) == 0 || r.PortBindings()["5000/tcp"] == nil { |
| 40 | + t.Fatal("registry port not exposed") |
| 41 | + } |
| 42 | + pb := r.PortBindings()["5000/tcp"][0] |
| 43 | + registryAddr := fmt.Sprintf("%s:%s", pb.HostIP, pb.HostPort) |
| 44 | + |
| 45 | + testReg := "reg.com" |
| 46 | + testRepo := "repo1" |
| 47 | + testTag := "tag1" |
| 48 | + testLocalImage := fmt.Sprintf("%s/%s:%s", testReg, testRepo, testTag) |
| 49 | + testRegistryImage := fmt.Sprintf("%s/%s:%s", registryAddr, testRepo, testTag) |
| 50 | + |
| 51 | + // ensure all images are removed |
| 52 | + t.Cleanup(func() { |
| 53 | + // remove local image |
| 54 | + cmd := commonImage.NewRemoveCmd() |
| 55 | + cmd.SetArgs([]string{testLocalImage}) |
| 56 | + cmd.Execute() |
| 57 | + |
| 58 | + // remove registry image |
| 59 | + cmd = commonImage.NewRemoveCmd() |
| 60 | + cmd.SetArgs([]string{testRegistryImage}) |
| 61 | + cmd.Execute() |
| 62 | + }) |
| 63 | + |
| 64 | + type testCase struct { |
| 65 | + name string |
| 66 | + cmd *cobra.Command |
| 67 | + args []string |
| 68 | + expectedStdout []string |
| 69 | + expectedStderr []string |
| 70 | + negateExpected bool |
| 71 | + } |
| 72 | + |
| 73 | + testCases := []testCase{ |
| 74 | + { |
| 75 | + name: "build", |
| 76 | + cmd: commonImage.NewBuildCmd(), |
| 77 | + args: []string{ |
| 78 | + "--builder-image", *testBuilderImage, "--tag", testLocalImage, "../../../gadgets/trace_open", |
| 79 | + }, |
| 80 | + expectedStdout: []string{ |
| 81 | + fmt.Sprintf("Successfully built %s", testLocalImage), |
| 82 | + }, |
| 83 | + }, |
| 84 | + { |
| 85 | + name: "list", |
| 86 | + cmd: commonImage.NewListCmd(), |
| 87 | + args: []string{}, |
| 88 | + expectedStdout: []string{ |
| 89 | + testRepo, |
| 90 | + testTag, |
| 91 | + }, |
| 92 | + }, |
| 93 | + { |
| 94 | + name: "tag", |
| 95 | + cmd: commonImage.NewTagCmd(), |
| 96 | + args: []string{testLocalImage, testRegistryImage}, |
| 97 | + expectedStdout: []string{ |
| 98 | + fmt.Sprintf("Successfully tagged with %s", testRegistryImage), |
| 99 | + }, |
| 100 | + }, |
| 101 | + { |
| 102 | + name: "push", |
| 103 | + cmd: commonImage.NewPushCmd(), |
| 104 | + args: []string{testRegistryImage, "--insecure"}, |
| 105 | + expectedStdout: []string{ |
| 106 | + fmt.Sprintf("Successfully pushed %s", testRegistryImage), |
| 107 | + }, |
| 108 | + }, |
| 109 | + { |
| 110 | + name: "push-invalid-image", |
| 111 | + cmd: commonImage.NewPushCmd(), |
| 112 | + args: []string{"unknown", "--insecure"}, |
| 113 | + expectedStderr: []string{ |
| 114 | + "failed to resolve ghcr.io/inspektor-gadget/gadget/unknown:latest: not found", |
| 115 | + }, |
| 116 | + }, |
| 117 | + { |
| 118 | + name: "push-unknown-tag", |
| 119 | + cmd: commonImage.NewPushCmd(), |
| 120 | + args: []string{fmt.Sprintf("%s/%s:%s", registryAddr, testRepo, "unknown"), "--insecure"}, |
| 121 | + expectedStderr: []string{ |
| 122 | + fmt.Sprintf("%s/%s:%s: not found", registryAddr, testRepo, "unknown"), |
| 123 | + }, |
| 124 | + }, |
| 125 | + { |
| 126 | + name: "remove-local-image", |
| 127 | + cmd: commonImage.NewRemoveCmd(), |
| 128 | + args: []string{testLocalImage}, |
| 129 | + expectedStdout: []string{ |
| 130 | + fmt.Sprintf("Successfully removed %s", testLocalImage), |
| 131 | + }, |
| 132 | + }, |
| 133 | + { |
| 134 | + name: "remove-registry-image", |
| 135 | + cmd: commonImage.NewRemoveCmd(), |
| 136 | + args: []string{testRegistryImage}, |
| 137 | + expectedStdout: []string{ |
| 138 | + fmt.Sprintf("Successfully removed %s", testRegistryImage), |
| 139 | + }, |
| 140 | + }, |
| 141 | + { |
| 142 | + name: "validate-remove", |
| 143 | + cmd: commonImage.NewListCmd(), |
| 144 | + args: []string{}, |
| 145 | + negateExpected: true, |
| 146 | + // can't use combined repository here as REPOSITORY column can be truncated |
| 147 | + expectedStdout: []string{ |
| 148 | + registryAddr, |
| 149 | + testRepo, |
| 150 | + }, |
| 151 | + }, |
| 152 | + { |
| 153 | + name: "pull", |
| 154 | + cmd: commonImage.NewPullCmd(), |
| 155 | + args: []string{testRegistryImage, "--insecure"}, |
| 156 | + expectedStdout: []string{ |
| 157 | + fmt.Sprintf("Successfully pulled %s", testRegistryImage), |
| 158 | + }, |
| 159 | + }, |
| 160 | + { |
| 161 | + name: "validate-pull", |
| 162 | + cmd: commonImage.NewListCmd(), |
| 163 | + args: []string{}, |
| 164 | + expectedStdout: []string{ |
| 165 | + registryAddr, |
| 166 | + testTag, |
| 167 | + }, |
| 168 | + }, |
| 169 | + { |
| 170 | + name: "pull-invalid-image", |
| 171 | + cmd: commonImage.NewPullCmd(), |
| 172 | + args: []string{"unknown", "--insecure"}, |
| 173 | + expectedStderr: []string{ |
| 174 | + "failed to resolve ghcr.io/inspektor-gadget/gadget/unknown:latest", |
| 175 | + }, |
| 176 | + }, |
| 177 | + { |
| 178 | + name: "pull-unknown-tag", |
| 179 | + cmd: commonImage.NewPullCmd(), |
| 180 | + args: []string{fmt.Sprintf("%s/%s:%s", registryAddr, testRepo, "unknown"), "--insecure"}, |
| 181 | + expectedStderr: []string{ |
| 182 | + fmt.Sprintf("%s/%s:%s: not found", registryAddr, testRepo, "unknown"), |
| 183 | + }, |
| 184 | + }, |
| 185 | + } |
| 186 | + |
| 187 | + for _, tc := range testCases { |
| 188 | + t.Run(tc.name, func(t *testing.T) { |
| 189 | + var stdout, stderr bytes.Buffer |
| 190 | + tc.cmd.SetArgs(tc.args) |
| 191 | + tc.cmd.SetOut(&stdout) |
| 192 | + tc.cmd.SetErr(&stderr) |
| 193 | + err := tc.cmd.Execute() |
| 194 | + if len(tc.expectedStderr) > 0 { |
| 195 | + require.NotNil(t, err) |
| 196 | + for _, expected := range tc.expectedStderr { |
| 197 | + if tc.negateExpected { |
| 198 | + require.NotContains(t, stderr.String(), expected) |
| 199 | + continue |
| 200 | + } |
| 201 | + require.Contains(t, stderr.String(), expected) |
| 202 | + } |
| 203 | + return |
| 204 | + } |
| 205 | + require.Nil(t, err) |
| 206 | + require.Empty(t, stderr.String()) |
| 207 | + for _, expected := range tc.expectedStdout { |
| 208 | + if tc.negateExpected { |
| 209 | + require.NotContains(t, stdout.String(), expected) |
| 210 | + continue |
| 211 | + } |
| 212 | + require.Contains(t, stdout.String(), expected) |
| 213 | + } |
| 214 | + }) |
| 215 | + } |
| 216 | +} |
0 commit comments