github.com/mmcquillan/packer@v1.1.1-0.20171009221028-c85cf0483a5d/post-processor/shell-local/post-processor_test.go (about)

     1  package shell_local
     2  
     3  import (
     4  	"github.com/hashicorp/packer/packer"
     5  	"io/ioutil"
     6  	"os"
     7  	"testing"
     8  )
     9  
    10  func TestPostProcessor_ImplementsPostProcessor(t *testing.T) {
    11  	var _ packer.PostProcessor = new(PostProcessor)
    12  }
    13  
    14  func testConfig() map[string]interface{} {
    15  	return map[string]interface{}{
    16  		"inline": []interface{}{"foo", "bar"},
    17  	}
    18  }
    19  
    20  func TestPostProcessor_Impl(t *testing.T) {
    21  	var raw interface{}
    22  	raw = &PostProcessor{}
    23  	if _, ok := raw.(packer.PostProcessor); !ok {
    24  		t.Fatalf("must be a post processor")
    25  	}
    26  }
    27  
    28  func TestPostProcessorPrepare_Defaults(t *testing.T) {
    29  	var p PostProcessor
    30  	config := testConfig()
    31  
    32  	err := p.Configure(config)
    33  	if err != nil {
    34  		t.Fatalf("err: %s", err)
    35  	}
    36  }
    37  
    38  func TestPostProcessorPrepare_InlineShebang(t *testing.T) {
    39  	config := testConfig()
    40  
    41  	delete(config, "inline_shebang")
    42  	p := new(PostProcessor)
    43  	err := p.Configure(config)
    44  	if err != nil {
    45  		t.Fatalf("should not have error: %s", err)
    46  	}
    47  
    48  	if p.config.InlineShebang != "/bin/sh -e" {
    49  		t.Fatalf("bad value: %s", p.config.InlineShebang)
    50  	}
    51  
    52  	// Test with a good one
    53  	config["inline_shebang"] = "foo"
    54  	p = new(PostProcessor)
    55  	err = p.Configure(config)
    56  	if err != nil {
    57  		t.Fatalf("should not have error: %s", err)
    58  	}
    59  
    60  	if p.config.InlineShebang != "foo" {
    61  		t.Fatalf("bad value: %s", p.config.InlineShebang)
    62  	}
    63  }
    64  
    65  func TestPostProcessorPrepare_InvalidKey(t *testing.T) {
    66  	var p PostProcessor
    67  	config := testConfig()
    68  
    69  	// Add a random key
    70  	config["i_should_not_be_valid"] = true
    71  	err := p.Configure(config)
    72  	if err == nil {
    73  		t.Fatal("should have error")
    74  	}
    75  }
    76  
    77  func TestPostProcessorPrepare_Script(t *testing.T) {
    78  	config := testConfig()
    79  	delete(config, "inline")
    80  
    81  	config["script"] = "/this/should/not/exist"
    82  	p := new(PostProcessor)
    83  	err := p.Configure(config)
    84  	if err == nil {
    85  		t.Fatal("should have error")
    86  	}
    87  
    88  	// Test with a good one
    89  	tf, err := ioutil.TempFile("", "packer")
    90  	if err != nil {
    91  		t.Fatalf("error tempfile: %s", err)
    92  	}
    93  	defer os.Remove(tf.Name())
    94  
    95  	config["script"] = tf.Name()
    96  	p = new(PostProcessor)
    97  	err = p.Configure(config)
    98  	if err != nil {
    99  		t.Fatalf("should not have error: %s", err)
   100  	}
   101  }
   102  
   103  func TestPostProcessorPrepare_ScriptAndInline(t *testing.T) {
   104  	var p PostProcessor
   105  	config := testConfig()
   106  
   107  	delete(config, "inline")
   108  	delete(config, "script")
   109  	err := p.Configure(config)
   110  	if err == nil {
   111  		t.Fatal("should have error")
   112  	}
   113  
   114  	// Test with both
   115  	tf, err := ioutil.TempFile("", "packer")
   116  	if err != nil {
   117  		t.Fatalf("error tempfile: %s", err)
   118  	}
   119  	defer os.Remove(tf.Name())
   120  
   121  	config["inline"] = []interface{}{"foo"}
   122  	config["script"] = tf.Name()
   123  	err = p.Configure(config)
   124  	if err == nil {
   125  		t.Fatal("should have error")
   126  	}
   127  }
   128  
   129  func TestPostProcessorPrepare_ScriptAndScripts(t *testing.T) {
   130  	var p PostProcessor
   131  	config := testConfig()
   132  
   133  	// Test with both
   134  	tf, err := ioutil.TempFile("", "packer")
   135  	if err != nil {
   136  		t.Fatalf("error tempfile: %s", err)
   137  	}
   138  	defer os.Remove(tf.Name())
   139  
   140  	config["inline"] = []interface{}{"foo"}
   141  	config["scripts"] = []string{tf.Name()}
   142  	err = p.Configure(config)
   143  	if err == nil {
   144  		t.Fatal("should have error")
   145  	}
   146  }
   147  
   148  func TestPostProcessorPrepare_Scripts(t *testing.T) {
   149  	config := testConfig()
   150  	delete(config, "inline")
   151  
   152  	config["scripts"] = []string{}
   153  	p := new(PostProcessor)
   154  	err := p.Configure(config)
   155  	if err == nil {
   156  		t.Fatal("should have error")
   157  	}
   158  
   159  	// Test with a good one
   160  	tf, err := ioutil.TempFile("", "packer")
   161  	if err != nil {
   162  		t.Fatalf("error tempfile: %s", err)
   163  	}
   164  	defer os.Remove(tf.Name())
   165  
   166  	config["scripts"] = []string{tf.Name()}
   167  	p = new(PostProcessor)
   168  	err = p.Configure(config)
   169  	if err != nil {
   170  		t.Fatalf("should not have error: %s", err)
   171  	}
   172  }
   173  
   174  func TestPostProcessorPrepare_EnvironmentVars(t *testing.T) {
   175  	config := testConfig()
   176  
   177  	// Test with a bad case
   178  	config["environment_vars"] = []string{"badvar", "good=var"}
   179  	p := new(PostProcessor)
   180  	err := p.Configure(config)
   181  	if err == nil {
   182  		t.Fatal("should have error")
   183  	}
   184  
   185  	// Test with a trickier case
   186  	config["environment_vars"] = []string{"=bad"}
   187  	p = new(PostProcessor)
   188  	err = p.Configure(config)
   189  	if err == nil {
   190  		t.Fatal("should have error")
   191  	}
   192  
   193  	// Test with a good case
   194  	// Note: baz= is a real env variable, just empty
   195  	config["environment_vars"] = []string{"FOO=bar", "baz="}
   196  	p = new(PostProcessor)
   197  	err = p.Configure(config)
   198  	if err != nil {
   199  		t.Fatalf("should not have error: %s", err)
   200  	}
   201  
   202  	// Test when the env variable value contains an equals sign
   203  	config["environment_vars"] = []string{"good=withequals=true"}
   204  	p = new(PostProcessor)
   205  	err = p.Configure(config)
   206  	if err != nil {
   207  		t.Fatalf("should not have error: %s", err)
   208  	}
   209  
   210  	// Test when the env variable value starts with an equals sign
   211  	config["environment_vars"] = []string{"good==true"}
   212  	p = new(PostProcessor)
   213  	err = p.Configure(config)
   214  	if err != nil {
   215  		t.Fatalf("should not have error: %s", err)
   216  	}
   217  }
   218  
   219  func TestPostProcessor_createFlattenedEnvVars(t *testing.T) {
   220  	var flattenedEnvVars string
   221  	config := testConfig()
   222  
   223  	userEnvVarTests := [][]string{
   224  		{},                     // No user env var
   225  		{"FOO=bar"},            // Single user env var
   226  		{"FOO=bar's"},          // User env var with single quote in value
   227  		{"FOO=bar", "BAZ=qux"}, // Multiple user env vars
   228  		{"FOO=bar=baz"},        // User env var with value containing equals
   229  		{"FOO==bar"},           // User env var with value starting with equals
   230  	}
   231  	expected := []string{
   232  		`PACKER_BUILDER_TYPE='iso' PACKER_BUILD_NAME='vmware' `,
   233  		`FOO='bar' PACKER_BUILDER_TYPE='iso' PACKER_BUILD_NAME='vmware' `,
   234  		`FOO='bar'"'"'s' PACKER_BUILDER_TYPE='iso' PACKER_BUILD_NAME='vmware' `,
   235  		`BAZ='qux' FOO='bar' PACKER_BUILDER_TYPE='iso' PACKER_BUILD_NAME='vmware' `,
   236  		`FOO='bar=baz' PACKER_BUILDER_TYPE='iso' PACKER_BUILD_NAME='vmware' `,
   237  		`FOO='=bar' PACKER_BUILDER_TYPE='iso' PACKER_BUILD_NAME='vmware' `,
   238  	}
   239  
   240  	p := new(PostProcessor)
   241  	p.Configure(config)
   242  
   243  	// Defaults provided by Packer
   244  	p.config.PackerBuildName = "vmware"
   245  	p.config.PackerBuilderType = "iso"
   246  
   247  	for i, expectedValue := range expected {
   248  		p.config.Vars = userEnvVarTests[i]
   249  		flattenedEnvVars = p.createFlattenedEnvVars()
   250  		if flattenedEnvVars != expectedValue {
   251  			t.Fatalf("expected flattened env vars to be: %s, got %s.", expectedValue, flattenedEnvVars)
   252  		}
   253  	}
   254  }