github.com/sneal/packer@v0.5.2/builder/docker/config_test.go (about)

     1  package docker
     2  
     3  import (
     4  	"testing"
     5  )
     6  
     7  func testConfig() map[string]interface{} {
     8  	return map[string]interface{}{
     9  		"export_path": "foo",
    10  		"image":       "bar",
    11  	}
    12  }
    13  
    14  func testConfigStruct(t *testing.T) *Config {
    15  	c, warns, errs := NewConfig(testConfig())
    16  	if len(warns) > 0 {
    17  		t.Fatalf("bad: %#v", len(warns))
    18  	}
    19  	if errs != nil {
    20  		t.Fatalf("bad: %#v", errs)
    21  	}
    22  
    23  	return c
    24  }
    25  
    26  func testConfigErr(t *testing.T, warns []string, err error) {
    27  	if len(warns) > 0 {
    28  		t.Fatalf("bad: %#v", warns)
    29  	}
    30  	if err == nil {
    31  		t.Fatal("should error")
    32  	}
    33  }
    34  
    35  func testConfigOk(t *testing.T, warns []string, err error) {
    36  	if len(warns) > 0 {
    37  		t.Fatalf("bad: %#v", warns)
    38  	}
    39  	if err != nil {
    40  		t.Fatalf("bad: %s", err)
    41  	}
    42  }
    43  
    44  func TestConfigPrepare_exportPath(t *testing.T) {
    45  	raw := testConfig()
    46  
    47  	// No export path
    48  	delete(raw, "export_path")
    49  	_, warns, errs := NewConfig(raw)
    50  	testConfigErr(t, warns, errs)
    51  
    52  	// Good export path
    53  	raw["export_path"] = "good"
    54  	_, warns, errs = NewConfig(raw)
    55  	testConfigOk(t, warns, errs)
    56  }
    57  
    58  func TestConfigPrepare_image(t *testing.T) {
    59  	raw := testConfig()
    60  
    61  	// No image
    62  	delete(raw, "image")
    63  	_, warns, errs := NewConfig(raw)
    64  	testConfigErr(t, warns, errs)
    65  
    66  	// Good image
    67  	raw["image"] = "path"
    68  	_, warns, errs = NewConfig(raw)
    69  	testConfigOk(t, warns, errs)
    70  }
    71  
    72  func TestConfigPrepare_pull(t *testing.T) {
    73  	raw := testConfig()
    74  
    75  	// No pull set
    76  	delete(raw, "pull")
    77  	c, warns, errs := NewConfig(raw)
    78  	testConfigOk(t, warns, errs)
    79  	if !c.Pull {
    80  		t.Fatal("should pull by default")
    81  	}
    82  
    83  	// Pull set
    84  	raw["pull"] = false
    85  	c, warns, errs = NewConfig(raw)
    86  	testConfigOk(t, warns, errs)
    87  	if c.Pull {
    88  		t.Fatal("should not pull")
    89  	}
    90  }