github.com/ddnomad/packer@v1.3.2/provisioner/chef-client/provisioner_test.go (about)

     1  package chefclient
     2  
     3  import (
     4  	"bytes"
     5  	"io/ioutil"
     6  	"os"
     7  	"strings"
     8  	"testing"
     9  
    10  	"github.com/hashicorp/packer/packer"
    11  )
    12  
    13  func testConfig() map[string]interface{} {
    14  	return map[string]interface{}{
    15  		"server_url": "foo",
    16  	}
    17  }
    18  
    19  func TestProvisioner_Impl(t *testing.T) {
    20  	var raw interface{}
    21  	raw = &Provisioner{}
    22  	if _, ok := raw.(packer.Provisioner); !ok {
    23  		t.Fatalf("must be a Provisioner")
    24  	}
    25  }
    26  
    27  func TestProvisionerPrepare_chefEnvironment(t *testing.T) {
    28  	var p Provisioner
    29  
    30  	config := testConfig()
    31  	config["chef_environment"] = "some-env"
    32  
    33  	err := p.Prepare(config)
    34  	if err != nil {
    35  		t.Fatalf("err: %s", err)
    36  	}
    37  
    38  	if p.config.ChefEnvironment != "some-env" {
    39  		t.Fatalf("unexpected: %#v", p.config.ChefEnvironment)
    40  	}
    41  }
    42  
    43  func TestProvisionerPrepare_configTemplate(t *testing.T) {
    44  	var err error
    45  	var p Provisioner
    46  
    47  	// Test no config template
    48  	config := testConfig()
    49  	delete(config, "config_template")
    50  	err = p.Prepare(config)
    51  	if err != nil {
    52  		t.Fatalf("err: %s", err)
    53  	}
    54  
    55  	// Test with a file
    56  	tf, err := ioutil.TempFile("", "packer")
    57  	if err != nil {
    58  		t.Fatalf("err: %s", err)
    59  	}
    60  	defer os.Remove(tf.Name())
    61  
    62  	config = testConfig()
    63  	config["config_template"] = tf.Name()
    64  	p = Provisioner{}
    65  	err = p.Prepare(config)
    66  	if err != nil {
    67  		t.Fatalf("err: %s", err)
    68  	}
    69  
    70  	// Test with a directory
    71  	td, err := ioutil.TempDir("", "packer")
    72  	if err != nil {
    73  		t.Fatalf("err: %s", err)
    74  	}
    75  	defer os.RemoveAll(td)
    76  
    77  	config = testConfig()
    78  	config["config_template"] = td
    79  	p = Provisioner{}
    80  	err = p.Prepare(config)
    81  	if err == nil {
    82  		t.Fatal("should have err")
    83  	}
    84  }
    85  
    86  func TestProvisionerPrepare_commands(t *testing.T) {
    87  	commands := []string{
    88  		"execute_command",
    89  		"install_command",
    90  		"knife_command",
    91  	}
    92  
    93  	for _, command := range commands {
    94  		var p Provisioner
    95  
    96  		// Test not set
    97  		config := testConfig()
    98  		delete(config, command)
    99  		err := p.Prepare(config)
   100  		if err != nil {
   101  			t.Fatalf("err: %s", err)
   102  		}
   103  
   104  		// Test invalid template
   105  		config = testConfig()
   106  		config[command] = "{{if NOPE}}"
   107  		err = p.Prepare(config)
   108  		if err == nil {
   109  			t.Fatal("should error")
   110  		}
   111  
   112  		// Test good template
   113  		config = testConfig()
   114  		config[command] = "{{.Foo}}"
   115  		err = p.Prepare(config)
   116  		if err != nil {
   117  			t.Fatalf("err: %s", err)
   118  		}
   119  	}
   120  }
   121  
   122  func TestProvisionerPrepare_serverUrl(t *testing.T) {
   123  	var p Provisioner
   124  
   125  	// Test not set
   126  	config := testConfig()
   127  	delete(config, "server_url")
   128  	err := p.Prepare(config)
   129  	if err == nil {
   130  		t.Fatal("should error")
   131  	}
   132  
   133  	// Test set
   134  	config = testConfig()
   135  	config["server_url"] = "foo"
   136  	err = p.Prepare(config)
   137  	if err != nil {
   138  		t.Fatalf("err: %s", err)
   139  	}
   140  }
   141  
   142  func TestProvisionerPrepare_encryptedDataBagSecretPath(t *testing.T) {
   143  	var err error
   144  	var p Provisioner
   145  
   146  	// Test no config template
   147  	config := testConfig()
   148  	delete(config, "encrypted_data_bag_secret_path")
   149  	err = p.Prepare(config)
   150  	if err != nil {
   151  		t.Fatalf("err: %s", err)
   152  	}
   153  
   154  	// Test with a file
   155  	tf, err := ioutil.TempFile("", "packer")
   156  	if err != nil {
   157  		t.Fatalf("err: %s", err)
   158  	}
   159  	defer os.Remove(tf.Name())
   160  
   161  	config = testConfig()
   162  	config["encrypted_data_bag_secret_path"] = tf.Name()
   163  	p = Provisioner{}
   164  	err = p.Prepare(config)
   165  	if err != nil {
   166  		t.Fatalf("err: %s", err)
   167  	}
   168  
   169  	// Test with a directory
   170  	td, err := ioutil.TempDir("", "packer")
   171  	if err != nil {
   172  		t.Fatalf("err: %s", err)
   173  	}
   174  	defer os.RemoveAll(td)
   175  
   176  	config = testConfig()
   177  	config["encrypted_data_bag_secret_path"] = td
   178  	p = Provisioner{}
   179  	err = p.Prepare(config)
   180  	if err == nil {
   181  		t.Fatal("should have err")
   182  	}
   183  }
   184  
   185  func TestProvisioner_createDir(t *testing.T) {
   186  	for _, sudo := range []bool{true, false} {
   187  		config := testConfig()
   188  		config["prevent_sudo"] = !sudo
   189  
   190  		p := &Provisioner{}
   191  		comm := &packer.MockCommunicator{}
   192  		ui := &packer.BasicUi{
   193  			Reader: new(bytes.Buffer),
   194  			Writer: new(bytes.Buffer),
   195  		}
   196  
   197  		err := p.Prepare(config)
   198  		if err != nil {
   199  			t.Fatalf("err: %s", err)
   200  		}
   201  
   202  		if err := p.createDir(ui, comm, "/tmp/foo"); err != nil {
   203  			t.Fatalf("err: %s", err)
   204  		}
   205  
   206  		if !sudo && strings.HasPrefix(comm.StartCmd.Command, "sudo") {
   207  			t.Fatalf("createDir should not use sudo, got: \"%s\"", comm.StartCmd.Command)
   208  		}
   209  
   210  		if sudo && !strings.HasPrefix(comm.StartCmd.Command, "sudo") {
   211  			t.Fatalf("createDir should use sudo, got: \"%s\"", comm.StartCmd.Command)
   212  		}
   213  	}
   214  }
   215  
   216  func TestProvisioner_removeDir(t *testing.T) {
   217  	for _, sudo := range []bool{true, false} {
   218  		config := testConfig()
   219  		config["prevent_sudo"] = !sudo
   220  
   221  		p := &Provisioner{}
   222  		comm := &packer.MockCommunicator{}
   223  		ui := &packer.BasicUi{
   224  			Reader: new(bytes.Buffer),
   225  			Writer: new(bytes.Buffer),
   226  		}
   227  
   228  		err := p.Prepare(config)
   229  		if err != nil {
   230  			t.Fatalf("err: %s", err)
   231  		}
   232  
   233  		if err := p.removeDir(ui, comm, "/tmp/foo"); err != nil {
   234  			t.Fatalf("err: %s", err)
   235  		}
   236  
   237  		if !sudo && strings.HasPrefix(comm.StartCmd.Command, "sudo") {
   238  			t.Fatalf("removeDir should not use sudo, got: \"%s\"", comm.StartCmd.Command)
   239  		}
   240  
   241  		if sudo && !strings.HasPrefix(comm.StartCmd.Command, "sudo") {
   242  			t.Fatalf("removeDir should use sudo, got: \"%s\"", comm.StartCmd.Command)
   243  		}
   244  	}
   245  }
   246  
   247  func TestProvisionerPrepare_policy(t *testing.T) {
   248  	var p Provisioner
   249  
   250  	var policyTests = []struct {
   251  		name    string
   252  		group   string
   253  		success bool
   254  	}{
   255  		{"", "", true},
   256  		{"a", "b", true},
   257  		{"a", "", false},
   258  		{"", "a", false},
   259  	}
   260  	for _, tt := range policyTests {
   261  		config := testConfig()
   262  		config["policy_name"] = tt.name
   263  		config["policy_group"] = tt.group
   264  		err := p.Prepare(config)
   265  		if (err == nil) != tt.success {
   266  			t.Fatalf("wasn't expecting %+v to fail: %s", tt, err.Error())
   267  		}
   268  	}
   269  }