2013-04-15 18:48:42 -04:00
|
|
|
package packer
|
|
|
|
|
|
|
|
import (
|
2013-08-09 17:43:34 -04:00
|
|
|
"io/ioutil"
|
2013-08-13 12:52:32 -04:00
|
|
|
"os"
|
2013-07-14 20:21:19 -04:00
|
|
|
"reflect"
|
2013-07-14 20:58:32 -04:00
|
|
|
"sort"
|
2013-04-15 18:48:42 -04:00
|
|
|
"testing"
|
2013-12-20 15:34:20 -05:00
|
|
|
"time"
|
2013-04-15 18:48:42 -04:00
|
|
|
)
|
|
|
|
|
2013-09-20 13:49:35 -04:00
|
|
|
func testTemplateComponentFinder() *ComponentFinder {
|
2013-11-02 23:31:12 -04:00
|
|
|
builder := new(MockBuilder)
|
2013-09-20 14:13:43 -04:00
|
|
|
pp := new(TestPostProcessor)
|
2013-09-20 13:49:35 -04:00
|
|
|
provisioner := &MockProvisioner{}
|
|
|
|
|
|
|
|
builderMap := map[string]Builder{
|
|
|
|
"test-builder": builder,
|
|
|
|
}
|
|
|
|
|
2013-09-20 14:13:43 -04:00
|
|
|
ppMap := map[string]PostProcessor{
|
|
|
|
"test-pp": pp,
|
|
|
|
}
|
|
|
|
|
2013-09-20 13:49:35 -04:00
|
|
|
provisionerMap := map[string]Provisioner{
|
|
|
|
"test-prov": provisioner,
|
|
|
|
}
|
|
|
|
|
|
|
|
builderFactory := func(n string) (Builder, error) { return builderMap[n], nil }
|
2013-09-20 14:13:43 -04:00
|
|
|
ppFactory := func(n string) (PostProcessor, error) { return ppMap[n], nil }
|
2013-09-20 13:49:35 -04:00
|
|
|
provFactory := func(n string) (Provisioner, error) { return provisionerMap[n], nil }
|
|
|
|
return &ComponentFinder{
|
2013-09-20 14:13:43 -04:00
|
|
|
Builder: builderFactory,
|
|
|
|
PostProcessor: ppFactory,
|
|
|
|
Provisioner: provFactory,
|
2013-09-20 13:49:35 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-09 17:43:34 -04:00
|
|
|
func TestParseTemplateFile_basic(t *testing.T) {
|
|
|
|
data := `
|
|
|
|
{
|
|
|
|
"builders": [{"type": "something"}]
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
tf, err := ioutil.TempFile("", "packer")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
tf.Write([]byte(data))
|
|
|
|
tf.Close()
|
|
|
|
|
2013-12-27 11:17:51 -05:00
|
|
|
result, err := ParseTemplateFile(tf.Name(), nil)
|
2013-08-09 17:43:34 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(result.Builders) != 1 {
|
|
|
|
t.Fatalf("bad: %#v", result.Builders)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-13 12:52:32 -04:00
|
|
|
func TestParseTemplateFile_stdin(t *testing.T) {
|
|
|
|
data := `
|
|
|
|
{
|
|
|
|
"builders": [{"type": "something"}]
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
tf, err := ioutil.TempFile("", "packer")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
defer tf.Close()
|
|
|
|
tf.Write([]byte(data))
|
|
|
|
|
|
|
|
// Sync and seek to the beginning so that we can re-read the contents
|
|
|
|
tf.Sync()
|
|
|
|
tf.Seek(0, 0)
|
|
|
|
|
|
|
|
// Set stdin to something we control
|
|
|
|
oldStdin := os.Stdin
|
|
|
|
defer func() { os.Stdin = oldStdin }()
|
|
|
|
os.Stdin = tf
|
|
|
|
|
2013-12-27 11:17:51 -05:00
|
|
|
result, err := ParseTemplateFile("-", nil)
|
2013-08-13 12:52:32 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(result.Builders) != 1 {
|
|
|
|
t.Fatalf("bad: %#v", result.Builders)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-15 18:48:42 -04:00
|
|
|
func TestParseTemplate_Basic(t *testing.T) {
|
|
|
|
data := `
|
|
|
|
{
|
2013-06-29 17:02:20 -04:00
|
|
|
"builders": [{"type": "something"}]
|
2013-04-15 18:48:42 -04:00
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2013-12-27 11:17:51 -05:00
|
|
|
result, err := ParseTemplate([]byte(data), nil)
|
2013-10-17 03:09:27 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if result == nil {
|
|
|
|
t.Fatal("should have result")
|
|
|
|
}
|
|
|
|
if len(result.Builders) != 1 {
|
|
|
|
t.Fatalf("bad: %#v", result.Builders)
|
|
|
|
}
|
2013-04-15 18:48:42 -04:00
|
|
|
}
|
|
|
|
|
2013-12-11 16:43:43 -05:00
|
|
|
func TestParseTemplate_Description(t *testing.T) {
|
|
|
|
data := `
|
|
|
|
{
|
|
|
|
"description": "Foo",
|
|
|
|
"builders": [{"type": "something"}]
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2013-12-27 11:17:51 -05:00
|
|
|
result, err := ParseTemplate([]byte(data), nil)
|
2013-12-11 16:43:43 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if result == nil {
|
|
|
|
t.Fatal("should have result")
|
|
|
|
}
|
|
|
|
if result.Description != "Foo" {
|
|
|
|
t.Fatalf("bad: %#v", result.Description)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-15 18:48:42 -04:00
|
|
|
func TestParseTemplate_Invalid(t *testing.T) {
|
|
|
|
// Note there is an extra comma below for a purposeful
|
|
|
|
// syntax error in the JSON.
|
|
|
|
data := `
|
|
|
|
{
|
2013-06-18 12:27:08 -04:00
|
|
|
"builders": [],
|
2013-04-15 18:48:42 -04:00
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2013-12-27 11:17:51 -05:00
|
|
|
result, err := ParseTemplate([]byte(data), nil)
|
2013-10-17 03:09:27 -04:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("shold have error")
|
|
|
|
}
|
|
|
|
if result != nil {
|
|
|
|
t.Fatal("should not have result")
|
|
|
|
}
|
2013-07-13 21:29:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseTemplate_InvalidKeys(t *testing.T) {
|
|
|
|
// Note there is an extra comma below for a purposeful
|
|
|
|
// syntax error in the JSON.
|
|
|
|
data := `
|
|
|
|
{
|
|
|
|
"builders": [{"type": "foo"}],
|
|
|
|
"what is this": ""
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2013-12-27 11:17:51 -05:00
|
|
|
result, err := ParseTemplate([]byte(data), nil)
|
2013-10-17 03:09:27 -04:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
|
|
|
if result != nil {
|
|
|
|
t.Fatal("should not have result")
|
|
|
|
}
|
2013-04-15 18:48:42 -04:00
|
|
|
}
|
2013-04-15 20:04:19 -04:00
|
|
|
|
2013-05-22 17:11:34 -04:00
|
|
|
func TestParseTemplate_BuilderWithoutType(t *testing.T) {
|
|
|
|
data := `
|
|
|
|
{
|
|
|
|
"builders": [{}]
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2013-12-27 11:17:51 -05:00
|
|
|
_, err := ParseTemplate([]byte(data), nil)
|
2013-10-17 03:09:27 -04:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
2013-05-22 17:11:34 -04:00
|
|
|
}
|
|
|
|
|
2013-05-22 17:14:40 -04:00
|
|
|
func TestParseTemplate_BuilderWithNonStringType(t *testing.T) {
|
|
|
|
data := `
|
|
|
|
{
|
|
|
|
"builders": [{
|
|
|
|
"type": 42
|
|
|
|
}]
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2013-12-27 11:17:51 -05:00
|
|
|
_, err := ParseTemplate([]byte(data), nil)
|
2013-10-17 03:09:27 -04:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
2013-05-22 17:14:40 -04:00
|
|
|
}
|
|
|
|
|
2013-04-15 20:04:19 -04:00
|
|
|
func TestParseTemplate_BuilderWithoutName(t *testing.T) {
|
|
|
|
data := `
|
|
|
|
{
|
|
|
|
"builders": [
|
|
|
|
{
|
|
|
|
"type": "amazon-ebs"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2013-12-27 11:17:51 -05:00
|
|
|
result, err := ParseTemplate([]byte(data), nil)
|
2013-10-17 03:09:27 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if result == nil {
|
|
|
|
t.Fatal("should have result")
|
|
|
|
}
|
|
|
|
if len(result.Builders) != 1 {
|
|
|
|
t.Fatalf("bad: %#v", result.Builders)
|
|
|
|
}
|
2013-04-15 20:04:19 -04:00
|
|
|
|
|
|
|
builder, ok := result.Builders["amazon-ebs"]
|
2013-10-17 03:09:27 -04:00
|
|
|
if !ok {
|
|
|
|
t.Fatal("should be ok")
|
|
|
|
}
|
|
|
|
if builder.Type != "amazon-ebs" {
|
|
|
|
t.Fatalf("bad: %#v", builder.Type)
|
|
|
|
}
|
2013-04-15 20:04:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseTemplate_BuilderWithName(t *testing.T) {
|
|
|
|
data := `
|
|
|
|
{
|
|
|
|
"builders": [
|
|
|
|
{
|
|
|
|
"name": "bob",
|
|
|
|
"type": "amazon-ebs"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2013-12-27 11:17:51 -05:00
|
|
|
result, err := ParseTemplate([]byte(data), nil)
|
2013-10-17 03:09:27 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if result == nil {
|
|
|
|
t.Fatal("should have result")
|
|
|
|
}
|
|
|
|
if len(result.Builders) != 1 {
|
|
|
|
t.Fatalf("bad: %#v", result.Builders)
|
|
|
|
}
|
2013-04-15 20:04:19 -04:00
|
|
|
|
|
|
|
builder, ok := result.Builders["bob"]
|
2013-10-17 03:09:27 -04:00
|
|
|
if !ok {
|
|
|
|
t.Fatal("should be ok")
|
|
|
|
}
|
|
|
|
if builder.Type != "amazon-ebs" {
|
|
|
|
t.Fatalf("bad: %#v", builder.Type)
|
|
|
|
}
|
2013-07-19 10:35:05 -04:00
|
|
|
|
2013-08-13 12:11:49 -04:00
|
|
|
RawConfig := builder.RawConfig
|
|
|
|
if RawConfig == nil {
|
2013-07-19 10:35:05 -04:00
|
|
|
t.Fatal("missing builder raw config")
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := map[string]interface{}{
|
|
|
|
"type": "amazon-ebs",
|
|
|
|
}
|
|
|
|
|
2013-08-13 12:11:49 -04:00
|
|
|
if !reflect.DeepEqual(RawConfig, expected) {
|
|
|
|
t.Fatalf("bad raw: %#v", RawConfig)
|
2013-07-19 10:35:05 -04:00
|
|
|
}
|
2013-04-15 20:04:19 -04:00
|
|
|
}
|
2013-04-21 15:36:55 -04:00
|
|
|
|
2013-05-22 17:11:34 -04:00
|
|
|
func TestParseTemplate_BuilderWithConflictingName(t *testing.T) {
|
|
|
|
data := `
|
|
|
|
{
|
|
|
|
"builders": [
|
|
|
|
{
|
|
|
|
"name": "bob",
|
|
|
|
"type": "amazon-ebs"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "bob",
|
|
|
|
"type": "foo",
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2013-12-27 11:17:51 -05:00
|
|
|
_, err := ParseTemplate([]byte(data), nil)
|
2013-10-17 03:09:27 -04:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
2013-05-22 17:11:34 -04:00
|
|
|
}
|
|
|
|
|
2013-05-11 01:47:20 -04:00
|
|
|
func TestParseTemplate_Hooks(t *testing.T) {
|
|
|
|
data := `
|
|
|
|
{
|
|
|
|
|
2013-06-29 17:02:20 -04:00
|
|
|
"builders": [{"type": "foo"}],
|
|
|
|
|
2013-05-11 01:47:20 -04:00
|
|
|
"hooks": {
|
|
|
|
"event": ["foo", "bar"]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2013-12-27 11:17:51 -05:00
|
|
|
result, err := ParseTemplate([]byte(data), nil)
|
2013-10-17 03:09:27 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if result == nil {
|
|
|
|
t.Fatal("should have result")
|
|
|
|
}
|
|
|
|
if len(result.Hooks) != 1 {
|
|
|
|
t.Fatalf("bad: %#v", result.Hooks)
|
|
|
|
}
|
2013-05-11 01:47:20 -04:00
|
|
|
|
|
|
|
hooks, ok := result.Hooks["event"]
|
2013-10-17 03:09:27 -04:00
|
|
|
if !ok {
|
|
|
|
t.Fatal("should be okay")
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(hooks, []string{"foo", "bar"}) {
|
|
|
|
t.Fatalf("bad: %#v", hooks)
|
|
|
|
}
|
2013-05-11 01:47:20 -04:00
|
|
|
}
|
|
|
|
|
2013-06-18 12:27:08 -04:00
|
|
|
func TestParseTemplate_PostProcessors(t *testing.T) {
|
|
|
|
data := `
|
|
|
|
{
|
2013-06-29 17:02:20 -04:00
|
|
|
"builders": [{"type": "foo"}],
|
|
|
|
|
2013-06-18 12:27:08 -04:00
|
|
|
"post-processors": [
|
|
|
|
"simple",
|
|
|
|
|
|
|
|
{ "type": "detailed" },
|
|
|
|
|
|
|
|
[ "foo", { "type": "bar" } ]
|
|
|
|
]
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2013-12-27 11:17:51 -05:00
|
|
|
tpl, err := ParseTemplate([]byte(data), nil)
|
2013-06-18 12:27:08 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error parsing: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(tpl.PostProcessors) != 3 {
|
|
|
|
t.Fatalf("bad number of post-processors: %d", len(tpl.PostProcessors))
|
|
|
|
}
|
|
|
|
|
|
|
|
pp := tpl.PostProcessors[0]
|
|
|
|
if len(pp) != 1 {
|
|
|
|
t.Fatalf("wrong number of configs in simple: %d", len(pp))
|
|
|
|
}
|
|
|
|
|
|
|
|
if pp[0].Type != "simple" {
|
|
|
|
t.Fatalf("wrong type for simple: %s", pp[0].Type)
|
|
|
|
}
|
|
|
|
|
|
|
|
pp = tpl.PostProcessors[1]
|
|
|
|
if len(pp) != 1 {
|
|
|
|
t.Fatalf("wrong number of configs in detailed: %d", len(pp))
|
|
|
|
}
|
|
|
|
|
|
|
|
if pp[0].Type != "detailed" {
|
|
|
|
t.Fatalf("wrong type for detailed: %s", pp[0].Type)
|
|
|
|
}
|
|
|
|
|
|
|
|
pp = tpl.PostProcessors[2]
|
|
|
|
if len(pp) != 2 {
|
|
|
|
t.Fatalf("wrong number of configs for sequence: %d", len(pp))
|
|
|
|
}
|
|
|
|
|
|
|
|
if pp[0].Type != "foo" {
|
|
|
|
t.Fatalf("wrong type for sequence 0: %s", pp[0].Type)
|
|
|
|
}
|
|
|
|
|
|
|
|
if pp[1].Type != "bar" {
|
|
|
|
t.Fatalf("wrong type for sequence 1: %s", pp[1].Type)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-22 18:12:36 -04:00
|
|
|
func TestParseTemplate_ProvisionerWithoutType(t *testing.T) {
|
|
|
|
data := `
|
|
|
|
{
|
2013-06-29 17:02:20 -04:00
|
|
|
"builders": [{"type": "foo"}],
|
|
|
|
|
2013-05-22 18:12:36 -04:00
|
|
|
"provisioners": [{}]
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2013-12-27 11:17:51 -05:00
|
|
|
_, err := ParseTemplate([]byte(data), nil)
|
2013-10-17 03:09:27 -04:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("err should not be nil")
|
|
|
|
}
|
2013-05-22 18:12:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseTemplate_ProvisionerWithNonStringType(t *testing.T) {
|
|
|
|
data := `
|
|
|
|
{
|
2013-06-29 17:02:20 -04:00
|
|
|
"builders": [{"type": "foo"}],
|
|
|
|
|
2013-05-22 18:12:36 -04:00
|
|
|
"provisioners": [{
|
|
|
|
"type": 42
|
|
|
|
}]
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2013-12-27 11:17:51 -05:00
|
|
|
_, err := ParseTemplate([]byte(data), nil)
|
2013-10-17 03:09:27 -04:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
2013-05-22 18:12:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseTemplate_Provisioners(t *testing.T) {
|
|
|
|
data := `
|
|
|
|
{
|
2013-06-29 17:02:20 -04:00
|
|
|
"builders": [{"type": "foo"}],
|
|
|
|
|
2013-05-22 18:12:36 -04:00
|
|
|
"provisioners": [
|
|
|
|
{
|
|
|
|
"type": "shell"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2013-12-27 11:17:51 -05:00
|
|
|
result, err := ParseTemplate([]byte(data), nil)
|
2013-10-17 03:09:27 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("err: %s", err)
|
|
|
|
}
|
|
|
|
if result == nil {
|
|
|
|
t.Fatal("should have result")
|
|
|
|
}
|
|
|
|
if len(result.Provisioners) != 1 {
|
|
|
|
t.Fatalf("bad: %#v", result.Provisioners)
|
|
|
|
}
|
|
|
|
if result.Provisioners[0].Type != "shell" {
|
|
|
|
t.Fatalf("bad: %#v", result.Provisioners[0].Type)
|
|
|
|
}
|
|
|
|
if result.Provisioners[0].RawConfig == nil {
|
|
|
|
t.Fatal("should have raw config")
|
|
|
|
}
|
2013-05-22 18:12:36 -04:00
|
|
|
}
|
|
|
|
|
2013-12-20 15:34:20 -05:00
|
|
|
func TestParseTemplate_ProvisionerPauseBefore(t *testing.T) {
|
|
|
|
data := `
|
|
|
|
{
|
|
|
|
"builders": [{"type": "foo"}],
|
|
|
|
|
|
|
|
"provisioners": [
|
|
|
|
{
|
|
|
|
"type": "shell",
|
|
|
|
"pause_before": "10s"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2013-12-27 11:17:51 -05:00
|
|
|
result, err := ParseTemplate([]byte(data), nil)
|
2013-12-20 15:34:20 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("err: %s", err)
|
|
|
|
}
|
|
|
|
if result == nil {
|
|
|
|
t.Fatal("should have result")
|
|
|
|
}
|
|
|
|
if len(result.Provisioners) != 1 {
|
|
|
|
t.Fatalf("bad: %#v", result.Provisioners)
|
|
|
|
}
|
|
|
|
if result.Provisioners[0].Type != "shell" {
|
|
|
|
t.Fatalf("bad: %#v", result.Provisioners[0].Type)
|
|
|
|
}
|
2013-12-21 00:36:41 -05:00
|
|
|
if result.Provisioners[0].pauseBefore != 10*time.Second {
|
2013-12-20 15:34:20 -05:00
|
|
|
t.Fatalf("bad: %s", result.Provisioners[0].pauseBefore)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-09 16:29:38 -04:00
|
|
|
func TestParseTemplate_Variables(t *testing.T) {
|
|
|
|
data := `
|
|
|
|
{
|
|
|
|
"variables": {
|
|
|
|
"foo": "bar",
|
2013-09-24 16:59:44 -04:00
|
|
|
"bar": null,
|
|
|
|
"baz": 27
|
2013-08-09 16:29:38 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
"builders": [{"type": "something"}]
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2013-12-27 11:17:51 -05:00
|
|
|
result, err := ParseTemplate([]byte(data), map[string]string{
|
|
|
|
"bar": "bar",
|
|
|
|
})
|
2013-08-09 16:29:38 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2013-09-24 16:59:44 -04:00
|
|
|
if result.Variables == nil || len(result.Variables) != 3 {
|
2013-08-09 16:29:38 -04:00
|
|
|
t.Fatalf("bad vars: %#v", result.Variables)
|
|
|
|
}
|
2013-08-31 20:33:17 -04:00
|
|
|
|
|
|
|
if result.Variables["foo"].Default != "bar" {
|
|
|
|
t.Fatal("foo default is not right")
|
|
|
|
}
|
|
|
|
if result.Variables["foo"].Required {
|
|
|
|
t.Fatal("foo should not be required")
|
|
|
|
}
|
2013-12-27 11:17:51 -05:00
|
|
|
if result.Variables["foo"].HasValue {
|
|
|
|
t.Fatal("foo should not have value")
|
|
|
|
}
|
2013-08-31 20:33:17 -04:00
|
|
|
|
|
|
|
if result.Variables["bar"].Default != "" {
|
|
|
|
t.Fatal("default should be empty")
|
|
|
|
}
|
|
|
|
if !result.Variables["bar"].Required {
|
|
|
|
t.Fatal("bar should be required")
|
|
|
|
}
|
2013-12-27 11:17:51 -05:00
|
|
|
if !result.Variables["bar"].HasValue {
|
|
|
|
t.Fatal("bar should have value")
|
|
|
|
}
|
|
|
|
if result.Variables["bar"].Value != "bar" {
|
|
|
|
t.Fatal("bad value")
|
|
|
|
}
|
2013-09-24 16:59:44 -04:00
|
|
|
|
|
|
|
if result.Variables["baz"].Default != "27" {
|
|
|
|
t.Fatal("default should be empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
if result.Variables["baz"].Required {
|
|
|
|
t.Fatal("baz should not be required")
|
|
|
|
}
|
2013-08-31 20:33:17 -04:00
|
|
|
}
|
|
|
|
|
2013-12-27 11:17:51 -05:00
|
|
|
func TestParseTemplate_variablesSet(t *testing.T) {
|
|
|
|
data := `
|
|
|
|
{
|
|
|
|
"variables": {
|
|
|
|
"foo": "bar"
|
|
|
|
},
|
|
|
|
|
|
|
|
"builders": [
|
|
|
|
{
|
|
|
|
"name": "test1",
|
|
|
|
"type": "test-builder"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
template, err := ParseTemplate([]byte(data), map[string]string{
|
|
|
|
"foo": "value",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(template.Variables) != 1 {
|
|
|
|
t.Fatalf("bad vars: %#v", template.Variables)
|
|
|
|
}
|
|
|
|
if template.Variables["foo"].Value != "value" {
|
|
|
|
t.Fatalf("bad: %#v", template.Variables["foo"])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseTemplate_variablesSetUnknown(t *testing.T) {
|
|
|
|
data := `
|
|
|
|
{
|
|
|
|
"variables": {
|
|
|
|
"foo": "bar"
|
|
|
|
},
|
|
|
|
|
|
|
|
"builders": [
|
|
|
|
{
|
|
|
|
"name": "test1",
|
|
|
|
"type": "test-builder"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
_, err := ParseTemplate([]byte(data), map[string]string{
|
|
|
|
"what": "value",
|
|
|
|
})
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-31 20:33:17 -04:00
|
|
|
func TestParseTemplate_variablesBadDefault(t *testing.T) {
|
|
|
|
data := `
|
|
|
|
{
|
|
|
|
"variables": {
|
|
|
|
"foo": 7,
|
|
|
|
},
|
|
|
|
|
|
|
|
"builders": [{"type": "something"}]
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2013-12-27 11:17:51 -05:00
|
|
|
_, err := ParseTemplate([]byte(data), nil)
|
2013-08-31 20:33:17 -04:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
2013-08-09 16:29:38 -04:00
|
|
|
}
|
|
|
|
|
2013-04-21 15:36:55 -04:00
|
|
|
func TestTemplate_BuildNames(t *testing.T) {
|
|
|
|
data := `
|
|
|
|
{
|
|
|
|
"builders": [
|
|
|
|
{
|
|
|
|
"name": "bob",
|
|
|
|
"type": "amazon-ebs"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "chris",
|
|
|
|
"type": "another"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2013-12-27 11:17:51 -05:00
|
|
|
result, err := ParseTemplate([]byte(data), nil)
|
2013-10-17 03:09:27 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2013-04-21 15:36:55 -04:00
|
|
|
|
|
|
|
buildNames := result.BuildNames()
|
|
|
|
sort.Strings(buildNames)
|
2013-10-17 03:09:27 -04:00
|
|
|
if !reflect.DeepEqual(buildNames, []string{"bob", "chris"}) {
|
|
|
|
t.Fatalf("bad: %#v", buildNames)
|
|
|
|
}
|
2013-04-21 15:36:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestTemplate_BuildUnknown(t *testing.T) {
|
|
|
|
data := `
|
|
|
|
{
|
|
|
|
"builders": [
|
|
|
|
{
|
|
|
|
"name": "test1",
|
|
|
|
"type": "test-builder"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2013-12-27 11:17:51 -05:00
|
|
|
template, err := ParseTemplate([]byte(data), nil)
|
2013-10-17 03:09:27 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("bad: %s", err)
|
|
|
|
}
|
2013-04-21 15:36:55 -04:00
|
|
|
|
|
|
|
build, err := template.Build("nope", nil)
|
2013-10-17 03:09:27 -04:00
|
|
|
if build != nil {
|
|
|
|
t.Fatalf("build should be nil: %#v", build)
|
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
2013-04-21 15:36:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestTemplate_BuildUnknownBuilder(t *testing.T) {
|
|
|
|
data := `
|
|
|
|
{
|
|
|
|
"builders": [
|
|
|
|
{
|
|
|
|
"name": "test1",
|
|
|
|
"type": "test-builder"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2013-12-27 11:17:51 -05:00
|
|
|
template, err := ParseTemplate([]byte(data), nil)
|
2013-10-17 03:09:27 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2013-04-21 15:36:55 -04:00
|
|
|
|
2013-05-07 23:42:49 -04:00
|
|
|
builderFactory := func(string) (Builder, error) { return nil, nil }
|
2013-05-11 12:56:42 -04:00
|
|
|
components := &ComponentFinder{Builder: builderFactory}
|
|
|
|
build, err := template.Build("test1", components)
|
2013-10-17 03:09:27 -04:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
|
|
|
if build != nil {
|
|
|
|
t.Fatalf("bad: %#v", build)
|
|
|
|
}
|
2013-04-21 15:36:55 -04:00
|
|
|
}
|
|
|
|
|
2013-12-28 11:34:17 -05:00
|
|
|
func TestTemplateBuild_envInVars(t *testing.T) {
|
|
|
|
data := `
|
|
|
|
{
|
|
|
|
"variables": {
|
|
|
|
"foo": "{{env \"foo\"}}"
|
|
|
|
},
|
|
|
|
|
|
|
|
"builders": [
|
|
|
|
{
|
|
|
|
"name": "test1",
|
|
|
|
"type": "test-builder"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
defer os.Setenv("foo", os.Getenv("foo"))
|
|
|
|
if err := os.Setenv("foo", "bar"); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
template, err := ParseTemplate([]byte(data), map[string]string{})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
b, err := template.Build("test1", testComponentFinder())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
coreBuild, ok := b.(*coreBuild)
|
|
|
|
if !ok {
|
|
|
|
t.Fatal("should be ok")
|
|
|
|
}
|
|
|
|
|
|
|
|
if coreBuild.variables["foo"] != "bar" {
|
|
|
|
t.Fatalf("bad: %#v", coreBuild.variables)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-27 11:43:59 -05:00
|
|
|
func TestTemplateBuild_names(t *testing.T) {
|
|
|
|
data := `
|
|
|
|
{
|
|
|
|
"variables": {
|
|
|
|
"foo": null
|
|
|
|
},
|
|
|
|
|
|
|
|
"builders": [
|
|
|
|
{
|
|
|
|
"name": "test1",
|
|
|
|
"type": "test-builder"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "test2-{{user \"foo\"}}",
|
|
|
|
"type": "test-builder"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
template, err := ParseTemplate([]byte(data), map[string]string{"foo": "bar"})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
b, err := template.Build("test1", testComponentFinder())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if b.Name() != "test1" {
|
|
|
|
t.Fatalf("bad: %#v", b.Name())
|
|
|
|
}
|
|
|
|
|
|
|
|
b, err = template.Build("test2-{{user \"foo\"}}", testComponentFinder())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if b.Name() != "test2-bar" {
|
|
|
|
t.Fatalf("bad: %#v", b.Name())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-22 20:02:34 -04:00
|
|
|
func TestTemplate_Build_NilBuilderFunc(t *testing.T) {
|
|
|
|
data := `
|
|
|
|
{
|
|
|
|
"builders": [
|
|
|
|
{
|
|
|
|
"name": "test1",
|
|
|
|
"type": "test-builder"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
|
|
|
|
"provisioners": [
|
|
|
|
{
|
|
|
|
"type": "test-prov"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2013-12-27 11:17:51 -05:00
|
|
|
template, err := ParseTemplate([]byte(data), nil)
|
2013-10-17 03:09:27 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2013-05-22 20:02:34 -04:00
|
|
|
|
|
|
|
defer func() {
|
|
|
|
p := recover()
|
2013-10-17 03:09:27 -04:00
|
|
|
if p == nil {
|
|
|
|
t.Fatal("should panic")
|
|
|
|
}
|
2013-05-22 20:02:34 -04:00
|
|
|
|
2013-10-17 03:09:27 -04:00
|
|
|
if p.(string) != "no builder function" {
|
|
|
|
t.Fatalf("bad panic: %s", p.(string))
|
2013-05-22 20:02:34 -04:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
template.Build("test1", &ComponentFinder{})
|
|
|
|
}
|
|
|
|
|
2013-05-22 20:05:36 -04:00
|
|
|
func TestTemplate_Build_NilProvisionerFunc(t *testing.T) {
|
|
|
|
data := `
|
|
|
|
{
|
|
|
|
"builders": [
|
|
|
|
{
|
|
|
|
"name": "test1",
|
|
|
|
"type": "test-builder"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
|
|
|
|
"provisioners": [
|
|
|
|
{
|
|
|
|
"type": "test-prov"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2013-12-27 11:17:51 -05:00
|
|
|
template, err := ParseTemplate([]byte(data), nil)
|
2013-10-17 03:09:27 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2013-05-22 20:05:36 -04:00
|
|
|
|
|
|
|
defer func() {
|
|
|
|
p := recover()
|
2013-10-17 03:09:27 -04:00
|
|
|
if p == nil {
|
|
|
|
t.Fatal("should panic")
|
|
|
|
}
|
2013-05-22 20:05:36 -04:00
|
|
|
|
2013-10-17 03:09:27 -04:00
|
|
|
if p.(string) != "no provisioner function" {
|
|
|
|
t.Fatalf("bad panic: %s", p.(string))
|
2013-05-22 20:05:36 -04:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
template.Build("test1", &ComponentFinder{
|
|
|
|
Builder: func(string) (Builder, error) { return nil, nil },
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTemplate_Build_NilProvisionerFunc_WithNoProvisioners(t *testing.T) {
|
|
|
|
data := `
|
|
|
|
{
|
|
|
|
"builders": [
|
|
|
|
{
|
|
|
|
"name": "test1",
|
|
|
|
"type": "test-builder"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
|
|
|
|
"provisioners": []
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2013-12-27 11:17:51 -05:00
|
|
|
template, err := ParseTemplate([]byte(data), nil)
|
2013-10-17 03:09:27 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2013-05-22 20:05:36 -04:00
|
|
|
|
|
|
|
template.Build("test1", &ComponentFinder{
|
|
|
|
Builder: func(string) (Builder, error) { return nil, nil },
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2013-04-21 15:36:55 -04:00
|
|
|
func TestTemplate_Build(t *testing.T) {
|
|
|
|
data := `
|
|
|
|
{
|
|
|
|
"builders": [
|
|
|
|
{
|
|
|
|
"name": "test1",
|
|
|
|
"type": "test-builder"
|
|
|
|
}
|
2013-05-22 19:15:57 -04:00
|
|
|
],
|
|
|
|
|
|
|
|
"provisioners": [
|
|
|
|
{
|
|
|
|
"type": "test-prov"
|
|
|
|
}
|
2013-06-18 12:58:39 -04:00
|
|
|
],
|
|
|
|
|
|
|
|
"post-processors": [
|
|
|
|
"simple",
|
2013-06-19 01:45:53 -04:00
|
|
|
[
|
|
|
|
"simple",
|
|
|
|
{ "type": "simple", "keep_input_artifact": true }
|
|
|
|
]
|
2013-04-21 15:36:55 -04:00
|
|
|
]
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2013-05-10 20:01:24 -04:00
|
|
|
expectedConfig := map[string]interface{}{
|
2013-04-21 15:36:55 -04:00
|
|
|
"type": "test-builder",
|
|
|
|
}
|
|
|
|
|
2013-12-27 11:17:51 -05:00
|
|
|
template, err := ParseTemplate([]byte(data), nil)
|
2013-10-17 03:09:27 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2013-04-21 15:36:55 -04:00
|
|
|
|
2013-11-02 23:31:12 -04:00
|
|
|
builder := new(MockBuilder)
|
2013-05-10 20:01:24 -04:00
|
|
|
builderMap := map[string]Builder{
|
2013-04-21 15:36:55 -04:00
|
|
|
"test-builder": builder,
|
|
|
|
}
|
|
|
|
|
2013-08-31 02:21:15 -04:00
|
|
|
provisioner := &MockProvisioner{}
|
2013-05-22 19:15:57 -04:00
|
|
|
provisionerMap := map[string]Provisioner{
|
|
|
|
"test-prov": provisioner,
|
|
|
|
}
|
|
|
|
|
2013-06-18 12:58:39 -04:00
|
|
|
pp := new(TestPostProcessor)
|
|
|
|
ppMap := map[string]PostProcessor{
|
|
|
|
"simple": pp,
|
|
|
|
}
|
|
|
|
|
2013-05-07 23:42:49 -04:00
|
|
|
builderFactory := func(n string) (Builder, error) { return builderMap[n], nil }
|
2013-06-18 12:58:39 -04:00
|
|
|
ppFactory := func(n string) (PostProcessor, error) { return ppMap[n], nil }
|
2013-05-22 19:15:57 -04:00
|
|
|
provFactory := func(n string) (Provisioner, error) { return provisionerMap[n], nil }
|
|
|
|
components := &ComponentFinder{
|
2013-06-18 12:58:39 -04:00
|
|
|
Builder: builderFactory,
|
|
|
|
PostProcessor: ppFactory,
|
|
|
|
Provisioner: provFactory,
|
2013-05-22 19:15:57 -04:00
|
|
|
}
|
2013-04-21 15:36:55 -04:00
|
|
|
|
|
|
|
// Get the build, verifying we can get it without issue, but also
|
|
|
|
// that the proper builder was looked up and used for the build.
|
2013-05-11 12:56:42 -04:00
|
|
|
build, err := template.Build("test1", components)
|
2013-10-17 03:09:27 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2013-04-21 15:36:55 -04:00
|
|
|
|
2013-05-22 19:15:57 -04:00
|
|
|
coreBuild, ok := build.(*coreBuild)
|
2013-10-17 03:09:27 -04:00
|
|
|
if !ok {
|
|
|
|
t.Fatal("should be ok")
|
|
|
|
}
|
|
|
|
if coreBuild.builder != builder {
|
|
|
|
t.Fatalf("bad: %#v", coreBuild.builder)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(coreBuild.builderConfig, expectedConfig) {
|
|
|
|
t.Fatalf("bad: %#v", coreBuild.builderConfig)
|
|
|
|
}
|
|
|
|
if len(coreBuild.provisioners) != 1 {
|
|
|
|
t.Fatalf("bad: %#v", coreBuild.provisioners)
|
|
|
|
}
|
|
|
|
if len(coreBuild.postProcessors) != 2 {
|
|
|
|
t.Fatalf("bad: %#v", coreBuild.postProcessors)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(coreBuild.postProcessors[0]) != 1 {
|
|
|
|
t.Fatalf("bad: %#v", coreBuild.postProcessors[0])
|
|
|
|
}
|
|
|
|
if len(coreBuild.postProcessors[1]) != 2 {
|
|
|
|
t.Fatalf("bad: %#v", coreBuild.postProcessors[1])
|
|
|
|
}
|
|
|
|
|
|
|
|
if coreBuild.postProcessors[1][0].keepInputArtifact {
|
|
|
|
t.Fatal("postProcessors[1][0] should not keep input artifact")
|
|
|
|
}
|
|
|
|
if !coreBuild.postProcessors[1][1].keepInputArtifact {
|
|
|
|
t.Fatal("postProcessors[1][1] should keep input artifact")
|
|
|
|
}
|
2013-08-19 19:00:13 -04:00
|
|
|
|
|
|
|
config := coreBuild.postProcessors[1][1].config
|
|
|
|
if _, ok := config["keep_input_artifact"]; ok {
|
|
|
|
t.Fatal("should not have keep_input_artifact")
|
|
|
|
}
|
2013-04-21 15:36:55 -04:00
|
|
|
}
|
2013-06-07 13:35:26 -04:00
|
|
|
|
2013-09-20 14:20:05 -04:00
|
|
|
func TestTemplateBuild_exceptOnlyPP(t *testing.T) {
|
|
|
|
data := `
|
|
|
|
{
|
|
|
|
"builders": [
|
|
|
|
{
|
|
|
|
"name": "test1",
|
|
|
|
"type": "test-builder"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "test2",
|
|
|
|
"type": "test-builder"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
|
|
|
|
"post-processors": [
|
|
|
|
{
|
|
|
|
"type": "test-pp",
|
|
|
|
"except": ["test1"],
|
|
|
|
"only": ["test1"]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2013-12-27 11:17:51 -05:00
|
|
|
_, err := ParseTemplate([]byte(data), nil)
|
2013-09-20 14:20:05 -04:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTemplateBuild_exceptOnlyProv(t *testing.T) {
|
|
|
|
data := `
|
|
|
|
{
|
|
|
|
"builders": [
|
|
|
|
{
|
|
|
|
"name": "test1",
|
|
|
|
"type": "test-builder"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "test2",
|
|
|
|
"type": "test-builder"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
|
|
|
|
"provisioners": [
|
|
|
|
{
|
|
|
|
"type": "test-prov",
|
|
|
|
"except": ["test1"],
|
|
|
|
"only": ["test1"]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2013-12-27 11:17:51 -05:00
|
|
|
_, err := ParseTemplate([]byte(data), nil)
|
2013-09-20 14:20:05 -04:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTemplateBuild_exceptPPInvalid(t *testing.T) {
|
2013-09-20 14:16:33 -04:00
|
|
|
data := `
|
|
|
|
{
|
|
|
|
"builders": [
|
|
|
|
{
|
|
|
|
"name": "test1",
|
|
|
|
"type": "test-builder"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "test2",
|
|
|
|
"type": "test-builder"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
|
|
|
|
"post-processors": [
|
|
|
|
{
|
|
|
|
"type": "test-pp",
|
2013-09-20 14:18:00 -04:00
|
|
|
"except": ["test5"]
|
2013-09-20 14:16:33 -04:00
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2013-12-27 11:17:51 -05:00
|
|
|
_, err := ParseTemplate([]byte(data), nil)
|
2013-09-20 14:16:33 -04:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTemplateBuild_exceptPP(t *testing.T) {
|
|
|
|
data := `
|
|
|
|
{
|
|
|
|
"builders": [
|
|
|
|
{
|
|
|
|
"name": "test1",
|
|
|
|
"type": "test-builder"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "test2",
|
|
|
|
"type": "test-builder"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
|
|
|
|
"post-processors": [
|
|
|
|
{
|
|
|
|
"type": "test-pp",
|
|
|
|
"except": ["test1"]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2013-12-27 11:17:51 -05:00
|
|
|
template, err := ParseTemplate([]byte(data), nil)
|
2013-09-20 14:16:33 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify test1 has no post-processors
|
|
|
|
build, err := template.Build("test1", testTemplateComponentFinder())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cbuild := build.(*coreBuild)
|
|
|
|
if len(cbuild.postProcessors) > 0 {
|
|
|
|
t.Fatal("should have no postProcessors")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify test2 has no post-processors
|
|
|
|
build, err = template.Build("test2", testTemplateComponentFinder())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cbuild = build.(*coreBuild)
|
|
|
|
if len(cbuild.postProcessors) != 1 {
|
|
|
|
t.Fatalf("invalid: %d", len(cbuild.postProcessors))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTemplateBuild_exceptProvInvalid(t *testing.T) {
|
|
|
|
data := `
|
|
|
|
{
|
|
|
|
"builders": [
|
|
|
|
{
|
|
|
|
"name": "test1",
|
|
|
|
"type": "test-builder"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "test2",
|
|
|
|
"type": "test-builder"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
|
|
|
|
"provisioners": [
|
|
|
|
{
|
|
|
|
"type": "test-prov",
|
2013-09-20 14:18:00 -04:00
|
|
|
"except": ["test5"]
|
2013-09-20 14:16:33 -04:00
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2013-12-27 11:17:51 -05:00
|
|
|
_, err := ParseTemplate([]byte(data), nil)
|
2013-09-20 14:16:33 -04:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTemplateBuild_exceptProv(t *testing.T) {
|
|
|
|
data := `
|
|
|
|
{
|
|
|
|
"builders": [
|
|
|
|
{
|
|
|
|
"name": "test1",
|
|
|
|
"type": "test-builder"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "test2",
|
|
|
|
"type": "test-builder"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
|
|
|
|
"provisioners": [
|
|
|
|
{
|
|
|
|
"type": "test-prov",
|
|
|
|
"except": ["test1"]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2013-12-27 11:17:51 -05:00
|
|
|
template, err := ParseTemplate([]byte(data), nil)
|
2013-09-20 14:16:33 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify test1 has no provisioners
|
|
|
|
build, err := template.Build("test1", testTemplateComponentFinder())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cbuild := build.(*coreBuild)
|
|
|
|
if len(cbuild.provisioners) > 0 {
|
|
|
|
t.Fatal("should have no provisioners")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify test2 has no provisioners
|
|
|
|
build, err = template.Build("test2", testTemplateComponentFinder())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cbuild = build.(*coreBuild)
|
|
|
|
if len(cbuild.provisioners) != 1 {
|
|
|
|
t.Fatalf("invalid: %d", len(cbuild.provisioners))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-20 14:13:43 -04:00
|
|
|
func TestTemplateBuild_onlyPPInvalid(t *testing.T) {
|
|
|
|
data := `
|
|
|
|
{
|
|
|
|
"builders": [
|
|
|
|
{
|
|
|
|
"name": "test1",
|
|
|
|
"type": "test-builder"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "test2",
|
|
|
|
"type": "test-builder"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
|
|
|
|
"post-processors": [
|
|
|
|
{
|
|
|
|
"type": "test-pp",
|
2013-09-20 14:18:00 -04:00
|
|
|
"only": ["test5"]
|
2013-09-20 14:13:43 -04:00
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2013-12-27 11:17:51 -05:00
|
|
|
_, err := ParseTemplate([]byte(data), nil)
|
2013-09-20 14:13:43 -04:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTemplateBuild_onlyPP(t *testing.T) {
|
|
|
|
data := `
|
|
|
|
{
|
|
|
|
"builders": [
|
|
|
|
{
|
|
|
|
"name": "test1",
|
|
|
|
"type": "test-builder"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "test2",
|
|
|
|
"type": "test-builder"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
|
|
|
|
"post-processors": [
|
|
|
|
{
|
|
|
|
"type": "test-pp",
|
|
|
|
"only": ["test2"]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2013-12-27 11:17:51 -05:00
|
|
|
template, err := ParseTemplate([]byte(data), nil)
|
2013-09-20 14:13:43 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify test1 has no post-processors
|
|
|
|
build, err := template.Build("test1", testTemplateComponentFinder())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cbuild := build.(*coreBuild)
|
|
|
|
if len(cbuild.postProcessors) > 0 {
|
|
|
|
t.Fatal("should have no postProcessors")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify test2 has no post-processors
|
|
|
|
build, err = template.Build("test2", testTemplateComponentFinder())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cbuild = build.(*coreBuild)
|
|
|
|
if len(cbuild.postProcessors) != 1 {
|
|
|
|
t.Fatalf("invalid: %d", len(cbuild.postProcessors))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-20 13:49:35 -04:00
|
|
|
func TestTemplateBuild_onlyProvInvalid(t *testing.T) {
|
|
|
|
data := `
|
|
|
|
{
|
|
|
|
"builders": [
|
|
|
|
{
|
|
|
|
"name": "test1",
|
|
|
|
"type": "test-builder"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "test2",
|
|
|
|
"type": "test-builder"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
|
|
|
|
"provisioners": [
|
|
|
|
{
|
|
|
|
"type": "test-prov",
|
2013-09-20 14:18:00 -04:00
|
|
|
"only": ["test5"]
|
2013-09-20 13:49:35 -04:00
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2013-12-27 11:17:51 -05:00
|
|
|
_, err := ParseTemplate([]byte(data), nil)
|
2013-09-20 13:49:35 -04:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTemplateBuild_onlyProv(t *testing.T) {
|
|
|
|
data := `
|
|
|
|
{
|
|
|
|
"builders": [
|
|
|
|
{
|
|
|
|
"name": "test1",
|
|
|
|
"type": "test-builder"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "test2",
|
|
|
|
"type": "test-builder"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
|
|
|
|
"provisioners": [
|
|
|
|
{
|
|
|
|
"type": "test-prov",
|
|
|
|
"only": ["test2"]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2013-12-27 11:17:51 -05:00
|
|
|
template, err := ParseTemplate([]byte(data), nil)
|
2013-09-20 13:49:35 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify test1 has no provisioners
|
|
|
|
build, err := template.Build("test1", testTemplateComponentFinder())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cbuild := build.(*coreBuild)
|
|
|
|
if len(cbuild.provisioners) > 0 {
|
|
|
|
t.Fatal("should have no provisioners")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify test2 has no provisioners
|
|
|
|
build, err = template.Build("test2", testTemplateComponentFinder())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cbuild = build.(*coreBuild)
|
|
|
|
if len(cbuild.provisioners) != 1 {
|
|
|
|
t.Fatalf("invalid: %d", len(cbuild.provisioners))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-07 13:35:26 -04:00
|
|
|
func TestTemplate_Build_ProvisionerOverride(t *testing.T) {
|
|
|
|
data := `
|
|
|
|
{
|
|
|
|
"builders": [
|
|
|
|
{
|
|
|
|
"name": "test1",
|
|
|
|
"type": "test-builder"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
|
|
|
|
"provisioners": [
|
|
|
|
{
|
|
|
|
"type": "test-prov",
|
|
|
|
|
|
|
|
"override": {
|
|
|
|
"test1": {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2013-12-27 11:17:51 -05:00
|
|
|
template, err := ParseTemplate([]byte(data), nil)
|
2013-07-14 20:21:19 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2013-08-13 12:11:49 -04:00
|
|
|
RawConfig := template.Provisioners[0].RawConfig
|
|
|
|
if RawConfig == nil {
|
2013-07-14 20:21:19 -04:00
|
|
|
t.Fatal("missing provisioner raw config")
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := map[string]interface{}{
|
|
|
|
"type": "test-prov",
|
|
|
|
}
|
|
|
|
|
2013-08-13 12:11:49 -04:00
|
|
|
if !reflect.DeepEqual(RawConfig, expected) {
|
|
|
|
t.Fatalf("bad raw: %#v", RawConfig)
|
2013-07-14 20:21:19 -04:00
|
|
|
}
|
2013-06-07 13:35:26 -04:00
|
|
|
|
2013-11-02 23:31:12 -04:00
|
|
|
builder := new(MockBuilder)
|
2013-06-07 13:35:26 -04:00
|
|
|
builderMap := map[string]Builder{
|
|
|
|
"test-builder": builder,
|
|
|
|
}
|
|
|
|
|
2013-08-31 02:21:15 -04:00
|
|
|
provisioner := &MockProvisioner{}
|
2013-06-07 13:35:26 -04:00
|
|
|
provisionerMap := map[string]Provisioner{
|
|
|
|
"test-prov": provisioner,
|
|
|
|
}
|
|
|
|
|
|
|
|
builderFactory := func(n string) (Builder, error) { return builderMap[n], nil }
|
|
|
|
provFactory := func(n string) (Provisioner, error) { return provisionerMap[n], nil }
|
|
|
|
components := &ComponentFinder{
|
|
|
|
Builder: builderFactory,
|
|
|
|
Provisioner: provFactory,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the build, verifying we can get it without issue, but also
|
|
|
|
// that the proper builder was looked up and used for the build.
|
|
|
|
build, err := template.Build("test1", components)
|
2013-10-17 03:09:27 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2013-06-07 13:35:26 -04:00
|
|
|
|
|
|
|
coreBuild, ok := build.(*coreBuild)
|
2013-10-17 03:09:27 -04:00
|
|
|
if !ok {
|
|
|
|
t.Fatal("should be okay")
|
|
|
|
}
|
|
|
|
if len(coreBuild.provisioners) != 1 {
|
|
|
|
t.Fatalf("bad: %#v", coreBuild.provisioners)
|
|
|
|
}
|
|
|
|
if len(coreBuild.provisioners[0].config) != 2 {
|
|
|
|
t.Fatalf("bad: %#v", coreBuild.provisioners[0].config)
|
|
|
|
}
|
2013-06-07 13:35:26 -04:00
|
|
|
}
|
2013-08-09 16:29:38 -04:00
|
|
|
|
2013-08-28 00:34:55 -04:00
|
|
|
func TestTemplate_Build_ProvisionerOverrideBad(t *testing.T) {
|
|
|
|
data := `
|
|
|
|
{
|
|
|
|
"builders": [
|
|
|
|
{
|
|
|
|
"name": "test1",
|
|
|
|
"type": "test-builder"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
|
|
|
|
"provisioners": [
|
|
|
|
{
|
|
|
|
"type": "test-prov",
|
|
|
|
|
|
|
|
"override": {
|
|
|
|
"testNope": {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2013-12-27 11:17:51 -05:00
|
|
|
_, err := ParseTemplate([]byte(data), nil)
|
2013-08-28 00:34:55 -04:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-21 00:44:15 -05:00
|
|
|
func TestTemplateBuild_ProvisionerPauseBefore(t *testing.T) {
|
|
|
|
data := `
|
|
|
|
{
|
|
|
|
"builders": [
|
|
|
|
{
|
|
|
|
"name": "test1",
|
|
|
|
"type": "test-builder"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
|
|
|
|
"provisioners": [
|
|
|
|
{
|
|
|
|
"type": "test-prov",
|
|
|
|
"pause_before": "5s"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2013-12-27 11:17:51 -05:00
|
|
|
template, err := ParseTemplate([]byte(data), nil)
|
2013-12-21 00:44:15 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
builder := new(MockBuilder)
|
|
|
|
builderMap := map[string]Builder{
|
|
|
|
"test-builder": builder,
|
|
|
|
}
|
|
|
|
|
|
|
|
provisioner := &MockProvisioner{}
|
|
|
|
provisionerMap := map[string]Provisioner{
|
|
|
|
"test-prov": provisioner,
|
|
|
|
}
|
|
|
|
|
|
|
|
builderFactory := func(n string) (Builder, error) { return builderMap[n], nil }
|
|
|
|
provFactory := func(n string) (Provisioner, error) { return provisionerMap[n], nil }
|
|
|
|
components := &ComponentFinder{
|
|
|
|
Builder: builderFactory,
|
|
|
|
Provisioner: provFactory,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the build, verifying we can get it without issue, but also
|
|
|
|
// that the proper builder was looked up and used for the build.
|
|
|
|
build, err := template.Build("test1", components)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
coreBuild, ok := build.(*coreBuild)
|
|
|
|
if !ok {
|
|
|
|
t.Fatal("should be okay")
|
|
|
|
}
|
|
|
|
if len(coreBuild.provisioners) != 1 {
|
|
|
|
t.Fatalf("bad: %#v", coreBuild.provisioners)
|
|
|
|
}
|
|
|
|
if pp, ok := coreBuild.provisioners[0].provisioner.(*PausedProvisioner); !ok {
|
|
|
|
t.Fatalf("should be paused provisioner")
|
|
|
|
} else {
|
|
|
|
if pp.PauseBefore != 5*time.Second {
|
|
|
|
t.Fatalf("bad: %#v", pp.PauseBefore)
|
|
|
|
}
|
|
|
|
}
|
2013-12-27 11:52:40 -05:00
|
|
|
|
|
|
|
config := coreBuild.provisioners[0].config[0].(map[string]interface{})
|
|
|
|
if _, ok := config["pause_before"]; ok {
|
|
|
|
t.Fatal("pause_before should be removed")
|
|
|
|
}
|
2013-12-21 00:44:15 -05:00
|
|
|
}
|
|
|
|
|
2013-08-09 16:29:38 -04:00
|
|
|
func TestTemplateBuild_variables(t *testing.T) {
|
|
|
|
data := `
|
|
|
|
{
|
|
|
|
"variables": {
|
|
|
|
"foo": "bar"
|
|
|
|
},
|
|
|
|
|
|
|
|
"builders": [
|
|
|
|
{
|
|
|
|
"name": "test1",
|
|
|
|
"type": "test-builder"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2013-12-27 11:17:51 -05:00
|
|
|
template, err := ParseTemplate([]byte(data), nil)
|
2013-08-09 16:29:38 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
build, err := template.Build("test1", testComponentFinder())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
coreBuild, ok := build.(*coreBuild)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("couldn't convert!")
|
|
|
|
}
|
|
|
|
|
2013-12-27 11:17:51 -05:00
|
|
|
expected := map[string]string{"foo": "bar"}
|
|
|
|
if !reflect.DeepEqual(coreBuild.variables, expected) {
|
2013-08-09 16:29:38 -04:00
|
|
|
t.Fatalf("bad vars: %#v", coreBuild.variables)
|
|
|
|
}
|
|
|
|
}
|
2013-12-27 11:17:51 -05:00
|
|
|
|
|
|
|
func TestTemplateBuild_variablesRequiredNotSet(t *testing.T) {
|
|
|
|
data := `
|
|
|
|
{
|
|
|
|
"variables": {
|
|
|
|
"foo": null
|
|
|
|
},
|
|
|
|
|
|
|
|
"builders": [
|
|
|
|
{
|
|
|
|
"name": "test1",
|
|
|
|
"type": "test-builder"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
template, err := ParseTemplate([]byte(data), map[string]string{})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = template.Build("test1", testComponentFinder())
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should error")
|
|
|
|
}
|
|
|
|
}
|