...

Source file src/helm.sh/helm/v3/pkg/storage/driver/cfgmaps_test.go

Documentation: helm.sh/helm/v3/pkg/storage/driver

     1  /*
     2  Copyright The Helm Authors.
     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      http://www.apache.org/licenses/LICENSE-2.0
     7  Unless required by applicable law or agreed to in writing, software
     8  distributed under the License is distributed on an "AS IS" BASIS,
     9  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    10  See the License for the specific language governing permissions and
    11  limitations under the License.
    12  */
    13  
    14  package driver
    15  
    16  import (
    17  	"encoding/base64"
    18  	"encoding/json"
    19  	"reflect"
    20  	"testing"
    21  
    22  	v1 "k8s.io/api/core/v1"
    23  
    24  	rspb "helm.sh/helm/v3/pkg/release"
    25  )
    26  
    27  func TestConfigMapName(t *testing.T) {
    28  	c := newTestFixtureCfgMaps(t)
    29  	if c.Name() != ConfigMapsDriverName {
    30  		t.Errorf("Expected name to be %q, got %q", ConfigMapsDriverName, c.Name())
    31  	}
    32  }
    33  
    34  func TestConfigMapGet(t *testing.T) {
    35  	vers := 1
    36  	name := "smug-pigeon"
    37  	namespace := "default"
    38  	key := testKey(name, vers)
    39  	rel := releaseStub(name, vers, namespace, rspb.StatusDeployed)
    40  
    41  	cfgmaps := newTestFixtureCfgMaps(t, []*rspb.Release{rel}...)
    42  
    43  	// get release with key
    44  	got, err := cfgmaps.Get(key)
    45  	if err != nil {
    46  		t.Fatalf("Failed to get release: %s", err)
    47  	}
    48  	// compare fetched release with original
    49  	if !reflect.DeepEqual(rel, got) {
    50  		t.Errorf("Expected {%v}, got {%v}", rel, got)
    51  	}
    52  }
    53  
    54  func TestUncompressedConfigMapGet(t *testing.T) {
    55  	vers := 1
    56  	name := "smug-pigeon"
    57  	namespace := "default"
    58  	key := testKey(name, vers)
    59  	rel := releaseStub(name, vers, namespace, rspb.StatusDeployed)
    60  
    61  	// Create a test fixture which contains an uncompressed release
    62  	cfgmap, err := newConfigMapsObject(key, rel, nil)
    63  	if err != nil {
    64  		t.Fatalf("Failed to create configmap: %s", err)
    65  	}
    66  	b, err := json.Marshal(rel)
    67  	if err != nil {
    68  		t.Fatalf("Failed to marshal release: %s", err)
    69  	}
    70  	cfgmap.Data["release"] = base64.StdEncoding.EncodeToString(b)
    71  	var mock MockConfigMapsInterface
    72  	mock.objects = map[string]*v1.ConfigMap{key: cfgmap}
    73  	cfgmaps := NewConfigMaps(&mock)
    74  
    75  	// get release with key
    76  	got, err := cfgmaps.Get(key)
    77  	if err != nil {
    78  		t.Fatalf("Failed to get release: %s", err)
    79  	}
    80  	// compare fetched release with original
    81  	if !reflect.DeepEqual(rel, got) {
    82  		t.Errorf("Expected {%v}, got {%v}", rel, got)
    83  	}
    84  }
    85  
    86  func TestConfigMapList(t *testing.T) {
    87  	cfgmaps := newTestFixtureCfgMaps(t, []*rspb.Release{
    88  		releaseStub("key-1", 1, "default", rspb.StatusUninstalled),
    89  		releaseStub("key-2", 1, "default", rspb.StatusUninstalled),
    90  		releaseStub("key-3", 1, "default", rspb.StatusDeployed),
    91  		releaseStub("key-4", 1, "default", rspb.StatusDeployed),
    92  		releaseStub("key-5", 1, "default", rspb.StatusSuperseded),
    93  		releaseStub("key-6", 1, "default", rspb.StatusSuperseded),
    94  	}...)
    95  
    96  	// list all deleted releases
    97  	del, err := cfgmaps.List(func(rel *rspb.Release) bool {
    98  		return rel.Info.Status == rspb.StatusUninstalled
    99  	})
   100  	// check
   101  	if err != nil {
   102  		t.Errorf("Failed to list deleted: %s", err)
   103  	}
   104  	if len(del) != 2 {
   105  		t.Errorf("Expected 2 deleted, got %d:\n%v\n", len(del), del)
   106  	}
   107  
   108  	// list all deployed releases
   109  	dpl, err := cfgmaps.List(func(rel *rspb.Release) bool {
   110  		return rel.Info.Status == rspb.StatusDeployed
   111  	})
   112  	// check
   113  	if err != nil {
   114  		t.Errorf("Failed to list deployed: %s", err)
   115  	}
   116  	if len(dpl) != 2 {
   117  		t.Errorf("Expected 2 deployed, got %d", len(dpl))
   118  	}
   119  
   120  	// list all superseded releases
   121  	ssd, err := cfgmaps.List(func(rel *rspb.Release) bool {
   122  		return rel.Info.Status == rspb.StatusSuperseded
   123  	})
   124  	// check
   125  	if err != nil {
   126  		t.Errorf("Failed to list superseded: %s", err)
   127  	}
   128  	if len(ssd) != 2 {
   129  		t.Errorf("Expected 2 superseded, got %d", len(ssd))
   130  	}
   131  	// Check if release having both system and custom labels, this is needed to ensure that selector filtering would work.
   132  	rls := ssd[0]
   133  	_, ok := rls.Labels["name"]
   134  	if !ok {
   135  		t.Fatalf("Expected 'name' label in results, actual %v", rls.Labels)
   136  	}
   137  	_, ok = rls.Labels["key1"]
   138  	if !ok {
   139  		t.Fatalf("Expected 'key1' label in results, actual %v", rls.Labels)
   140  	}
   141  }
   142  
   143  func TestConfigMapQuery(t *testing.T) {
   144  	cfgmaps := newTestFixtureCfgMaps(t, []*rspb.Release{
   145  		releaseStub("key-1", 1, "default", rspb.StatusUninstalled),
   146  		releaseStub("key-2", 1, "default", rspb.StatusUninstalled),
   147  		releaseStub("key-3", 1, "default", rspb.StatusDeployed),
   148  		releaseStub("key-4", 1, "default", rspb.StatusDeployed),
   149  		releaseStub("key-5", 1, "default", rspb.StatusSuperseded),
   150  		releaseStub("key-6", 1, "default", rspb.StatusSuperseded),
   151  	}...)
   152  
   153  	rls, err := cfgmaps.Query(map[string]string{"status": "deployed"})
   154  	if err != nil {
   155  		t.Errorf("Failed to query: %s", err)
   156  	}
   157  	if len(rls) != 2 {
   158  		t.Errorf("Expected 2 results, got %d", len(rls))
   159  	}
   160  
   161  	_, err = cfgmaps.Query(map[string]string{"name": "notExist"})
   162  	if err != ErrReleaseNotFound {
   163  		t.Errorf("Expected {%v}, got {%v}", ErrReleaseNotFound, err)
   164  	}
   165  }
   166  
   167  func TestConfigMapCreate(t *testing.T) {
   168  	cfgmaps := newTestFixtureCfgMaps(t)
   169  
   170  	vers := 1
   171  	name := "smug-pigeon"
   172  	namespace := "default"
   173  	key := testKey(name, vers)
   174  	rel := releaseStub(name, vers, namespace, rspb.StatusDeployed)
   175  
   176  	// store the release in a configmap
   177  	if err := cfgmaps.Create(key, rel); err != nil {
   178  		t.Fatalf("Failed to create release with key %q: %s", key, err)
   179  	}
   180  
   181  	// get the release back
   182  	got, err := cfgmaps.Get(key)
   183  	if err != nil {
   184  		t.Fatalf("Failed to get release with key %q: %s", key, err)
   185  	}
   186  
   187  	// compare created release with original
   188  	if !reflect.DeepEqual(rel, got) {
   189  		t.Errorf("Expected {%v}, got {%v}", rel, got)
   190  	}
   191  }
   192  
   193  func TestConfigMapUpdate(t *testing.T) {
   194  	vers := 1
   195  	name := "smug-pigeon"
   196  	namespace := "default"
   197  	key := testKey(name, vers)
   198  	rel := releaseStub(name, vers, namespace, rspb.StatusDeployed)
   199  
   200  	cfgmaps := newTestFixtureCfgMaps(t, []*rspb.Release{rel}...)
   201  
   202  	// modify release status code
   203  	rel.Info.Status = rspb.StatusSuperseded
   204  
   205  	// perform the update
   206  	if err := cfgmaps.Update(key, rel); err != nil {
   207  		t.Fatalf("Failed to update release: %s", err)
   208  	}
   209  
   210  	// fetch the updated release
   211  	got, err := cfgmaps.Get(key)
   212  	if err != nil {
   213  		t.Fatalf("Failed to get release with key %q: %s", key, err)
   214  	}
   215  
   216  	// check release has actually been updated by comparing modified fields
   217  	if rel.Info.Status != got.Info.Status {
   218  		t.Errorf("Expected status %s, got status %s", rel.Info.Status.String(), got.Info.Status.String())
   219  	}
   220  }
   221  
   222  func TestConfigMapDelete(t *testing.T) {
   223  	vers := 1
   224  	name := "smug-pigeon"
   225  	namespace := "default"
   226  	key := testKey(name, vers)
   227  	rel := releaseStub(name, vers, namespace, rspb.StatusDeployed)
   228  
   229  	cfgmaps := newTestFixtureCfgMaps(t, []*rspb.Release{rel}...)
   230  
   231  	// perform the delete on a non-existent release
   232  	_, err := cfgmaps.Delete("nonexistent")
   233  	if err != ErrReleaseNotFound {
   234  		t.Fatalf("Expected ErrReleaseNotFound: got {%v}", err)
   235  	}
   236  
   237  	// perform the delete
   238  	rls, err := cfgmaps.Delete(key)
   239  	if err != nil {
   240  		t.Fatalf("Failed to delete release with key %q: %s", key, err)
   241  	}
   242  	if !reflect.DeepEqual(rel, rls) {
   243  		t.Errorf("Expected {%v}, got {%v}", rel, rls)
   244  	}
   245  
   246  	// fetch the deleted release
   247  	_, err = cfgmaps.Get(key)
   248  	if !reflect.DeepEqual(ErrReleaseNotFound, err) {
   249  		t.Errorf("Expected {%v}, got {%v}", ErrReleaseNotFound, err)
   250  	}
   251  }
   252  

View as plain text