|
| 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 | + "encoding/json" |
| 21 | + "testing" |
| 22 | + "time" |
| 23 | + |
| 24 | + "gotest.tools/v3/assert" |
| 25 | + |
| 26 | + "github.com/containerd/nerdctl/v2/pkg/inspecttypes/dockercompat" |
| 27 | + "github.com/containerd/nerdctl/v2/pkg/testutil" |
| 28 | + "github.com/containerd/nerdctl/v2/pkg/testutil/nerdtest" |
| 29 | + "github.com/containerd/nerdctl/v2/pkg/testutil/test" |
| 30 | +) |
| 31 | + |
| 32 | +func TestCreate(t *testing.T) { |
| 33 | + testCase := nerdtest.Setup() |
| 34 | + testCase.Setup = func(data test.Data, helpers test.Helpers) { |
| 35 | + helpers.Ensure("create", "--name", data.Identifier("container"), testutil.CommonImage, "echo", "foo") |
| 36 | + data.Set("cID", data.Identifier("container")) |
| 37 | + } |
| 38 | + testCase.Cleanup = func(data test.Data, helpers test.Helpers) { |
| 39 | + helpers.Anyhow("rm", "-f", data.Identifier("container")) |
| 40 | + } |
| 41 | + |
| 42 | + testCase.SubTests = []*test.Case{ |
| 43 | + { |
| 44 | + Description: "ps -a", |
| 45 | + NoParallel: true, |
| 46 | + Command: test.Command("ps", "-a"), |
| 47 | + // FIXME: this might get a false positive if other tests have created a container |
| 48 | + Expected: test.Expects(0, nil, test.Contains("Created")), |
| 49 | + }, |
| 50 | + { |
| 51 | + Description: "start", |
| 52 | + NoParallel: true, |
| 53 | + Command: func(data test.Data, helpers test.Helpers) test.TestableCommand { |
| 54 | + return helpers.Command("start", data.Get("cID")) |
| 55 | + }, |
| 56 | + Expected: test.Expects(0, nil, nil), |
| 57 | + }, |
| 58 | + { |
| 59 | + Description: "logs", |
| 60 | + NoParallel: true, |
| 61 | + Command: func(data test.Data, helpers test.Helpers) test.TestableCommand { |
| 62 | + return helpers.Command("logs", data.Get("cID")) |
| 63 | + }, |
| 64 | + Expected: test.Expects(0, nil, test.Contains("foo")), |
| 65 | + }, |
| 66 | + } |
| 67 | + |
| 68 | + testCase.Run(t) |
| 69 | +} |
| 70 | + |
| 71 | +func TestCreateHyperVContainer(t *testing.T) { |
| 72 | + testCase := nerdtest.Setup() |
| 73 | + |
| 74 | + testCase.Require = nerdtest.HyperV |
| 75 | + |
| 76 | + testCase.Setup = func(data test.Data, helpers test.Helpers) { |
| 77 | + helpers.Ensure("create", "--isolation", "hyperv", "--name", data.Identifier("container"), testutil.CommonImage, "echo", "foo") |
| 78 | + data.Set("cID", data.Identifier("container")) |
| 79 | + } |
| 80 | + |
| 81 | + testCase.Cleanup = func(data test.Data, helpers test.Helpers) { |
| 82 | + helpers.Anyhow("rm", "-f", data.Identifier("container")) |
| 83 | + } |
| 84 | + |
| 85 | + testCase.SubTests = []*test.Case{ |
| 86 | + { |
| 87 | + Description: "ps -a", |
| 88 | + NoParallel: true, |
| 89 | + Command: test.Command("ps", "-a"), |
| 90 | + // FIXME: this might get a false positive if other tests have created a container |
| 91 | + Expected: test.Expects(0, nil, test.Contains("Created")), |
| 92 | + }, |
| 93 | + { |
| 94 | + Description: "start", |
| 95 | + NoParallel: true, |
| 96 | + Setup: func(data test.Data, helpers test.Helpers) { |
| 97 | + helpers.Ensure("start", data.Get("cID")) |
| 98 | + ran := false |
| 99 | + for i := 0; i < 10 && !ran; i++ { |
| 100 | + helpers.Command("container", "inspect", data.Get("cID")). |
| 101 | + Run(&test.Expected{ |
| 102 | + ExitCode: test.ExitCodeNoCheck, |
| 103 | + Output: func(stdout string, info string, t *testing.T) { |
| 104 | + var dc []dockercompat.Container |
| 105 | + err := json.Unmarshal([]byte(stdout), &dc) |
| 106 | + if err != nil || len(dc) == 0 { |
| 107 | + return |
| 108 | + } |
| 109 | + assert.Equal(t, len(dc), 1, "Unexpectedly got multiple results\n"+info) |
| 110 | + ran = dc[0].State.Status == "exited" |
| 111 | + }, |
| 112 | + }) |
| 113 | + time.Sleep(time.Second) |
| 114 | + } |
| 115 | + assert.Assert(t, ran, "container did not ran after 10 seconds") |
| 116 | + }, |
| 117 | + Command: func(data test.Data, helpers test.Helpers) test.TestableCommand { |
| 118 | + return helpers.Command("logs", data.Get("cID")) |
| 119 | + }, |
| 120 | + Expected: test.Expects(0, nil, test.Contains("foo")), |
| 121 | + }, |
| 122 | + } |
| 123 | + |
| 124 | + testCase.Run(t) |
| 125 | +} |
0 commit comments