PageRenderTime 143ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/github.com/coreos/rkt/tests/rkt_list_test.go

https://gitlab.com/unofficial-mirrors/openshift-origin
Go | 182 lines | 119 code | 31 blank | 32 comment | 15 complexity | 8eff186b5d99a2df167a8be4866bb15e MD5 | raw file
  1. // Copyright 2015 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. "os"
  19. "strings"
  20. "testing"
  21. "time"
  22. "github.com/coreos/rkt/tests/testutils"
  23. )
  24. const delta = 4 * time.Second
  25. const precision = 2 * time.Second
  26. // compareTime checks if a and b are roughly equal
  27. func compareTime(a time.Time, b time.Time) bool {
  28. diff := a.Sub(b)
  29. if diff < 0 {
  30. diff = -diff
  31. }
  32. return diff < precision
  33. }
  34. func TestRktList(t *testing.T) {
  35. const imgName = "rkt-list-test"
  36. image := patchTestACI(fmt.Sprintf("%s.aci", imgName), fmt.Sprintf("--name=%s", imgName))
  37. defer os.Remove(image)
  38. imageHash := getHashOrPanic(image)
  39. imgID := ImageID{image, imageHash}
  40. ctx := testutils.NewRktRunCtx()
  41. defer ctx.Cleanup()
  42. // Prepare image
  43. cmd := fmt.Sprintf("%s --insecure-options=image prepare %s", ctx.Cmd(), imgID.path)
  44. podUuid := runRktAndGetUUID(t, cmd)
  45. // Get hash
  46. imageID := fmt.Sprintf("sha512-%s", imgID.hash[:12])
  47. // Define tests
  48. tests := []struct {
  49. cmd string
  50. shouldSucceed bool
  51. expect string
  52. }{
  53. // Test that pod UUID is in output
  54. {
  55. "list --full",
  56. true,
  57. podUuid,
  58. },
  59. // Test that image name is in output
  60. {
  61. "list",
  62. true,
  63. imgName,
  64. },
  65. // Test that imageID is in output
  66. {
  67. "list --full",
  68. true,
  69. imageID,
  70. },
  71. // Remove the image
  72. {
  73. fmt.Sprintf("image rm %s", imageID),
  74. true,
  75. "successfully removed",
  76. },
  77. // Name should still show up in rkt list
  78. {
  79. "list",
  80. true,
  81. imgName,
  82. },
  83. // Test that imageID is still in output
  84. {
  85. "list --full",
  86. true,
  87. imageID,
  88. },
  89. }
  90. // Run tests
  91. for i, tt := range tests {
  92. runCmd := fmt.Sprintf("%s %s", ctx.Cmd(), tt.cmd)
  93. t.Logf("Running test #%d, %s", i, runCmd)
  94. runRktAndCheckOutput(t, runCmd, tt.expect, !tt.shouldSucceed)
  95. }
  96. }
  97. func getCreationStartTime(t *testing.T, ctx *testutils.RktRunCtx, imageID string) (creation time.Time, start time.Time) {
  98. // Run rkt list --full
  99. rktCmd := fmt.Sprintf("%s list --full", ctx.Cmd())
  100. child := spawnOrFail(t, rktCmd)
  101. child.Wait()
  102. // Get creation time
  103. match := fmt.Sprintf(".*%s\t.*\t(.*)\t(.*)\t", imageID)
  104. result, out, err := expectRegexWithOutput(child, match)
  105. if err != nil {
  106. t.Fatalf("%q regex not found, Error: %v\nOutput: %v", match, err, out)
  107. }
  108. tmStr := strings.TrimSpace(result[1])
  109. creation, err = time.Parse(defaultTimeLayout, tmStr)
  110. if err != nil {
  111. t.Fatalf("Error parsing creation time: %q", err)
  112. }
  113. tmStr = strings.TrimSpace(result[2])
  114. start, err = time.Parse(defaultTimeLayout, tmStr)
  115. if err != nil {
  116. t.Fatalf("Error parsing start time: %q", err)
  117. }
  118. return creation, start
  119. }
  120. func TestRktListCreatedStarted(t *testing.T) {
  121. const imgName = "rkt-list-creation-start-time-test"
  122. image := patchTestACI(fmt.Sprintf("%s.aci", imgName), fmt.Sprintf("--exec=/inspect --exit-code=0"))
  123. defer os.Remove(image)
  124. imageHash := getHashOrPanic(image)
  125. imgID := ImageID{image, imageHash}
  126. ctx := testutils.NewRktRunCtx()
  127. defer ctx.Cleanup()
  128. // Prepare image
  129. cmd := fmt.Sprintf("%s --insecure-options=image prepare %s", ctx.Cmd(), imgID.path)
  130. podUuid := runRktAndGetUUID(t, cmd)
  131. // t0: prepare
  132. expectPrepare := time.Now()
  133. // Get hash
  134. imageID := fmt.Sprintf("sha512-%s", imgID.hash[:12])
  135. tmpDir := mustTempDir(imgName)
  136. defer os.RemoveAll(tmpDir)
  137. time.Sleep(delta)
  138. // Run image
  139. cmd = fmt.Sprintf("%s run-prepared %s", ctx.Cmd(), podUuid)
  140. rktChild := spawnOrFail(t, cmd)
  141. // t1: run
  142. expectRun := time.Now()
  143. waitOrFail(t, rktChild, 0)
  144. creation, start := getCreationStartTime(t, ctx, imageID)
  145. if !compareTime(expectPrepare, creation) {
  146. t.Fatalf("rkt list returned an incorrect creation time. Got: %q Expect: %q", creation, expectPrepare)
  147. }
  148. if !compareTime(expectRun, start) {
  149. t.Fatalf("rkt list returned an incorrect start time. Got: %q Expect: %q", start, expectRun)
  150. }
  151. }