github.com/argoproj/argo-cd/v2@v2.10.9/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  )
     9  
    10  func TestMetricClusterConnectivity(t *testing.T) {
    11  	type testCases struct {
    12  		testCombination
    13  		skip         bool
    14  		description  string
    15  		metricLabels []string
    16  		clustersInfo []gitopsCache.ClusterInfo
    17  	}
    18  	cases := []testCases{
    19  		{
    20  			description:  "metric will have value 1 if connected with the cluster",
    21  			skip:         false,
    22  			metricLabels: []string{"non-existing"},
    23  			testCombination: testCombination{
    24  				applications: []string{fakeApp},
    25  				responseContains: `
    26  # TYPE argocd_cluster_connection_status gauge
    27  argocd_cluster_connection_status{k8s_version="1.21",server="server1"} 1
    28  `,
    29  			},
    30  			clustersInfo: []gitopsCache.ClusterInfo{
    31  				{
    32  					Server:     "server1",
    33  					K8SVersion: "1.21",
    34  					SyncError:  nil,
    35  				},
    36  			},
    37  		},
    38  		{
    39  			description:  "metric will have value 0 if not connected with the cluster",
    40  			skip:         false,
    41  			metricLabels: []string{"non-existing"},
    42  			testCombination: testCombination{
    43  				applications: []string{fakeApp},
    44  				responseContains: `
    45  # TYPE argocd_cluster_connection_status gauge
    46  argocd_cluster_connection_status{k8s_version="1.21",server="server1"} 0
    47  `,
    48  			},
    49  			clustersInfo: []gitopsCache.ClusterInfo{
    50  				{
    51  					Server:     "server1",
    52  					K8SVersion: "1.21",
    53  					SyncError:  errors.New("error connecting with cluster"),
    54  				},
    55  			},
    56  		},
    57  		{
    58  			description:  "will have one metric per cluster",
    59  			skip:         false,
    60  			metricLabels: []string{"non-existing"},
    61  			testCombination: testCombination{
    62  				applications: []string{fakeApp},
    63  				responseContains: `
    64  # TYPE argocd_cluster_connection_status gauge
    65  argocd_cluster_connection_status{k8s_version="1.21",server="server1"} 1
    66  argocd_cluster_connection_status{k8s_version="1.21",server="server2"} 1
    67  argocd_cluster_connection_status{k8s_version="1.21",server="server3"} 1
    68  `,
    69  			},
    70  			clustersInfo: []gitopsCache.ClusterInfo{
    71  				{
    72  					Server:     "server1",
    73  					K8SVersion: "1.21",
    74  					SyncError:  nil,
    75  				},
    76  				{
    77  					Server:     "server2",
    78  					K8SVersion: "1.21",
    79  					SyncError:  nil,
    80  				},
    81  				{
    82  					Server:     "server3",
    83  					K8SVersion: "1.21",
    84  					SyncError:  nil,
    85  				},
    86  			},
    87  		},
    88  	}
    89  
    90  	for _, c := range cases {
    91  		c := c
    92  		t.Run(c.description, func(t *testing.T) {
    93  			if !c.skip {
    94  				cfg := TestMetricServerConfig{
    95  					FakeAppYAMLs:     c.applications,
    96  					ExpectedResponse: c.responseContains,
    97  					AppLabels:        c.metricLabels,
    98  					ClustersInfo:     c.clustersInfo,
    99  				}
   100  				runTest(t, cfg)
   101  			}
   102  		})
   103  	}
   104  }