github.com/qsis/helm@v3.0.0-beta.3+incompatible/pkg/storage/driver/records_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  	"testing"
    21  
    22  	rspb "helm.sh/helm/pkg/release"
    23  )
    24  
    25  func TestRecordsAdd(t *testing.T) {
    26  	rs := records([]*record{
    27  		newRecord("rls-a.v1", releaseStub("rls-a", 1, "default", rspb.StatusSuperseded)),
    28  		newRecord("rls-a.v2", releaseStub("rls-a", 2, "default", rspb.StatusDeployed)),
    29  	})
    30  
    31  	var tests = []struct {
    32  		desc string
    33  		key  string
    34  		ok   bool
    35  		rec  *record
    36  	}{
    37  		{
    38  			"add valid key",
    39  			"rls-a.v3",
    40  			false,
    41  			newRecord("rls-a.v3", releaseStub("rls-a", 3, "default", rspb.StatusSuperseded)),
    42  		},
    43  		{
    44  			"add already existing key",
    45  			"rls-a.v1",
    46  			true,
    47  			newRecord("rls-a.v1", releaseStub("rls-a", 1, "default", rspb.StatusDeployed)),
    48  		},
    49  	}
    50  
    51  	for _, tt := range tests {
    52  		if err := rs.Add(tt.rec); err != nil {
    53  			if !tt.ok {
    54  				t.Fatalf("failed: %q: %s\n", tt.desc, err)
    55  			}
    56  		}
    57  	}
    58  }
    59  
    60  func TestRecordsRemove(t *testing.T) {
    61  	var tests = []struct {
    62  		desc string
    63  		key  string
    64  		ok   bool
    65  	}{
    66  		{"remove valid key", "rls-a.v1", false},
    67  		{"remove invalid key", "rls-a.v", true},
    68  		{"remove non-existent key", "rls-z.v1", true},
    69  	}
    70  
    71  	rs := records([]*record{
    72  		newRecord("rls-a.v1", releaseStub("rls-a", 1, "default", rspb.StatusSuperseded)),
    73  		newRecord("rls-a.v2", releaseStub("rls-a", 2, "default", rspb.StatusDeployed)),
    74  	})
    75  
    76  	startLen := rs.Len()
    77  
    78  	for _, tt := range tests {
    79  		if r := rs.Remove(tt.key); r == nil {
    80  			if !tt.ok {
    81  				t.Fatalf("Failed to %q (key = %s). Expected nil, got %v",
    82  					tt.desc,
    83  					tt.key,
    84  					r,
    85  				)
    86  			}
    87  		}
    88  	}
    89  
    90  	// We expect the total number of records will be less now than there were
    91  	// when we started.
    92  	endLen := rs.Len()
    93  	if endLen >= startLen {
    94  		t.Errorf("expected ending length %d to be less than starting length %d", endLen, startLen)
    95  	}
    96  }
    97  
    98  func TestRecordsRemoveAt(t *testing.T) {
    99  	rs := records([]*record{
   100  		newRecord("rls-a.v1", releaseStub("rls-a", 1, "default", rspb.StatusSuperseded)),
   101  		newRecord("rls-a.v2", releaseStub("rls-a", 2, "default", rspb.StatusDeployed)),
   102  	})
   103  
   104  	if len(rs) != 2 {
   105  		t.Fatal("Expected len=2 for mock")
   106  	}
   107  
   108  	rs.Remove("rls-a.v1")
   109  	if len(rs) != 1 {
   110  		t.Fatalf("Expected length of rs to be 1, got %d", len(rs))
   111  	}
   112  }