github.com/StackPointCloud/packer@v0.10.2-0.20180716202532-b28098e0f79b/builder/amazon/chroot/builder_test.go (about)

     1  package chroot
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/hashicorp/packer/packer"
     7  )
     8  
     9  func testConfig() map[string]interface{} {
    10  	return map[string]interface{}{
    11  		"ami_name":   "foo",
    12  		"source_ami": "foo",
    13  		"region":     "us-east-1",
    14  	}
    15  }
    16  
    17  func TestBuilder_ImplementsBuilder(t *testing.T) {
    18  	var raw interface{}
    19  	raw = &Builder{}
    20  	if _, ok := raw.(packer.Builder); !ok {
    21  		t.Fatalf("Builder should be a builder")
    22  	}
    23  }
    24  
    25  func TestBuilderPrepare_AMIName(t *testing.T) {
    26  	var b Builder
    27  	config := testConfig()
    28  
    29  	// Test good
    30  	config["ami_name"] = "foo"
    31  	warnings, err := b.Prepare(config)
    32  	if len(warnings) > 0 {
    33  		t.Fatalf("bad: %#v", warnings)
    34  	}
    35  	if err != nil {
    36  		t.Fatalf("should not have error: %s", err)
    37  	}
    38  
    39  	// Test bad
    40  	config["ami_name"] = "foo {{"
    41  	b = Builder{}
    42  	warnings, err = b.Prepare(config)
    43  	if len(warnings) > 0 {
    44  		t.Fatalf("bad: %#v", warnings)
    45  	}
    46  	if err == nil {
    47  		t.Fatal("should have error")
    48  	}
    49  
    50  	// Test bad
    51  	delete(config, "ami_name")
    52  	b = Builder{}
    53  	warnings, err = b.Prepare(config)
    54  	if len(warnings) > 0 {
    55  		t.Fatalf("bad: %#v", warnings)
    56  	}
    57  	if err == nil {
    58  		t.Fatal("should have error")
    59  	}
    60  }
    61  
    62  func TestBuilderPrepare_ChrootMounts(t *testing.T) {
    63  	b := &Builder{}
    64  	config := testConfig()
    65  
    66  	config["chroot_mounts"] = nil
    67  	warnings, err := b.Prepare(config)
    68  	if len(warnings) > 0 {
    69  		t.Fatalf("bad: %#v", warnings)
    70  	}
    71  	if err != nil {
    72  		t.Errorf("err: %s", err)
    73  	}
    74  }
    75  
    76  func TestBuilderPrepare_ChrootMountsBadDefaults(t *testing.T) {
    77  	b := &Builder{}
    78  	config := testConfig()
    79  
    80  	config["chroot_mounts"] = [][]string{
    81  		{"bad"},
    82  	}
    83  	warnings, err := b.Prepare(config)
    84  	if len(warnings) > 0 {
    85  		t.Fatalf("bad: %#v", warnings)
    86  	}
    87  	if err == nil {
    88  		t.Fatal("should have error")
    89  	}
    90  }
    91  func TestBuilderPrepare_SourceAmi(t *testing.T) {
    92  	b := &Builder{}
    93  	config := testConfig()
    94  
    95  	config["source_ami"] = ""
    96  	warnings, err := b.Prepare(config)
    97  	if len(warnings) > 0 {
    98  		t.Fatalf("bad: %#v", warnings)
    99  	}
   100  	if err == nil {
   101  		t.Fatal("should have error")
   102  	}
   103  
   104  	config["source_ami"] = "foo"
   105  	warnings, err = b.Prepare(config)
   106  	if len(warnings) > 0 {
   107  		t.Fatalf("bad: %#v", warnings)
   108  	}
   109  	if err != nil {
   110  		t.Errorf("err: %s", err)
   111  	}
   112  }
   113  
   114  func TestBuilderPrepare_CommandWrapper(t *testing.T) {
   115  	b := &Builder{}
   116  	config := testConfig()
   117  
   118  	config["command_wrapper"] = "echo hi; {{.Command}}"
   119  	warnings, err := b.Prepare(config)
   120  	if len(warnings) > 0 {
   121  		t.Fatalf("bad: %#v", warnings)
   122  	}
   123  	if err != nil {
   124  		t.Errorf("err: %s", err)
   125  	}
   126  }
   127  
   128  func TestBuilderPrepare_CopyFiles(t *testing.T) {
   129  	b := &Builder{}
   130  	config := testConfig()
   131  
   132  	warnings, err := b.Prepare(config)
   133  	if len(warnings) > 0 {
   134  		t.Fatalf("bad: %#v", warnings)
   135  	}
   136  	if err != nil {
   137  		t.Errorf("err: %s", err)
   138  	}
   139  
   140  	if len(b.config.CopyFiles) != 1 && b.config.CopyFiles[0] != "/etc/resolv.conf" {
   141  		t.Errorf("Was expecting default value for copy_files.")
   142  	}
   143  }
   144  
   145  func TestBuilderPrepare_CopyFilesNoDefault(t *testing.T) {
   146  	b := &Builder{}
   147  	config := testConfig()
   148  
   149  	config["copy_files"] = []string{}
   150  	warnings, err := b.Prepare(config)
   151  	if len(warnings) > 0 {
   152  		t.Fatalf("bad: %#v", warnings)
   153  	}
   154  	if err != nil {
   155  		t.Errorf("err: %s", err)
   156  	}
   157  
   158  	if len(b.config.CopyFiles) > 0 {
   159  		t.Errorf("Was expecting no default value for copy_files.")
   160  	}
   161  }