github.com/argoproj/argo-cd/v3@v3.2.1/cmd/argocd/commands/cluster_test.go (about) 1 package commands 2 3 import ( 4 "testing" 5 6 "github.com/stretchr/testify/assert" 7 "github.com/stretchr/testify/require" 8 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 9 "k8s.io/client-go/rest" 10 "k8s.io/client-go/tools/clientcmd" 11 12 "github.com/argoproj/argo-cd/v3/pkg/apis/application/v1alpha1" 13 ) 14 15 func Test_getQueryBySelector(t *testing.T) { 16 query := getQueryBySelector("my-cluster") 17 assert.Equal(t, "my-cluster", query.Name) 18 assert.Empty(t, query.Server) 19 20 query = getQueryBySelector("http://my-server") 21 assert.Empty(t, query.Name) 22 assert.Equal(t, "http://my-server", query.Server) 23 24 query = getQueryBySelector("https://my-server") 25 assert.Empty(t, query.Name) 26 assert.Equal(t, "https://my-server", query.Server) 27 } 28 29 func Test_printClusterTable(_ *testing.T) { 30 printClusterTable([]v1alpha1.Cluster{ 31 { 32 Server: "my-server", 33 Name: "my-name", 34 Config: v1alpha1.ClusterConfig{ 35 Username: "my-username", 36 Password: "my-password", 37 BearerToken: "my-bearer-token", 38 TLSClientConfig: v1alpha1.TLSClientConfig{}, 39 AWSAuthConfig: nil, 40 DisableCompression: false, 41 }, 42 ConnectionState: v1alpha1.ConnectionState{ 43 Status: "my-status", 44 Message: "my-message", 45 ModifiedAt: &metav1.Time{}, 46 }, 47 ServerVersion: "my-version", 48 }, 49 }) 50 } 51 52 func Test_getRestConfig(t *testing.T) { 53 type args struct { 54 pathOpts *clientcmd.PathOptions 55 ctxName string 56 } 57 pathOpts := &clientcmd.PathOptions{ 58 GlobalFile: "./testdata/config", 59 LoadingRules: clientcmd.NewDefaultClientConfigLoadingRules(), 60 } 61 tests := []struct { 62 name string 63 args args 64 expected *rest.Config 65 wantErr bool 66 expectedErr string 67 }{ 68 { 69 "Load config for context successfully", 70 args{ 71 pathOpts, 72 "argocd2.example.com:443", 73 }, 74 &rest.Config{Host: "argocd2.example.com:443"}, 75 false, 76 "", 77 }, 78 { 79 "Load config for current-context successfully", 80 args{ 81 pathOpts, 82 "localhost:8080", 83 }, 84 &rest.Config{Host: "localhost:8080"}, 85 false, 86 "", 87 }, 88 { 89 "Context not found", 90 args{ 91 pathOpts, 92 "not-exist", 93 }, 94 nil, 95 true, 96 "context not-exist does not exist in kubeconfig", 97 }, 98 } 99 for _, tt := range tests { 100 t.Run(tt.name, func(t *testing.T) { 101 got, err := getRestConfig(tt.args.pathOpts, tt.args.ctxName) 102 if tt.wantErr { 103 require.EqualError(t, err, tt.expectedErr) 104 } else { 105 require.NoErrorf(t, err, "An unexpected error occurred during test %s", tt.name) 106 require.Equal(t, tt.expected, got) 107 } 108 }) 109 } 110 }