|
| 1 | +/* |
| 2 | + Copyright The containerd Authors. |
| 3 | +
|
| 4 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + you may not use this file except in compliance with the License. |
| 6 | + You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | + Unless required by applicable law or agreed to in writing, software |
| 11 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + See the License for the specific language governing permissions and |
| 14 | + limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package container |
| 18 | + |
| 19 | +import ( |
| 20 | + "archive/tar" |
| 21 | + "io" |
| 22 | + "os" |
| 23 | + "path/filepath" |
| 24 | + "runtime" |
| 25 | + "testing" |
| 26 | + |
| 27 | + "gotest.tools/v3/assert" |
| 28 | + |
| 29 | + "github.com/containerd/nerdctl/mod/tigron/test" |
| 30 | + "github.com/containerd/nerdctl/mod/tigron/tig" |
| 31 | + |
| 32 | + "github.com/containerd/nerdctl/v2/pkg/testutil" |
| 33 | + "github.com/containerd/nerdctl/v2/pkg/testutil/nerdtest" |
| 34 | +) |
| 35 | + |
| 36 | +// validateExportedTar checks that the tar file exists and contains /bin/busybox |
| 37 | +func validateExportedTar(outFile string) test.Comparator { |
| 38 | + return func(stdout string, t tig.T) { |
| 39 | + // Check if the tar file was created |
| 40 | + _, err := os.Stat(outFile) |
| 41 | + assert.Assert(t, !os.IsNotExist(err), "exported tar file %s was not created", outFile) |
| 42 | + |
| 43 | + // Open and read the tar file to check for /bin/busybox |
| 44 | + file, err := os.Open(outFile) |
| 45 | + assert.NilError(t, err, "failed to open tar file %s", outFile) |
| 46 | + defer file.Close() |
| 47 | + |
| 48 | + tarReader := tar.NewReader(file) |
| 49 | + busyboxFound := false |
| 50 | + |
| 51 | + for { |
| 52 | + header, err := tarReader.Next() |
| 53 | + if err == io.EOF { |
| 54 | + break |
| 55 | + } |
| 56 | + assert.NilError(t, err, "failed to read tar entry") |
| 57 | + |
| 58 | + if header.Name == "bin/busybox" || header.Name == "./bin/busybox" { |
| 59 | + busyboxFound = true |
| 60 | + break |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + assert.Assert(t, busyboxFound, "exported tar file %s does not contain /bin/busybox", outFile) |
| 65 | + t.Log("Export validation passed: tar file exists and contains /bin/busybox") |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +func TestExportStoppedContainer(t *testing.T) { |
| 70 | + if runtime.GOOS == "windows" { |
| 71 | + t.Skip("export is not supported on Windows") |
| 72 | + } |
| 73 | + |
| 74 | + testCase := nerdtest.Setup() |
| 75 | + testCase.Setup = func(data test.Data, helpers test.Helpers) { |
| 76 | + identifier := data.Identifier("container") |
| 77 | + helpers.Ensure("create", "--name", identifier, testutil.CommonImage) |
| 78 | + data.Labels().Set("cID", identifier) |
| 79 | + data.Labels().Set("outFile", filepath.Join(os.TempDir(), identifier+".tar")) |
| 80 | + } |
| 81 | + testCase.Cleanup = func(data test.Data, helpers test.Helpers) { |
| 82 | + helpers.Anyhow("container", "rm", "-f", data.Labels().Get("cID")) |
| 83 | + helpers.Anyhow("rm", "-f", data.Labels().Get("cID")) |
| 84 | + os.Remove(data.Labels().Get("outFile")) |
| 85 | + } |
| 86 | + |
| 87 | + testCase.SubTests = []*test.Case{ |
| 88 | + { |
| 89 | + Description: "export command succeeds", |
| 90 | + NoParallel: true, |
| 91 | + Command: func(data test.Data, helpers test.Helpers) test.TestableCommand { |
| 92 | + return helpers.Command("export", "-o", data.Labels().Get("outFile"), data.Labels().Get("cID")) |
| 93 | + }, |
| 94 | + Expected: test.Expects(0, nil, nil), |
| 95 | + }, |
| 96 | + { |
| 97 | + Description: "tar file exists and has content", |
| 98 | + NoParallel: true, |
| 99 | + Command: func(data test.Data, helpers test.Helpers) test.TestableCommand { |
| 100 | + // Use a simple command that always succeeds to trigger the validation |
| 101 | + return helpers.Custom("echo", "validating tar file") |
| 102 | + }, |
| 103 | + Expected: func(data test.Data, helpers test.Helpers) *test.Expected { |
| 104 | + return &test.Expected{ |
| 105 | + ExitCode: 0, |
| 106 | + Output: validateExportedTar(data.Labels().Get("outFile")), |
| 107 | + } |
| 108 | + }, |
| 109 | + }, |
| 110 | + } |
| 111 | + |
| 112 | + testCase.Run(t) |
| 113 | +} |
| 114 | + |
| 115 | +func TestExportRunningContainer(t *testing.T) { |
| 116 | + if runtime.GOOS == "windows" { |
| 117 | + t.Skip("export is not supported on Windows") |
| 118 | + } |
| 119 | + |
| 120 | + testCase := nerdtest.Setup() |
| 121 | + testCase.Setup = func(data test.Data, helpers test.Helpers) { |
| 122 | + identifier := data.Identifier("container") |
| 123 | + helpers.Ensure("run", "-d", "--name", identifier, testutil.CommonImage, "sleep", nerdtest.Infinity) |
| 124 | + data.Labels().Set("cID", identifier) |
| 125 | + data.Labels().Set("outFile", filepath.Join(os.TempDir(), identifier+".tar")) |
| 126 | + } |
| 127 | + testCase.Cleanup = func(data test.Data, helpers test.Helpers) { |
| 128 | + helpers.Anyhow("rm", "-f", data.Labels().Get("cID")) |
| 129 | + os.Remove(data.Labels().Get("outFile")) |
| 130 | + } |
| 131 | + |
| 132 | + testCase.SubTests = []*test.Case{ |
| 133 | + { |
| 134 | + Description: "export command succeeds", |
| 135 | + NoParallel: true, |
| 136 | + Command: func(data test.Data, helpers test.Helpers) test.TestableCommand { |
| 137 | + return helpers.Command("export", "-o", data.Labels().Get("outFile"), data.Labels().Get("cID")) |
| 138 | + }, |
| 139 | + Expected: test.Expects(0, nil, nil), |
| 140 | + }, |
| 141 | + { |
| 142 | + Description: "tar file exists and has content", |
| 143 | + NoParallel: true, |
| 144 | + Command: func(data test.Data, helpers test.Helpers) test.TestableCommand { |
| 145 | + // Use a simple command that always succeeds to trigger the validation |
| 146 | + return helpers.Custom("echo", "validating tar file") |
| 147 | + }, |
| 148 | + Expected: func(data test.Data, helpers test.Helpers) *test.Expected { |
| 149 | + return &test.Expected{ |
| 150 | + ExitCode: 0, |
| 151 | + Output: validateExportedTar(data.Labels().Get("outFile")), |
| 152 | + } |
| 153 | + }, |
| 154 | + }, |
| 155 | + } |
| 156 | + |
| 157 | + testCase.Run(t) |
| 158 | +} |
| 159 | + |
| 160 | +func TestExportNonexistentContainer(t *testing.T) { |
| 161 | + if runtime.GOOS == "windows" { |
| 162 | + t.Skip("export is not supported on Windows") |
| 163 | + } |
| 164 | + |
| 165 | + testCase := nerdtest.Setup() |
| 166 | + testCase.Command = test.Command("export", "nonexistent-container") |
| 167 | + testCase.Expected = test.Expects(1, nil, nil) |
| 168 | + |
| 169 | + testCase.Run(t) |
| 170 | +} |
0 commit comments