github.com/argoproj/argo-cd@v1.8.7/server/cluster/cluster_test.go (about)

     1  package cluster
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/argoproj/gitops-engine/pkg/utils/kube/kubetest"
     9  	"github.com/stretchr/testify/assert"
    10  	"github.com/stretchr/testify/mock"
    11  	"github.com/stretchr/testify/require"
    12  	"k8s.io/client-go/kubernetes/fake"
    13  	"k8s.io/utils/pointer"
    14  
    15  	"github.com/argoproj/argo-cd/common"
    16  	clusterapi "github.com/argoproj/argo-cd/pkg/apiclient/cluster"
    17  	"github.com/argoproj/argo-cd/pkg/apis/application/v1alpha1"
    18  	servercache "github.com/argoproj/argo-cd/server/cache"
    19  	"github.com/argoproj/argo-cd/test"
    20  	cacheutil "github.com/argoproj/argo-cd/util/cache"
    21  	appstatecache "github.com/argoproj/argo-cd/util/cache/appstate"
    22  	dbmocks "github.com/argoproj/argo-cd/util/db/mocks"
    23  	"github.com/argoproj/argo-cd/util/rbac"
    24  )
    25  
    26  func newServerInMemoryCache() *servercache.Cache {
    27  	return servercache.NewCache(
    28  		appstatecache.NewCache(
    29  			cacheutil.NewCache(cacheutil.NewInMemoryCache(1*time.Hour)),
    30  			1*time.Minute,
    31  		),
    32  		1*time.Minute,
    33  		1*time.Minute,
    34  		1*time.Minute,
    35  	)
    36  }
    37  
    38  func newNoopEnforcer() *rbac.Enforcer {
    39  	enf := rbac.NewEnforcer(fake.NewSimpleClientset(test.NewFakeConfigMap()), test.FakeArgoCDNamespace, common.ArgoCDConfigMapName, nil)
    40  	enf.Enforcer.EnableEnforce(false)
    41  	return enf
    42  }
    43  
    44  func TestUpdateCluster_NoFieldsPaths(t *testing.T) {
    45  	db := &dbmocks.ArgoDB{}
    46  	var updated *v1alpha1.Cluster
    47  	db.On("UpdateCluster", mock.Anything, mock.MatchedBy(func(c *v1alpha1.Cluster) bool {
    48  		updated = c
    49  		return true
    50  	})).Return(&v1alpha1.Cluster{}, nil)
    51  
    52  	server := NewServer(db, newNoopEnforcer(), newServerInMemoryCache(), &kubetest.MockKubectlCmd{})
    53  
    54  	_, err := server.Update(context.Background(), &clusterapi.ClusterUpdateRequest{
    55  		Cluster: &v1alpha1.Cluster{
    56  			Name:       "minikube",
    57  			Namespaces: []string{"default", "kube-system"},
    58  		},
    59  	})
    60  
    61  	require.NoError(t, err)
    62  
    63  	assert.Equal(t, updated.Name, "minikube")
    64  	assert.Equal(t, updated.Namespaces, []string{"default", "kube-system"})
    65  }
    66  
    67  func TestUpdateCluster_FieldsPathSet(t *testing.T) {
    68  	db := &dbmocks.ArgoDB{}
    69  	var updated *v1alpha1.Cluster
    70  	db.On("GetCluster", mock.Anything, "https://127.0.0.1").Return(&v1alpha1.Cluster{
    71  		Name:       "minikube",
    72  		Server:     "https://127.0.0.1",
    73  		Namespaces: []string{"default", "kube-system"},
    74  	}, nil)
    75  	db.On("UpdateCluster", mock.Anything, mock.MatchedBy(func(c *v1alpha1.Cluster) bool {
    76  		updated = c
    77  		return true
    78  	})).Return(&v1alpha1.Cluster{}, nil)
    79  
    80  	server := NewServer(db, newNoopEnforcer(), newServerInMemoryCache(), &kubetest.MockKubectlCmd{})
    81  
    82  	_, err := server.Update(context.Background(), &clusterapi.ClusterUpdateRequest{
    83  		Cluster: &v1alpha1.Cluster{
    84  			Server: "https://127.0.0.1",
    85  			Shard:  pointer.Int64Ptr(1),
    86  		},
    87  		UpdatedFields: []string{"shard"},
    88  	})
    89  
    90  	require.NoError(t, err)
    91  
    92  	assert.Equal(t, updated.Name, "minikube")
    93  	assert.Equal(t, updated.Namespaces, []string{"default", "kube-system"})
    94  	assert.Equal(t, *updated.Shard, int64(1))
    95  }