TestIssue2993 tests https://github.com/containerd/nerdctl/issues/2993
(t *testing.T)
| 314 | |
| 315 | // TestIssue2993 tests https://github.com/containerd/nerdctl/issues/2993 |
| 316 | func TestIssue2993(t *testing.T) { |
| 317 | testCase := nerdtest.Setup() |
| 318 | |
| 319 | testCase.Require = require.Not(nerdtest.Docker) |
| 320 | |
| 321 | const ( |
| 322 | containersPathKey = "containersPath" |
| 323 | etchostsPathKey = "etchostsPath" |
| 324 | ) |
| 325 | |
| 326 | getAddrHash := func(addr string) string { |
| 327 | const addrHashLen = 8 |
| 328 | |
| 329 | d := digest.SHA256.FromString(addr) |
| 330 | h := d.Encoded()[0:addrHashLen] |
| 331 | |
| 332 | return h |
| 333 | } |
| 334 | |
| 335 | testCase.SubTests = []*test.Case{ |
| 336 | { |
| 337 | Description: "Issue #2993 - nerdctl no longer leaks containers and etchosts directories and files when container creation fails.", |
| 338 | Setup: func(data test.Data, helpers test.Helpers) { |
| 339 | dataRoot := data.Temp().Path() |
| 340 | |
| 341 | helpers.Ensure("run", "--data-root", dataRoot, "--name", data.Identifier(), "-d", testutil.AlpineImage, "sleep", nerdtest.Infinity) |
| 342 | |
| 343 | h := getAddrHash(defaults.DefaultAddress) |
| 344 | dataStore := filepath.Join(dataRoot, h) |
| 345 | |
| 346 | namespace := string(helpers.Read(nerdtest.Namespace)) |
| 347 | |
| 348 | containersPath := filepath.Join(dataStore, "containers", namespace) |
| 349 | containersDirs, err := os.ReadDir(containersPath) |
| 350 | assert.NilError(t, err) |
| 351 | assert.Equal(t, len(containersDirs), 1) |
| 352 | |
| 353 | etchostsPath := filepath.Join(dataStore, "etchosts", namespace) |
| 354 | etchostsDirs, err := os.ReadDir(etchostsPath) |
| 355 | assert.NilError(t, err) |
| 356 | assert.Equal(t, len(etchostsDirs), 1) |
| 357 | |
| 358 | data.Labels().Set(containersPathKey, containersPath) |
| 359 | data.Labels().Set(etchostsPathKey, etchostsPath) |
| 360 | }, |
| 361 | Cleanup: func(data test.Data, helpers test.Helpers) { |
| 362 | helpers.Anyhow("rm", "--data-root", data.Temp().Path(), "-f", data.Identifier()) |
| 363 | }, |
| 364 | Command: func(data test.Data, helpers test.Helpers) test.TestableCommand { |
| 365 | return helpers.Command("run", "--data-root", data.Temp().Path(), "--name", data.Identifier(), "-d", testutil.AlpineImage, "sleep", nerdtest.Infinity) |
| 366 | }, |
| 367 | Expected: func(data test.Data, helpers test.Helpers) *test.Expected { |
| 368 | return &test.Expected{ |
| 369 | ExitCode: expect.ExitCodeGenericFail, |
| 370 | Errors: []error{errors.New("is already used by ID")}, |
| 371 | Output: func(stdout string, t tig.T) { |
| 372 | containersDirs, err := os.ReadDir(data.Labels().Get(containersPathKey)) |
| 373 | assert.NilError(t, err) |
nothing calls this directly
no test coverage detected
searching dependent graphs…