-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
278 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
package test | ||
|
||
import ( | ||
"github.com/gruntwork-io/terratest/modules/helm" | ||
"github.com/stretchr/testify/assert" | ||
v1 "k8s.io/api/core/v1" | ||
"testing" | ||
) | ||
|
||
var tplConfigMap = []string{"templates/configmap.yaml"} | ||
|
||
func Test_ConfigMap_GivenBackupPlans_WhenCustomField_ThenRenderCustomFile(t *testing.T) { | ||
options := &helm.Options{ | ||
ValuesFiles: []string{"values/configmap_1.yaml"}, | ||
} | ||
|
||
expectedContent := "mycustomcontent=invalid-znapzend-config" | ||
|
||
output := helm.RenderTemplate(t, options, helmChartPath, releaseName, tplConfigMap) | ||
|
||
var configmap v1.ConfigMap | ||
helm.UnmarshalK8SYaml(t, output, &configmap) | ||
|
||
assert.Equal(t, expectedContent, configmap.Data["plan0"]) | ||
|
||
} | ||
|
||
func Test_ConfigMap_GivenPlanField_WhenMinimalConfig_ThenRenderPlanWithDefaults(t *testing.T) { | ||
options := &helm.Options{ | ||
ValuesFiles: []string{"values/configmap_2.yaml"}, | ||
SetValues: map[string]string{ | ||
"metrics.enabled": "false", | ||
}, | ||
} | ||
|
||
output := helm.RenderTemplate(t, options, helmChartPath, releaseName, tplConfigMap) | ||
|
||
var configmap v1.ConfigMap | ||
helm.UnmarshalK8SYaml(t, output, &configmap) | ||
|
||
plan := configmap.Data["plan0"] | ||
expectedPlan := `enabled=on | ||
src_plan=1days=>2hours,2weeks=>1days,6months=>1weeks | ||
recursive=off | ||
tsformat=%Y-%m-%d-%H%M%S | ||
mbuffer_size=1G | ||
mbuffer=off | ||
zend_delay=0` | ||
|
||
assert.Equal(t, expectedPlan, plan) | ||
} | ||
|
||
func Test_ConfigMap_GivenPlanField_WhenMetricsEnabled_ThenRenderSourceWithReset(t *testing.T) { | ||
options := &helm.Options{ | ||
ValuesFiles: []string{"values/configmap_2.yaml"}, | ||
} | ||
|
||
output := helm.RenderTemplate(t, options, helmChartPath, releaseName, tplConfigMap) | ||
|
||
var configmap v1.ConfigMap | ||
helm.UnmarshalK8SYaml(t, output, &configmap) | ||
|
||
plan := configmap.Data["plan0"] | ||
expectedPlan := `enabled=on | ||
src_plan=1days=>2hours,2weeks=>1days,6months=>1weeks | ||
pre_znap_cmd=/usr/bin/curl -sS localhost:8080/presnap/tank/test/dataset | ||
post_znap_cmd=/usr/bin/curl -sS localhost:8080/postsnap/tank/test/dataset?SelfResetAfter=5m | ||
recursive=off | ||
tsformat=%Y-%m-%d-%H%M%S | ||
mbuffer_size=1G | ||
mbuffer=off | ||
zend_delay=0` | ||
|
||
assert.Equal(t, expectedPlan, plan) | ||
} | ||
|
||
func Test_ConfigMap_GivenPlanField_WhenTargetsGiven_ThenRenderTargets(t *testing.T) { | ||
options := &helm.Options{ | ||
ValuesFiles: []string{"values/configmap_3.yaml"}, | ||
SetValues: map[string]string{ | ||
"metrics.enabled": "false", | ||
}, | ||
} | ||
|
||
output := helm.RenderTemplate(t, options, helmChartPath, releaseName, tplConfigMap) | ||
|
||
var configmap v1.ConfigMap | ||
helm.UnmarshalK8SYaml(t, output, &configmap) | ||
|
||
plan := configmap.Data["plan0"] | ||
expectedPlan := `enabled=on | ||
src_plan=1days=>2hours,2weeks=>1days,6months=>1weeks | ||
dst_0=target.host:backup/target | ||
dst_0_plan=1days=>2hours | ||
recursive=on | ||
tsformat=%Y-%m-%d-%H%M%S | ||
mbuffer_size=1G | ||
mbuffer=off | ||
zend_delay=600` | ||
|
||
assert.Equal(t, expectedPlan, plan) | ||
} | ||
|
||
func Test_ConfigMap_GivenPlanField_WhenTargetsAndMetricsGiven_ThenRenderTargetsWithMetrics(t *testing.T) { | ||
options := &helm.Options{ | ||
ValuesFiles: []string{"values/configmap_3.yaml"}, | ||
} | ||
|
||
output := helm.RenderTemplate(t, options, helmChartPath, releaseName, tplConfigMap) | ||
|
||
var configmap v1.ConfigMap | ||
helm.UnmarshalK8SYaml(t, output, &configmap) | ||
|
||
plan := configmap.Data["plan0"] | ||
expectedPlan := `enabled=on | ||
src_plan=1days=>2hours,2weeks=>1days,6months=>1weeks | ||
dst_0=target.host:backup/target | ||
dst_0_plan=1days=>2hours | ||
dst_0_precmd=/usr/bin/curl -sS localhost:8080/presend/tank/test/dataset | ||
dst_0_pstcmd=/usr/bin/curl -sS localhost:8080/postsend/tank/test/dataset?SelfResetAfter=1h | ||
pre_znap_cmd=/usr/bin/curl -sS localhost:8080/presnap/tank/test/dataset | ||
post_znap_cmd=/usr/bin/curl -sS localhost:8080/postsnap/tank/test/dataset | ||
recursive=on | ||
tsformat=%Y-%m-%d-%H%M%S | ||
mbuffer_size=1G | ||
mbuffer=off | ||
zend_delay=600` | ||
|
||
assert.Equal(t, expectedPlan, plan) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package test | ||
|
||
import ( | ||
"github.com/gruntwork-io/terratest/modules/helm" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
batchv1 "k8s.io/api/batch/v1" | ||
v1 "k8s.io/api/core/v1" | ||
"testing" | ||
) | ||
|
||
var tplJob = []string{"templates/job.yaml"} | ||
|
||
func Test_Job_GivenBackupPlans_WhenEnabled_ThenRenderEnvironmentVariables(t *testing.T) { | ||
options := &helm.Options{ | ||
ValuesFiles: []string{"values/job_1.yaml"}, | ||
} | ||
|
||
expectedKeys := []string{"KEY1", "ANOTHER_KEY"} | ||
expectedValues := []string{"value", "another value"} | ||
|
||
output := helm.RenderTemplate(t, options, helmChartPath, releaseName, tplJob) | ||
|
||
var job batchv1.Job | ||
helm.UnmarshalK8SYaml(t, output, &job) | ||
|
||
envs := job.Spec.Template.Spec.Containers[0].Env | ||
for i, _ := range envs { | ||
require.Contains(t, envs, v1.EnvVar{ | ||
Name: expectedKeys[i], | ||
Value: expectedValues[i], | ||
}) | ||
} | ||
} | ||
|
||
func Test_Job_GivenBackupPlans_WhenSshIdentitiesProvided_ThenRenderVolumes(t *testing.T) { | ||
options := &helm.Options{ | ||
ValuesFiles: []string{"values/job_2.yaml"}, | ||
} | ||
|
||
output := helm.RenderTemplate(t, options, helmChartPath, releaseName, tplJob) | ||
|
||
var job batchv1.Job | ||
helm.UnmarshalK8SYaml(t, output, &job) | ||
|
||
volumeMounts := job.Spec.Template.Spec.Containers[0].VolumeMounts | ||
assertSshVolumeMounts(t, volumeMounts) | ||
|
||
volumes := job.Spec.Template.Spec.Volumes | ||
assertSshVolume(t, volumes) | ||
} | ||
|
||
func Test_Job_GivenBackupPlans_WhenMultiplePlans_ThenRenderCommandArgs(t *testing.T) { | ||
options := &helm.Options{ | ||
ValuesFiles: []string{"values/job_2.yaml"}, | ||
} | ||
|
||
output := helm.RenderTemplate(t, options, helmChartPath, releaseName, tplJob) | ||
|
||
var job batchv1.Job | ||
helm.UnmarshalK8SYaml(t, output, &job) | ||
|
||
args := job.Spec.Template.Spec.Containers[0].Args | ||
expectedArgs := `set -e && | ||
znapzendzetup import --write pool/dataset1 /etc/znapzend/plan0 && | ||
znapzendzetup import --write pool/dataset2 /etc/znapzend/plan1 && | ||
echo "$?" > /tmp/znapzend/success | ||
` | ||
|
||
assert.Equal(t, args[0], expectedArgs) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,35 @@ | ||
package test | ||
|
||
import ( | ||
"github.com/stretchr/testify/require" | ||
v1 "k8s.io/api/core/v1" | ||
"testing" | ||
) | ||
|
||
var ( | ||
helmChartPath = ".." | ||
releaseName = "test-release" | ||
) | ||
|
||
func assertSshVolume(t *testing.T, volumes []v1.Volume) { | ||
require.Contains(t, volumes, v1.Volume{ | ||
Name: "ssh", | ||
VolumeSource: v1.VolumeSource{ | ||
Secret: &v1.SecretVolumeSource{ | ||
SecretName: releaseName + "-znapzend", | ||
DefaultMode: getIntPointer(0600), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func assertSshVolumeMounts(t *testing.T, volumeMounts []v1.VolumeMount) { | ||
require.Contains(t, volumeMounts, v1.VolumeMount{ | ||
Name: "zfs", | ||
MountPath: "/dev/zfs", | ||
}) | ||
require.Contains(t, volumeMounts, v1.VolumeMount{ | ||
Name: "ssh", | ||
MountPath: "/root/.ssh", | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
znapzend: | ||
backupPlans: | ||
- custom: | | ||
mycustomcontent=invalid-znapzend-config |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
znapzend: | ||
backupPlans: | ||
- dataset: tank/test/dataset | ||
plan: | ||
source: | ||
retention: 1days=>2hours,2weeks=>1days,6months=>1weeks | ||
selfResetAfter: 5m |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
znapzend: | ||
backupPlans: | ||
- dataset: tank/test/dataset | ||
plan: | ||
source: | ||
retention: 1days=>2hours,2weeks=>1days,6months=>1weeks | ||
recursive: true | ||
targets: | ||
- host: target.host | ||
retention: 1days=>2hours | ||
dataset: backup/target | ||
delaySeconds: 600 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
env: | ||
KEY1: value | ||
ANOTHER_KEY: another value | ||
znapzend: | ||
backupPlans: | ||
- dataset: pool/dataset | ||
custom: | | ||
a plan |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
ssh: | ||
identities: | ||
id_rsa: | | ||
----BEGIN---- | ||
----END---- | ||
znapzend: | ||
backupPlans: | ||
- dataset: pool/dataset1 | ||
custom: | | ||
a plan | ||
- dataset: pool/dataset2 | ||
custom: | | ||
a plan |