PageRenderTime 4998ms CodeModel.GetById 31ms RepoModel.GetById 5ms app.codeStats 0ms

/tests/rkt_gc_test.go

https://gitlab.com/admin-github-cloud/rkt
Go | 120 lines | 77 code | 25 blank | 18 comment | 17 complexity | 58e4828f6f22cbfa10ada27587a54251 MD5 | raw file
  1. // Copyright 2016 The rkt 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. // +build host coreos src kvm
  15. package main
  16. import (
  17. "fmt"
  18. "io/ioutil"
  19. "os"
  20. "path/filepath"
  21. "testing"
  22. "github.com/coreos/rkt/common"
  23. "github.com/coreos/rkt/tests/testutils"
  24. )
  25. func TestGC(t *testing.T) {
  26. ctx := testutils.NewRktRunCtx()
  27. defer ctx.Cleanup()
  28. imagePath := getInspectImagePath()
  29. // Finished pods.
  30. importImageAndRun(imagePath, t, ctx)
  31. // Prepared pods.
  32. importImageAndPrepare(imagePath, t, ctx)
  33. // Abort prepare.
  34. cmd := fmt.Sprintf("%s --insecure-options=image prepare %s %s", ctx.Cmd(), imagePath, imagePath)
  35. spawnAndWaitOrFail(t, cmd, 1)
  36. gcCmd := fmt.Sprintf("%s gc --mark-only=true --expire-prepared=0 --grace-period=0", ctx.Cmd())
  37. spawnAndWaitOrFail(t, gcCmd, 0)
  38. pods := podsRemaining(t, ctx)
  39. if len(pods) == 0 {
  40. t.Fatalf("pods should still be present in rkt's data directory")
  41. }
  42. gcCmd = fmt.Sprintf("%s gc --mark-only=false --expire-prepared=0 --grace-period=0", ctx.Cmd())
  43. spawnAndWaitOrFail(t, gcCmd, 0)
  44. pods = podsRemaining(t, ctx)
  45. if len(pods) != 0 {
  46. t.Fatalf("no pods should exist rkt's data directory, but found: %v", pods)
  47. }
  48. }
  49. func podsRemaining(t *testing.T, ctx *testutils.RktRunCtx) []os.FileInfo {
  50. gcDirs := []string{
  51. filepath.Join(ctx.DataDir(), "pods", "exited-garbage"),
  52. filepath.Join(ctx.DataDir(), "pods", "prepared"),
  53. filepath.Join(ctx.DataDir(), "pods", "garbage"),
  54. filepath.Join(ctx.DataDir(), "pods", "run"),
  55. }
  56. var remainingPods []os.FileInfo
  57. for _, dir := range gcDirs {
  58. pods, err := ioutil.ReadDir(dir)
  59. if err != nil {
  60. t.Fatalf("cannot read gc directory %q: %v", dir, err)
  61. }
  62. remainingPods = append(remainingPods, pods...)
  63. }
  64. return remainingPods
  65. }
  66. func TestGCAfterUnmount(t *testing.T) {
  67. if !common.SupportsOverlay() {
  68. t.Skip("Overlay fs not supported.")
  69. }
  70. ctx := testutils.NewRktRunCtx()
  71. defer ctx.Cleanup()
  72. imagePath := getInspectImagePath()
  73. for _, rmNetns := range []bool{false, true} {
  74. _, err := importImageAndFetchHash(t, ctx, "", imagePath)
  75. if err != nil {
  76. t.Fatalf("%v", err)
  77. }
  78. cmd := fmt.Sprintf("%s --insecure-options=image prepare %s", ctx.Cmd(), imagePath)
  79. uuid := runRktAndGetUUID(t, cmd)
  80. cmd = fmt.Sprintf("%s run-prepared %s", ctx.Cmd(), uuid)
  81. runRktAndCheckOutput(t, cmd, "", false)
  82. unmountPod(t, ctx, uuid, rmNetns)
  83. pods := podsRemaining(t, ctx)
  84. if len(pods) == 0 {
  85. t.Fatalf("pods should still be present in rkt's data directory")
  86. }
  87. gcCmd := fmt.Sprintf("%s gc --mark-only=false --expire-prepared=0 --grace-period=0", ctx.Cmd())
  88. // check we don't get any output (an error) after "executing net-plugin..."
  89. runRktAndCheckRegexOutput(t, gcCmd, `executing net-plugin .*\n\z`)
  90. pods = podsRemaining(t, ctx)
  91. if len(pods) != 0 {
  92. t.Fatalf("no pods should exist rkt's data directory, but found: %v", pods)
  93. }
  94. }
  95. }