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