PageRenderTime 41ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://gitlab.com/unofficial-mirrors/openshift-origin
Go | 169 lines | 132 code | 20 blank | 17 comment | 9 complexity | d7e2690ed56d1eee712ca4adc0b0b9c4 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. package main
  15. import (
  16. "fmt"
  17. "os"
  18. "path/filepath"
  19. "testing"
  20. "github.com/coreos/rkt/tests/testutils"
  21. )
  22. const (
  23. testFile = "test.txt"
  24. testContent = "ThisIsATest"
  25. )
  26. type ExportTestCase struct {
  27. runArgs string
  28. writeArgs string
  29. readArgs string
  30. exportArgs string
  31. expectedResult string
  32. unmountOverlay bool
  33. multiAppPod bool
  34. NeedsOverlay bool
  35. NeedsUserNS bool
  36. }
  37. type exportTest []ExportTestCase
  38. var exportTestCases = map[string]ExportTestCase{
  39. "noOverlaySimpleTest": {
  40. "--no-overlay --insecure-options=image",
  41. "--write-file --file-name=" + testFile + " --content=" + testContent,
  42. "--read-file --file-name=" + testFile,
  43. "",
  44. testContent,
  45. false,
  46. false,
  47. false,
  48. false,
  49. },
  50. "specifiedAppTest": {
  51. "--no-overlay --insecure-options=image",
  52. "--write-file --file-name=" + testFile + " --content=" + testContent,
  53. "--read-file --file-name=" + testFile,
  54. "--app=rkt-inspect",
  55. testContent,
  56. false,
  57. false,
  58. false,
  59. false,
  60. },
  61. "multiAppPodTest": {
  62. "--no-overlay --insecure-options=image",
  63. "--write-file --file-name=" + testFile + " --content=" + testContent,
  64. "--read-file --file-name=" + testFile,
  65. "--app=rkt-inspect",
  66. testContent,
  67. false,
  68. true,
  69. false,
  70. false,
  71. },
  72. "userNS": {
  73. "--private-users --no-overlay --insecure-options=image",
  74. "--write-file --file-name=" + testFile + " --content=" + testContent,
  75. "--read-file --file-name=" + testFile,
  76. "",
  77. testContent,
  78. false,
  79. false,
  80. false,
  81. true,
  82. },
  83. "overlaySimpleTest": {
  84. "--insecure-options=image",
  85. "--write-file --file-name=" + testFile + " --content=" + testContent,
  86. "--read-file --file-name=" + testFile,
  87. "",
  88. testContent,
  89. false,
  90. false,
  91. true,
  92. false,
  93. },
  94. "overlaySimulateReboot": {
  95. "--insecure-options=image",
  96. "--write-file --file-name=" + testFile + " --content=" + testContent,
  97. "--read-file --file-name=" + testFile,
  98. "",
  99. testContent,
  100. true,
  101. false,
  102. true,
  103. false,
  104. },
  105. }
  106. func (ct ExportTestCase) Execute(t *testing.T, ctx *testutils.RktRunCtx) {
  107. tmpDir := mustTempDir("rkt-TestExport-tmp-")
  108. defer os.RemoveAll(tmpDir)
  109. tmpTestAci := filepath.Join(tmpDir, "test.aci")
  110. // Prepare the image with modifications
  111. var additionalRunArgs string
  112. if ct.multiAppPod {
  113. tmpAdditionalAci := patchTestACI("other.aci", "--name=other")
  114. defer os.Remove(tmpAdditionalAci)
  115. const otherArgs = "--write-file --file-name=test.txt --content=NotTheRightContent"
  116. additionalRunArgs = fmt.Sprintf("%s --exec=/inspect -- %s", tmpAdditionalAci, otherArgs)
  117. } else {
  118. additionalRunArgs = ""
  119. }
  120. const runInspect = "%s %s %s %s --exec=/inspect -- %s --- %s"
  121. prepareCmd := fmt.Sprintf(runInspect, ctx.Cmd(), "prepare", ct.runArgs, getInspectImagePath(), ct.writeArgs, additionalRunArgs)
  122. t.Logf("Preparing 'inspect --write-file'")
  123. uuid := runRktAndGetUUID(t, prepareCmd)
  124. runCmd := fmt.Sprintf("%s run-prepared %s", ctx.Cmd(), uuid)
  125. t.Logf("Running 'inspect --write-file'")
  126. child := spawnOrFail(t, runCmd)
  127. waitOrFail(t, child, 0)
  128. if ct.unmountOverlay {
  129. unmountPod(t, ctx, uuid, true)
  130. }
  131. // Export the image
  132. exportCmd := fmt.Sprintf("%s export %s %s %s", ctx.Cmd(), ct.exportArgs, uuid, tmpTestAci)
  133. t.Logf("Running 'export'")
  134. child = spawnOrFail(t, exportCmd)
  135. waitOrFail(t, child, 0)
  136. // Run the newly created ACI and check the output
  137. readCmd := fmt.Sprintf(runInspect, ctx.Cmd(), "run", ct.runArgs, tmpTestAci, ct.readArgs, "")
  138. t.Logf("Running 'inspect --read-file'")
  139. child = spawnOrFail(t, readCmd)
  140. if ct.expectedResult != "" {
  141. if _, out, err := expectRegexWithOutput(child, ct.expectedResult); err != nil {
  142. t.Fatalf("expected %q but not found: %v\n%s", ct.expectedResult, err, out)
  143. }
  144. }
  145. waitOrFail(t, child, 0)
  146. // run garbage collector on pods and images
  147. runGC(t, ctx)
  148. runImageGC(t, ctx)
  149. }