|
| 1 | +package template_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "path/filepath" |
| 5 | + "strings" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/stretchr/testify/require" |
| 9 | + appsv1 "k8s.io/api/apps/v1" |
| 10 | + |
| 11 | + "github.com/gruntwork-io/terratest/modules/helm" |
| 12 | + "github.com/gruntwork-io/terratest/modules/k8s" |
| 13 | + "github.com/gruntwork-io/terratest/modules/random" |
| 14 | +) |
| 15 | + |
| 16 | +func TestChartTemplateTopologySpreadConstraintClass(t *testing.T) { |
| 17 | + |
| 18 | + // Path to the helm chart we will test |
| 19 | + helmChartPath, err := filepath.Abs("../../charts") |
| 20 | + releaseName := "marklogic-topologyspreadconstraint-test" |
| 21 | + t.Log(helmChartPath, releaseName) |
| 22 | + require.NoError(t, err) |
| 23 | + |
| 24 | + // Set up the namespace; confirm that the template renders the expected value for the namespace. |
| 25 | + namespaceName := "marklogic-" + strings.ToLower(random.UniqueId()) |
| 26 | + t.Logf("Namespace: %s\n", namespaceName) |
| 27 | + |
| 28 | + // Setup the args for helm install |
| 29 | + options := &helm.Options{ |
| 30 | + SetValues: map[string]string{ |
| 31 | + "image.repository": "marklogicdb/marklogic-db", |
| 32 | + "image.tag": "latest", |
| 33 | + "persistence.enabled": "false", |
| 34 | + }, |
| 35 | + KubectlOptions: k8s.NewKubectlOptions("", "", namespaceName), |
| 36 | + } |
| 37 | + |
| 38 | + // render the tempate |
| 39 | + output := helm.RenderTemplate(t, options, helmChartPath, releaseName, []string{"templates/statefulset.yaml"}) |
| 40 | + |
| 41 | + var statefulset appsv1.StatefulSet |
| 42 | + helm.UnmarshalK8SYaml(t, output, &statefulset) |
| 43 | + |
| 44 | + // Verify the name and namespace matches |
| 45 | + require.Equal(t, namespaceName, statefulset.Namespace) |
| 46 | + |
| 47 | + // Verify the topologySpreadConstraint rule is set |
| 48 | + expectedLabelSelector := map[string]string{ |
| 49 | + "app.kubernetes.io/name": "marklogic", |
| 50 | + } |
| 51 | + expectedMaxSkewValue := 1 |
| 52 | + expectedHostTopologyKey := "kubernetes.io/hostname" |
| 53 | + expectedHostScheduleCondition := "DoNotSchedule" |
| 54 | + expectedZoneTopologyKey := "topology.kubernetes.io/zone" |
| 55 | + expectedZoneScheduleCondition := "ScheduleAnyway" |
| 56 | + |
| 57 | + topologySpreadConstraintsRule := statefulset.Spec.Template.Spec.TopologySpreadConstraints |
| 58 | + |
| 59 | + hostMaxSkew := int(topologySpreadConstraintsRule[0].MaxSkew) |
| 60 | + hostTopologyKey := topologySpreadConstraintsRule[0].TopologyKey |
| 61 | + hostScheduleCondition := topologySpreadConstraintsRule[0].WhenUnsatisfiable |
| 62 | + hostLabelSelector := topologySpreadConstraintsRule[0].LabelSelector.MatchLabels |
| 63 | + |
| 64 | + zoneMaxSkew := int(topologySpreadConstraintsRule[1].MaxSkew) |
| 65 | + zoneTopologyKey := topologySpreadConstraintsRule[1].TopologyKey |
| 66 | + zoneScheduleCondition := topologySpreadConstraintsRule[1].WhenUnsatisfiable |
| 67 | + zoneLabelSelector := topologySpreadConstraintsRule[1].LabelSelector.MatchLabels |
| 68 | + |
| 69 | + require.Equal(t, hostMaxSkew, expectedMaxSkewValue) |
| 70 | + require.Equal(t, hostTopologyKey, expectedHostTopologyKey) |
| 71 | + require.Equal(t, string(hostScheduleCondition), expectedHostScheduleCondition) |
| 72 | + require.Equal(t, hostLabelSelector, expectedLabelSelector) |
| 73 | + |
| 74 | + require.Equal(t, zoneMaxSkew, expectedMaxSkewValue) |
| 75 | + require.Equal(t, zoneTopologyKey, expectedZoneTopologyKey) |
| 76 | + require.Equal(t, string(zoneScheduleCondition), expectedZoneScheduleCondition) |
| 77 | + require.Equal(t, zoneLabelSelector, expectedLabelSelector) |
| 78 | +} |
0 commit comments