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

     1  package saltmasterless
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"strings"
     7  	"testing"
     8  
     9  	"github.com/hashicorp/packer/packer"
    10  )
    11  
    12  func testConfig() map[string]interface{} {
    13  	return map[string]interface{}{
    14  		"local_state_tree": os.TempDir(),
    15  	}
    16  }
    17  
    18  func TestProvisioner_Impl(t *testing.T) {
    19  	var raw interface{}
    20  	raw = &Provisioner{}
    21  	if _, ok := raw.(packer.Provisioner); !ok {
    22  		t.Fatalf("must be a Provisioner")
    23  	}
    24  }
    25  
    26  func TestProvisionerPrepare_Defaults(t *testing.T) {
    27  	var p Provisioner
    28  	config := testConfig()
    29  
    30  	err := p.Prepare(config)
    31  	if err != nil {
    32  		t.Fatalf("err: %s", err)
    33  	}
    34  
    35  	if p.config.TempConfigDir != p.guestOSTypeConfig.tempDir {
    36  		t.Errorf("unexpected temp config dir: %s", p.config.TempConfigDir)
    37  	}
    38  }
    39  
    40  func TestProvisionerPrepare_InvalidKey(t *testing.T) {
    41  	var p Provisioner
    42  	config := testConfig()
    43  
    44  	// Add a random key
    45  	config["i_should_not_be_valid"] = true
    46  	err := p.Prepare(config)
    47  	if err == nil {
    48  		t.Fatal("should have error")
    49  	}
    50  }
    51  
    52  func TestProvisionerPrepare_CustomState(t *testing.T) {
    53  	var p Provisioner
    54  	config := testConfig()
    55  
    56  	err := p.Prepare(config)
    57  	if err != nil {
    58  		t.Fatalf("err: %s", err)
    59  	}
    60  
    61  	if !strings.Contains(p.config.CmdArgs, "state.") {
    62  		t.Fatal("a state should be specified in CmdArgs")
    63  	}
    64  
    65  	config["custom_state"] = "birb"
    66  	err = p.Prepare(config)
    67  	if err != nil {
    68  		t.Fatalf("err: %s", err)
    69  	}
    70  
    71  	if !strings.Contains(p.config.CmdArgs, "state.sls birb") {
    72  		t.Fatal("birb state should be specified in CmdArgs")
    73  	}
    74  }
    75  
    76  func TestProvisionerPrepare_MinionConfig(t *testing.T) {
    77  	var p Provisioner
    78  	config := testConfig()
    79  
    80  	config["minion_config"] = "/i/dont/exist/i/think"
    81  	err := p.Prepare(config)
    82  	if err == nil {
    83  		t.Fatal("should have error")
    84  	}
    85  
    86  	tf, err := ioutil.TempFile("", "minion")
    87  	if err != nil {
    88  		t.Fatalf("error tempfile: %s", err)
    89  	}
    90  	defer os.Remove(tf.Name())
    91  
    92  	config["minion_config"] = tf.Name()
    93  	err = p.Prepare(config)
    94  	if err != nil {
    95  		t.Fatalf("err: %s", err)
    96  	}
    97  }
    98  
    99  func TestProvisionerPrepare_MinionConfig_RemoteStateTree(t *testing.T) {
   100  	var p Provisioner
   101  	config := testConfig()
   102  
   103  	config["minion_config"] = "/i/dont/exist/i/think"
   104  	config["remote_state_tree"] = "/i/dont/exist/remote_state_tree"
   105  	err := p.Prepare(config)
   106  	if err == nil {
   107  		t.Fatal("minion_config and remote_state_tree should cause error")
   108  	}
   109  }
   110  
   111  func TestProvisionerPrepare_MinionConfig_RemotePillarRoots(t *testing.T) {
   112  	var p Provisioner
   113  	config := testConfig()
   114  
   115  	config["minion_config"] = "/i/dont/exist/i/think"
   116  	config["remote_pillar_roots"] = "/i/dont/exist/remote_pillar_roots"
   117  	err := p.Prepare(config)
   118  	if err == nil {
   119  		t.Fatal("minion_config and remote_pillar_roots should cause error")
   120  	}
   121  }
   122  
   123  func TestProvisionerPrepare_GrainsFile(t *testing.T) {
   124  	var p Provisioner
   125  	config := testConfig()
   126  
   127  	config["grains_file"] = "/i/dont/exist/i/think"
   128  	err := p.Prepare(config)
   129  	if err == nil {
   130  		t.Fatal("should have error")
   131  	}
   132  
   133  	tf, err := ioutil.TempFile("", "grains")
   134  	if err != nil {
   135  		t.Fatalf("error tempfile: %s", err)
   136  	}
   137  	defer os.Remove(tf.Name())
   138  
   139  	config["grains_file"] = tf.Name()
   140  	err = p.Prepare(config)
   141  	if err != nil {
   142  		t.Fatalf("err: %s", err)
   143  	}
   144  }
   145  
   146  func TestProvisionerPrepare_LocalStateTree(t *testing.T) {
   147  	var p Provisioner
   148  	config := testConfig()
   149  
   150  	config["local_state_tree"] = "/i/dont/exist/i/think"
   151  	err := p.Prepare(config)
   152  	if err == nil {
   153  		t.Fatal("should have error")
   154  	}
   155  
   156  	config["local_state_tree"] = os.TempDir()
   157  	err = p.Prepare(config)
   158  	if err != nil {
   159  		t.Fatalf("err: %s", err)
   160  	}
   161  }
   162  
   163  func TestProvisionerPrepare_LocalPillarRoots(t *testing.T) {
   164  	var p Provisioner
   165  	config := testConfig()
   166  
   167  	config["local_pillar_roots"] = "/i/dont/exist/i/think"
   168  	err := p.Prepare(config)
   169  	if err == nil {
   170  		t.Fatal("should have error")
   171  	}
   172  
   173  	config["local_pillar_roots"] = os.TempDir()
   174  	err = p.Prepare(config)
   175  	if err != nil {
   176  		t.Fatalf("err: %s", err)
   177  	}
   178  }
   179  
   180  func TestProvisionerSudo(t *testing.T) {
   181  	var p Provisioner
   182  	config := testConfig()
   183  
   184  	err := p.Prepare(config)
   185  	if err != nil {
   186  		t.Fatalf("err: %s", err)
   187  	}
   188  
   189  	withSudo := p.sudo("echo hello")
   190  	if withSudo != "sudo echo hello" {
   191  		t.Fatalf("sudo command not generated correctly")
   192  	}
   193  
   194  	config["disable_sudo"] = true
   195  	err = p.Prepare(config)
   196  	if err != nil {
   197  		t.Fatalf("err: %s", err)
   198  	}
   199  	withoutSudo := p.sudo("echo hello")
   200  	if withoutSudo != "echo hello" {
   201  		t.Fatalf("sudo-less command not generated correctly")
   202  	}
   203  }
   204  
   205  func TestProvisionerPrepare_RemoteStateTree(t *testing.T) {
   206  	var p Provisioner
   207  	config := testConfig()
   208  
   209  	config["remote_state_tree"] = "/remote_state_tree"
   210  	err := p.Prepare(config)
   211  	if err != nil {
   212  		t.Fatalf("err: %s", err)
   213  	}
   214  
   215  	if !strings.Contains(p.config.CmdArgs, "--file-root=/remote_state_tree") {
   216  		t.Fatal("--file-root should be set in CmdArgs")
   217  	}
   218  }
   219  
   220  func TestProvisionerPrepare_RemotePillarRoots(t *testing.T) {
   221  	var p Provisioner
   222  	config := testConfig()
   223  
   224  	config["remote_pillar_roots"] = "/remote_pillar_roots"
   225  	err := p.Prepare(config)
   226  	if err != nil {
   227  		t.Fatalf("err: %s", err)
   228  	}
   229  
   230  	if !strings.Contains(p.config.CmdArgs, "--pillar-root=/remote_pillar_roots") {
   231  		t.Fatal("--pillar-root should be set in CmdArgs")
   232  	}
   233  }
   234  
   235  func TestProvisionerPrepare_RemoteStateTree_Default(t *testing.T) {
   236  	var p Provisioner
   237  	config := testConfig()
   238  
   239  	// no minion_config, no remote_state_tree
   240  	err := p.Prepare(config)
   241  	if err != nil {
   242  		t.Fatalf("err: %s", err)
   243  	}
   244  
   245  	if !strings.Contains(p.config.CmdArgs, "--file-root=/srv/salt") {
   246  		t.Fatal("--file-root should be set in CmdArgs")
   247  	}
   248  }
   249  
   250  func TestProvisionerPrepare_RemotePillarRoots_Default(t *testing.T) {
   251  	var p Provisioner
   252  	config := testConfig()
   253  
   254  	// no minion_config, no remote_pillar_roots
   255  	err := p.Prepare(config)
   256  	if err != nil {
   257  		t.Fatalf("err: %s", err)
   258  	}
   259  
   260  	if !strings.Contains(p.config.CmdArgs, "--pillar-root=/srv/pillar") {
   261  		t.Fatal("--pillar-root should be set in CmdArgs")
   262  	}
   263  }
   264  
   265  func TestProvisionerPrepare_NoExitOnFailure(t *testing.T) {
   266  	var p Provisioner
   267  	config := testConfig()
   268  
   269  	err := p.Prepare(config)
   270  	if err != nil {
   271  		t.Fatalf("err: %s", err)
   272  	}
   273  
   274  	if !strings.Contains(p.config.CmdArgs, "--retcode-passthrough") {
   275  		t.Fatal("--retcode-passthrough should be set in CmdArgs")
   276  	}
   277  
   278  	config["no_exit_on_failure"] = true
   279  	err = p.Prepare(config)
   280  	if err != nil {
   281  		t.Fatalf("err: %s", err)
   282  	}
   283  
   284  	if strings.Contains(p.config.CmdArgs, "--retcode-passthrough") {
   285  		t.Fatal("--retcode-passthrough should not be set in CmdArgs")
   286  	}
   287  }
   288  
   289  func TestProvisionerPrepare_LogLevel(t *testing.T) {
   290  	var p Provisioner
   291  	config := testConfig()
   292  
   293  	err := p.Prepare(config)
   294  	if err != nil {
   295  		t.Fatalf("err: %s", err)
   296  	}
   297  
   298  	if !strings.Contains(p.config.CmdArgs, "-l info") {
   299  		t.Fatal("-l info should be set in CmdArgs")
   300  	}
   301  
   302  	config["log_level"] = "debug"
   303  	err = p.Prepare(config)
   304  	if err != nil {
   305  		t.Fatalf("err: %s", err)
   306  	}
   307  
   308  	if !strings.Contains(p.config.CmdArgs, "-l debug") {
   309  		t.Fatal("-l debug should be set in CmdArgs")
   310  	}
   311  }
   312  
   313  func TestProvisionerPrepare_GuestOSType(t *testing.T) {
   314  	var p Provisioner
   315  	config := testConfig()
   316  
   317  	config["guest_os_type"] = "Windows"
   318  
   319  	err := p.Prepare(config)
   320  	if err != nil {
   321  		t.Fatalf("err: %s", err)
   322  	}
   323  
   324  	if p.config.GuestOSType != "windows" {
   325  		t.Fatalf("GuestOSType should be 'windows'")
   326  	}
   327  }