github.com/sgoings/helm@v2.0.0-alpha.2.0.20170406211108-734e92851ac3+incompatible/pkg/storage/driver/mock_test.go (about) 1 /* 2 Copyright 2016 The Kubernetes Authors All rights reserved. 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 driver // import "k8s.io/helm/pkg/storage/driver" 18 19 import ( 20 "fmt" 21 "testing" 22 23 "k8s.io/kubernetes/pkg/api" 24 kberrs "k8s.io/kubernetes/pkg/api/errors" 25 "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/typed/core/internalversion" 26 27 rspb "k8s.io/helm/pkg/proto/hapi/release" 28 ) 29 30 func releaseStub(name string, vers int32, namespace string, code rspb.Status_Code) *rspb.Release { 31 return &rspb.Release{ 32 Name: name, 33 Version: vers, 34 Namespace: namespace, 35 Info: &rspb.Info{Status: &rspb.Status{Code: code}}, 36 } 37 } 38 39 func testKey(name string, vers int32) string { 40 return fmt.Sprintf("%s.v%d", name, vers) 41 } 42 43 func tsFixtureMemory(t *testing.T) *Memory { 44 hs := []*rspb.Release{ 45 // rls-a 46 releaseStub("rls-a", 4, "default", rspb.Status_DEPLOYED), 47 releaseStub("rls-a", 1, "default", rspb.Status_SUPERSEDED), 48 releaseStub("rls-a", 3, "default", rspb.Status_SUPERSEDED), 49 releaseStub("rls-a", 2, "default", rspb.Status_SUPERSEDED), 50 // rls-b 51 releaseStub("rls-b", 4, "default", rspb.Status_DEPLOYED), 52 releaseStub("rls-b", 1, "default", rspb.Status_SUPERSEDED), 53 releaseStub("rls-b", 3, "default", rspb.Status_SUPERSEDED), 54 releaseStub("rls-b", 2, "default", rspb.Status_SUPERSEDED), 55 } 56 57 mem := NewMemory() 58 for _, tt := range hs { 59 err := mem.Create(testKey(tt.Name, tt.Version), tt) 60 if err != nil { 61 t.Fatalf("Test setup failed to create: %s\n", err) 62 } 63 } 64 return mem 65 } 66 67 // newTestFixture initializes a MockConfigMapsInterface. 68 // ConfigMaps are created for each release provided. 69 func newTestFixtureCfgMaps(t *testing.T, releases ...*rspb.Release) *ConfigMaps { 70 var mock MockConfigMapsInterface 71 mock.Init(t, releases...) 72 73 return NewConfigMaps(&mock) 74 } 75 76 // MockConfigMapsInterface mocks a kubernetes ConfigMapsInterface 77 type MockConfigMapsInterface struct { 78 internalversion.ConfigMapInterface 79 80 objects map[string]*api.ConfigMap 81 } 82 83 // Init initializes the MockConfigMapsInterface with the set of releases. 84 func (mock *MockConfigMapsInterface) Init(t *testing.T, releases ...*rspb.Release) { 85 mock.objects = map[string]*api.ConfigMap{} 86 87 for _, rls := range releases { 88 objkey := testKey(rls.Name, rls.Version) 89 90 cfgmap, err := newConfigMapsObject(objkey, rls, nil) 91 if err != nil { 92 t.Fatalf("Failed to create configmap: %s", err) 93 } 94 mock.objects[objkey] = cfgmap 95 } 96 } 97 98 // Get returns the ConfigMap by name. 99 func (mock *MockConfigMapsInterface) Get(name string) (*api.ConfigMap, error) { 100 object, ok := mock.objects[name] 101 if !ok { 102 return nil, kberrs.NewNotFound(api.Resource("tests"), name) 103 } 104 return object, nil 105 } 106 107 // List returns the a of ConfigMaps. 108 func (mock *MockConfigMapsInterface) List(opts api.ListOptions) (*api.ConfigMapList, error) { 109 var list api.ConfigMapList 110 for _, cfgmap := range mock.objects { 111 list.Items = append(list.Items, *cfgmap) 112 } 113 return &list, nil 114 } 115 116 // Create creates a new ConfigMap. 117 func (mock *MockConfigMapsInterface) Create(cfgmap *api.ConfigMap) (*api.ConfigMap, error) { 118 name := cfgmap.ObjectMeta.Name 119 if object, ok := mock.objects[name]; ok { 120 return object, kberrs.NewAlreadyExists(api.Resource("tests"), name) 121 } 122 mock.objects[name] = cfgmap 123 return cfgmap, nil 124 } 125 126 // Update updates a ConfigMap. 127 func (mock *MockConfigMapsInterface) Update(cfgmap *api.ConfigMap) (*api.ConfigMap, error) { 128 name := cfgmap.ObjectMeta.Name 129 if _, ok := mock.objects[name]; !ok { 130 return nil, kberrs.NewNotFound(api.Resource("tests"), name) 131 } 132 mock.objects[name] = cfgmap 133 return cfgmap, nil 134 } 135 136 // Delete deletes a ConfigMap by name. 137 func (mock *MockConfigMapsInterface) Delete(name string, opts *api.DeleteOptions) error { 138 if _, ok := mock.objects[name]; !ok { 139 return kberrs.NewNotFound(api.Resource("tests"), name) 140 } 141 delete(mock.objects, name) 142 return nil 143 }