github.com/stackdocker/rkt@v0.10.1-0.20151109095037-1aa827478248/rkt/pods_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 "io/ioutil" 19 "os" 20 "path/filepath" 21 "testing" 22 23 "github.com/coreos/rkt/pkg/lock" 24 ) 25 26 func TestWalkPods(t *testing.T) { 27 tests := [][]*struct { 28 uuid string 29 exited bool 30 garbage bool 31 deleting bool 32 expected bool 33 n_matched int 34 }{ 35 { // nothing 36 }, 37 { // single executing pod 38 { 39 uuid: "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa", 40 exited: false, 41 garbage: false, 42 deleting: false, 43 44 expected: true, 45 }, 46 }, 47 { // single exited pod 48 { 49 uuid: "bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb", 50 exited: true, 51 garbage: false, 52 deleting: false, 53 54 expected: true, 55 }, 56 }, 57 { // single garbage pod 58 { 59 uuid: "cccccccc-cccc-cccc-cccc-cccccccccccc", 60 exited: true, 61 garbage: true, 62 deleting: false, 63 64 expected: true, 65 }, 66 }, 67 { // single deleting pod 68 { 69 uuid: "dddddddd-dddd-dddd-dddd-dddddddddddd", 70 exited: true, 71 garbage: true, 72 deleting: true, 73 74 expected: true, 75 }, 76 }, 77 { // one of each 78 { // executing 79 uuid: "eeeeeeee-eeee-eeee-eeee-eeeeeeeeeeee", 80 exited: false, 81 garbage: false, 82 deleting: false, 83 84 expected: true, 85 }, 86 { // exited 87 uuid: "ffffffff-ffff-ffff-ffff-ffffffffffff", 88 exited: true, 89 garbage: false, 90 deleting: false, 91 92 expected: true, 93 }, 94 { // garbage 95 uuid: "f0f0f0f0-f0f0-f0f0-f0f0-f0f0f0f0f0f0", 96 exited: true, 97 garbage: true, 98 deleting: false, 99 100 expected: true, 101 }, 102 { // deleting 103 uuid: "f1f1f1f1-f1f1-f1f1-f1f1-f1f1f1f1f1f1", 104 exited: true, 105 garbage: true, 106 deleting: true, 107 108 expected: true, 109 }, 110 }, 111 // TODO(vc): update to test new prepared/prepare-failed/non-exited-garbage states.. 112 } 113 114 for _, tt := range tests { 115 // start every test with a clean slate 116 d, err := ioutil.TempDir("", "") 117 if err != nil { 118 t.Fatalf("error creating tmpdir: %v", err) 119 } 120 defer os.RemoveAll(d) 121 122 globalFlags.Dir = d 123 if err := initPods(); err != nil { 124 t.Fatalf("error initializing pods: %v", err) 125 } 126 127 var ( 128 n_expected int 129 n_walked int 130 n_matched int 131 included includeMask 132 ) 133 134 // create the pod dirs as specified by the test 135 for _, ct := range tt { 136 var cp string 137 if ct.garbage { 138 cp = filepath.Join(exitedGarbageDir(), ct.uuid) 139 included |= includeExitedGarbageDir 140 } else { 141 cp = filepath.Join(runDir(), ct.uuid) 142 included |= includeRunDir 143 } 144 145 if err := os.MkdirAll(cp, 0700); err != nil { 146 t.Fatalf("error creating pod directory: %v", err) 147 } 148 149 if !ct.exited || ct.deleting { // acquire lock to simulate running and deleting pods 150 l, err := lock.ExclusiveLock(cp, lock.Dir) 151 if err != nil { 152 t.Fatalf("error locking pod: %v", err) 153 } 154 defer l.Close() 155 } 156 157 if ct.expected { 158 n_expected++ 159 } 160 } 161 162 // match what walk provided against the set in the test 163 if err := walkPods(included, func(ch *pod) { 164 n_walked++ 165 for _, ct := range tt { 166 if ch.uuid.String() == ct.uuid && 167 ch.isExitedGarbage == ct.garbage && 168 ch.isExited == ct.exited && 169 ch.isExitedDeleting == ct.deleting { 170 171 ct.n_matched++ 172 if ct.n_matched > 1 { 173 t.Errorf("no pods should match multiple times") 174 } 175 n_matched++ 176 } 177 } 178 }); err != nil { 179 t.Fatalf("error walking pods: %v", err) 180 } 181 182 if n_expected != n_matched { 183 t.Errorf("walked: %d expected: %d matched: %d", n_walked, n_expected, n_matched) 184 } 185 186 for _, ct := range tt { 187 if ct.expected && ct.n_matched == 0 { 188 t.Errorf("pod %q expected but not matched", ct.uuid) 189 } 190 191 if !ct.expected && ct.n_matched != 0 { 192 t.Errorf("pod %q matched but not expected", ct.uuid) 193 } 194 } 195 } 196 197 }