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