argo-cd
100 строк · 3.4 Кб
1package controller
2
3import (
4"context"
5"fmt"
6"testing"
7"time"
8
9v1 "k8s.io/api/core/v1"
10metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
11
12"github.com/argoproj/argo-cd/v2/common"
13
14"github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
15appsfake "github.com/argoproj/argo-cd/v2/pkg/client/clientset/versioned/fake"
16appinformers "github.com/argoproj/argo-cd/v2/pkg/client/informers/externalversions/application/v1alpha1"
17applisters "github.com/argoproj/argo-cd/v2/pkg/client/listers/application/v1alpha1"
18cacheutil "github.com/argoproj/argo-cd/v2/util/cache"
19"github.com/argoproj/argo-cd/v2/util/cache/appstate"
20"github.com/argoproj/argo-cd/v2/util/db"
21"github.com/argoproj/argo-cd/v2/util/settings"
22
23clustercache "github.com/argoproj/gitops-engine/pkg/cache"
24"github.com/stretchr/testify/assert"
25"k8s.io/client-go/kubernetes/fake"
26"k8s.io/client-go/tools/cache"
27)
28
29// Expect cluster cache update is persisted in cluster secret
30func TestClusterSecretUpdater(t *testing.T) {
31const fakeNamespace = "fake-ns"
32const updatedK8sVersion = "1.0"
33now := time.Now()
34
35var tests = []struct {
36LastCacheSyncTime *time.Time
37SyncError error
38ExpectedStatus v1alpha1.ConnectionStatus
39}{
40{nil, nil, v1alpha1.ConnectionStatusUnknown},
41{&now, nil, v1alpha1.ConnectionStatusSuccessful},
42{&now, fmt.Errorf("sync failed"), v1alpha1.ConnectionStatusFailed},
43}
44
45emptyArgoCDConfigMap := &v1.ConfigMap{
46ObjectMeta: metav1.ObjectMeta{
47Name: common.ArgoCDConfigMapName,
48Namespace: fakeNamespace,
49Labels: map[string]string{
50"app.kubernetes.io/part-of": "argocd",
51},
52},
53Data: map[string]string{},
54}
55argoCDSecret := &v1.Secret{
56ObjectMeta: metav1.ObjectMeta{
57Name: common.ArgoCDSecretName,
58Namespace: fakeNamespace,
59Labels: map[string]string{
60"app.kubernetes.io/part-of": "argocd",
61},
62},
63Data: map[string][]byte{
64"admin.password": nil,
65"server.secretkey": nil,
66},
67}
68kubeclientset := fake.NewSimpleClientset(emptyArgoCDConfigMap, argoCDSecret)
69appclientset := appsfake.NewSimpleClientset()
70appInformer := appinformers.NewApplicationInformer(appclientset, "", time.Minute, cache.Indexers{})
71settingsManager := settings.NewSettingsManager(context.Background(), kubeclientset, fakeNamespace)
72argoDB := db.NewDB(fakeNamespace, settingsManager, kubeclientset)
73ctx, cancel := context.WithCancel(context.Background())
74defer cancel()
75
76appCache := appstate.NewCache(cacheutil.NewCache(cacheutil.NewInMemoryCache(time.Minute)), time.Minute)
77cluster, err := argoDB.CreateCluster(ctx, &v1alpha1.Cluster{Server: "http://minikube"})
78assert.NoError(t, err, "Test prepare test data create cluster failed")
79
80for _, test := range tests {
81info := &clustercache.ClusterInfo{
82Server: cluster.Server,
83K8SVersion: updatedK8sVersion,
84LastCacheSyncTime: test.LastCacheSyncTime,
85SyncError: test.SyncError,
86}
87
88lister := applisters.NewApplicationLister(appInformer.GetIndexer()).Applications(fakeNamespace)
89updater := NewClusterInfoUpdater(nil, argoDB, lister, appCache, nil, nil, fakeNamespace)
90
91err = updater.updateClusterInfo(context.Background(), *cluster, info)
92assert.NoError(t, err, "Invoking updateClusterInfo failed.")
93
94var clusterInfo v1alpha1.ClusterInfo
95err = appCache.GetClusterInfo(cluster.Server, &clusterInfo)
96assert.NoError(t, err)
97assert.Equal(t, updatedK8sVersion, clusterInfo.ServerVersion)
98assert.Equal(t, test.ExpectedStatus, clusterInfo.ConnectionState.Status)
99}
100}
101