github.com/mirantis/virtlet@v1.5.2-0.20191204181327-1659b8a48e9b/pkg/metadata/metadata_test.go (about) 1 /* 2 Copyright 2018 Mirantis 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package metadata 18 19 import ( 20 "testing" 21 22 "github.com/boltdb/bolt" 23 "github.com/jonboulle/clockwork" 24 25 "github.com/Mirantis/virtlet/pkg/metadata/fake" 26 "github.com/Mirantis/virtlet/pkg/metadata/types" 27 ) 28 29 func dumpDB(t *testing.T, store Store, context string) error { 30 db := store.(*boltClient).db 31 t.Logf("==[ %s ]==> Start DB dump", context) 32 err := db.View(func(tx *bolt.Tx) error { 33 var iterateOverElements func(tx *bolt.Tx, bucket *bolt.Bucket, indent string) 34 iterateOverElements = func(tx *bolt.Tx, bucket *bolt.Bucket, indent string) { 35 var c *bolt.Cursor 36 if bucket == nil { 37 c = tx.Cursor() 38 } else { 39 c = bucket.Cursor() 40 } 41 for k, v := c.First(); k != nil; k, v = c.Next() { 42 if v == nil { 43 // SubBucket 44 t.Logf(" %s BUCKET: %s", indent, string(k)) 45 if bucket == nil { 46 // root bucket 47 iterateOverElements(tx, tx.Bucket(k), " "+indent) 48 } else { 49 iterateOverElements(tx, bucket.Bucket(k), " "+indent) 50 } 51 } else { 52 t.Logf(" %s %s: %s\n", indent, string(k), string(v)) 53 } 54 } 55 } 56 iterateOverElements(tx, nil, "|_") 57 return nil 58 }) 59 t.Logf("==[ %s ]==> End DB dump", context) 60 61 return err 62 } 63 64 func setUpTestStore(t *testing.T, sandboxConfigs []*types.PodSandboxConfig, containerConfigs []*fake.ContainerTestConfig, clock clockwork.Clock) Store { 65 store, err := NewFakeStore() 66 if err != nil { 67 t.Fatal(err) 68 } 69 // Check filter on empty DB 70 sandboxList, err := store.ListPodSandboxes(&types.PodSandboxFilter{}) 71 if err != nil { 72 t.Fatal(err) 73 } 74 if len(sandboxList) != 0 { 75 t.Errorf("ListPodSandboxes() returned non-empty result for an empty db") 76 } 77 78 if clock == nil { 79 clock = clockwork.NewRealClock() 80 } 81 for _, sandbox := range sandboxConfigs { 82 psi, _ := NewPodSandboxInfo(sandbox, "", types.PodSandboxState_SANDBOX_READY, clock) 83 if err := store.PodSandbox(sandbox.Uid).Save( 84 func(c *types.PodSandboxInfo) (*types.PodSandboxInfo, error) { 85 return psi, nil 86 }, 87 ); err != nil { 88 t.Fatal(err) 89 } 90 } 91 92 for _, container := range containerConfigs { 93 ci := &types.ContainerInfo{ 94 Name: container.Name, 95 CreatedAt: clock.Now().UnixNano(), 96 Config: types.VMConfig{ 97 PodSandboxID: container.SandboxID, 98 Image: container.Image, 99 ContainerLabels: container.Labels, 100 ContainerAnnotations: container.Annotations, 101 }, 102 } 103 if err := store.Container(container.ContainerID).Save( 104 func(c *types.ContainerInfo) (*types.ContainerInfo, error) { 105 return ci, nil 106 }, 107 ); err != nil { 108 t.Fatal(err) 109 } 110 } 111 112 dumpDB(t, store, "init") 113 114 return store 115 }