...

Source file src/k8s.io/kubectl/pkg/cmd/set/helper_test.go

Documentation: k8s.io/kubectl/pkg/cmd/set

     1  /*
     2  Copyright 2021 The Kubernetes 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 set
    18  
    19  import (
    20  	"reflect"
    21  	"testing"
    22  
    23  	v1 "k8s.io/api/core/v1"
    24  )
    25  
    26  func Test_updateEnv(t *testing.T) {
    27  	var (
    28  		env1 = v1.EnvVar{
    29  			Name:  "env1",
    30  			Value: "env1",
    31  		}
    32  		env2 = v1.EnvVar{
    33  			Name:  "env2",
    34  			Value: "env2",
    35  		}
    36  		env3 = v1.EnvVar{
    37  			Name:  "env3",
    38  			Value: "env3",
    39  		}
    40  	)
    41  
    42  	type args struct {
    43  		existing []v1.EnvVar
    44  		add      []v1.EnvVar
    45  		remove   []string
    46  	}
    47  	tests := []struct {
    48  		name string
    49  		args args
    50  		want []v1.EnvVar
    51  	}{
    52  		{
    53  			name: "case 1: add a new and remove another one",
    54  			args: args{
    55  				existing: []v1.EnvVar{env1},
    56  				add:      []v1.EnvVar{env2},
    57  				remove:   []string{env1.Name},
    58  			},
    59  			want: []v1.EnvVar{env2},
    60  		},
    61  		{
    62  			name: "case 2: in a collection of multiple env, add a new and remove another one",
    63  			args: args{
    64  				existing: []v1.EnvVar{env1, env2},
    65  				add:      []v1.EnvVar{env3},
    66  				remove:   []string{env1.Name},
    67  			},
    68  			want: []v1.EnvVar{env2, env3},
    69  		},
    70  		{
    71  			name: "case 3: items added are deduplicated",
    72  			args: args{
    73  				existing: []v1.EnvVar{env1},
    74  				add:      []v1.EnvVar{env2, env2},
    75  				remove:   []string{env1.Name},
    76  			},
    77  			want: []v1.EnvVar{env2},
    78  		},
    79  		{
    80  			name: "case 4: multi add and single remove",
    81  			args: args{
    82  				existing: []v1.EnvVar{env1},
    83  				add:      []v1.EnvVar{env2, env2, env2, env3},
    84  				remove:   []string{env1.Name},
    85  			},
    86  			want: []v1.EnvVar{env2, env3},
    87  		},
    88  		{
    89  			name: "case 5: add and remove the same env",
    90  			args: args{
    91  				existing: []v1.EnvVar{env1},
    92  				add:      []v1.EnvVar{env2, env1},
    93  				remove:   []string{env1.Name, env1.Name},
    94  			},
    95  			want: []v1.EnvVar{env2},
    96  		},
    97  		{
    98  			name: "case 6: existing duplicate unmodified by unrelated addition",
    99  			args: args{
   100  				existing: []v1.EnvVar{env1, env1},
   101  				add:      []v1.EnvVar{env2},
   102  				remove:   nil,
   103  			},
   104  			want: []v1.EnvVar{env1, env1, env2},
   105  		},
   106  		{
   107  			name: "case 7: existing duplicate removed when added yet again",
   108  			args: args{
   109  				existing: []v1.EnvVar{env1, env1, env2},
   110  				add:      []v1.EnvVar{env1},
   111  				remove:   nil,
   112  			},
   113  			want: []v1.EnvVar{env1, env2},
   114  		},
   115  	}
   116  	for _, tt := range tests {
   117  		t.Run(tt.name, func(t *testing.T) {
   118  			if got := updateEnv(tt.args.existing, tt.args.add, tt.args.remove); !reflect.DeepEqual(got, tt.want) {
   119  				t.Errorf("updateEnv() = %v, want %v", got, tt.want)
   120  			}
   121  		})
   122  	}
   123  }
   124  

View as plain text