github.com/argoproj/argo-cd/v3@v3.2.1/controller/metrics/clustercollector_test.go (about)

     1  package metrics
     2  
     3  import (
     4  	"errors"
     5  	"testing"
     6  
     7  	gitopsCache "github.com/argoproj/gitops-engine/pkg/cache"
     8  	"github.com/stretchr/testify/mock"
     9  
    10  	dbmocks "github.com/argoproj/argo-cd/v3/util/db/mocks"
    11  
    12  	"github.com/argoproj/argo-cd/v3/pkg/apis/application/v1alpha1"
    13  )
    14  
    15  func TestMetricClusterConnectivity(t *testing.T) {
    16  	db := dbmocks.ArgoDB{}
    17  	cluster1 := v1alpha1.Cluster{Name: "cluster1", Server: "server1", Labels: map[string]string{"env": "dev", "team": "team1"}}
    18  	cluster2 := v1alpha1.Cluster{Name: "cluster2", Server: "server2", Labels: map[string]string{"env": "staging", "team": "team2"}}
    19  	cluster3 := v1alpha1.Cluster{Name: "cluster3", Server: "server3", Labels: map[string]string{"env": "production", "team": "team3"}}
    20  	clusterList := &v1alpha1.ClusterList{Items: []v1alpha1.Cluster{cluster1, cluster2, cluster3}}
    21  	db.On("ListClusters", mock.Anything).Return(clusterList, nil)
    22  
    23  	type testCases struct {
    24  		testCombination
    25  		skip          bool
    26  		description   string
    27  		metricLabels  []string
    28  		clusterLabels []string
    29  		clustersInfo  []gitopsCache.ClusterInfo
    30  	}
    31  
    32  	cases := []testCases{
    33  		{
    34  			description:   "metric will have value 1 if connected with the cluster",
    35  			skip:          false,
    36  			metricLabels:  []string{"non-existing"},
    37  			clusterLabels: []string{"env"},
    38  			testCombination: testCombination{
    39  				applications: []string{fakeApp},
    40  				responseContains: `
    41  # TYPE argocd_cluster_connection_status gauge
    42  argocd_cluster_connection_status{k8s_version="1.21",server="server1"} 1
    43  `,
    44  			},
    45  			clustersInfo: []gitopsCache.ClusterInfo{
    46  				{
    47  					Server:     "server1",
    48  					K8SVersion: "1.21",
    49  					SyncError:  nil,
    50  				},
    51  			},
    52  		},
    53  		{
    54  			description:   "metric will have value 0 if not connected with the cluster",
    55  			skip:          false,
    56  			metricLabels:  []string{"non-existing"},
    57  			clusterLabels: []string{"env"},
    58  			testCombination: testCombination{
    59  				applications: []string{fakeApp},
    60  				responseContains: `
    61  # TYPE argocd_cluster_connection_status gauge
    62  argocd_cluster_connection_status{k8s_version="1.21",server="server1"} 0
    63  `,
    64  			},
    65  			clustersInfo: []gitopsCache.ClusterInfo{
    66  				{
    67  					Server:     "server1",
    68  					K8SVersion: "1.21",
    69  					SyncError:  errors.New("error connecting with cluster"),
    70  				},
    71  			},
    72  		},
    73  		{
    74  			description:   "will have one metric per cluster",
    75  			skip:          false,
    76  			metricLabels:  []string{"non-existing"},
    77  			clusterLabels: []string{"env", "team"},
    78  			testCombination: testCombination{
    79  				applications: []string{fakeApp},
    80  				responseContains: `
    81  # TYPE argocd_cluster_connection_status gauge
    82  argocd_cluster_connection_status{k8s_version="1.21",server="server1"} 1
    83  argocd_cluster_connection_status{k8s_version="1.21",server="server2"} 1
    84  argocd_cluster_connection_status{k8s_version="1.21",server="server3"} 1
    85  
    86  # TYPE argocd_cluster_info gauge
    87  argocd_cluster_info{k8s_version="1.21",name="cluster1",server="server1"} 1
    88  argocd_cluster_info{k8s_version="1.21",name="cluster2",server="server2"} 1
    89  argocd_cluster_info{k8s_version="1.21",name="cluster3",server="server3"} 1
    90  
    91  # TYPE argocd_cluster_labels gauge
    92  argocd_cluster_labels{label_env="dev",label_team="team1",name="cluster1",server="server1"} 1
    93  argocd_cluster_labels{label_env="staging",label_team="team2",name="cluster2",server="server2"} 1
    94  argocd_cluster_labels{label_env="production",label_team="team3",name="cluster3",server="server3"} 1
    95  `,
    96  			},
    97  			clustersInfo: []gitopsCache.ClusterInfo{
    98  				{
    99  					Server:     "server1",
   100  					K8SVersion: "1.21",
   101  					SyncError:  nil,
   102  				},
   103  				{
   104  					Server:     "server2",
   105  					K8SVersion: "1.21",
   106  					SyncError:  nil,
   107  				},
   108  				{
   109  					Server:     "server3",
   110  					K8SVersion: "1.21",
   111  					SyncError:  nil,
   112  				},
   113  			},
   114  		},
   115  	}
   116  
   117  	for _, c := range cases {
   118  		c := c
   119  		t.Run(c.description, func(t *testing.T) {
   120  			if !c.skip {
   121  				cfg := TestMetricServerConfig{
   122  					FakeAppYAMLs:     c.applications,
   123  					ExpectedResponse: c.responseContains,
   124  					AppLabels:        c.metricLabels,
   125  					ClusterLabels:    c.clusterLabels,
   126  					ClustersInfo:     c.clustersInfo,
   127  					ClusterLister:    db.ListClusters,
   128  				}
   129  				runTest(t, cfg)
   130  			}
   131  		})
   132  	}
   133  }