Skip to content

Commit

Permalink
Merge pull request #1462 from dgageot/test-actual-deploy
Browse files Browse the repository at this point in the history
Test what was actually deployed
  • Loading branch information
dgageot authored Jan 14, 2019
2 parents d23064a + 68ab4d9 commit ae14267
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 9 deletions.
40 changes: 31 additions & 9 deletions pkg/skaffold/deploy/kubectl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,9 +217,31 @@ func TestKubectlRedeploy(t *testing.T) {
defer func(c util.Command) { util.DefaultExecCommand = c }(util.DefaultExecCommand)
util.DefaultExecCommand = testutil.NewFakeCmd(t).
WithRunOut("kubectl version --client -ojson", "1.12").
WithRun("kubectl --context kubecontext --namespace testNamespace apply --force -f -").
WithRun("kubectl --context kubecontext --namespace testNamespace apply --force -f -").
WithRun("kubectl --context kubecontext --namespace testNamespace apply --force -f -")
WithRunInput("kubectl --context kubecontext --namespace testNamespace apply --force -f -", `apiVersion: v1
kind: Pod
metadata:
name: leeroy-app
spec:
containers:
- image: leeroy-app:v1
name: leeroy-app
---
apiVersion: v1
kind: Pod
metadata:
name: leeroy-web
spec:
containers:
- image: leeroy-web:v1
name: leeroy-web`).
WithRunInput("kubectl --context kubecontext --namespace testNamespace apply --force -f -", `apiVersion: v1
kind: Pod
metadata:
name: leeroy-app
spec:
containers:
- image: leeroy-app:v2
name: leeroy-app`)

tmpDir, cleanup := testutil.NewTempDir(t)
defer cleanup()
Expand All @@ -232,23 +254,23 @@ func TestKubectlRedeploy(t *testing.T) {
deployer := NewKubectlDeployer(tmpDir.Root(), cfg, testKubeContext, testNamespace, "")

// Deploy one manifest
deployed, err := deployer.Deploy(context.Background(), ioutil.Discard, []build.Artifact{
_, err := deployer.Deploy(context.Background(), ioutil.Discard, []build.Artifact{
{ImageName: "leeroy-web", Tag: "leeroy-web:v1"},
{ImageName: "leeroy-app", Tag: "leeroy-app:v1"},
})
testutil.CheckErrorAndDeepEqual(t, false, err, 2, len(deployed))
testutil.CheckError(t, false, err)

// Deploy one manifest since only one image is updated
deployed, err = deployer.Deploy(context.Background(), ioutil.Discard, []build.Artifact{
_, err = deployer.Deploy(context.Background(), ioutil.Discard, []build.Artifact{
{ImageName: "leeroy-web", Tag: "leeroy-web:v1"},
{ImageName: "leeroy-app", Tag: "leeroy-app:v2"},
})
testutil.CheckErrorAndDeepEqual(t, false, err, 1, len(deployed))
testutil.CheckError(t, false, err)

// Deploy zero manifest since no image is updated
deployed, err = deployer.Deploy(context.Background(), ioutil.Discard, []build.Artifact{
_, err = deployer.Deploy(context.Background(), ioutil.Discard, []build.Artifact{
{ImageName: "leeroy-web", Tag: "leeroy-web:v1"},
{ImageName: "leeroy-app", Tag: "leeroy-app:v2"},
})
testutil.CheckErrorAndDeepEqual(t, false, err, 0, len(deployed))
testutil.CheckError(t, false, err)
}
26 changes: 26 additions & 0 deletions testutil/cmd_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package testutil

import (
"io/ioutil"
"os/exec"
"strings"
"testing"
Expand All @@ -31,6 +32,7 @@ type FakeCmd struct {

type run struct {
command string
input []byte
output []byte
err error
}
Expand Down Expand Up @@ -62,6 +64,13 @@ func (c *FakeCmd) WithRun(command string) *FakeCmd {
})
}

func (c *FakeCmd) WithRunInput(command, input string) *FakeCmd {
return c.addRun(run{
command: command,
input: []byte(input),
})
}

func (c *FakeCmd) WithRunErr(command string, err error) *FakeCmd {
return c.addRun(run{
command: command,
Expand Down Expand Up @@ -119,5 +128,22 @@ func (c *FakeCmd) RunCmd(cmd *exec.Cmd) error {
c.t.Errorf("expected RunCmdOut(%s) to be called. Got RunCmd(%s)", r.command, command)
}

if r.input != nil {
if cmd.Stdin == nil {
c.t.Error("expected to run the command with a custom stdin", command)
}

buf, err := ioutil.ReadAll(cmd.Stdin)
if err != nil {
return err
}

actualInput := string(buf)
expectedInput := string(r.input)
if actualInput != expectedInput {
c.t.Errorf("wrong input. Expected: %s. Got %s", expectedInput, actualInput)
}
}

return r.err
}

0 comments on commit ae14267

Please sign in to comment.