github.com/supr/packer@v0.3.10-0.20131015195147-7b09e24ac3c1/provisioner/chef-solo/provisioner_test.go (about) 1 package chefsolo 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 return map[string]interface{}{} 12 } 13 14 func TestProvisioner_Impl(t *testing.T) { 15 var raw interface{} 16 raw = &Provisioner{} 17 if _, ok := raw.(packer.Provisioner); !ok { 18 t.Fatalf("must be a Provisioner") 19 } 20 } 21 22 func TestProvisionerPrepare_chefEnvironment(t *testing.T) { 23 var p Provisioner 24 25 config := testConfig() 26 config["chef_environment"] = "some-env" 27 28 err := p.Prepare(config) 29 if err != nil { 30 t.Fatalf("err: %s", err) 31 } 32 33 if p.config.ChefEnvironment != "some-env" { 34 t.Fatalf("unexpected: %#v", p.config.ChefEnvironment) 35 } 36 } 37 38 func TestProvisionerPrepare_configTemplate(t *testing.T) { 39 var err error 40 var p Provisioner 41 42 // Test no config template 43 config := testConfig() 44 delete(config, "config_template") 45 err = p.Prepare(config) 46 if err != nil { 47 t.Fatalf("err: %s", err) 48 } 49 50 // Test with a file 51 tf, err := ioutil.TempFile("", "packer") 52 if err != nil { 53 t.Fatalf("err: %s", err) 54 } 55 defer os.Remove(tf.Name()) 56 57 config = testConfig() 58 config["config_template"] = tf.Name() 59 p = Provisioner{} 60 err = p.Prepare(config) 61 if err != nil { 62 t.Fatalf("err: %s", err) 63 } 64 65 // Test with a directory 66 td, err := ioutil.TempDir("", "packer") 67 if err != nil { 68 t.Fatalf("err: %s", err) 69 } 70 defer os.RemoveAll(td) 71 72 config = testConfig() 73 config["config_template"] = td 74 p = Provisioner{} 75 err = p.Prepare(config) 76 if err == nil { 77 t.Fatal("should have err") 78 } 79 } 80 81 func TestProvisionerPrepare_cookbookPaths(t *testing.T) { 82 var p Provisioner 83 84 path1, err := ioutil.TempDir("", "cookbooks_one") 85 if err != nil { 86 t.Fatalf("err: %s", err) 87 } 88 89 path2, err := ioutil.TempDir("", "cookbooks_two") 90 if err != nil { 91 t.Fatalf("err: %s", err) 92 } 93 94 rolesPath, err := ioutil.TempDir("", "roles") 95 if err != nil { 96 t.Fatalf("err: %s", err) 97 } 98 99 dataBagsPath, err := ioutil.TempDir("", "data_bags") 100 if err != nil { 101 t.Fatalf("err: %s", err) 102 } 103 104 defer os.Remove(path1) 105 defer os.Remove(path2) 106 defer os.Remove(rolesPath) 107 defer os.Remove(dataBagsPath) 108 109 config := testConfig() 110 config["cookbook_paths"] = []string{path1, path2} 111 config["roles_path"] = rolesPath 112 config["data_bags_path"] = dataBagsPath 113 114 err = p.Prepare(config) 115 if err != nil { 116 t.Fatalf("err: %s", err) 117 } 118 119 if len(p.config.CookbookPaths) != 2 { 120 t.Fatalf("unexpected: %#v", p.config.CookbookPaths) 121 } 122 123 if p.config.CookbookPaths[0] != path1 || p.config.CookbookPaths[1] != path2 { 124 t.Fatalf("unexpected: %#v", p.config.CookbookPaths) 125 } 126 127 if p.config.RolesPath != rolesPath { 128 t.Fatalf("unexpected: %#v", p.config.RolesPath) 129 } 130 131 if p.config.DataBagsPath != dataBagsPath { 132 t.Fatalf("unexpected: %#v", p.config.DataBagsPath) 133 } 134 } 135 136 func TestProvisionerPrepare_dataBagsPath(t *testing.T) { 137 var p Provisioner 138 139 dataBagsPath, err := ioutil.TempDir("", "data_bags") 140 if err != nil { 141 t.Fatalf("err: %s", err) 142 } 143 defer os.Remove(dataBagsPath) 144 145 config := testConfig() 146 config["data_bags_path"] = dataBagsPath 147 148 err = p.Prepare(config) 149 if err != nil { 150 t.Fatalf("err: %s", err) 151 } 152 153 if p.config.DataBagsPath != dataBagsPath { 154 t.Fatalf("unexpected: %#v", p.config.DataBagsPath) 155 } 156 } 157 158 func TestProvisionerPrepare_environmentsPath(t *testing.T) { 159 var p Provisioner 160 161 environmentsPath, err := ioutil.TempDir("", "environments") 162 if err != nil { 163 t.Fatalf("err: %s", err) 164 } 165 defer os.Remove(environmentsPath) 166 167 config := testConfig() 168 config["environments_path"] = environmentsPath 169 170 err = p.Prepare(config) 171 if err != nil { 172 t.Fatalf("err: %s", err) 173 } 174 175 if p.config.EnvironmentsPath != environmentsPath { 176 t.Fatalf("unexpected: %#v", p.config.EnvironmentsPath) 177 } 178 } 179 180 func TestProvisionerPrepare_rolesPath(t *testing.T) { 181 var p Provisioner 182 183 rolesPath, err := ioutil.TempDir("", "roles") 184 if err != nil { 185 t.Fatalf("err: %s", err) 186 } 187 defer os.Remove(rolesPath) 188 189 config := testConfig() 190 config["roles_path"] = rolesPath 191 192 err = p.Prepare(config) 193 if err != nil { 194 t.Fatalf("err: %s", err) 195 } 196 197 if p.config.RolesPath != rolesPath { 198 t.Fatalf("unexpected: %#v", p.config.RolesPath) 199 } 200 } 201 202 func TestProvisionerPrepare_json(t *testing.T) { 203 config := testConfig() 204 config["json"] = map[string]interface{}{ 205 "foo": "{{ user `foo` }}", 206 } 207 208 config[packer.UserVariablesConfigKey] = map[string]string{ 209 "foo": `"bar\baz"`, 210 } 211 212 var p Provisioner 213 err := p.Prepare(config) 214 if err != nil { 215 t.Fatalf("err: %s", err) 216 } 217 218 if p.config.Json["foo"] != `"bar\baz"` { 219 t.Fatalf("bad: %#v", p.config.Json) 220 } 221 }