Skip to content

Commit

Permalink
Improve tests
Browse files Browse the repository at this point in the history
Signed-off-by: David Gageot <david@gageot.net>
  • Loading branch information
dgageot committed Jan 28, 2019
1 parent 627d7ce commit ba8ca7e
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 52 deletions.
4 changes: 4 additions & 0 deletions pkg/skaffold/deploy/kubectl.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,5 +153,9 @@ func (k *KubectlDeployer) readManifests(ctx context.Context) (kubectl.ManifestLi
return nil, errors.Wrap(err, "listing manifests")
}

if len(manifests) == 0 {
return kubectl.ManifestList{}, nil
}

return k.kubectl.ReadManifests(ctx, manifests)
}
88 changes: 36 additions & 52 deletions pkg/skaffold/deploy/kubectl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ spec:
- name: leeroy-web
image: leeroy-web`

const deploymentAppYaml = `apiVersion: v1
const deploymentAppYAML = `apiVersion: v1
kind: Pod
metadata:
name: leeroy-app
Expand All @@ -57,6 +57,7 @@ func TestKubectlDeploy(t *testing.T) {
defer cleanup()

tmpDir.Write("deployment.yaml", deploymentWebYAML)
tmpDir.Write("empty.ignored", "")

var tests = []struct {
description string
Expand All @@ -66,30 +67,23 @@ func TestKubectlDeploy(t *testing.T) {
shouldErr bool
}{
{
description: "parameter mismatch",
shouldErr: true,
cfg: &latest.KubectlDeploy{
Manifests: []string{"deployment.yaml"},
},
builds: []build.Artifact{
{
ImageName: "leeroy-web",
Tag: "leeroy-web:v1",
},
},
description: "no manifest",
cfg: &latest.KubectlDeploy{},
command: testutil.NewFakeCmd(t).WithRunOut("kubectl version --client -ojson", kubectlVersion),
},
{
description: "missing manifest file",
shouldErr: true,
cfg: &latest.KubectlDeploy{
Manifests: []string{"deployment.yaml"},
Manifests: []string{"missing.yaml"},
},
builds: []build.Artifact{
{
ImageName: "leeroy-web",
Tag: "leeroy-web:123",
},
command: testutil.NewFakeCmd(t).WithRunOut("kubectl version --client -ojson", kubectlVersion),
},
{
description: "ignore non-manifest",
cfg: &latest.KubectlDeploy{
Manifests: []string{"*.ignored"},
},
command: testutil.NewFakeCmd(t).WithRunOut("kubectl version --client -ojson", kubectlVersion),
},
{
description: "deploy success",
Expand All @@ -100,33 +94,28 @@ func TestKubectlDeploy(t *testing.T) {
WithRunOut("kubectl version --client -ojson", kubectlVersion).
WithRunOut("kubectl --context kubecontext --namespace testNamespace create --dry-run -oyaml -f "+tmpDir.Path("deployment.yaml"), deploymentWebYAML).
WithRun("kubectl --context kubecontext --namespace testNamespace apply --force -f -"),
builds: []build.Artifact{
{
ImageName: "leeroy-web",
Tag: "leeroy-web:123",
},
},
builds: []build.Artifact{{
ImageName: "leeroy-web",
Tag: "leeroy-web:123",
}},
},
{
description: "deploy command error",
shouldErr: true,
cfg: &latest.KubectlDeploy{
Manifests: []string{"deployment.yaml"},
},
command: testutil.NewFakeCmd(t).
WithRunOut("kubectl version --client -ojson", kubectlVersion).
WithRunOut("kubectl --context kubecontext --namespace testNamespace create --dry-run -oyaml -f "+tmpDir.Path("deployment.yaml"), deploymentWebYAML).
WithRunErr("kubectl --context kubecontext --namespace testNamespace apply --force -f -", fmt.Errorf("")),
builds: []build.Artifact{
{
ImageName: "leeroy-web",
Tag: "leeroy-web:123",
},
},
builds: []build.Artifact{{
ImageName: "leeroy-web",
Tag: "leeroy-web:123",
}},
shouldErr: true,
},
{
description: "additional flags",
shouldErr: true,
cfg: &latest.KubectlDeploy{
Manifests: []string{"deployment.yaml"},
Flags: latest.KubectlFlags{
Expand All @@ -139,21 +128,18 @@ func TestKubectlDeploy(t *testing.T) {
WithRunOut("kubectl version --client -ojson", kubectlVersion).
WithRunOut("kubectl --context kubecontext --namespace testNamespace -v=0 create --dry-run -oyaml -f "+tmpDir.Path("deployment.yaml"), deploymentWebYAML).
WithRunErr("kubectl --context kubecontext --namespace testNamespace -v=0 apply --overwrite=true --force -f -", fmt.Errorf("")),
builds: []build.Artifact{
{
ImageName: "leeroy-web",
Tag: "leeroy-web:123",
},
},
builds: []build.Artifact{{
ImageName: "leeroy-web",
Tag: "leeroy-web:123",
}},
shouldErr: true,
},
}

for _, test := range tests {
t.Run(test.description, func(t *testing.T) {
if test.command != nil {
defer func(c util.Command) { util.DefaultExecCommand = c }(util.DefaultExecCommand)
util.DefaultExecCommand = test.command
}
defer func(c util.Command) { util.DefaultExecCommand = c }(util.DefaultExecCommand)
util.DefaultExecCommand = test.command

k := NewKubectlDeployer(tmpDir.Root(), test.cfg, testKubeContext, testNamespace, "")
err := k.Deploy(context.Background(), ioutil.Discard, test.builds, nil)
Expand Down Expand Up @@ -212,10 +198,8 @@ func TestKubectlCleanup(t *testing.T) {

for _, test := range tests {
t.Run(test.description, func(t *testing.T) {
if test.command != nil {
defer func(c util.Command) { util.DefaultExecCommand = c }(util.DefaultExecCommand)
util.DefaultExecCommand = test.command
}
defer func(c util.Command) { util.DefaultExecCommand = c }(util.DefaultExecCommand)
util.DefaultExecCommand = test.command

k := NewKubectlDeployer(tmpDir.Root(), test.cfg, testKubeContext, testNamespace, "")
err := k.Cleanup(context.Background(), ioutil.Discard)
Expand All @@ -229,12 +213,12 @@ func TestKubectlRedeploy(t *testing.T) {
tmpDir, cleanup := testutil.NewTempDir(t)
defer cleanup()
tmpDir.Write("deployment-web.yaml", deploymentWebYAML)
tmpDir.Write("deployment-app.yaml", deploymentAppYaml)
tmpDir.Write("deployment-app.yaml", deploymentAppYAML)

defer func(c util.Command) { util.DefaultExecCommand = c }(util.DefaultExecCommand)
util.DefaultExecCommand = testutil.NewFakeCmd(t).
WithRunOut("kubectl version --client -ojson", kubectlVersion).
WithRunOut("kubectl --context kubecontext --namespace testNamespace create --dry-run -oyaml -f "+tmpDir.Path("deployment-app.yaml")+" -f "+tmpDir.Path("deployment-web.yaml"), deploymentAppYaml+"\n"+deploymentWebYAML).
WithRunOut("kubectl --context kubecontext --namespace testNamespace create --dry-run -oyaml -f "+tmpDir.Path("deployment-app.yaml")+" -f "+tmpDir.Path("deployment-web.yaml"), deploymentAppYAML+"\n"+deploymentWebYAML).
WithRunInput("kubectl --context kubecontext --namespace testNamespace apply --force -f -", `apiVersion: v1
kind: Pod
metadata:
Expand All @@ -252,7 +236,7 @@ spec:
containers:
- image: leeroy-web:v1
name: leeroy-web`).
WithRunOut("kubectl --context kubecontext --namespace testNamespace create --dry-run -oyaml -f "+tmpDir.Path("deployment-app.yaml")+" -f "+tmpDir.Path("deployment-web.yaml"), deploymentAppYaml+"\n"+deploymentWebYAML).
WithRunOut("kubectl --context kubecontext --namespace testNamespace create --dry-run -oyaml -f "+tmpDir.Path("deployment-app.yaml")+" -f "+tmpDir.Path("deployment-web.yaml"), deploymentAppYAML+"\n"+deploymentWebYAML).
WithRunInput("kubectl --context kubecontext --namespace testNamespace apply --force -f -", `apiVersion: v1
kind: Pod
metadata:
Expand All @@ -261,10 +245,10 @@ spec:
containers:
- image: leeroy-app:v2
name: leeroy-app`).
WithRunOut("kubectl --context kubecontext --namespace testNamespace create --dry-run -oyaml -f "+tmpDir.Path("deployment-app.yaml")+" -f "+tmpDir.Path("deployment-web.yaml"), deploymentAppYaml+"\n"+deploymentWebYAML)
WithRunOut("kubectl --context kubecontext --namespace testNamespace create --dry-run -oyaml -f "+tmpDir.Path("deployment-app.yaml")+" -f "+tmpDir.Path("deployment-web.yaml"), deploymentAppYAML+"\n"+deploymentWebYAML)

cfg := &latest.KubectlDeploy{
Manifests: []string{"deployment-web.yaml", "deployment-app.yaml"},
Manifests: []string{"*.yaml"},
}
deployer := NewKubectlDeployer(tmpDir.Root(), cfg, testKubeContext, testNamespace, "")

Expand Down

0 comments on commit ba8ca7e

Please sign in to comment.