Files
cd/controller/metrics/clustercollector_test.go
zeekayandHanzo Dev cbf668a980 Build on Go 1.27
go fix applied the modernizers: promoted-field keys in struct literals,
strings.Cut, SplitSeq, new(expr), slices and maps helpers, wg.Go, reflect.TypeFor
and errors.AsType. omitzero is left out, since omitting a zero struct such as
time.Time{} is a per-field wire decision and not a mechanical one.

Two changes the modernizers could not make for themselves. AsType needs its type
argument to be an error, so the git exit-status reader embeds error in the
interface it asks for. And an error built by concatenating a tag list into its
own format string now says so with a verb, which is what vet was pointing at.

Co-authored-by: Hanzo Dev <[email protected]>
2026-09-17 01:47:46 -07:00

127 lines
3.8 KiB
Go

package metrics
import (
"errors"
"testing"
gitopsCache "github.com/hanzoai/cd/gitops-engine/pkg/cache"
"github.com/stretchr/testify/mock"
dbmocks "github.com/hanzoai/cd/util/db/mocks"
"github.com/hanzoai/cd/pkg/apis/application/v1alpha1"
)
func TestMetricClusterConnectivity(t *testing.T) {
db := &dbmocks.DB{}
cluster1 := v1alpha1.Cluster{Name: "cluster1", Server: "server1", Labels: map[string]string{"env": "dev", "team": "team1"}}
cluster2 := v1alpha1.Cluster{Name: "cluster2", Server: "server2", Labels: map[string]string{"env": "staging", "team": "team2"}}
cluster3 := v1alpha1.Cluster{Name: "cluster3", Server: "server3", Labels: map[string]string{"env": "production", "team": "team3"}}
clusterList := &v1alpha1.ClusterList{Items: []v1alpha1.Cluster{cluster1, cluster2, cluster3}}
db.EXPECT().ListClusters(mock.Anything).Return(clusterList, nil)
type testCases struct {
testCombination
skip bool
description string
metricLabels []string
clusterLabels []string
clustersInfo []gitopsCache.ClusterInfo
}
cases := []testCases{
{
description: "metric will have value 1 if connected with the cluster",
skip: false,
metricLabels: []string{"non-existing"},
clusterLabels: []string{"env"},
applications: []string{fakeApp},
responseContains: `
# TYPE cd_cluster_connection_status gauge
cd_cluster_connection_status{k8s_version="1.21",server="server1"} 1
`,
clustersInfo: []gitopsCache.ClusterInfo{
{
Server: "server1",
K8SVersion: "1.21",
SyncError: nil,
},
},
},
{
description: "metric will have value 0 if not connected with the cluster",
skip: false,
metricLabels: []string{"non-existing"},
clusterLabels: []string{"env"},
applications: []string{fakeApp},
responseContains: `
# TYPE cd_cluster_connection_status gauge
cd_cluster_connection_status{k8s_version="1.21",server="server1"} 0
`,
clustersInfo: []gitopsCache.ClusterInfo{
{
Server: "server1",
K8SVersion: "1.21",
SyncError: errors.New("error connecting with cluster"),
},
},
},
{
description: "will have one metric per cluster",
skip: false,
metricLabels: []string{"non-existing"},
clusterLabels: []string{"env", "team"},
applications: []string{fakeApp},
responseContains: `
# TYPE cd_cluster_connection_status gauge
cd_cluster_connection_status{k8s_version="1.21",server="server1"} 1
cd_cluster_connection_status{k8s_version="1.21",server="server2"} 1
cd_cluster_connection_status{k8s_version="1.21",server="server3"} 1
# TYPE cd_cluster_info gauge
cd_cluster_info{k8s_version="1.21",name="cluster1",server="server1"} 1
cd_cluster_info{k8s_version="1.21",name="cluster2",server="server2"} 1
cd_cluster_info{k8s_version="1.21",name="cluster3",server="server3"} 1
# TYPE cd_cluster_labels gauge
cd_cluster_labels{label_env="dev",label_team="team1",name="cluster1",server="server1"} 1
cd_cluster_labels{label_env="staging",label_team="team2",name="cluster2",server="server2"} 1
cd_cluster_labels{label_env="production",label_team="team3",name="cluster3",server="server3"} 1
`,
clustersInfo: []gitopsCache.ClusterInfo{
{
Server: "server1",
K8SVersion: "1.21",
SyncError: nil,
},
{
Server: "server2",
K8SVersion: "1.21",
SyncError: nil,
},
{
Server: "server3",
K8SVersion: "1.21",
SyncError: nil,
},
},
},
}
for _, c := range cases {
t.Run(c.description, func(t *testing.T) {
if !c.skip {
cfg := TestMetricServerConfig{
FakeAppYAMLs: c.applications,
ExpectedResponse: c.responseContains,
AppLabels: c.metricLabels,
ClusterLabels: c.clusterLabels,
ClustersInfo: c.clustersInfo,
ClusterLister: db.ListClusters,
}
runTest(t, cfg)
}
})
}
}