Skip to content
This repository has been archived by the owner on Jun 29, 2022. It is now read-only.

Commit

Permalink
metallb: Add tests to verify conversion
Browse files Browse the repository at this point in the history
This commit adds tests that check if the specified component attributes
in hcl configs are correctly converted to corresponding Kubernetes yaml
objects.

Signed-off-by: Suraj Deshmukh <suraj@kinvolk.io>
  • Loading branch information
surajssd committed Sep 9, 2020
1 parent f4eaad4 commit 60312a5
Showing 1 changed file with 165 additions and 0 deletions.
165 changes: 165 additions & 0 deletions pkg/components/metallb/component_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,13 @@
package metallb

import (
"reflect"
"testing"

"github.com/hashicorp/hcl/v2"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"sigs.k8s.io/yaml"

"github.com/kinvolk/lokomotive/pkg/components/util"
)
Expand Down Expand Up @@ -103,3 +107,164 @@ component "metallb" {
`
testRenderManifest(t, configHCL)
}

func getSpeakerDaemonset(t *testing.T, m map[string]string) *appsv1.DaemonSet {
dsStr, ok := m["daemonset-speaker.yaml"]
if !ok {
t.Fatalf("speaker daemonset config not found")
}

ds := &appsv1.DaemonSet{}
if err := yaml.Unmarshal([]byte(dsStr), ds); err != nil {
t.Fatalf("failed unmarshaling manifest: %v", err)
}

return ds
}

func getDeployController(t *testing.T, m map[string]string) *appsv1.Deployment {
deployStr, ok := m["deployment-controller.yaml"]
if !ok {
t.Fatalf("controller deployment config not found")
}

deploy := &appsv1.Deployment{}
if err := yaml.Unmarshal([]byte(deployStr), deploy); err != nil {
t.Fatalf("failed unmarshaling manifest: %v", err)
}

return deploy
}

func TestConversion(t *testing.T) {

configHCL := `
component "metallb" {
address_pools = {
default = ["1.1.1.1/32"]
}
speaker_toleration {
key = "speaker_key1"
operator = "Equal"
value = "value1"
}
speaker_toleration {
key = "speaker_key2"
operator = "Equal"
value = "value2"
}
speaker_node_selectors = {
"speaker_node_key1" = "speaker_node_value1"
"speaker_node_key2" = "speaker_node_value2"
}
controller_toleration {
key = "controller_key1"
operator = "Equal"
value = "value1"
}
controller_toleration {
key = "controller_key2"
operator = "Equal"
value = "value2"
}
controller_node_selectors = {
"controller_node_key1" = "controller_node_value1"
"controller_node_key2" = "controller_node_value2"
}
service_monitor = true
}`

testRenderManifest(t, configHCL)
m := renderManifest(t, configHCL)

tcs := []struct {
Name string
Test func(*testing.T, map[string]string)
}{
{
"SpeakerConversions", func(t *testing.T, m map[string]string) {
ds := getSpeakerDaemonset(t, m)
expected := []corev1.Toleration{
{Key: "speaker_key1", Operator: "Equal", Value: "value1"},
{Key: "speaker_key2", Operator: "Equal", Value: "value2"},
}
if !reflect.DeepEqual(expected, ds.Spec.Template.Spec.Tolerations) {
t.Fatalf("expected: %#v, got: %#v", expected, ds.Spec.Template.Spec.Tolerations)
}
},
},
{
"SpeakerNodeSelectors", func(t *testing.T, m map[string]string) {
ds := getSpeakerDaemonset(t, m)
expected := map[string]string{
"beta.kubernetes.io/os": "linux",
"speaker_node_key1": "speaker_node_value1",
"speaker_node_key2": "speaker_node_value2",
}
if !reflect.DeepEqual(expected, ds.Spec.Template.Spec.NodeSelector) {
t.Fatalf("expected: %v, got: %v", expected, ds.Spec.Template.Spec.NodeSelector)
}
},
},
{
"ControllerTolerations", func(t *testing.T, m map[string]string) {
// This fails right now.
t.Skip()
deploy := getDeployController(t, m)
expected := []corev1.Toleration{
{Key: "controller_key1", Operator: "Equal", Value: "value1"},
{Key: "controller_key2", Operator: "Equal", Value: "value2"},
}
if !reflect.DeepEqual(expected, deploy.Spec.Template.Spec.Tolerations) {
t.Fatalf("expected: %+v, got: %+v", expected, deploy.Spec.Template.Spec.Tolerations)
}
},
},
{
"ControllerNodeSelectors", func(t *testing.T, m map[string]string) {
// This fails right now.
t.Skip()
deploy := getDeployController(t, m)
expected := map[string]string{
"beta.kubernetes.io/os": "linux",
"controller_node_key1": "controller_node_value1",
"controller_node_key2": "controller_node_value2",
}
if !reflect.DeepEqual(expected, deploy.Spec.Template.Spec.NodeSelector) {
t.Fatalf("expected: %v, got: %v", expected, deploy.Spec.Template.Spec.NodeSelector)
}
},
},
{
"MonitoringConfig", func(t *testing.T, m map[string]string) {
expectedConfig := []string{
"service.yaml",
"service-monitor.yaml",
"grafana-dashboard.yaml",
"grafana-alertmanager-rule.yaml",
}

for _, ec := range expectedConfig {
if _, ok := m[ec]; !ok {
t.Fatalf("expected %s to be generated but it is not available", ec)
}
}
},
},
}

for _, tc := range tcs {
tc := tc
t.Run(tc.Name, func(t *testing.T) {
t.Parallel()
tc.Test(t, m)
})
}
}

0 comments on commit 60312a5

Please sign in to comment.