github.com/mitchellh/packer@v1.3.2/builder/docker/config_test.go (about)

     1  package docker
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"testing"
     7  )
     8  
     9  func testConfig() map[string]interface{} {
    10  	return map[string]interface{}{
    11  		"export_path": "foo",
    12  		"image":       "bar",
    13  	}
    14  }
    15  
    16  func testConfigStruct(t *testing.T) *Config {
    17  	c, warns, errs := NewConfig(testConfig())
    18  	if len(warns) > 0 {
    19  		t.Fatalf("bad: %#v", len(warns))
    20  	}
    21  	if errs != nil {
    22  		t.Fatalf("bad: %#v", errs)
    23  	}
    24  
    25  	return c
    26  }
    27  
    28  func testConfigErr(t *testing.T, warns []string, err error) {
    29  	if len(warns) > 0 {
    30  		t.Fatalf("bad: %#v", warns)
    31  	}
    32  	if err == nil {
    33  		t.Fatal("should error")
    34  	}
    35  }
    36  
    37  func testConfigOk(t *testing.T, warns []string, err error) {
    38  	if len(warns) > 0 {
    39  		t.Fatalf("bad: %#v", warns)
    40  	}
    41  	if err != nil {
    42  		t.Fatalf("bad: %s", err)
    43  	}
    44  }
    45  
    46  func TestConfigPrepare_exportPath(t *testing.T) {
    47  	td, err := ioutil.TempDir("", "packer")
    48  	if err != nil {
    49  		t.Fatalf("err: %s", err)
    50  	}
    51  	defer os.RemoveAll(td)
    52  
    53  	raw := testConfig()
    54  
    55  	// No export path. This is invalid. Previously this would not error during
    56  	// validation and as a result the failure would happen at build time.
    57  	delete(raw, "export_path")
    58  	_, warns, errs := NewConfig(raw)
    59  	testConfigErr(t, warns, errs)
    60  
    61  	// Good export path
    62  	raw["export_path"] = "good"
    63  	_, warns, errs = NewConfig(raw)
    64  	testConfigOk(t, warns, errs)
    65  
    66  	// Bad export path (directory)
    67  	raw["export_path"] = td
    68  	_, warns, errs = NewConfig(raw)
    69  	testConfigErr(t, warns, errs)
    70  }
    71  
    72  func TestConfigPrepare_exportPathAndCommit(t *testing.T) {
    73  	raw := testConfig()
    74  
    75  	// Export but no commit (explicit default)
    76  	raw["commit"] = false
    77  	_, warns, errs := NewConfig(raw)
    78  	testConfigOk(t, warns, errs)
    79  
    80  	// Commit AND export specified (invalid)
    81  	raw["commit"] = true
    82  	_, warns, errs = NewConfig(raw)
    83  	testConfigErr(t, warns, errs)
    84  
    85  	// Commit but no export
    86  	delete(raw, "export_path")
    87  	_, warns, errs = NewConfig(raw)
    88  	testConfigOk(t, warns, errs)
    89  }
    90  
    91  func TestConfigPrepare_exportDiscard(t *testing.T) {
    92  	raw := testConfig()
    93  
    94  	// Export but no discard (explicit default)
    95  	raw["discard"] = false
    96  	_, warns, errs := NewConfig(raw)
    97  	testConfigOk(t, warns, errs)
    98  
    99  	// Discard AND export (invalid)
   100  	raw["discard"] = true
   101  	_, warns, errs = NewConfig(raw)
   102  	testConfigErr(t, warns, errs)
   103  
   104  	// Discard but no export
   105  	raw["discard"] = true
   106  	delete(raw, "export_path")
   107  	_, warns, errs = NewConfig(raw)
   108  	testConfigOk(t, warns, errs)
   109  }
   110  
   111  func TestConfigPrepare_image(t *testing.T) {
   112  	raw := testConfig()
   113  
   114  	// No image
   115  	delete(raw, "image")
   116  	_, warns, errs := NewConfig(raw)
   117  	testConfigErr(t, warns, errs)
   118  
   119  	// Good image
   120  	raw["image"] = "path"
   121  	_, warns, errs = NewConfig(raw)
   122  	testConfigOk(t, warns, errs)
   123  }
   124  
   125  func TestConfigPrepare_pull(t *testing.T) {
   126  	raw := testConfig()
   127  
   128  	// No pull set
   129  	delete(raw, "pull")
   130  	c, warns, errs := NewConfig(raw)
   131  	testConfigOk(t, warns, errs)
   132  	if !c.Pull {
   133  		t.Fatal("should pull by default")
   134  	}
   135  
   136  	// Pull set
   137  	raw["pull"] = false
   138  	c, warns, errs = NewConfig(raw)
   139  	testConfigOk(t, warns, errs)
   140  	if c.Pull {
   141  		t.Fatal("should not pull")
   142  	}
   143  }