github.com/dacamp/packer@v0.10.2/provisioner/ansible-local/provisioner_test.go (about)

     1  package ansiblelocal
     2  
     3  import (
     4  	"github.com/mitchellh/packer/packer"
     5  	"io/ioutil"
     6  	"os"
     7  	"testing"
     8  )
     9  
    10  func testConfig() map[string]interface{} {
    11  	m := make(map[string]interface{})
    12  	return m
    13  }
    14  
    15  func TestProvisioner_Impl(t *testing.T) {
    16  	var raw interface{}
    17  	raw = &Provisioner{}
    18  	if _, ok := raw.(packer.Provisioner); !ok {
    19  		t.Fatalf("must be a Provisioner")
    20  	}
    21  }
    22  
    23  func TestProvisionerPrepare_Defaults(t *testing.T) {
    24  	var p Provisioner
    25  	config := testConfig()
    26  
    27  	playbook_file, err := ioutil.TempFile("", "playbook")
    28  	if err != nil {
    29  		t.Fatalf("err: %s", err)
    30  	}
    31  	defer os.Remove(playbook_file.Name())
    32  
    33  	config["playbook_file"] = playbook_file.Name()
    34  	err = p.Prepare(config)
    35  	if err != nil {
    36  		t.Fatalf("err: %s", err)
    37  	}
    38  
    39  	if p.config.StagingDir != DefaultStagingDir {
    40  		t.Fatalf("unexpected staging dir %s, expected %s",
    41  			p.config.StagingDir, DefaultStagingDir)
    42  	}
    43  }
    44  
    45  func TestProvisionerPrepare_PlaybookFile(t *testing.T) {
    46  	var p Provisioner
    47  	config := testConfig()
    48  
    49  	err := p.Prepare(config)
    50  	if err == nil {
    51  		t.Fatal("should have error")
    52  	}
    53  
    54  	config["playbook_file"] = ""
    55  	err = p.Prepare(config)
    56  	if err == nil {
    57  		t.Fatal("should have error")
    58  	}
    59  
    60  	playbook_file, err := ioutil.TempFile("", "playbook")
    61  	if err != nil {
    62  		t.Fatalf("err: %s", err)
    63  	}
    64  	defer os.Remove(playbook_file.Name())
    65  
    66  	config["playbook_file"] = playbook_file.Name()
    67  	err = p.Prepare(config)
    68  	if err != nil {
    69  		t.Fatalf("err: %s", err)
    70  	}
    71  }
    72  
    73  func TestProvisionerPrepare_InventoryFile(t *testing.T) {
    74  	var p Provisioner
    75  	config := testConfig()
    76  
    77  	err := p.Prepare(config)
    78  	if err == nil {
    79  		t.Fatal("should have error")
    80  	}
    81  
    82  	config["playbook_file"] = ""
    83  	err = p.Prepare(config)
    84  	if err == nil {
    85  		t.Fatal("should have error")
    86  	}
    87  
    88  	playbook_file, err := ioutil.TempFile("", "playbook")
    89  	if err != nil {
    90  		t.Fatalf("err: %s", err)
    91  	}
    92  	defer os.Remove(playbook_file.Name())
    93  
    94  	config["playbook_file"] = playbook_file.Name()
    95  	err = p.Prepare(config)
    96  	if err != nil {
    97  		t.Fatalf("err: %s", err)
    98  	}
    99  
   100  	inventory_file, err := ioutil.TempFile("", "inventory")
   101  	if err != nil {
   102  		t.Fatalf("err: %s", err)
   103  	}
   104  	defer os.Remove(inventory_file.Name())
   105  
   106  	config["inventory_file"] = inventory_file.Name()
   107  	err = p.Prepare(config)
   108  	if err != nil {
   109  		t.Fatalf("err: %s", err)
   110  	}
   111  }
   112  
   113  func TestProvisionerPrepare_Dirs(t *testing.T) {
   114  	var p Provisioner
   115  	config := testConfig()
   116  
   117  	err := p.Prepare(config)
   118  	if err == nil {
   119  		t.Fatal("should have error")
   120  	}
   121  
   122  	config["playbook_file"] = ""
   123  	err = p.Prepare(config)
   124  	if err == nil {
   125  		t.Fatal("should have error")
   126  	}
   127  
   128  	playbook_file, err := ioutil.TempFile("", "playbook")
   129  	if err != nil {
   130  		t.Fatalf("err: %s", err)
   131  	}
   132  	defer os.Remove(playbook_file.Name())
   133  
   134  	config["playbook_file"] = playbook_file.Name()
   135  	err = p.Prepare(config)
   136  	if err != nil {
   137  		t.Fatalf("err: %s", err)
   138  	}
   139  
   140  	config["playbook_paths"] = []string{playbook_file.Name()}
   141  	err = p.Prepare(config)
   142  	if err == nil {
   143  		t.Fatal("should error if playbook paths is not a dir")
   144  	}
   145  
   146  	config["playbook_paths"] = []string{os.TempDir()}
   147  	err = p.Prepare(config)
   148  	if err != nil {
   149  		t.Fatalf("err: %s", err)
   150  	}
   151  
   152  	config["role_paths"] = []string{playbook_file.Name()}
   153  	err = p.Prepare(config)
   154  	if err == nil {
   155  		t.Fatal("should error if role paths is not a dir")
   156  	}
   157  
   158  	config["role_paths"] = []string{os.TempDir()}
   159  	err = p.Prepare(config)
   160  	if err != nil {
   161  		t.Fatalf("err: %s", err)
   162  	}
   163  
   164  	config["group_vars"] = playbook_file.Name()
   165  	err = p.Prepare(config)
   166  	if err == nil {
   167  		t.Fatalf("should error if group_vars path is not a dir")
   168  	}
   169  
   170  	config["group_vars"] = os.TempDir()
   171  	err = p.Prepare(config)
   172  	if err != nil {
   173  		t.Fatalf("err: %s", err)
   174  	}
   175  
   176  	config["host_vars"] = playbook_file.Name()
   177  	err = p.Prepare(config)
   178  	if err == nil {
   179  		t.Fatalf("should error if host_vars path is not a dir")
   180  	}
   181  
   182  	config["host_vars"] = os.TempDir()
   183  	err = p.Prepare(config)
   184  	if err != nil {
   185  		t.Fatalf("err: %s", err)
   186  	}
   187  }