github.com/doitroot/helm@v3.0.0-beta.3+incompatible/pkg/storage/driver/mock_test.go (about)

     1  /*
     2  Copyright The Helm Authors.
     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 "helm.sh/helm/pkg/storage/driver"
    18  
    19  import (
    20  	"fmt"
    21  	"testing"
    22  
    23  	v1 "k8s.io/api/core/v1"
    24  	apierrors "k8s.io/apimachinery/pkg/api/errors"
    25  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    26  	corev1 "k8s.io/client-go/kubernetes/typed/core/v1"
    27  
    28  	rspb "helm.sh/helm/pkg/release"
    29  )
    30  
    31  func releaseStub(name string, vers int, namespace string, status rspb.Status) *rspb.Release {
    32  	return &rspb.Release{
    33  		Name:      name,
    34  		Version:   vers,
    35  		Namespace: namespace,
    36  		Info:      &rspb.Info{Status: status},
    37  	}
    38  }
    39  
    40  func testKey(name string, vers int) 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.StatusDeployed),
    48  		releaseStub("rls-a", 1, "default", rspb.StatusSuperseded),
    49  		releaseStub("rls-a", 3, "default", rspb.StatusSuperseded),
    50  		releaseStub("rls-a", 2, "default", rspb.StatusSuperseded),
    51  		// rls-b
    52  		releaseStub("rls-b", 4, "default", rspb.StatusDeployed),
    53  		releaseStub("rls-b", 1, "default", rspb.StatusSuperseded),
    54  		releaseStub("rls-b", 3, "default", rspb.StatusSuperseded),
    55  		releaseStub("rls-b", 2, "default", rspb.StatusSuperseded),
    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  	corev1.ConfigMapInterface
    80  
    81  	objects map[string]*v1.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]*v1.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) (*v1.ConfigMap, error) {
   101  	object, ok := mock.objects[name]
   102  	if !ok {
   103  		return nil, apierrors.NewNotFound(v1.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) (*v1.ConfigMapList, error) {
   110  	var list v1.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 *v1.ConfigMap) (*v1.ConfigMap, error) {
   119  	name := cfgmap.ObjectMeta.Name
   120  	if object, ok := mock.objects[name]; ok {
   121  		return object, apierrors.NewAlreadyExists(v1.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 *v1.ConfigMap) (*v1.ConfigMap, error) {
   129  	name := cfgmap.ObjectMeta.Name
   130  	if _, ok := mock.objects[name]; !ok {
   131  		return nil, apierrors.NewNotFound(v1.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(v1.Resource("tests"), name)
   141  	}
   142  	delete(mock.objects, name)
   143  	return nil
   144  }
   145  
   146  // newTestFixture initializes a MockSecretsInterface.
   147  // Secrets are created for each release provided.
   148  func newTestFixtureSecrets(t *testing.T, releases ...*rspb.Release) *Secrets {
   149  	var mock MockSecretsInterface
   150  	mock.Init(t, releases...)
   151  
   152  	return NewSecrets(&mock)
   153  }
   154  
   155  // MockSecretsInterface mocks a kubernetes SecretsInterface
   156  type MockSecretsInterface struct {
   157  	corev1.SecretInterface
   158  
   159  	objects map[string]*v1.Secret
   160  }
   161  
   162  // Init initializes the MockSecretsInterface with the set of releases.
   163  func (mock *MockSecretsInterface) Init(t *testing.T, releases ...*rspb.Release) {
   164  	mock.objects = map[string]*v1.Secret{}
   165  
   166  	for _, rls := range releases {
   167  		objkey := testKey(rls.Name, rls.Version)
   168  
   169  		secret, err := newSecretsObject(objkey, rls, nil)
   170  		if err != nil {
   171  			t.Fatalf("Failed to create secret: %s", err)
   172  		}
   173  		mock.objects[objkey] = secret
   174  	}
   175  }
   176  
   177  // Get returns the Secret by name.
   178  func (mock *MockSecretsInterface) Get(name string, options metav1.GetOptions) (*v1.Secret, error) {
   179  	object, ok := mock.objects[name]
   180  	if !ok {
   181  		return nil, apierrors.NewNotFound(v1.Resource("tests"), name)
   182  	}
   183  	return object, nil
   184  }
   185  
   186  // List returns the a of Secret.
   187  func (mock *MockSecretsInterface) List(opts metav1.ListOptions) (*v1.SecretList, error) {
   188  	var list v1.SecretList
   189  	for _, secret := range mock.objects {
   190  		list.Items = append(list.Items, *secret)
   191  	}
   192  	return &list, nil
   193  }
   194  
   195  // Create creates a new Secret.
   196  func (mock *MockSecretsInterface) Create(secret *v1.Secret) (*v1.Secret, error) {
   197  	name := secret.ObjectMeta.Name
   198  	if object, ok := mock.objects[name]; ok {
   199  		return object, apierrors.NewAlreadyExists(v1.Resource("tests"), name)
   200  	}
   201  	mock.objects[name] = secret
   202  	return secret, nil
   203  }
   204  
   205  // Update updates a Secret.
   206  func (mock *MockSecretsInterface) Update(secret *v1.Secret) (*v1.Secret, error) {
   207  	name := secret.ObjectMeta.Name
   208  	if _, ok := mock.objects[name]; !ok {
   209  		return nil, apierrors.NewNotFound(v1.Resource("tests"), name)
   210  	}
   211  	mock.objects[name] = secret
   212  	return secret, nil
   213  }
   214  
   215  // Delete deletes a Secret by name.
   216  func (mock *MockSecretsInterface) Delete(name string, opts *metav1.DeleteOptions) error {
   217  	if _, ok := mock.objects[name]; !ok {
   218  		return apierrors.NewNotFound(v1.Resource("tests"), name)
   219  	}
   220  	delete(mock.objects, name)
   221  	return nil
   222  }