github.com/coreos/rocket@v1.30.1-0.20200224141603-171c416fac02/rkt/metadata_service_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 "reflect" 19 "testing" 20 21 "github.com/appc/spec/schema" 22 "github.com/appc/spec/schema/types" 23 ) 24 25 func setupPodStoreTest(t *testing.T) (*podStore, *types.UUID, string, string) { 26 ps := newPodStore() 27 28 uuid, err := types.NewUUID("de305d54-75b4-431b-adb2-eb6b9e546013") 29 if err != nil { 30 panic("bad uuid literal") 31 } 32 33 ip := "1.2.3.4" 34 app := "myapp" 35 36 pm := &schema.PodManifest{} 37 ps.addPod(uuid, ip, pm) 38 39 im := &schema.ImageManifest{} 40 err = ps.addApp(uuid, app, im) 41 if err != nil { 42 t.Fatalf("addApp failed with %v", err) 43 } 44 45 return ps, uuid, ip, app 46 } 47 48 func TestPodStoreAddApp(t *testing.T) { 49 ps, _, _, app := setupPodStoreTest(t) 50 51 uuid2, err := types.NewUUID("fe305d54-75b4-431b-adb2-eb6b9e546013") 52 if err != nil { 53 panic("bad uuid literal") 54 } 55 56 im := &schema.ImageManifest{} 57 if err = ps.addApp(uuid2, app, im); err != errPodNotFound { 58 t.Errorf("addApp with unknown pod returned: %v", err) 59 } 60 } 61 62 func TestPodStoreGetUUID(t *testing.T) { 63 ps, uuid, ip, _ := setupPodStoreTest(t) 64 65 u, err := ps.getUUID(ip) 66 if err != nil { 67 t.Errorf("getUUID failed with %v", err) 68 } 69 70 if !reflect.DeepEqual(*u, *uuid) { 71 t.Errorf("getUUID mismatch: got %v, expected %v", u, uuid) 72 } 73 74 if _, err := ps.getUUID("2.3.4.5"); err != errPodNotFound { 75 t.Errorf("getUUID with unknown pod returned: %v", err) 76 } 77 } 78 79 func TestPodStoreGetPodManifest(t *testing.T) { 80 ps, _, ip, _ := setupPodStoreTest(t) 81 82 if _, err := ps.getPodManifest(ip); err != nil { 83 t.Errorf("getPodManifest failed with %v", err) 84 } 85 86 if _, err := ps.getPodManifest("2.3.4.5"); err != errPodNotFound { 87 t.Errorf("getPodManifest with unknown pood returned %v", err) 88 } 89 } 90 91 func TestPodStoreGetManifests(t *testing.T) { 92 ps, _, ip, app := setupPodStoreTest(t) 93 94 if _, _, err := ps.getManifests(ip, app); err != nil { 95 t.Errorf("getManifests failed with %v", err) 96 } 97 98 if _, _, err := ps.getManifests("2.3.4.5", app); err != errPodNotFound { 99 t.Errorf("getManifests with unknown pod returned %v", err) 100 } 101 102 if _, _, err := ps.getManifests(ip, "foo"); err != errAppNotFound { 103 t.Errorf("getManifests with unknown app returned %v", err) 104 } 105 } 106 107 func TestPodStoreRemove(t *testing.T) { 108 ps, uuid, _, _ := setupPodStoreTest(t) 109 110 err := ps.remove(uuid) 111 if err != nil { 112 t.Errorf("remove failed with %v", err) 113 } 114 115 if err := ps.remove(uuid); err != errPodNotFound { 116 t.Errorf("remove with unknown pod returned %v", err) 117 } 118 }