From 6b7831b3a32bb41dee7d068d18dbd90ea3ac0792 Mon Sep 17 00:00:00 2001 From: Andy Pliszka Date: Tue, 19 Sep 2023 16:28:44 -0400 Subject: [PATCH 1/5] feat(labels): adds node type label to resources * default type is FullNode * allowed values are FullNode|Sentry --- internal/fullnode/configmap_builder_test.go | 37 +++++++++++++++++++++ internal/fullnode/labels.go | 6 ++++ internal/fullnode/node_key_builder_test.go | 37 +++++++++++++++++++++ internal/fullnode/pod_builder_test.go | 33 ++++++++++++++++++ internal/fullnode/pvc_builder_test.go | 37 +++++++++++++++++++++ internal/fullnode/service_builder_test.go | 37 +++++++++++++++++++++ 6 files changed, 187 insertions(+) diff --git a/internal/fullnode/configmap_builder_test.go b/internal/fullnode/configmap_builder_test.go index 57bd39eb..7fd844b9 100644 --- a/internal/fullnode/configmap_builder_test.go +++ b/internal/fullnode/configmap_builder_test.go @@ -60,6 +60,7 @@ func TestBuildConfigMaps(t *testing.T) { "app.kubernetes.io/instance": "agoric-0", "app.kubernetes.io/version": "v6.0.0", "cosmos.strange.love/network": "testnet", + "cosmos.strange.love/type": "FullNode", } require.Empty(t, cm.Annotations) @@ -387,4 +388,40 @@ func TestBuildConfigMaps(t *testing.T) { require.Contains(t, err.Error(), "invalid toml in app overrides") }) }) + + t.Run("sets labels for", func(t *testing.T) { + t.Run("type", func(t *testing.T) { + crd := defaultCRD() + crd.Spec.Replicas = 3 + + t.Run("given unspecified type sets type to FullNode", func(t *testing.T) { + cms, err := BuildConfigMaps(&crd, nil) + require.NoError(t, err) + + require.Equal(t, "FullNode", cms[0].Object().Labels["cosmos.strange.love/type"]) + require.Equal(t, "FullNode", cms[1].Object().Labels["cosmos.strange.love/type"]) + require.Equal(t, "FullNode", cms[2].Object().Labels["cosmos.strange.love/type"]) + }) + + t.Run("given Sentry type", func(t *testing.T) { + crd.Spec.Type = "Sentry" + cms, err := BuildConfigMaps(&crd, nil) + require.NoError(t, err) + + require.Equal(t, "Sentry", cms[0].Object().Labels["cosmos.strange.love/type"]) + require.Equal(t, "Sentry", cms[1].Object().Labels["cosmos.strange.love/type"]) + require.Equal(t, "Sentry", cms[2].Object().Labels["cosmos.strange.love/type"]) + }) + + t.Run("given FullNode type", func(t *testing.T) { + crd.Spec.Type = "FullNode" + cms, err := BuildConfigMaps(&crd, nil) + require.NoError(t, err) + + require.Equal(t, "FullNode", cms[0].Object().Labels["cosmos.strange.love/type"]) + require.Equal(t, "FullNode", cms[1].Object().Labels["cosmos.strange.love/type"]) + require.Equal(t, "FullNode", cms[2].Object().Labels["cosmos.strange.love/type"]) + }) + }) + }) } diff --git a/internal/fullnode/labels.go b/internal/fullnode/labels.go index e742dc3f..03841ca4 100644 --- a/internal/fullnode/labels.go +++ b/internal/fullnode/labels.go @@ -10,6 +10,7 @@ import ( const ( networkLabel = "cosmos.strange.love/network" + typeLabel = "cosmos.strange.love/type" ) // kv is a list of extra kv pairs to add to the labels. Must be even. @@ -17,12 +18,17 @@ func defaultLabels(crd *cosmosv1.CosmosFullNode, kvPairs ...string) map[string]s if len(kvPairs)%2 != 0 { panic(errors.New("key/value pairs must be even")) } + nodeType := "FullNode" + if crd.Spec.Type != "" { + nodeType = string(crd.Spec.Type) + } labels := map[string]string{ kube.ControllerLabel: "cosmos-operator", kube.ComponentLabel: cosmosv1.CosmosFullNodeController, kube.NameLabel: appName(crd), kube.VersionLabel: kube.ParseImageVersion(crd.Spec.PodTemplate.Image), networkLabel: crd.Spec.ChainSpec.Network, + typeLabel: nodeType, } for i := 0; i < len(kvPairs); i += 2 { labels[kvPairs[i]] = kvPairs[i+1] diff --git a/internal/fullnode/node_key_builder_test.go b/internal/fullnode/node_key_builder_test.go index 4530ced6..c633d000 100644 --- a/internal/fullnode/node_key_builder_test.go +++ b/internal/fullnode/node_key_builder_test.go @@ -41,6 +41,7 @@ func TestBuildNodeKeySecrets(t *testing.T) { "app.kubernetes.io/instance": fmt.Sprintf("juno-%d", i), "app.kubernetes.io/version": "v1.2.3", "cosmos.strange.love/network": "mainnet", + "cosmos.strange.love/type": "FullNode", } require.Equal(t, wantLabels, got.Labels) @@ -89,4 +90,40 @@ func TestBuildNodeKeySecrets(t *testing.T) { require.NoError(t, err) require.Empty(t, secrets) }) + + t.Run("sets label for", func(t *testing.T) { + var crd cosmosv1.CosmosFullNode + crd.Spec.Replicas = 3 + + t.Run("type", func(t *testing.T) { + t.Run("given unspecified type sets type to FullNode", func(t *testing.T) { + secrets, err := BuildNodeKeySecrets(nil, &crd) + require.NoError(t, err) + + require.Equal(t, "FullNode", secrets[0].Object().Labels["cosmos.strange.love/type"]) + require.Equal(t, "FullNode", secrets[1].Object().Labels["cosmos.strange.love/type"]) + require.Equal(t, "FullNode", secrets[2].Object().Labels["cosmos.strange.love/type"]) + }) + + t.Run("given Sentry type", func(t *testing.T) { + crd.Spec.Type = "Sentry" + secrets, err := BuildNodeKeySecrets(nil, &crd) + require.NoError(t, err) + + require.Equal(t, "Sentry", secrets[0].Object().Labels["cosmos.strange.love/type"]) + require.Equal(t, "Sentry", secrets[1].Object().Labels["cosmos.strange.love/type"]) + require.Equal(t, "Sentry", secrets[2].Object().Labels["cosmos.strange.love/type"]) + }) + + t.Run("given FullNode type", func(t *testing.T) { + crd.Spec.Type = "FullNode" + secrets, err := BuildNodeKeySecrets(nil, &crd) + require.NoError(t, err) + + require.Equal(t, "FullNode", secrets[0].Object().Labels["cosmos.strange.love/type"]) + require.Equal(t, "FullNode", secrets[1].Object().Labels["cosmos.strange.love/type"]) + require.Equal(t, "FullNode", secrets[2].Object().Labels["cosmos.strange.love/type"]) + }) + }) + }) } diff --git a/internal/fullnode/pod_builder_test.go b/internal/fullnode/pod_builder_test.go index 77f912dd..5dfd435a 100644 --- a/internal/fullnode/pod_builder_test.go +++ b/internal/fullnode/pod_builder_test.go @@ -63,6 +63,7 @@ func TestPodBuilder(t *testing.T) { "app.kubernetes.io/name": "osmosis", "app.kubernetes.io/version": "v1.2.3", "cosmos.strange.love/network": "mainnet", + "cosmos.strange.love/type": "FullNode", } require.Equal(t, wantLabels, pod.Labels) require.NotNil(t, pod.Annotations) @@ -553,6 +554,38 @@ gaiad start --home /home/operator/cosmos` require.ElementsMatch(t, []string{"clean-init", "chain-init", "new-init", "genesis-init", "config-merge"}, lo.Keys(initConts)) require.Equal(t, "foo:latest", initConts["chain-init"].Image) }) + + t.Run("sets labels for", func(t *testing.T) { + t.Run("type", func(t *testing.T) { + crd := defaultCRD() + + t.Run("given unspecified type sets type to FullNode", func(t *testing.T) { + builder := NewPodBuilder(&crd) + pod, err := builder.WithOrdinal(5).Build() + require.NoError(t, err) + + require.Equal(t, "FullNode", pod.Labels["cosmos.strange.love/type"]) + }) + + t.Run("given Sentry type", func(t *testing.T) { + crd.Spec.Type = "Sentry" + builder := NewPodBuilder(&crd) + pod, err := builder.WithOrdinal(5).Build() + require.NoError(t, err) + + require.Equal(t, "Sentry", pod.Labels["cosmos.strange.love/type"]) + }) + + t.Run("given FullNode type", func(t *testing.T) { + crd.Spec.Type = "FullNode" + builder := NewPodBuilder(&crd) + pod, err := builder.WithOrdinal(5).Build() + require.NoError(t, err) + + require.Equal(t, "FullNode", pod.Labels["cosmos.strange.love/type"]) + }) + }) + }) } func TestChainHomeDir(t *testing.T) { diff --git a/internal/fullnode/pvc_builder_test.go b/internal/fullnode/pvc_builder_test.go index 1a1e68f6..941a5a15 100644 --- a/internal/fullnode/pvc_builder_test.go +++ b/internal/fullnode/pvc_builder_test.go @@ -58,6 +58,7 @@ func TestBuildPVCs(t *testing.T) { "app.kubernetes.io/instance": fmt.Sprintf("juno-%d", i), "app.kubernetes.io/version": "v1.2.3", "cosmos.strange.love/network": "mainnet", + "cosmos.strange.love/type": "FullNode", } require.Equal(t, wantLabels, got.Labels) @@ -172,4 +173,40 @@ func TestBuildPVCs(t *testing.T) { require.Equal(t, want, pvcs[0].Object().Spec.Resources.Requests, tt) } }) + + t.Run("sets label for", func(t *testing.T) { + crd := defaultCRD() + crd.Spec.Replicas = 3 + + t.Run("type", func(t *testing.T) { + t.Run("given unspecified type sets type to FullNode", func(t *testing.T) { + pvcs := BuildPVCs(&crd) + require.NotEmpty(t, pvcs) + + require.Equal(t, "FullNode", pvcs[0].Object().Labels["cosmos.strange.love/type"]) + require.Equal(t, "FullNode", pvcs[1].Object().Labels["cosmos.strange.love/type"]) + require.Equal(t, "FullNode", pvcs[2].Object().Labels["cosmos.strange.love/type"]) + }) + + t.Run("given Sentry type", func(t *testing.T) { + crd.Spec.Type = "Sentry" + pvcs := BuildPVCs(&crd) + require.NotEmpty(t, pvcs) + + require.Equal(t, "Sentry", pvcs[0].Object().Labels["cosmos.strange.love/type"]) + require.Equal(t, "Sentry", pvcs[1].Object().Labels["cosmos.strange.love/type"]) + require.Equal(t, "Sentry", pvcs[2].Object().Labels["cosmos.strange.love/type"]) + }) + + t.Run("given FullNode type", func(t *testing.T) { + crd.Spec.Type = "FullNode" + pvcs := BuildPVCs(&crd) + require.NotEmpty(t, pvcs) + + require.Equal(t, "FullNode", pvcs[0].Object().Labels["cosmos.strange.love/type"]) + require.Equal(t, "FullNode", pvcs[1].Object().Labels["cosmos.strange.love/type"]) + require.Equal(t, "FullNode", pvcs[2].Object().Labels["cosmos.strange.love/type"]) + }) + }) + }) } diff --git a/internal/fullnode/service_builder_test.go b/internal/fullnode/service_builder_test.go index 81613979..e589327b 100644 --- a/internal/fullnode/service_builder_test.go +++ b/internal/fullnode/service_builder_test.go @@ -42,6 +42,7 @@ func TestBuildServices(t *testing.T) { "app.kubernetes.io/version": "v6.0.0", "app.kubernetes.io/instance": fmt.Sprintf("terra-%d", i), "cosmos.strange.love/network": "testnet", + "cosmos.strange.love/type": "FullNode", } require.Equal(t, wantLabels, p2p.Labels) @@ -186,6 +187,7 @@ func TestBuildServices(t *testing.T) { "app.kubernetes.io/component": "rpc", "app.kubernetes.io/version": "v6.0.0", "cosmos.strange.love/network": "testnet", + "cosmos.strange.love/type": "FullNode", } require.Equal(t, wantLabels, rpc.Labels) @@ -265,4 +267,39 @@ func TestBuildServices(t *testing.T) { test.RequireValidMetadata(t, svc.Object()) } }) + + t.Run("sets labels for", func(t *testing.T) { + t.Run("type", func(t *testing.T) { + crd := defaultCRD() + crd.Spec.Replicas = 3 + + t.Run("given unspecified type sets type to FullNode", func(t *testing.T) { + svcs := BuildServices(&crd) + + require.Equal(t, "FullNode", svcs[0].Object().Labels["cosmos.strange.love/type"]) + require.Equal(t, "FullNode", svcs[1].Object().Labels["cosmos.strange.love/type"]) + require.Equal(t, "FullNode", svcs[2].Object().Labels["cosmos.strange.love/type"]) + }) + + t.Run("given Sentry type", func(t *testing.T) { + crd.Spec.Type = "Sentry" + + svcs := BuildServices(&crd) + + require.Equal(t, "Sentry", svcs[0].Object().Labels["cosmos.strange.love/type"]) + require.Equal(t, "Sentry", svcs[1].Object().Labels["cosmos.strange.love/type"]) + require.Equal(t, "Sentry", svcs[2].Object().Labels["cosmos.strange.love/type"]) + }) + + t.Run("given FullNode type", func(t *testing.T) { + crd.Spec.Type = "FullNode" + + svcs := BuildServices(&crd) + + require.Equal(t, "FullNode", svcs[0].Object().Labels["cosmos.strange.love/type"]) + require.Equal(t, "FullNode", svcs[1].Object().Labels["cosmos.strange.love/type"]) + require.Equal(t, "FullNode", svcs[2].Object().Labels["cosmos.strange.love/type"]) + }) + }) + }) } From 5fa04d4a2ef3b135707ca05d33aa8c6f68fe8bf4 Mon Sep 17 00:00:00 2001 From: Andy Pliszka Date: Wed, 20 Sep 2023 13:05:25 -0400 Subject: [PATCH 2/5] fix(labels): test names --- internal/fullnode/node_key_builder_test.go | 2 +- internal/fullnode/pvc_builder_test.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/fullnode/node_key_builder_test.go b/internal/fullnode/node_key_builder_test.go index c633d000..d10f5d26 100644 --- a/internal/fullnode/node_key_builder_test.go +++ b/internal/fullnode/node_key_builder_test.go @@ -91,7 +91,7 @@ func TestBuildNodeKeySecrets(t *testing.T) { require.Empty(t, secrets) }) - t.Run("sets label for", func(t *testing.T) { + t.Run("sets labels for", func(t *testing.T) { var crd cosmosv1.CosmosFullNode crd.Spec.Replicas = 3 diff --git a/internal/fullnode/pvc_builder_test.go b/internal/fullnode/pvc_builder_test.go index 941a5a15..abe9cbbd 100644 --- a/internal/fullnode/pvc_builder_test.go +++ b/internal/fullnode/pvc_builder_test.go @@ -174,7 +174,7 @@ func TestBuildPVCs(t *testing.T) { } }) - t.Run("sets label for", func(t *testing.T) { + t.Run("sets labels for", func(t *testing.T) { crd := defaultCRD() crd.Spec.Replicas = 3 From 476f59582c3f765d30b4e270d56a658913907636 Mon Sep 17 00:00:00 2001 From: Andy Pliszka Date: Wed, 20 Sep 2023 14:55:40 -0400 Subject: [PATCH 3/5] fix: uses constant for node type --- internal/fullnode/labels.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/internal/fullnode/labels.go b/internal/fullnode/labels.go index 03841ca4..bb63aaea 100644 --- a/internal/fullnode/labels.go +++ b/internal/fullnode/labels.go @@ -18,9 +18,9 @@ func defaultLabels(crd *cosmosv1.CosmosFullNode, kvPairs ...string) map[string]s if len(kvPairs)%2 != 0 { panic(errors.New("key/value pairs must be even")) } - nodeType := "FullNode" + nodeType := cosmosv1.FullNode if crd.Spec.Type != "" { - nodeType = string(crd.Spec.Type) + nodeType = crd.Spec.Type } labels := map[string]string{ kube.ControllerLabel: "cosmos-operator", @@ -28,7 +28,7 @@ func defaultLabels(crd *cosmosv1.CosmosFullNode, kvPairs ...string) map[string]s kube.NameLabel: appName(crd), kube.VersionLabel: kube.ParseImageVersion(crd.Spec.PodTemplate.Image), networkLabel: crd.Spec.ChainSpec.Network, - typeLabel: nodeType, + typeLabel: string(nodeType), } for i := 0; i < len(kvPairs); i += 2 { labels[kvPairs[i]] = kvPairs[i+1] From cb8af879a71ce02303d7e1bc86f13621206cbd91 Mon Sep 17 00:00:00 2001 From: Andy Pliszka Date: Wed, 20 Sep 2023 14:58:29 -0400 Subject: [PATCH 4/5] refactor: adds HasTypeLabel assertion --- internal/fullnode/configmap_builder_test.go | 41 ++++---------------- internal/fullnode/node_key_builder_test.go | 42 ++++----------------- internal/fullnode/pod_builder_test.go | 34 ++--------------- internal/fullnode/pvc_builder_test.go | 41 ++++---------------- internal/fullnode/service_builder_test.go | 40 ++++---------------- internal/test/assertions.go | 42 +++++++++++++++++++++ 6 files changed, 75 insertions(+), 165 deletions(-) create mode 100644 internal/test/assertions.go diff --git a/internal/fullnode/configmap_builder_test.go b/internal/fullnode/configmap_builder_test.go index 7fd844b9..1d9f5069 100644 --- a/internal/fullnode/configmap_builder_test.go +++ b/internal/fullnode/configmap_builder_test.go @@ -389,39 +389,12 @@ func TestBuildConfigMaps(t *testing.T) { }) }) - t.Run("sets labels for", func(t *testing.T) { - t.Run("type", func(t *testing.T) { - crd := defaultCRD() - crd.Spec.Replicas = 3 - - t.Run("given unspecified type sets type to FullNode", func(t *testing.T) { - cms, err := BuildConfigMaps(&crd, nil) - require.NoError(t, err) - - require.Equal(t, "FullNode", cms[0].Object().Labels["cosmos.strange.love/type"]) - require.Equal(t, "FullNode", cms[1].Object().Labels["cosmos.strange.love/type"]) - require.Equal(t, "FullNode", cms[2].Object().Labels["cosmos.strange.love/type"]) - }) - - t.Run("given Sentry type", func(t *testing.T) { - crd.Spec.Type = "Sentry" - cms, err := BuildConfigMaps(&crd, nil) - require.NoError(t, err) - - require.Equal(t, "Sentry", cms[0].Object().Labels["cosmos.strange.love/type"]) - require.Equal(t, "Sentry", cms[1].Object().Labels["cosmos.strange.love/type"]) - require.Equal(t, "Sentry", cms[2].Object().Labels["cosmos.strange.love/type"]) - }) - - t.Run("given FullNode type", func(t *testing.T) { - crd.Spec.Type = "FullNode" - cms, err := BuildConfigMaps(&crd, nil) - require.NoError(t, err) - - require.Equal(t, "FullNode", cms[0].Object().Labels["cosmos.strange.love/type"]) - require.Equal(t, "FullNode", cms[1].Object().Labels["cosmos.strange.love/type"]) - require.Equal(t, "FullNode", cms[2].Object().Labels["cosmos.strange.love/type"]) - }) - }) + test.HasTypeLabel(t, func(crd cosmosv1.CosmosFullNode) []map[string]string { + cms, _ := BuildConfigMaps(&crd, nil) + labels := make([]map[string]string, 0) + for _, cm := range cms { + labels = append(labels, cm.Object().Labels) + } + return labels }) } diff --git a/internal/fullnode/node_key_builder_test.go b/internal/fullnode/node_key_builder_test.go index d10f5d26..188fe512 100644 --- a/internal/fullnode/node_key_builder_test.go +++ b/internal/fullnode/node_key_builder_test.go @@ -3,6 +3,7 @@ package fullnode import ( "encoding/json" "fmt" + "github.com/strangelove-ventures/cosmos-operator/internal/test" "testing" cosmosv1 "github.com/strangelove-ventures/cosmos-operator/api/v1" @@ -91,39 +92,12 @@ func TestBuildNodeKeySecrets(t *testing.T) { require.Empty(t, secrets) }) - t.Run("sets labels for", func(t *testing.T) { - var crd cosmosv1.CosmosFullNode - crd.Spec.Replicas = 3 - - t.Run("type", func(t *testing.T) { - t.Run("given unspecified type sets type to FullNode", func(t *testing.T) { - secrets, err := BuildNodeKeySecrets(nil, &crd) - require.NoError(t, err) - - require.Equal(t, "FullNode", secrets[0].Object().Labels["cosmos.strange.love/type"]) - require.Equal(t, "FullNode", secrets[1].Object().Labels["cosmos.strange.love/type"]) - require.Equal(t, "FullNode", secrets[2].Object().Labels["cosmos.strange.love/type"]) - }) - - t.Run("given Sentry type", func(t *testing.T) { - crd.Spec.Type = "Sentry" - secrets, err := BuildNodeKeySecrets(nil, &crd) - require.NoError(t, err) - - require.Equal(t, "Sentry", secrets[0].Object().Labels["cosmos.strange.love/type"]) - require.Equal(t, "Sentry", secrets[1].Object().Labels["cosmos.strange.love/type"]) - require.Equal(t, "Sentry", secrets[2].Object().Labels["cosmos.strange.love/type"]) - }) - - t.Run("given FullNode type", func(t *testing.T) { - crd.Spec.Type = "FullNode" - secrets, err := BuildNodeKeySecrets(nil, &crd) - require.NoError(t, err) - - require.Equal(t, "FullNode", secrets[0].Object().Labels["cosmos.strange.love/type"]) - require.Equal(t, "FullNode", secrets[1].Object().Labels["cosmos.strange.love/type"]) - require.Equal(t, "FullNode", secrets[2].Object().Labels["cosmos.strange.love/type"]) - }) - }) + test.HasTypeLabel(t, func(crd cosmosv1.CosmosFullNode) []map[string]string { + secrets, _ := BuildNodeKeySecrets(nil, &crd) + labels := make([]map[string]string, 0) + for _, secret := range secrets { + labels = append(labels, secret.Object().Labels) + } + return labels }) } diff --git a/internal/fullnode/pod_builder_test.go b/internal/fullnode/pod_builder_test.go index 5dfd435a..82c7041e 100644 --- a/internal/fullnode/pod_builder_test.go +++ b/internal/fullnode/pod_builder_test.go @@ -555,36 +555,10 @@ gaiad start --home /home/operator/cosmos` require.Equal(t, "foo:latest", initConts["chain-init"].Image) }) - t.Run("sets labels for", func(t *testing.T) { - t.Run("type", func(t *testing.T) { - crd := defaultCRD() - - t.Run("given unspecified type sets type to FullNode", func(t *testing.T) { - builder := NewPodBuilder(&crd) - pod, err := builder.WithOrdinal(5).Build() - require.NoError(t, err) - - require.Equal(t, "FullNode", pod.Labels["cosmos.strange.love/type"]) - }) - - t.Run("given Sentry type", func(t *testing.T) { - crd.Spec.Type = "Sentry" - builder := NewPodBuilder(&crd) - pod, err := builder.WithOrdinal(5).Build() - require.NoError(t, err) - - require.Equal(t, "Sentry", pod.Labels["cosmos.strange.love/type"]) - }) - - t.Run("given FullNode type", func(t *testing.T) { - crd.Spec.Type = "FullNode" - builder := NewPodBuilder(&crd) - pod, err := builder.WithOrdinal(5).Build() - require.NoError(t, err) - - require.Equal(t, "FullNode", pod.Labels["cosmos.strange.love/type"]) - }) - }) + test.HasTypeLabel(t, func(crd cosmosv1.CosmosFullNode) []map[string]string { + builder := NewPodBuilder(&crd) + pod, _ := builder.WithOrdinal(5).Build() + return []map[string]string{pod.Labels} }) } diff --git a/internal/fullnode/pvc_builder_test.go b/internal/fullnode/pvc_builder_test.go index abe9cbbd..8cd9acf6 100644 --- a/internal/fullnode/pvc_builder_test.go +++ b/internal/fullnode/pvc_builder_test.go @@ -174,39 +174,12 @@ func TestBuildPVCs(t *testing.T) { } }) - t.Run("sets labels for", func(t *testing.T) { - crd := defaultCRD() - crd.Spec.Replicas = 3 - - t.Run("type", func(t *testing.T) { - t.Run("given unspecified type sets type to FullNode", func(t *testing.T) { - pvcs := BuildPVCs(&crd) - require.NotEmpty(t, pvcs) - - require.Equal(t, "FullNode", pvcs[0].Object().Labels["cosmos.strange.love/type"]) - require.Equal(t, "FullNode", pvcs[1].Object().Labels["cosmos.strange.love/type"]) - require.Equal(t, "FullNode", pvcs[2].Object().Labels["cosmos.strange.love/type"]) - }) - - t.Run("given Sentry type", func(t *testing.T) { - crd.Spec.Type = "Sentry" - pvcs := BuildPVCs(&crd) - require.NotEmpty(t, pvcs) - - require.Equal(t, "Sentry", pvcs[0].Object().Labels["cosmos.strange.love/type"]) - require.Equal(t, "Sentry", pvcs[1].Object().Labels["cosmos.strange.love/type"]) - require.Equal(t, "Sentry", pvcs[2].Object().Labels["cosmos.strange.love/type"]) - }) - - t.Run("given FullNode type", func(t *testing.T) { - crd.Spec.Type = "FullNode" - pvcs := BuildPVCs(&crd) - require.NotEmpty(t, pvcs) - - require.Equal(t, "FullNode", pvcs[0].Object().Labels["cosmos.strange.love/type"]) - require.Equal(t, "FullNode", pvcs[1].Object().Labels["cosmos.strange.love/type"]) - require.Equal(t, "FullNode", pvcs[2].Object().Labels["cosmos.strange.love/type"]) - }) - }) + test.HasTypeLabel(t, func(crd cosmosv1.CosmosFullNode) []map[string]string { + pvcs := BuildPVCs(&crd) + labels := make([]map[string]string, 0) + for _, pvc := range pvcs { + labels = append(labels, pvc.Object().Labels) + } + return labels }) } diff --git a/internal/fullnode/service_builder_test.go b/internal/fullnode/service_builder_test.go index e589327b..246af0fc 100644 --- a/internal/fullnode/service_builder_test.go +++ b/internal/fullnode/service_builder_test.go @@ -268,38 +268,12 @@ func TestBuildServices(t *testing.T) { } }) - t.Run("sets labels for", func(t *testing.T) { - t.Run("type", func(t *testing.T) { - crd := defaultCRD() - crd.Spec.Replicas = 3 - - t.Run("given unspecified type sets type to FullNode", func(t *testing.T) { - svcs := BuildServices(&crd) - - require.Equal(t, "FullNode", svcs[0].Object().Labels["cosmos.strange.love/type"]) - require.Equal(t, "FullNode", svcs[1].Object().Labels["cosmos.strange.love/type"]) - require.Equal(t, "FullNode", svcs[2].Object().Labels["cosmos.strange.love/type"]) - }) - - t.Run("given Sentry type", func(t *testing.T) { - crd.Spec.Type = "Sentry" - - svcs := BuildServices(&crd) - - require.Equal(t, "Sentry", svcs[0].Object().Labels["cosmos.strange.love/type"]) - require.Equal(t, "Sentry", svcs[1].Object().Labels["cosmos.strange.love/type"]) - require.Equal(t, "Sentry", svcs[2].Object().Labels["cosmos.strange.love/type"]) - }) - - t.Run("given FullNode type", func(t *testing.T) { - crd.Spec.Type = "FullNode" - - svcs := BuildServices(&crd) - - require.Equal(t, "FullNode", svcs[0].Object().Labels["cosmos.strange.love/type"]) - require.Equal(t, "FullNode", svcs[1].Object().Labels["cosmos.strange.love/type"]) - require.Equal(t, "FullNode", svcs[2].Object().Labels["cosmos.strange.love/type"]) - }) - }) + test.HasTypeLabel(t, func(crd cosmosv1.CosmosFullNode) []map[string]string { + svcs := BuildServices(&crd) + labels := make([]map[string]string, 0) + for _, svc := range svcs { + labels = append(labels, svc.Object().Labels) + } + return labels }) } diff --git a/internal/test/assertions.go b/internal/test/assertions.go new file mode 100644 index 00000000..68d0512d --- /dev/null +++ b/internal/test/assertions.go @@ -0,0 +1,42 @@ +package test + +import ( + cosmosv1 "github.com/strangelove-ventures/cosmos-operator/api/v1" + "github.com/stretchr/testify/require" + "testing" +) + +func HasTypeLabel(t *testing.T, builder func(crd cosmosv1.CosmosFullNode) []map[string]string) { + t.Run("sets labels for", func(t *testing.T) { + var crd cosmosv1.CosmosFullNode + crd.Spec.Replicas = 3 + + t.Run("type", func(t *testing.T) { + t.Run("given unspecified type sets type to FullNode", func(t *testing.T) { + resources := builder(crd) + + for _, resource := range resources { + require.Equal(t, "FullNode", resource["cosmos.strange.love/type"]) + } + }) + + t.Run("given Sentry type", func(t *testing.T) { + crd.Spec.Type = "Sentry" + resources := builder(crd) + + for _, resource := range resources { + require.Equal(t, "Sentry", resource["cosmos.strange.love/type"]) + } + }) + + t.Run("given FullNode type", func(t *testing.T) { + crd.Spec.Type = "FullNode" + resources := builder(crd) + + for _, resource := range resources { + require.Equal(t, "FullNode", resource["cosmos.strange.love/type"]) + } + }) + }) + }) +} From 1bec2e94088b81012e624b633a7bb44e42fe6acb Mon Sep 17 00:00:00 2001 From: Andy Pliszka Date: Wed, 20 Sep 2023 15:35:40 -0400 Subject: [PATCH 5/5] style: goimports --- internal/fullnode/node_key_builder_test.go | 3 ++- internal/test/assertions.go | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/internal/fullnode/node_key_builder_test.go b/internal/fullnode/node_key_builder_test.go index 188fe512..6aad78f5 100644 --- a/internal/fullnode/node_key_builder_test.go +++ b/internal/fullnode/node_key_builder_test.go @@ -3,9 +3,10 @@ package fullnode import ( "encoding/json" "fmt" - "github.com/strangelove-ventures/cosmos-operator/internal/test" "testing" + "github.com/strangelove-ventures/cosmos-operator/internal/test" + cosmosv1 "github.com/strangelove-ventures/cosmos-operator/api/v1" "github.com/stretchr/testify/require" corev1 "k8s.io/api/core/v1" diff --git a/internal/test/assertions.go b/internal/test/assertions.go index 68d0512d..666a7b70 100644 --- a/internal/test/assertions.go +++ b/internal/test/assertions.go @@ -1,9 +1,10 @@ package test import ( + "testing" + cosmosv1 "github.com/strangelove-ventures/cosmos-operator/api/v1" "github.com/stretchr/testify/require" - "testing" ) func HasTypeLabel(t *testing.T, builder func(crd cosmosv1.CosmosFullNode) []map[string]string) {