package shell import ( "github.com/mitchellh/packer/packer" "io/ioutil" "os" "testing" ) func testConfig() map[string]interface{} { return map[string]interface{}{ "inline": []interface{}{"foo", "bar"}, } } func TestProvisioner_Impl(t *testing.T) { var raw interface{} raw = &Provisioner{} if _, ok := raw.(packer.Provisioner); !ok { t.Fatalf("must be a Provisioner") } } func TestProvisionerPrepare_Defaults(t *testing.T) { var p Provisioner config := testConfig() err := p.Prepare(config) if err != nil { t.Fatalf("err: %s", err) } if p.config.RemotePath != DefaultRemotePath { t.Errorf("unexpected remote path: %s", p.config.RemotePath) } } func TestProvisionerPrepare_InlineShebang(t *testing.T) { config := testConfig() delete(config, "inline_shebang") p := new(Provisioner) err := p.Prepare(config) if err != nil { t.Fatalf("should not have error: %s", err) } if p.config.InlineShebang != "/bin/sh" { t.Fatalf("bad value: %s", p.config.InlineShebang) } // Test with a good one config["inline_shebang"] = "foo" p = new(Provisioner) err = p.Prepare(config) if err != nil { t.Fatalf("should not have error: %s", err) } if p.config.InlineShebang != "foo" { t.Fatalf("bad value: %s", p.config.InlineShebang) } } func TestProvisionerPrepare_InvalidKey(t *testing.T) { var p Provisioner config := testConfig() // Add a random key config["i_should_not_be_valid"] = true err := p.Prepare(config) if err == nil { t.Fatal("should have error") } } func TestProvisionerPrepare_Script(t *testing.T) { config := testConfig() delete(config, "inline") config["script"] = "/this/should/not/exist" p := new(Provisioner) err := p.Prepare(config) if err == nil { t.Fatal("should have error") } // Test with a good one tf, err := ioutil.TempFile("", "packer") if err != nil { t.Fatalf("error tempfile: %s", err) } defer os.Remove(tf.Name()) config["script"] = tf.Name() p = new(Provisioner) err = p.Prepare(config) if err != nil { t.Fatalf("should not have error: %s", err) } } func TestProvisionerPrepare_ScriptAndInline(t *testing.T) { var p Provisioner config := testConfig() delete(config, "inline") delete(config, "script") err := p.Prepare(config) if err == nil { t.Fatal("should have error") } // Test with both tf, err := ioutil.TempFile("", "packer") if err != nil { t.Fatalf("error tempfile: %s", err) } defer os.Remove(tf.Name()) config["inline"] = []interface{}{"foo"} config["script"] = tf.Name() err = p.Prepare(config) if err == nil { t.Fatal("should have error") } } func TestProvisionerPrepare_ScriptAndScripts(t *testing.T) { var p Provisioner config := testConfig() // Test with both tf, err := ioutil.TempFile("", "packer") if err != nil { t.Fatalf("error tempfile: %s", err) } defer os.Remove(tf.Name()) config["inline"] = []interface{}{"foo"} config["scripts"] = []string{tf.Name()} err = p.Prepare(config) if err == nil { t.Fatal("should have error") } } func TestProvisionerPrepare_Scripts(t *testing.T) { config := testConfig() delete(config, "inline") config["scripts"] = []string{} p := new(Provisioner) err := p.Prepare(config) if err == nil { t.Fatal("should have error") } // Test with a good one tf, err := ioutil.TempFile("", "packer") if err != nil { t.Fatalf("error tempfile: %s", err) } defer os.Remove(tf.Name()) config["scripts"] = []string{tf.Name()} p = new(Provisioner) err = p.Prepare(config) if err != nil { t.Fatalf("should not have error: %s", err) } } func TestProvisionerPrepare_EnvironmentVars(t *testing.T) { config := testConfig() // Test with a bad case config["environment_vars"] = []string{"badvar", "good=var"} p := new(Provisioner) err := p.Prepare(config) if err == nil { t.Fatal("should have error") } // Test with a trickier case config["environment_vars"] = []string{"=bad"} p = new(Provisioner) err = p.Prepare(config) if err == nil { t.Fatal("should have error") } // Test with a good case // Note: baz= is a real env variable, just empty config["environment_vars"] = []string{"FOO=bar", "baz="} p = new(Provisioner) err = p.Prepare(config) if err != nil { t.Fatalf("should not have error: %s", err) } }