Files
Packer-Cn/builder/docker/config_test.go
T

148 lines
3.0 KiB
Go
Raw Normal View History

2013-11-09 11:47:32 -08:00
package docker
import (
"io/ioutil"
"os"
2013-11-09 11:47:32 -08:00
"testing"
)
2013-11-09 17:07:14 -08:00
func testConfig() map[string]interface{} {
return map[string]interface{}{
"export_path": "foo",
"image": "bar",
2013-11-09 11:47:32 -08:00
}
}
2013-11-09 17:07:14 -08:00
func testConfigStruct(t *testing.T) *Config {
2019-12-17 11:25:56 +01:00
var c Config
warns, errs := c.Prepare(testConfig())
2013-11-09 17:07:14 -08:00
if len(warns) > 0 {
t.Fatalf("bad: %#v", len(warns))
}
if errs != nil {
t.Fatalf("bad: %#v", errs)
}
2019-12-17 11:25:56 +01:00
return &c
2013-11-09 17:07:14 -08:00
}
2013-11-09 17:21:24 -08:00
func testConfigErr(t *testing.T, warns []string, err error) {
if len(warns) > 0 {
t.Fatalf("bad: %#v", warns)
}
if err == nil {
t.Fatal("should error")
}
}
func testConfigOk(t *testing.T, warns []string, err error) {
if len(warns) > 0 {
t.Fatalf("bad: %#v", warns)
}
if err != nil {
t.Fatalf("bad: %s", err)
}
}
2013-11-09 11:47:32 -08:00
func TestConfigPrepare_exportPath(t *testing.T) {
td, err := ioutil.TempDir("", "packer")
if err != nil {
t.Fatalf("err: %s", err)
}
defer os.RemoveAll(td)
2013-11-09 17:07:14 -08:00
raw := testConfig()
2013-11-09 11:47:32 -08:00
// No export path. This is invalid. Previously this would not error during
// validation and as a result the failure would happen at build time.
2013-11-09 17:07:14 -08:00
delete(raw, "export_path")
2019-12-17 11:25:56 +01:00
var c Config
warns, errs := c.Prepare(raw)
testConfigErr(t, warns, errs)
2013-11-09 11:47:32 -08:00
// Good export path
2013-11-09 17:07:14 -08:00
raw["export_path"] = "good"
2019-12-17 11:25:56 +01:00
warns, errs = c.Prepare(raw)
testConfigOk(t, warns, errs)
// Bad export path (directory)
raw["export_path"] = td
2019-12-17 11:25:56 +01:00
warns, errs = c.Prepare(raw)
testConfigErr(t, warns, errs)
}
func TestConfigPrepare_exportPathAndCommit(t *testing.T) {
raw := testConfig()
// Export but no commit (explicit default)
raw["commit"] = false
2019-12-17 11:25:56 +01:00
warns, errs := (&Config{}).Prepare(raw)
testConfigOk(t, warns, errs)
// Commit AND export specified (invalid)
raw["commit"] = true
2019-12-17 11:25:56 +01:00
warns, errs = (&Config{}).Prepare(raw)
testConfigErr(t, warns, errs)
// Commit but no export
delete(raw, "export_path")
2019-12-17 11:25:56 +01:00
warns, errs = (&Config{}).Prepare(raw)
testConfigOk(t, warns, errs)
}
func TestConfigPrepare_exportDiscard(t *testing.T) {
raw := testConfig()
// Export but no discard (explicit default)
raw["discard"] = false
2019-12-17 11:25:56 +01:00
warns, errs := (&Config{}).Prepare(raw)
testConfigOk(t, warns, errs)
// Discard AND export (invalid)
raw["discard"] = true
2019-12-17 11:25:56 +01:00
warns, errs = (&Config{}).Prepare(raw)
testConfigErr(t, warns, errs)
// Discard but no export
raw["discard"] = true
delete(raw, "export_path")
2019-12-17 11:25:56 +01:00
warns, errs = (&Config{}).Prepare(raw)
2013-11-09 17:21:24 -08:00
testConfigOk(t, warns, errs)
2013-11-09 11:47:32 -08:00
}
func TestConfigPrepare_image(t *testing.T) {
2013-11-09 17:07:14 -08:00
raw := testConfig()
2013-11-09 11:47:32 -08:00
// No image
2013-11-09 17:07:14 -08:00
delete(raw, "image")
2019-12-17 11:25:56 +01:00
var c Config
warns, errs := c.Prepare(raw)
2013-11-09 17:21:24 -08:00
testConfigErr(t, warns, errs)
2013-11-09 11:47:32 -08:00
// Good image
2013-11-09 17:07:14 -08:00
raw["image"] = "path"
2019-12-17 11:25:56 +01:00
warns, errs = c.Prepare(raw)
2013-11-09 17:21:24 -08:00
testConfigOk(t, warns, errs)
}
func TestConfigPrepare_pull(t *testing.T) {
raw := testConfig()
// No pull set
delete(raw, "pull")
2019-12-17 11:25:56 +01:00
var c Config
warns, errs := c.Prepare(raw)
2013-11-09 17:21:24 -08:00
testConfigOk(t, warns, errs)
if !c.Pull {
t.Fatal("should pull by default")
2013-11-09 11:47:32 -08:00
}
2013-11-09 17:21:24 -08:00
// Pull set
raw["pull"] = false
2019-12-17 11:25:56 +01:00
warns, errs = c.Prepare(raw)
2013-11-09 17:21:24 -08:00
testConfigOk(t, warns, errs)
if c.Pull {
t.Fatal("should not pull")
2013-11-09 11:47:32 -08:00
}
}