github.com/Azure/draft-classic@v0.16.0/pkg/storage/inprocess/store_test.go (about) 1 package inprocess 2 3 import ( 4 "context" 5 "github.com/Azure/draft/pkg/storage" 6 "github.com/golang/protobuf/ptypes" 7 "reflect" 8 "testing" 9 ) 10 11 func TestStoreDeleteBuilds(t *testing.T) { 12 var ( 13 store = NewStoreWithMocks() 14 ctx = context.TODO() 15 ) 16 builds, err := store.DeleteBuilds(ctx, "app1") 17 if err != nil { 18 t.Fatalf("failed to delete build entries: %v", err) 19 } 20 if len(store.builds["app1"]) > 0 { 21 t.Fatal("expected build entries to empty") 22 } 23 assertEqual(t, "DeleteBuilds", builds, []*storage.Object{ 24 objectStub("foo1", "bar1", []byte("foobar1")), 25 objectStub("foo2", "bar2", []byte("foobar2")), 26 objectStub("foo3", "bar3", []byte("foobar3")), 27 objectStub("foo4", "bar4", []byte("foobar4")), 28 }) 29 } 30 31 func TestStoreDeleteBuild(t *testing.T) { 32 var ( 33 store = NewStoreWithMocks() 34 ctx = context.TODO() 35 ) 36 build, err := store.DeleteBuild(ctx, "app1", "foo1") 37 if err != nil { 38 t.Fatalf("failed to delete build entry: %v", err) 39 } 40 assertEqual(t, "DeleteBuild", build, objectStub("foo1", "bar1", []byte("foobar1"))) 41 } 42 43 func TestStoreCreateBuild(t *testing.T) { 44 var ( 45 build = objectStub("foo", "bar", []byte("foobar")) 46 store = NewStoreWithMocks() 47 ctx = context.TODO() 48 ) 49 if err := store.CreateBuild(ctx, "app2", build); err != nil { 50 t.Fatalf("failed to create storage entry: %v", err) 51 } 52 alt, err := store.GetBuild(ctx, "app2", build.BuildID) 53 if err != nil { 54 t.Fatalf("failed to get build entry: %v", err) 55 } 56 assertEqual(t, "CreateBuild", build, alt) 57 58 // try creating a second time; this should fail with ErrAppStorageExists. 59 if err := store.CreateBuild(ctx, "app2", build); err == nil { 60 t.Fatalf("expected second CreateBuild to fail") 61 } 62 } 63 64 func TestStoreUpdateBuild(t *testing.T) { 65 var ( 66 build = objectStub("foo", "bar", []byte("foobar")) 67 store = NewStoreWithMocks() 68 ctx = context.TODO() 69 ) 70 if err := store.UpdateBuild(ctx, "app2", build); err != nil { 71 t.Fatalf("failed to update storage entry: %v", err) 72 } 73 alt, err := store.GetBuild(ctx, "app2", build.BuildID) 74 if err != nil { 75 t.Fatalf("failed to get build entry: %v", err) 76 } 77 assertEqual(t, "UpdateBuild", build, alt) 78 } 79 80 func TestStoreGetBuilds(t *testing.T) { 81 var ( 82 store = NewStoreWithMocks() 83 ctx = context.TODO() 84 ) 85 // make sure the build is returnable by appID 86 ls, err := store.GetBuilds(ctx, "app1") 87 if err != nil { 88 t.Fatalf("could not get builds: %v", err) 89 } 90 assertEqual(t, "GetBuilds", ls, []*storage.Object{ 91 objectStub("foo1", "bar1", []byte("foobar1")), 92 objectStub("foo2", "bar2", []byte("foobar2")), 93 objectStub("foo3", "bar3", []byte("foobar3")), 94 objectStub("foo4", "bar4", []byte("foobar4")), 95 }) 96 // try fetching a build with an unknown appID; should fail. 97 if alt, err := store.GetBuilds(ctx, "bad"); err == nil { 98 t.Fatalf("want err != nil; got alt: %+v", alt) 99 } 100 } 101 102 func TestStoreGetBuild(t *testing.T) { 103 var ( 104 store = NewStoreWithMocks() 105 ctx = context.TODO() 106 ) 107 // make sure the build is returnable by appID 108 obj, err := store.GetBuild(ctx, "app1", "foo1") 109 if err != nil { 110 t.Fatalf("could not get build: %v", err) 111 } 112 assertEqual(t, "GetBuild", obj, objectStub("foo1", "bar1", []byte("foobar1"))) 113 // try fetching a build with an unknown appID; should fail. 114 if alt, err := store.GetBuild(ctx, "bad", ""); err == nil { 115 t.Fatalf("want err != nil; got alt: %+v", alt) 116 } 117 } 118 119 func NewStoreWithMocks() *Store { 120 store := NewStore() 121 store.builds["app1"] = []*storage.Object{ 122 objectStub("foo1", "bar1", []byte("foobar1")), 123 objectStub("foo2", "bar2", []byte("foobar2")), 124 objectStub("foo3", "bar3", []byte("foobar3")), 125 objectStub("foo4", "bar4", []byte("foobar4")), 126 } 127 return store 128 } 129 130 func assertEqual(t *testing.T, label string, a, b interface{}) { 131 if !reflect.DeepEqual(a, b) { 132 t.Errorf("failed equality for %s", label) 133 } 134 } 135 136 func objectStub(buildID, release string, contextID []byte) *storage.Object { 137 return &storage.Object{ 138 BuildID: buildID, 139 Release: release, 140 ContextID: contextID, 141 CreatedAt: createdAt, 142 } 143 } 144 145 var createdAt = ptypes.TimestampNow()