|
| 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 | + "testing" |
| 21 | + |
| 22 | + "gotest.tools/v3/assert" |
| 23 | + |
| 24 | + "github.com/containerd/nerdctl/mod/tigron/require" |
| 25 | + "github.com/containerd/nerdctl/mod/tigron/test" |
| 26 | + "github.com/containerd/nerdctl/mod/tigron/tig" |
| 27 | + |
| 28 | + "github.com/containerd/nerdctl/v2/pkg/testutil" |
| 29 | + "github.com/containerd/nerdctl/v2/pkg/testutil/nerdtest" |
| 30 | +) |
| 31 | + |
| 32 | +func TestContainerInspectGateway(t *testing.T) { |
| 33 | + testCase := nerdtest.Setup() |
| 34 | + |
| 35 | + // This test validates nerdctl's inspect conversion path |
| 36 | + // Running this against Docker would not use this code path |
| 37 | + testCase.Require = require.All( |
| 38 | + require.Not(require.Windows), |
| 39 | + require.Not(nerdtest.Docker), |
| 40 | + nerdtest.Rootful, |
| 41 | + ) |
| 42 | + |
| 43 | + testCase.Setup = func(data test.Data, helpers test.Helpers) { |
| 44 | + helpers.Ensure("network", "create", data.Identifier("net")) |
| 45 | + |
| 46 | + helpers.Ensure("run", "-d", |
| 47 | + "--name", data.Identifier("ctr"), |
| 48 | + "--network", data.Identifier("net"), |
| 49 | + testutil.CommonImage, "sleep", nerdtest.Infinity) |
| 50 | + |
| 51 | + nerdtest.EnsureContainerStarted(helpers, data.Identifier("ctr")) |
| 52 | + } |
| 53 | + |
| 54 | + testCase.Cleanup = func(data test.Data, helpers test.Helpers) { |
| 55 | + helpers.Anyhow("rm", "-f", data.Identifier("ctr")) |
| 56 | + helpers.Anyhow("network", "rm", data.Identifier("net")) |
| 57 | + } |
| 58 | + |
| 59 | + testCase.Command = func(data test.Data, helpers test.Helpers) test.TestableCommand { |
| 60 | + return helpers.Command("container", "inspect", data.Identifier("ctr")) |
| 61 | + } |
| 62 | + |
| 63 | + testCase.Expected = func(data test.Data, helpers test.Helpers) *test.Expected { |
| 64 | + return &test.Expected{ |
| 65 | + ExitCode: 0, |
| 66 | + Output: func(_ string, t tig.T) { |
| 67 | + netInspect := nerdtest.InspectNetwork(helpers, data.Identifier("net")) |
| 68 | + assert.Assert(t, len(netInspect.IPAM.Config) > 0) |
| 69 | + assert.Assert(t, netInspect.IPAM.Config[0].Gateway != "") |
| 70 | + |
| 71 | + ctrInspect := nerdtest.InspectContainer(helpers, data.Identifier("ctr")) |
| 72 | + assert.Assert(t, ctrInspect.NetworkSettings != nil) |
| 73 | + assert.Assert(t, ctrInspect.NetworkSettings.Gateway != "") |
| 74 | + assert.Equal(t, ctrInspect.NetworkSettings.Gateway, netInspect.IPAM.Config[0].Gateway) |
| 75 | + }, |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + testCase.Run(t) |
| 80 | +} |
0 commit comments