Files
deploy/controller/metrics/clustercollector_test.go
zeekayandHanzo Dev 1ab6e799c1 cd: the module is github.com/hanzoai/cd, where it is actually hosted
go.mod still declared `module github.com/hanzoai/deploy` after the repo was
renamed. A Go module path is not cosmetic — it is the resolvable location. So
`go get github.com/hanzoai/cd` could not work, and anything importing this
module had to spell a name that no longer exists at that address. The build log
of v3.6.0 is where it surfaced: the compiler was still emitting
github.com/hanzoai/deploy/cmd/cd/commands.

3541 import paths across 690 .go, 19 .proto, 4 .yaml, 3 .sh, 2 .mod, Procfile
and Makefile. Plus the 62 ghcr.io/hanzoai/deploy image references, moved to
ghcr.io/hanzoai/cd to match hanzo.yml's build target — a distinct string from
the module path, replaced separately so neither could clobber the other.

go mod tidy clean, CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build ./... clean.

Co-authored-by: Hanzo Dev <[email protected]>
2026-07-28 21:16:24 -07:00

133 lines
3.9 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"},
testCombination: testCombination{
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"},
testCombination: testCombination{
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"},
testCombination: testCombination{
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)
}
})
}
}