Scott Crunkleton 7190fbeed8 Adding support for googlecompute startup scripts.
- Startup scripts can be provided through the instance creation metadata field 'startup-script'.
- Script log can be copied to a GCS location by setting the metadata field 'startup-script-log-dest'.
Added Retry method to googlecompute package.
Added GetSerialPortOutput to googlecompute Drivers.
Added StepWaitInstanceStartup (and associated test) which waits for an
instance startup-script to finish.
Changed the instance service account to use the same service account as the one provided in the Packer config template. It was the project default service account.

Tested googlecompute package with 'go test' and also performed builds
with a startup script and without a startup script.
2016-07-20 14:54:36 -07:00

64 lines
1.8 KiB
Go

package googlecompute
import (
"fmt"
"testing"
)
func TestRetry(t *testing.T) {
numTries := uint(0)
// Test that a passing function only gets called once.
err := Retry(0, 0, 0, func() (bool, error) {
numTries++
return true, nil
})
if numTries != 1 {
t.Fatal("Passing function should not have been retried.")
}
if err != nil {
t.Fatalf("Passing function should not have returned a retry error. Error: %s", err)
}
// Test that a failing function gets retried (once in this example).
numTries = 0
results := []bool{false, true}
err = Retry(0, 0, 0, func() (bool, error) {
result := results[numTries]
numTries++
return result, nil
})
if numTries != 2 {
t.Fatalf("Retried function should have been tried twice. Tried %d times.", numTries)
}
if err != nil {
t.Fatalf("Successful retried function should not have returned a retry error. Error: %s", err)
}
// Test that a function error gets returned, and the function does not get called again.
numTries = 0
funcErr := fmt.Errorf("This function had an error!")
err = Retry(0, 0, 0, func() (bool, error) {
numTries++
return false, funcErr
})
if numTries != 1 {
t.Fatal("Errant function should not have been retried.")
}
if err != funcErr {
t.Fatalf("Errant function did not return the right error %s. Error: %s", funcErr, err)
}
// Test when a function exhausts its retries.
numTries = 0
expectedTries := uint(3)
err = Retry(0, 0, expectedTries, func() (bool, error) {
numTries++
return false, nil
})
if numTries != expectedTries {
t.Fatalf("Unsuccessul retry function should have been called %d times. Only called %d times.", expectedTries, numTries)
}
if err != RetryExhaustedError {
t.Fatalf("Unsuccessful retry function should have returned a retry exhausted error. Actual error: %s", err)
}
}