test provisionning timeout

This commit is contained in:
Adrien Delorme 2019-04-08 13:41:06 +02:00
parent 06941a86a3
commit d7b1b597a7
3 changed files with 123 additions and 0 deletions

View File

@ -205,4 +205,8 @@ func cleanup() {
os.RemoveAll("pear.txt") os.RemoveAll("pear.txt")
os.RemoveAll("tomato.txt") os.RemoveAll("tomato.txt")
os.RemoveAll("unnamed.txt") os.RemoveAll("unnamed.txt")
os.RemoveAll("roses.txt")
os.RemoveAll("fuchsias.txt")
os.RemoveAll("lilas.txt")
os.RemoveAll("campanules.txt")
} }

View File

@ -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)
}
}
}

View File

@ -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"
]
}
]
}