argo-cd

Форк
0
/
clusterinfoupdater_test.go 
100 строк · 3.4 Кб
1
package controller
2

3
import (
4
	"context"
5
	"fmt"
6
	"testing"
7
	"time"
8

9
	v1 "k8s.io/api/core/v1"
10
	metav1 "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"
15
	appsfake "github.com/argoproj/argo-cd/v2/pkg/client/clientset/versioned/fake"
16
	appinformers "github.com/argoproj/argo-cd/v2/pkg/client/informers/externalversions/application/v1alpha1"
17
	applisters "github.com/argoproj/argo-cd/v2/pkg/client/listers/application/v1alpha1"
18
	cacheutil "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

23
	clustercache "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
30
func TestClusterSecretUpdater(t *testing.T) {
31
	const fakeNamespace = "fake-ns"
32
	const updatedK8sVersion = "1.0"
33
	now := time.Now()
34

35
	var tests = []struct {
36
		LastCacheSyncTime *time.Time
37
		SyncError         error
38
		ExpectedStatus    v1alpha1.ConnectionStatus
39
	}{
40
		{nil, nil, v1alpha1.ConnectionStatusUnknown},
41
		{&now, nil, v1alpha1.ConnectionStatusSuccessful},
42
		{&now, fmt.Errorf("sync failed"), v1alpha1.ConnectionStatusFailed},
43
	}
44

45
	emptyArgoCDConfigMap := &v1.ConfigMap{
46
		ObjectMeta: metav1.ObjectMeta{
47
			Name:      common.ArgoCDConfigMapName,
48
			Namespace: fakeNamespace,
49
			Labels: map[string]string{
50
				"app.kubernetes.io/part-of": "argocd",
51
			},
52
		},
53
		Data: map[string]string{},
54
	}
55
	argoCDSecret := &v1.Secret{
56
		ObjectMeta: metav1.ObjectMeta{
57
			Name:      common.ArgoCDSecretName,
58
			Namespace: fakeNamespace,
59
			Labels: map[string]string{
60
				"app.kubernetes.io/part-of": "argocd",
61
			},
62
		},
63
		Data: map[string][]byte{
64
			"admin.password":   nil,
65
			"server.secretkey": nil,
66
		},
67
	}
68
	kubeclientset := fake.NewSimpleClientset(emptyArgoCDConfigMap, argoCDSecret)
69
	appclientset := appsfake.NewSimpleClientset()
70
	appInformer := appinformers.NewApplicationInformer(appclientset, "", time.Minute, cache.Indexers{})
71
	settingsManager := settings.NewSettingsManager(context.Background(), kubeclientset, fakeNamespace)
72
	argoDB := db.NewDB(fakeNamespace, settingsManager, kubeclientset)
73
	ctx, cancel := context.WithCancel(context.Background())
74
	defer cancel()
75

76
	appCache := appstate.NewCache(cacheutil.NewCache(cacheutil.NewInMemoryCache(time.Minute)), time.Minute)
77
	cluster, err := argoDB.CreateCluster(ctx, &v1alpha1.Cluster{Server: "http://minikube"})
78
	assert.NoError(t, err, "Test prepare test data create cluster failed")
79

80
	for _, test := range tests {
81
		info := &clustercache.ClusterInfo{
82
			Server:            cluster.Server,
83
			K8SVersion:        updatedK8sVersion,
84
			LastCacheSyncTime: test.LastCacheSyncTime,
85
			SyncError:         test.SyncError,
86
		}
87

88
		lister := applisters.NewApplicationLister(appInformer.GetIndexer()).Applications(fakeNamespace)
89
		updater := NewClusterInfoUpdater(nil, argoDB, lister, appCache, nil, nil, fakeNamespace)
90

91
		err = updater.updateClusterInfo(context.Background(), *cluster, info)
92
		assert.NoError(t, err, "Invoking updateClusterInfo failed.")
93

94
		var clusterInfo v1alpha1.ClusterInfo
95
		err = appCache.GetClusterInfo(cluster.Server, &clusterInfo)
96
		assert.NoError(t, err)
97
		assert.Equal(t, updatedK8sVersion, clusterInfo.ServerVersion)
98
		assert.Equal(t, test.ExpectedStatus, clusterInfo.ConnectionState.Status)
99
	}
100
}
101

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.