github.com/grafana/tanka@v0.26.1-0.20240506093700-c22cfc35c21a/pkg/kubernetes/client/apply_test.go (about)

     1  package client
     2  
     3  import (
     4  	"testing"
     5  
     6  	"k8s.io/apimachinery/pkg/util/sets"
     7  
     8  	"github.com/grafana/tanka/pkg/kubernetes/manifest"
     9  )
    10  
    11  func TestKubectl_applyCtl(t *testing.T) {
    12  	info := Info{
    13  		Kubeconfig: Config{
    14  			Context: Context{
    15  				Name: "foo-context",
    16  			},
    17  		},
    18  	}
    19  
    20  	type args struct {
    21  		data manifest.List
    22  		opts ApplyOpts
    23  	}
    24  	tests := []struct {
    25  		name           string
    26  		args           args
    27  		expectedArgs   []string
    28  		unExpectedArgs []string
    29  	}{
    30  		{
    31  			name: "test default",
    32  			args: args{
    33  				opts: ApplyOpts{Validate: true},
    34  			},
    35  			expectedArgs:   []string{"--context", info.Kubeconfig.Context.Name},
    36  			unExpectedArgs: []string{"--force", "--dry-run=server", "--validate=false"},
    37  		},
    38  		{
    39  			name: "test force",
    40  			args: args{
    41  				opts: ApplyOpts{Validate: true, Force: true},
    42  			},
    43  			expectedArgs:   []string{"--force"},
    44  			unExpectedArgs: []string{"--validate=false"},
    45  		},
    46  		{
    47  			name: "test validate",
    48  			args: args{
    49  				opts: ApplyOpts{Validate: false},
    50  			},
    51  			expectedArgs: []string{"--validate=false"},
    52  		},
    53  		{
    54  			name: "test dry-run",
    55  			args: args{
    56  				opts: ApplyOpts{Validate: true, DryRun: "server"},
    57  			},
    58  			expectedArgs:   []string{"--dry-run=server"},
    59  			unExpectedArgs: []string{"--validate=false"},
    60  		},
    61  	}
    62  
    63  	for _, tt := range tests {
    64  		t.Run(tt.name, func(t *testing.T) {
    65  			k := Kubectl{
    66  				info: info,
    67  			}
    68  			got := k.applyCtl(tt.args.data, tt.args.opts)
    69  			gotSet := sets.NewString(got.Args...)
    70  			if !gotSet.HasAll(tt.expectedArgs...) {
    71  				t.Errorf("Kubectl.applyCtl() = %v doesn't have (all) expectedArgs='%v'", got.Args, tt.expectedArgs)
    72  			}
    73  			if gotSet.HasAny(tt.unExpectedArgs...) {
    74  				t.Errorf("Kubectl.applyCtl() = %v has (any) unExpectedArgs='%v'", got.Args, tt.unExpectedArgs)
    75  			}
    76  		})
    77  	}
    78  }