From d7b1b597a759a5b1e67d06441b75a05785ba7e3e Mon Sep 17 00:00:00 2001 From: Adrien Delorme Date: Mon, 8 Apr 2019 13:41:06 +0200 Subject: [PATCH] test provisionning timeout --- command/build_test.go | 4 + command/build_timeout_test.go | 82 +++++++++++++++++++++ command/test-fixtures/timeout/template.json | 37 ++++++++++ 3 files changed, 123 insertions(+) create mode 100644 command/build_timeout_test.go create mode 100644 command/test-fixtures/timeout/template.json diff --git a/command/build_test.go b/command/build_test.go index 50f3db119..8f3661b95 100644 --- a/command/build_test.go +++ b/command/build_test.go @@ -205,4 +205,8 @@ func cleanup() { os.RemoveAll("pear.txt") os.RemoveAll("tomato.txt") os.RemoveAll("unnamed.txt") + os.RemoveAll("roses.txt") + os.RemoveAll("fuchsias.txt") + os.RemoveAll("lilas.txt") + os.RemoveAll("campanules.txt") } diff --git a/command/build_timeout_test.go b/command/build_timeout_test.go new file mode 100644 index 000000000..bba3bbca7 --- /dev/null +++ b/command/build_timeout_test.go @@ -0,0 +1,82 @@ +package command + +import ( + "bytes" + "path/filepath" + "testing" + + "github.com/hashicorp/packer/builder/file" + "github.com/hashicorp/packer/packer" + shell_local "github.com/hashicorp/packer/provisioner/shell-local" + "github.com/hashicorp/packer/provisioner/sleep" +) + +// testCoreConfigBuilder creates a packer CoreConfig that has a file builder +// available. This allows us to test a builder that writes files to disk. +func testCoreConfigSleepBuilder(t *testing.T) *packer.CoreConfig { + components := packer.ComponentFinder{ + Builder: func(n string) (packer.Builder, error) { + switch n { + case "file": + return &file.Builder{}, nil + default: + panic(n) + } + }, + Provisioner: func(n string) (packer.Provisioner, error) { + switch n { + case "shell-local": + return &shell_local.Provisioner{}, nil + case "sleep": + return &sleep.Provisioner{}, nil + default: + panic(n) + } + }, + } + return &packer.CoreConfig{ + Components: components, + } +} + +// testMetaFile creates a Meta object that includes a file builder +func testMetaSleepFile(t *testing.T) Meta { + var out, err bytes.Buffer + return Meta{ + CoreConfig: testCoreConfigSleepBuilder(t), + Ui: &packer.BasicUi{ + Writer: &out, + ErrorWriter: &err, + }, + } +} + +func TestBuildSleepTimeout(t *testing.T) { + defer cleanup() + + c := &BuildCommand{ + Meta: testMetaSleepFile(t), + } + + args := []string{ + filepath.Join(testFixture("timeout"), "template.json"), + } + + defer cleanup() + + if code := c.Run(args); code == 0 { + fatalCommand(t, c.Meta) + } + + for _, f := range []string{"roses.txt", "fuchsias.txt", "lilas.txt"} { + if !fileExists(f) { + t.Errorf("Expected to find %s", f) + } + } + + for _, f := range []string{"campanules.txt"} { + if fileExists(f) { + t.Errorf("Expected to not find %s", f) + } + } +} diff --git a/command/test-fixtures/timeout/template.json b/command/test-fixtures/timeout/template.json new file mode 100644 index 000000000..b577d21a2 --- /dev/null +++ b/command/test-fixtures/timeout/template.json @@ -0,0 +1,37 @@ +{ + "builders": [ + { + "name": "roses", + "type": "file", + "content": "roses", + "target": "roses.txt" + } + ], + "provisioners": [ + { + "type": "shell-local", + "inline": [ + "touch fuchsias.txt" + ], + "timeout": "5s" + }, + { + "type": "shell-local", + "inline": [ + "touch lilas.txt" + ] + }, + { + "type": "sleep", + "duration": "2m", + + "timeout": "1ms" + }, + { + "type": "shell-local", + "inline": [ + "touch campanules.txt" + ] + } + ] +} \ No newline at end of file