argo-cd
197 строк · 5.1 Кб
1package controller2
3import (4"context"5"testing"6"time"7
8"github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"9"github.com/sirupsen/logrus/hooks/test"10"github.com/stretchr/testify/assert"11"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"12"k8s.io/apimachinery/pkg/runtime"13"k8s.io/client-go/dynamic/fake"14k8sfake "k8s.io/client-go/kubernetes/fake"15"k8s.io/client-go/tools/cache"16)
17
18func TestIsAppSyncStatusRefreshed(t *testing.T) {19logger, _ := test.NewNullLogger()20logEntry := logger.WithField("", "")21
22tests := []struct {23name string24app *unstructured.Unstructured25expectedValue bool26}{27{28name: "No OperationState",29app: &unstructured.Unstructured{30Object: map[string]interface{}{31"status": map[string]interface{}{},32},33},34expectedValue: true,35},36{37name: "No FinishedAt, Completed Phase",38app: &unstructured.Unstructured{39Object: map[string]interface{}{40"status": map[string]interface{}{41"operationState": map[string]interface{}{42"phase": "Succeeded",43},44},45},46},47expectedValue: false,48},49{50name: "FinishedAt After ReconciledAt & ObservedAt",51app: &unstructured.Unstructured{52Object: map[string]interface{}{53"status": map[string]interface{}{54"operationState": map[string]interface{}{55"finishedAt": "2021-01-01T01:05:00Z",56"phase": "Succeeded",57},58"reconciledAt": "2021-01-01T01:02:00Z",59"observedAt": "2021-01-01T01:04:00Z",60},61},62},63expectedValue: false,64},65{66name: "FinishedAt Before ReconciledAt & ObservedAt",67app: &unstructured.Unstructured{68Object: map[string]interface{}{69"status": map[string]interface{}{70"operationState": map[string]interface{}{71"finishedAt": "2021-01-01T01:02:00Z",72"phase": "Succeeded",73},74"reconciledAt": "2021-01-01T01:04:00Z",75"observedAt": "2021-01-01T01:06:00Z",76},77},78},79expectedValue: true,80},81}82
83for _, test := range tests {84t.Run(test.name, func(t *testing.T) {85actualValue := isAppSyncStatusRefreshed(test.app, logEntry)86assert.Equal(t, test.expectedValue, actualValue)87})88}89}
90
91func TestGetAppProj_invalidProjectNestedString(t *testing.T) {92app := &unstructured.Unstructured{93Object: map[string]interface{}{94"spec": map[string]interface{}{},95},96}97informer := cache.NewSharedIndexInformer(nil, nil, 0, nil)98proj := getAppProj(app, informer)99
100assert.Nil(t, proj)101}
102
103func TestInit(t *testing.T) {104scheme := runtime.NewScheme()105err := v1alpha1.SchemeBuilder.AddToScheme(scheme)106if err != nil {107t.Fatalf("Error registering the resource: %v", err)108}109dynamicClient := fake.NewSimpleDynamicClient(scheme)110k8sClient := k8sfake.NewSimpleClientset()111appLabelSelector := "app=test"112
113selfServiceNotificationEnabledFlags := []bool{false, true}114for _, selfServiceNotificationEnabled := range selfServiceNotificationEnabledFlags {115nc := NewController(116k8sClient,117dynamicClient,118nil,119"default",120[]string{},121appLabelSelector,122nil,123"my-secret",124"my-configmap",125selfServiceNotificationEnabled,126)127
128assert.NotNil(t, nc)129
130ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)131defer cancel()132
133err = nc.Init(ctx)134
135assert.NoError(t, err)136}137}
138
139func TestInitTimeout(t *testing.T) {140scheme := runtime.NewScheme()141err := v1alpha1.SchemeBuilder.AddToScheme(scheme)142if err != nil {143t.Fatalf("Error registering the resource: %v", err)144}145dynamicClient := fake.NewSimpleDynamicClient(scheme)146k8sClient := k8sfake.NewSimpleClientset()147appLabelSelector := "app=test"148
149nc := NewController(150k8sClient,151dynamicClient,152nil,153"default",154[]string{},155appLabelSelector,156nil,157"my-secret",158"my-configmap",159false,160)161
162assert.NotNil(t, nc)163
164// Use a short timeout to simulate a timeout during cache synchronization165ctx, cancel := context.WithTimeout(context.Background(), 1*time.Millisecond)166defer cancel()167
168err = nc.Init(ctx)169
170// Expect an error & add assertion for the error message171assert.Error(t, err)172assert.Equal(t, "Timed out waiting for caches to sync", err.Error())173}
174
175func TestCheckAppNotInAdditionalNamespaces(t *testing.T) {176app := &unstructured.Unstructured{177Object: map[string]interface{}{178"spec": map[string]interface{}{},179},180}181namespace := "argocd"182var applicationNamespaces []string183applicationNamespaces = append(applicationNamespaces, "namespace1")184applicationNamespaces = append(applicationNamespaces, "namespace2")185
186// app is in same namespace as controller's namespace187app.SetNamespace(namespace)188assert.False(t, checkAppNotInAdditionalNamespaces(app, namespace, applicationNamespaces))189
190// app is not in the namespace as controller's namespace, but it is in one of the applicationNamespaces191app.SetNamespace("namespace2")192assert.False(t, checkAppNotInAdditionalNamespaces(app, "", applicationNamespaces))193
194// app is not in the namespace as controller's namespace, and it is not in any of the applicationNamespaces195app.SetNamespace("namespace3")196assert.True(t, checkAppNotInAdditionalNamespaces(app, "", applicationNamespaces))197}
198