github.com/argoproj/argo-cd@v1.8.7/cmd/argocd-util/commands/apps_test.go (about) 1 package commands 2 3 import ( 4 "testing" 5 6 "github.com/argoproj/argo-cd/test" 7 8 clustermocks "github.com/argoproj/gitops-engine/pkg/cache/mocks" 9 "github.com/argoproj/gitops-engine/pkg/health" 10 "github.com/argoproj/gitops-engine/pkg/utils/kube" 11 "github.com/stretchr/testify/assert" 12 "github.com/stretchr/testify/mock" 13 corev1 "k8s.io/api/core/v1" 14 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 15 "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" 16 kubefake "k8s.io/client-go/kubernetes/fake" 17 "k8s.io/client-go/tools/cache" 18 19 "github.com/argoproj/argo-cd/common" 20 statecache "github.com/argoproj/argo-cd/controller/cache" 21 cachemocks "github.com/argoproj/argo-cd/controller/cache/mocks" 22 "github.com/argoproj/argo-cd/controller/metrics" 23 "github.com/argoproj/argo-cd/pkg/apis/application/v1alpha1" 24 appfake "github.com/argoproj/argo-cd/pkg/client/clientset/versioned/fake" 25 "github.com/argoproj/argo-cd/reposerver/apiclient" 26 "github.com/argoproj/argo-cd/reposerver/apiclient/mocks" 27 "github.com/argoproj/argo-cd/util/db" 28 "github.com/argoproj/argo-cd/util/settings" 29 ) 30 31 func TestGetReconcileResults(t *testing.T) { 32 appClientset := appfake.NewSimpleClientset(&v1alpha1.Application{ 33 ObjectMeta: metav1.ObjectMeta{ 34 Name: "test", 35 Namespace: "default", 36 }, 37 Status: v1alpha1.ApplicationStatus{ 38 Health: v1alpha1.HealthStatus{Status: health.HealthStatusHealthy}, 39 Sync: v1alpha1.SyncStatus{Status: v1alpha1.SyncStatusCodeOutOfSync}, 40 }, 41 }) 42 43 result, err := getReconcileResults(appClientset, "default", "") 44 if !assert.NoError(t, err) { 45 return 46 } 47 48 expectedResults := []appReconcileResult{{ 49 Name: "test", 50 Health: &v1alpha1.HealthStatus{Status: health.HealthStatusHealthy}, 51 Sync: &v1alpha1.SyncStatus{Status: v1alpha1.SyncStatusCodeOutOfSync}, 52 }} 53 assert.ElementsMatch(t, expectedResults, result) 54 } 55 56 func TestGetReconcileResults_Refresh(t *testing.T) { 57 cm := corev1.ConfigMap{ 58 ObjectMeta: metav1.ObjectMeta{ 59 Name: "argocd-cm", 60 Namespace: "default", 61 Labels: map[string]string{ 62 "app.kubernetes.io/part-of": "argocd", 63 }, 64 }, 65 } 66 proj := &v1alpha1.AppProject{ 67 ObjectMeta: metav1.ObjectMeta{ 68 Name: "default", 69 Namespace: "default", 70 }, 71 Spec: v1alpha1.AppProjectSpec{Destinations: []v1alpha1.ApplicationDestination{{Namespace: "*", Server: "*"}}}, 72 } 73 74 app := &v1alpha1.Application{ 75 ObjectMeta: metav1.ObjectMeta{ 76 Name: "test", 77 Namespace: "default", 78 }, 79 Spec: v1alpha1.ApplicationSpec{ 80 Project: "default", 81 Destination: v1alpha1.ApplicationDestination{ 82 Server: common.KubernetesInternalAPIServerAddr, 83 Namespace: "default", 84 }, 85 }, 86 } 87 88 appClientset := appfake.NewSimpleClientset(app, proj) 89 deployment := test.NewDeployment() 90 kubeClientset := kubefake.NewSimpleClientset(deployment, &cm) 91 clusterCache := clustermocks.ClusterCache{} 92 clusterCache.On("IsNamespaced", mock.Anything).Return(true, nil) 93 repoServerClient := mocks.RepoServerServiceClient{} 94 repoServerClient.On("GenerateManifest", mock.Anything, mock.Anything).Return(&apiclient.ManifestResponse{ 95 Manifests: []string{test.DeploymentManifest}, 96 }, nil) 97 repoServerClientset := mocks.Clientset{RepoServerServiceClient: &repoServerClient} 98 liveStateCache := cachemocks.LiveStateCache{} 99 liveStateCache.On("GetManagedLiveObjs", mock.Anything, mock.Anything).Return(map[kube.ResourceKey]*unstructured.Unstructured{ 100 kube.GetResourceKey(deployment): deployment, 101 }, nil) 102 liveStateCache.On("GetVersionsInfo", mock.Anything).Return("v1.2.3", nil, nil) 103 liveStateCache.On("Init").Return(nil, nil) 104 liveStateCache.On("GetClusterCache", mock.Anything).Return(&clusterCache, nil) 105 liveStateCache.On("IsNamespaced", mock.Anything, mock.Anything).Return(true, nil) 106 107 result, err := reconcileApplications(kubeClientset, appClientset, "default", &repoServerClientset, "", 108 func(argoDB db.ArgoDB, appInformer cache.SharedIndexInformer, settingsMgr *settings.SettingsManager, server *metrics.MetricsServer) statecache.LiveStateCache { 109 return &liveStateCache 110 }, 111 ) 112 113 if !assert.NoError(t, err) { 114 return 115 } 116 117 assert.Equal(t, result[0].Health.Status, health.HealthStatusMissing) 118 assert.Equal(t, result[0].Sync.Status, v1alpha1.SyncStatusCodeOutOfSync) 119 } 120 121 func TestDiffReconcileResults_NoDifferences(t *testing.T) { 122 logs, err := captureStdout(func() { 123 assert.NoError(t, diffReconcileResults( 124 reconcileResults{Applications: []appReconcileResult{{ 125 Name: "app1", 126 Sync: &v1alpha1.SyncStatus{Status: v1alpha1.SyncStatusCodeOutOfSync}, 127 }}}, 128 reconcileResults{Applications: []appReconcileResult{{ 129 Name: "app1", 130 Sync: &v1alpha1.SyncStatus{Status: v1alpha1.SyncStatusCodeOutOfSync}, 131 }}}, 132 )) 133 }) 134 assert.NoError(t, err) 135 assert.Equal(t, "app1\n", logs) 136 } 137 138 func TestDiffReconcileResults_DifferentApps(t *testing.T) { 139 logs, err := captureStdout(func() { 140 assert.NoError(t, diffReconcileResults( 141 reconcileResults{Applications: []appReconcileResult{{ 142 Name: "app1", 143 Sync: &v1alpha1.SyncStatus{Status: v1alpha1.SyncStatusCodeOutOfSync}, 144 }, { 145 Name: "app2", 146 Sync: &v1alpha1.SyncStatus{Status: v1alpha1.SyncStatusCodeOutOfSync}, 147 }}}, 148 reconcileResults{Applications: []appReconcileResult{{ 149 Name: "app1", 150 Sync: &v1alpha1.SyncStatus{Status: v1alpha1.SyncStatusCodeOutOfSync}, 151 }, { 152 Name: "app3", 153 Sync: &v1alpha1.SyncStatus{Status: v1alpha1.SyncStatusCodeOutOfSync}, 154 }}}, 155 )) 156 }) 157 assert.NoError(t, err) 158 assert.Equal(t, `app1 159 app2 160 1,9d0 161 < conditions: null 162 < health: null 163 < name: app2 164 < sync: 165 < comparedTo: 166 < destination: {} 167 < source: 168 < repoURL: "" 169 < status: OutOfSync 170 app3 171 0a1,9 172 > conditions: null 173 > health: null 174 > name: app3 175 > sync: 176 > comparedTo: 177 > destination: {} 178 > source: 179 > repoURL: "" 180 > status: OutOfSync 181 `, logs) 182 }