github.com/rstandt/terraform@v0.12.32-0.20230710220336-b1063613405c/builtin/provisioners/puppet/linux_provisioner_test.go (about) 1 package puppet 2 3 import ( 4 "io" 5 "strings" 6 "testing" 7 8 "github.com/hashicorp/terraform/communicator" 9 "github.com/hashicorp/terraform/communicator/remote" 10 "github.com/hashicorp/terraform/helper/schema" 11 "github.com/hashicorp/terraform/terraform" 12 ) 13 14 func TestResourceProvisioner_linuxUploadFile(t *testing.T) { 15 cases := map[string]struct { 16 Config map[string]interface{} 17 Commands map[string]bool 18 CommandFunc func(*remote.Cmd) error 19 ExpectedError bool 20 Uploads map[string]string 21 File io.Reader 22 Dir string 23 Filename string 24 }{ 25 "Successful upload": { 26 Config: map[string]interface{}{ 27 "server": "puppet.test.com", 28 "use_sudo": false, 29 }, 30 Commands: map[string]bool{ 31 "mkdir -p /etc/puppetlabs/puppet": true, 32 "mv /tmp/csr_attributes.yaml /etc/puppetlabs/puppet/csr_attributes.yaml": true, 33 }, 34 Uploads: map[string]string{ 35 "/tmp/csr_attributes.yaml": "", 36 }, 37 Dir: "/etc/puppetlabs/puppet", 38 Filename: "csr_attributes.yaml", 39 File: strings.NewReader(""), 40 }, 41 "Failure when creating the directory": { 42 Config: map[string]interface{}{ 43 "server": "puppet.test.com", 44 "use_sudo": false, 45 }, 46 Commands: map[string]bool{ 47 "mkdir -p /etc/puppetlabs/puppet": true, 48 }, 49 Dir: "/etc/puppetlabs/puppet", 50 Filename: "csr_attributes.yaml", 51 File: strings.NewReader(""), 52 CommandFunc: func(r *remote.Cmd) error { 53 r.SetExitStatus(1, &remote.ExitError{ 54 Command: "mkdir -p /etc/puppetlabs/puppet", 55 ExitStatus: 1, 56 Err: nil, 57 }) 58 return nil 59 }, 60 ExpectedError: true, 61 }, 62 } 63 64 for k, tc := range cases { 65 p, err := decodeConfig( 66 schema.TestResourceDataRaw(t, Provisioner().(*schema.Provisioner).Schema, tc.Config), 67 ) 68 if err != nil { 69 t.Fatalf("Error: %v", err) 70 } 71 72 c := new(communicator.MockCommunicator) 73 c.Commands = tc.Commands 74 c.Uploads = tc.Uploads 75 if tc.CommandFunc != nil { 76 c.CommandFunc = tc.CommandFunc 77 } 78 p.comm = c 79 p.output = new(terraform.MockUIOutput) 80 81 err = p.linuxUploadFile(tc.File, tc.Dir, tc.Filename) 82 if tc.ExpectedError { 83 if err == nil { 84 t.Fatalf("Expected error, but no error returned") 85 } 86 } else { 87 if err != nil { 88 t.Fatalf("Test %q failed: %v", k, err) 89 } 90 } 91 } 92 } 93 94 func TestResourceProvisioner_linuxDefaultCertname(t *testing.T) { 95 cases := map[string]struct { 96 Config map[string]interface{} 97 Commands map[string]bool 98 CommandFunc func(*remote.Cmd) error 99 ExpectedError bool 100 }{ 101 "No sudo": { 102 Config: map[string]interface{}{ 103 "server": "puppet.test.com", 104 "use_sudo": false, 105 }, 106 Commands: map[string]bool{ 107 "hostname -f": true, 108 }, 109 }, 110 "With sudo": { 111 Config: map[string]interface{}{ 112 "server": "puppet.test.com", 113 "use_sudo": true, 114 }, 115 Commands: map[string]bool{ 116 "sudo hostname -f": true, 117 }, 118 }, 119 "Failed execution": { 120 Config: map[string]interface{}{ 121 "server": "puppet.test.com", 122 "use_sudo": false, 123 }, 124 Commands: map[string]bool{ 125 "hostname -f": true, 126 }, 127 CommandFunc: func(r *remote.Cmd) error { 128 if r.Command == "hostname -f" { 129 r.SetExitStatus(1, &remote.ExitError{ 130 Command: "hostname -f", 131 ExitStatus: 1, 132 Err: nil, 133 }) 134 } else { 135 r.SetExitStatus(0, nil) 136 } 137 return nil 138 }, 139 ExpectedError: true, 140 }, 141 } 142 143 for k, tc := range cases { 144 p, err := decodeConfig( 145 schema.TestResourceDataRaw(t, Provisioner().(*schema.Provisioner).Schema, tc.Config), 146 ) 147 if err != nil { 148 t.Fatalf("Error: %v", err) 149 } 150 151 c := new(communicator.MockCommunicator) 152 c.Commands = tc.Commands 153 if tc.CommandFunc != nil { 154 c.CommandFunc = tc.CommandFunc 155 } 156 p.comm = c 157 p.output = new(terraform.MockUIOutput) 158 159 _, err = p.linuxDefaultCertname() 160 if tc.ExpectedError { 161 if err == nil { 162 t.Fatalf("Expected error, but no error returned") 163 } 164 } else { 165 if err != nil { 166 t.Fatalf("Test %q failed: %v", k, err) 167 } 168 } 169 } 170 } 171 172 func TestResourceProvisioner_linuxInstallPuppetAgent(t *testing.T) { 173 cases := map[string]struct { 174 Config map[string]interface{} 175 Commands map[string]bool 176 CommandFunc func(*remote.Cmd) error 177 ExpectedError bool 178 }{ 179 "Everything runs succcessfully": { 180 Config: map[string]interface{}{ 181 "server": "puppet.test.com", 182 "use_sudo": false, 183 }, 184 Commands: map[string]bool{ 185 "curl -kO https://puppet.test.com:8140/packages/current/install.bash": true, 186 "bash -- ./install.bash --puppet-service-ensure stopped": true, 187 "rm -f install.bash": true, 188 }, 189 }, 190 "Respects the use_sudo config flag": { 191 Config: map[string]interface{}{ 192 "server": "puppet.test.com", 193 "use_sudo": true, 194 }, 195 Commands: map[string]bool{ 196 "sudo curl -kO https://puppet.test.com:8140/packages/current/install.bash": true, 197 "sudo bash -- ./install.bash --puppet-service-ensure stopped": true, 198 "sudo rm -f install.bash": true, 199 }, 200 }, 201 "When the curl command fails": { 202 Config: map[string]interface{}{ 203 "server": "puppet.test.com", 204 "use_sudo": false, 205 }, 206 Commands: map[string]bool{ 207 "curl -kO https://puppet.test.com:8140/packages/current/install.bash": true, 208 "bash -- ./install.bash --puppet-service-ensure stopped": false, 209 "rm -f install.bash": false, 210 }, 211 CommandFunc: func(r *remote.Cmd) error { 212 if r.Command == "curl -kO https://puppet.test.com:8140/packages/current/install.bash" { 213 r.SetExitStatus(1, &remote.ExitError{ 214 Command: "curl -kO https://puppet.test.com:8140/packages/current/install.bash", 215 ExitStatus: 1, 216 Err: nil, 217 }) 218 } else { 219 r.SetExitStatus(0, nil) 220 } 221 return nil 222 }, 223 ExpectedError: true, 224 }, 225 "When the install script fails": { 226 Config: map[string]interface{}{ 227 "server": "puppet.test.com", 228 "use_sudo": false, 229 }, 230 Commands: map[string]bool{ 231 "curl -kO https://puppet.test.com:8140/packages/current/install.bash": true, 232 "bash -- ./install.bash --puppet-service-ensure stopped": true, 233 "rm -f install.bash": false, 234 }, 235 CommandFunc: func(r *remote.Cmd) error { 236 if r.Command == "bash -- ./install.bash --puppet-service-ensure stopped" { 237 r.SetExitStatus(1, &remote.ExitError{ 238 Command: "bash -- ./install.bash --puppet-service-ensure stopped", 239 ExitStatus: 1, 240 Err: nil, 241 }) 242 } else { 243 r.SetExitStatus(0, nil) 244 } 245 return nil 246 }, 247 ExpectedError: true, 248 }, 249 "When the cleanup rm fails": { 250 Config: map[string]interface{}{ 251 "server": "puppet.test.com", 252 "use_sudo": false, 253 }, 254 Commands: map[string]bool{ 255 "curl -kO https://puppet.test.com:8140/packages/current/install.bash": true, 256 "bash -- ./install.bash --puppet-service-ensure stopped": true, 257 "rm -f install.bash": true, 258 }, 259 CommandFunc: func(r *remote.Cmd) error { 260 if r.Command == "rm -f install.bash" { 261 r.SetExitStatus(1, &remote.ExitError{ 262 Command: "rm -f install.bash", 263 ExitStatus: 1, 264 Err: nil, 265 }) 266 } else { 267 r.SetExitStatus(0, nil) 268 } 269 return nil 270 }, 271 ExpectedError: true, 272 }, 273 } 274 275 for k, tc := range cases { 276 p, err := decodeConfig( 277 schema.TestResourceDataRaw(t, Provisioner().(*schema.Provisioner).Schema, tc.Config), 278 ) 279 if err != nil { 280 t.Fatalf("Error: %v", err) 281 } 282 283 c := new(communicator.MockCommunicator) 284 c.Commands = tc.Commands 285 if tc.CommandFunc != nil { 286 c.CommandFunc = tc.CommandFunc 287 } 288 p.comm = c 289 p.output = new(terraform.MockUIOutput) 290 291 err = p.linuxInstallPuppetAgent() 292 if tc.ExpectedError { 293 if err == nil { 294 t.Fatalf("Expected error, but no error returned") 295 } 296 } else { 297 if err != nil { 298 t.Fatalf("Test %q failed: %v", k, err) 299 } 300 } 301 } 302 } 303 304 func TestResourceProvisioner_linuxRunPuppetAgent(t *testing.T) { 305 cases := map[string]struct { 306 Config map[string]interface{} 307 Commands map[string]bool 308 CommandFunc func(*remote.Cmd) error 309 ExpectedError bool 310 }{ 311 "When puppet returns 0": { 312 Config: map[string]interface{}{ 313 "server": "puppet.test.com", 314 "use_sudo": false, 315 }, 316 Commands: map[string]bool{ 317 "/opt/puppetlabs/puppet/bin/puppet agent --test --server puppet.test.com --environment production": true, 318 }, 319 }, 320 "When puppet returns 2 (changes applied without error)": { 321 Config: map[string]interface{}{ 322 "server": "puppet.test.com", 323 "use_sudo": false, 324 }, 325 CommandFunc: func(r *remote.Cmd) error { 326 r.SetExitStatus(2, &remote.ExitError{ 327 Command: "/opt/puppetlabs/puppet/bin/puppet agent --test --server puppet.test.com", 328 ExitStatus: 2, 329 Err: nil, 330 }) 331 return nil 332 }, 333 ExpectedError: false, 334 }, 335 "When puppet returns something not 0 or 2": { 336 Config: map[string]interface{}{ 337 "server": "puppet.test.com", 338 "use_sudo": false, 339 }, 340 CommandFunc: func(r *remote.Cmd) error { 341 r.SetExitStatus(1, &remote.ExitError{ 342 Command: "/opt/puppetlabs/puppet/bin/puppet agent --test --server puppet.test.com", 343 ExitStatus: 1, 344 Err: nil, 345 }) 346 return nil 347 }, 348 ExpectedError: true, 349 }, 350 } 351 352 for k, tc := range cases { 353 p, err := decodeConfig( 354 schema.TestResourceDataRaw(t, Provisioner().(*schema.Provisioner).Schema, tc.Config), 355 ) 356 if err != nil { 357 t.Fatalf("Error: %v", err) 358 } 359 360 c := new(communicator.MockCommunicator) 361 c.Commands = tc.Commands 362 if tc.CommandFunc != nil { 363 c.CommandFunc = tc.CommandFunc 364 } 365 p.comm = c 366 p.output = new(terraform.MockUIOutput) 367 368 err = p.linuxRunPuppetAgent() 369 if tc.ExpectedError { 370 if err == nil { 371 t.Fatalf("Expected error, but no error returned") 372 } 373 } else { 374 if err != nil { 375 t.Fatalf("Test %q failed: %v", k, err) 376 } 377 } 378 } 379 }