github.com/argoproj/argo-cd/v2@v2.10.9/applicationset/utils/clusterUtils_test.go (about)

     1  package utils
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  	corev1 "k8s.io/api/core/v1"
    10  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    11  	"k8s.io/apimachinery/pkg/runtime"
    12  	"k8s.io/client-go/kubernetes/fake"
    13  	kubetesting "k8s.io/client-go/testing"
    14  
    15  	argoappv1 "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
    16  )
    17  
    18  const (
    19  	fakeNamespace = "fake-ns"
    20  )
    21  
    22  // From Argo CD util/db/cluster_test.go
    23  func Test_secretToCluster(t *testing.T) {
    24  	secret := &corev1.Secret{
    25  		ObjectMeta: metav1.ObjectMeta{
    26  			Name:      "mycluster",
    27  			Namespace: fakeNamespace,
    28  		},
    29  		Data: map[string][]byte{
    30  			"name":   []byte("test"),
    31  			"server": []byte("http://mycluster"),
    32  			"config": []byte("{\"username\":\"foo\"}"),
    33  		},
    34  	}
    35  	cluster, err := secretToCluster(secret)
    36  	assert.Nil(t, err)
    37  	assert.Equal(t, *cluster, argoappv1.Cluster{
    38  		Name:   "test",
    39  		Server: "http://mycluster",
    40  		Config: argoappv1.ClusterConfig{
    41  			Username: "foo",
    42  		},
    43  	})
    44  }
    45  
    46  // From Argo CD util/db/cluster_test.go
    47  func Test_secretToCluster_NoConfig(t *testing.T) {
    48  	secret := &corev1.Secret{
    49  		ObjectMeta: metav1.ObjectMeta{
    50  			Name:      "mycluster",
    51  			Namespace: fakeNamespace,
    52  		},
    53  		Data: map[string][]byte{
    54  			"name":   []byte("test"),
    55  			"server": []byte("http://mycluster"),
    56  		},
    57  	}
    58  	cluster, err := secretToCluster(secret)
    59  	assert.Nil(t, err)
    60  	assert.Equal(t, *cluster, argoappv1.Cluster{
    61  		Name:   "test",
    62  		Server: "http://mycluster",
    63  	})
    64  }
    65  
    66  func createClusterSecret(secretName string, clusterName string, clusterServer string) *corev1.Secret {
    67  
    68  	secret := &corev1.Secret{
    69  		ObjectMeta: metav1.ObjectMeta{
    70  			Name:      secretName,
    71  			Namespace: fakeNamespace,
    72  			Labels: map[string]string{
    73  				ArgoCDSecretTypeLabel: ArgoCDSecretTypeCluster,
    74  			},
    75  		},
    76  		Data: map[string][]byte{
    77  			"name":   []byte(clusterName),
    78  			"server": []byte(clusterServer),
    79  			"config": []byte("{\"username\":\"foo\",\"password\":\"foo\"}"),
    80  		},
    81  	}
    82  
    83  	return secret
    84  
    85  }
    86  
    87  // From util/argo/argo_test.go
    88  // (ported to use kubeclientset)
    89  func TestValidateDestination(t *testing.T) {
    90  
    91  	t.Run("Validate destination with server url", func(t *testing.T) {
    92  
    93  		dest := argoappv1.ApplicationDestination{
    94  			Server:    "https://127.0.0.1:6443",
    95  			Namespace: "default",
    96  		}
    97  
    98  		appCond := ValidateDestination(context.Background(), &dest, nil, fakeNamespace)
    99  		assert.Nil(t, appCond)
   100  		assert.False(t, dest.IsServerInferred())
   101  	})
   102  
   103  	t.Run("Validate destination with server name", func(t *testing.T) {
   104  		dest := argoappv1.ApplicationDestination{
   105  			Name: "minikube",
   106  		}
   107  
   108  		secret := createClusterSecret("my-secret", "minikube", "https://127.0.0.1:6443")
   109  		objects := []runtime.Object{}
   110  		objects = append(objects, secret)
   111  		kubeclientset := fake.NewSimpleClientset(objects...)
   112  
   113  		appCond := ValidateDestination(context.Background(), &dest, kubeclientset, fakeNamespace)
   114  		assert.Nil(t, appCond)
   115  		assert.Equal(t, "https://127.0.0.1:6443", dest.Server)
   116  		assert.True(t, dest.IsServerInferred())
   117  	})
   118  
   119  	t.Run("Error when having both server url and name", func(t *testing.T) {
   120  		dest := argoappv1.ApplicationDestination{
   121  			Server:    "https://127.0.0.1:6443",
   122  			Name:      "minikube",
   123  			Namespace: "default",
   124  		}
   125  
   126  		err := ValidateDestination(context.Background(), &dest, nil, fakeNamespace)
   127  		assert.Equal(t, "application destination can't have both name and server defined: minikube https://127.0.0.1:6443", err.Error())
   128  		assert.False(t, dest.IsServerInferred())
   129  	})
   130  
   131  	t.Run("List clusters fails", func(t *testing.T) {
   132  		dest := argoappv1.ApplicationDestination{
   133  			Name: "minikube",
   134  		}
   135  		kubeclientset := fake.NewSimpleClientset()
   136  
   137  		kubeclientset.PrependReactor("list", "*", func(action kubetesting.Action) (handled bool, ret runtime.Object, err error) {
   138  			return true, nil, fmt.Errorf("an error occurred")
   139  		})
   140  
   141  		err := ValidateDestination(context.Background(), &dest, kubeclientset, fakeNamespace)
   142  		assert.Equal(t, "unable to find destination server: an error occurred", err.Error())
   143  		assert.False(t, dest.IsServerInferred())
   144  	})
   145  
   146  	t.Run("Destination cluster does not exist", func(t *testing.T) {
   147  		dest := argoappv1.ApplicationDestination{
   148  			Name: "minikube",
   149  		}
   150  
   151  		secret := createClusterSecret("dind", "dind", "https://127.0.0.1:6443")
   152  		objects := []runtime.Object{}
   153  		objects = append(objects, secret)
   154  		kubeclientset := fake.NewSimpleClientset(objects...)
   155  
   156  		err := ValidateDestination(context.Background(), &dest, kubeclientset, fakeNamespace)
   157  		assert.Equal(t, "unable to find destination server: there are no clusters with this name: minikube", err.Error())
   158  		assert.False(t, dest.IsServerInferred())
   159  	})
   160  
   161  	t.Run("Validate too many clusters with the same name", func(t *testing.T) {
   162  		dest := argoappv1.ApplicationDestination{
   163  			Name: "dind",
   164  		}
   165  
   166  		secret := createClusterSecret("dind", "dind", "https://127.0.0.1:2443")
   167  		secret2 := createClusterSecret("dind2", "dind", "https://127.0.0.1:8443")
   168  
   169  		objects := []runtime.Object{}
   170  		objects = append(objects, secret, secret2)
   171  		kubeclientset := fake.NewSimpleClientset(objects...)
   172  
   173  		err := ValidateDestination(context.Background(), &dest, kubeclientset, fakeNamespace)
   174  		assert.Equal(t, "unable to find destination server: there are 2 clusters with the same name: [https://127.0.0.1:2443 https://127.0.0.1:8443]", err.Error())
   175  		assert.False(t, dest.IsServerInferred())
   176  	})
   177  
   178  }