Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check data persistence after pod deletion #467

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions test/pod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import (
const (
hostPathVolumeType = "hostPath"
localVolumeType = "local"
normalDataPath = "/data"
subPathDataPath = "/nginx"
)

type PodTestSuite struct {
Expand Down Expand Up @@ -83,30 +85,35 @@ func (p *PodTestSuite) TestPodWithHostPathVolume() {
p.kustomizeDir = "pod"

runTest(p, []string{p.config.IMAGE}, "ready", hostPathVolumeType)
runPersistenceTest(p, []string{p.config.IMAGE}, normalDataPath)
}

func (p *PodTestSuite) TestPodWithLocalVolume() {
p.kustomizeDir = "pod-with-local-volume"

runTest(p, []string{p.config.IMAGE}, "ready", localVolumeType)
runPersistenceTest(p, []string{p.config.IMAGE}, normalDataPath)
}

func (p *PodTestSuite) TestPodWithLocalVolumeDefault() {
p.kustomizeDir = "pod-with-default-local-volume"

runTest(p, []string{p.config.IMAGE}, "ready", localVolumeType)
runPersistenceTest(p, []string{p.config.IMAGE}, normalDataPath)
}

func (p *PodTestSuite) TestPodWithNodeAffinity() {
p.kustomizeDir = "pod-with-node-affinity"

runTest(p, []string{p.config.IMAGE}, "ready", hostPathVolumeType)
runPersistenceTest(p, []string{p.config.IMAGE}, normalDataPath)
}

func (p *PodTestSuite) TestPodWithRWOPVolume() {
p.kustomizeDir = "pod-with-rwop-volume"

runTest(p, []string{p.config.IMAGE}, "ready", localVolumeType)
runPersistenceTest(p, []string{p.config.IMAGE}, normalDataPath)
}

func (p *PodTestSuite) TestPodWithSecurityContext() {
Expand Down Expand Up @@ -137,12 +144,15 @@ loop:
break
}
}

runPersistenceTest(p, []string{p.config.IMAGE}, normalDataPath)
}

func (p *PodTestSuite) TestPodWithSubpath() {
p.kustomizeDir = "pod-with-subpath"

runTest(p, []string{p.config.IMAGE}, "ready", hostPathVolumeType)
runPersistenceTest(p, []string{p.config.IMAGE}, subPathDataPath)
}

func (p *PodTestSuite) xxTestPodWithMultipleStorageClasses() {
Expand All @@ -155,6 +165,7 @@ func (p *PodTestSuite) TestPodWithCustomPathPatternStorageClasses() {
p.kustomizeDir = "custom-path-pattern"

runTest(p, []string{p.config.IMAGE}, "ready", hostPathVolumeType)
runPersistenceTest(p, []string{p.config.IMAGE}, normalDataPath)
}

func runTest(p *PodTestSuite, images []string, waitCondition, volumeType string) {
Expand Down Expand Up @@ -198,3 +209,48 @@ func runTest(p *PodTestSuite, images []string, waitCondition, volumeType string)
p.FailNow("volume Type not correct")
}
}

func runPersistenceTest(p *PodTestSuite, images []string, dataPath string) {
kustomizeDir := testdataFile(p.kustomizeDir)

var cmds []string
for _, image := range images {
if len(image) > 0 {
cmds = append(cmds, fmt.Sprintf("kustomize edit set image %s", image))
}
}

cmds = append(
cmds,
fmt.Sprintf("kubectl exec volume-test -- sh -c 'echo foo > %s/bar'", dataPath),
"kubectl delete pod/volume-test",
"kubectl wait --for=delete pod/volume-test --timeout=120s",
"kustomize build | kubectl apply -f -",
"kubectl wait pod volume-test --for condition=ready --timeout=120s",
)

for _, cmd := range cmds {
_, err := runCmd(
p.T(),
cmd,
kustomizeDir,
p.config.envs(),
nil,
)
if err != nil {
p.FailNow("", "failed to run command", cmd, err)
break
}
}

catDataCmd := fmt.Sprintf("kubectl exec volume-test -- cat %s/bar", dataPath)
c := createCmd(p.T(), catDataCmd, kustomizeDir, p.config.envs(), nil)
catDataOutput, err := c.CombinedOutput()
if err != nil {
p.FailNow("", "failed to cat 'bar' from %s: %v", dataPath, err)
}

if len(catDataOutput) == 0 || !strings.Contains(string(catDataOutput), "foo") {
p.FailNow("data was not persisted as expected")
}
}