github.com/dacamp/packer@v0.10.2/provisioner/ansible/provisioner_test.go (about) 1 package ansible 2 3 import ( 4 "crypto/rand" 5 "fmt" 6 "io" 7 "io/ioutil" 8 "os" 9 "path" 10 "testing" 11 12 "github.com/mitchellh/packer/packer" 13 ) 14 15 // Be sure to remove the Ansible stub file in each test with: 16 // defer os.Remove(config["command"].(string)) 17 func testConfig(t *testing.T) map[string]interface{} { 18 m := make(map[string]interface{}) 19 wd, err := os.Getwd() 20 if err != nil { 21 t.Fatalf("err: %s", err) 22 } 23 ansible_stub := path.Join(wd, "packer-ansible-stub.sh") 24 25 err = ioutil.WriteFile(ansible_stub, []byte("#!/usr/bin/env bash\necho ansible 1.6.0"), 0777) 26 if err != nil { 27 t.Fatalf("err: %s", err) 28 } 29 m["command"] = ansible_stub 30 31 return m 32 } 33 34 func TestProvisioner_Impl(t *testing.T) { 35 var raw interface{} 36 raw = &Provisioner{} 37 if _, ok := raw.(packer.Provisioner); !ok { 38 t.Fatalf("must be a Provisioner") 39 } 40 } 41 42 func TestProvisionerPrepare_Defaults(t *testing.T) { 43 var p Provisioner 44 config := testConfig(t) 45 defer os.Remove(config["command"].(string)) 46 47 err := p.Prepare(config) 48 if err == nil { 49 t.Fatalf("should have error") 50 } 51 52 hostkey_file, err := ioutil.TempFile("", "hostkey") 53 if err != nil { 54 t.Fatalf("err: %s", err) 55 } 56 defer os.Remove(hostkey_file.Name()) 57 58 publickey_file, err := ioutil.TempFile("", "publickey") 59 if err != nil { 60 t.Fatalf("err: %s", err) 61 } 62 defer os.Remove(publickey_file.Name()) 63 64 playbook_file, err := ioutil.TempFile("", "playbook") 65 if err != nil { 66 t.Fatalf("err: %s", err) 67 } 68 defer os.Remove(playbook_file.Name()) 69 70 config["ssh_host_key_file"] = hostkey_file.Name() 71 config["ssh_authorized_key_file"] = publickey_file.Name() 72 config["playbook_file"] = playbook_file.Name() 73 err = p.Prepare(config) 74 if err != nil { 75 t.Fatalf("err: %s", err) 76 } 77 } 78 79 func TestProvisionerPrepare_PlaybookFile(t *testing.T) { 80 var p Provisioner 81 config := testConfig(t) 82 defer os.Remove(config["command"].(string)) 83 84 hostkey_file, err := ioutil.TempFile("", "hostkey") 85 if err != nil { 86 t.Fatalf("err: %s", err) 87 } 88 defer os.Remove(hostkey_file.Name()) 89 90 publickey_file, err := ioutil.TempFile("", "publickey") 91 if err != nil { 92 t.Fatalf("err: %s", err) 93 } 94 defer os.Remove(publickey_file.Name()) 95 96 config["ssh_host_key_file"] = hostkey_file.Name() 97 config["ssh_authorized_key_file"] = publickey_file.Name() 98 99 err = p.Prepare(config) 100 if err == nil { 101 t.Fatal("should have error") 102 } 103 104 playbook_file, err := ioutil.TempFile("", "playbook") 105 if err != nil { 106 t.Fatalf("err: %s", err) 107 } 108 defer os.Remove(playbook_file.Name()) 109 110 config["playbook_file"] = playbook_file.Name() 111 err = p.Prepare(config) 112 if err != nil { 113 t.Fatalf("err: %s", err) 114 } 115 } 116 117 func TestProvisionerPrepare_HostKeyFile(t *testing.T) { 118 var p Provisioner 119 config := testConfig(t) 120 defer os.Remove(config["command"].(string)) 121 122 publickey_file, err := ioutil.TempFile("", "publickey") 123 if err != nil { 124 t.Fatalf("err: %s", err) 125 } 126 defer os.Remove(publickey_file.Name()) 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 filename := make([]byte, 10) 135 n, err := io.ReadFull(rand.Reader, filename) 136 if n != len(filename) || err != nil { 137 t.Fatal("could not create random file name") 138 } 139 140 config["ssh_host_key_file"] = fmt.Sprintf("%x", filename) 141 config["ssh_authorized_key_file"] = publickey_file.Name() 142 config["playbook_file"] = playbook_file.Name() 143 144 err = p.Prepare(config) 145 if err == nil { 146 t.Fatal("should error if ssh_host_key_file does not exist") 147 } 148 149 hostkey_file, err := ioutil.TempFile("", "hostkey") 150 if err != nil { 151 t.Fatalf("err: %s", err) 152 } 153 defer os.Remove(hostkey_file.Name()) 154 155 config["ssh_host_key_file"] = hostkey_file.Name() 156 err = p.Prepare(config) 157 if err != nil { 158 t.Fatalf("err: %s", err) 159 } 160 } 161 162 func TestProvisionerPrepare_AuthorizedKeyFile(t *testing.T) { 163 var p Provisioner 164 config := testConfig(t) 165 defer os.Remove(config["command"].(string)) 166 167 hostkey_file, err := ioutil.TempFile("", "hostkey") 168 if err != nil { 169 t.Fatalf("err: %s", err) 170 } 171 defer os.Remove(hostkey_file.Name()) 172 173 playbook_file, err := ioutil.TempFile("", "playbook") 174 if err != nil { 175 t.Fatalf("err: %s", err) 176 } 177 defer os.Remove(playbook_file.Name()) 178 179 filename := make([]byte, 10) 180 n, err := io.ReadFull(rand.Reader, filename) 181 if n != len(filename) || err != nil { 182 t.Fatal("could not create random file name") 183 } 184 185 config["ssh_host_key_file"] = hostkey_file.Name() 186 config["playbook_file"] = playbook_file.Name() 187 config["ssh_authorized_key_file"] = fmt.Sprintf("%x", filename) 188 189 err = p.Prepare(config) 190 if err == nil { 191 t.Errorf("should error if ssh_authorized_key_file does not exist") 192 } 193 194 publickey_file, err := ioutil.TempFile("", "publickey") 195 if err != nil { 196 t.Fatalf("err: %s", err) 197 } 198 defer os.Remove(publickey_file.Name()) 199 200 config["ssh_authorized_key_file"] = publickey_file.Name() 201 err = p.Prepare(config) 202 if err != nil { 203 t.Errorf("err: %s", err) 204 } 205 } 206 207 func TestProvisionerPrepare_LocalPort(t *testing.T) { 208 var p Provisioner 209 config := testConfig(t) 210 defer os.Remove(config["command"].(string)) 211 212 hostkey_file, err := ioutil.TempFile("", "hostkey") 213 if err != nil { 214 t.Fatalf("err: %s", err) 215 } 216 defer os.Remove(hostkey_file.Name()) 217 218 publickey_file, err := ioutil.TempFile("", "publickey") 219 if err != nil { 220 t.Fatalf("err: %s", err) 221 } 222 defer os.Remove(publickey_file.Name()) 223 224 playbook_file, err := ioutil.TempFile("", "playbook") 225 if err != nil { 226 t.Fatalf("err: %s", err) 227 } 228 defer os.Remove(playbook_file.Name()) 229 230 config["ssh_host_key_file"] = hostkey_file.Name() 231 config["ssh_authorized_key_file"] = publickey_file.Name() 232 config["playbook_file"] = playbook_file.Name() 233 234 config["local_port"] = "65537" 235 err = p.Prepare(config) 236 if err == nil { 237 t.Fatal("should have error") 238 } 239 240 config["local_port"] = "22222" 241 err = p.Prepare(config) 242 if err != nil { 243 t.Fatalf("err: %s", err) 244 } 245 }