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