From 7f474d8f788b5eec291725b7581849aa41ecbf97 Mon Sep 17 00:00:00 2001 From: Lars Wander Date: Thu, 3 Nov 2016 12:45:52 -0400 Subject: [PATCH] builder/googlecompute: Tests added --- .../step_create_instance_test.go | 31 +++++++++++++++++++ .../googlecompute/step_create_ssh_key_test.go | 17 ++++++++++ builder/googlecompute/test-fixtures/fake-key | 27 ++++++++++++++++ 3 files changed, 75 insertions(+) create mode 100644 builder/googlecompute/test-fixtures/fake-key diff --git a/builder/googlecompute/step_create_instance_test.go b/builder/googlecompute/step_create_instance_test.go index 8c567479b..c0122584a 100644 --- a/builder/googlecompute/step_create_instance_test.go +++ b/builder/googlecompute/step_create_instance_test.go @@ -2,6 +2,7 @@ package googlecompute import ( "errors" + "strings" "testing" "time" @@ -210,3 +211,33 @@ func TestStepCreateInstance_errorTimeout(t *testing.T) { _, ok = state.GetOk("instance_name") assert.False(t, ok, "State should not have an instance name.") } + +func TestCreateInstanceMetadata(t *testing.T) { + state := testState(t) + c := state.Get("config").(*Config) + image := StubImage("test-image", "test-project", []string{}, 100) + key := "abcdefgh12345678" + + // create our metadata + metadata, err := c.createInstanceMetadata(image, key) + + assert.True(t, err == nil, "Metadata creation should have succeeded.") + + // ensure our key is listed + assert.True(t, strings.Contains(metadata["sshKeys"], key), "Instance metadata should contain provided key") +} + +func TestCreateInstanceMetadata_noPublicKey(t *testing.T) { + state := testState(t) + c := state.Get("config").(*Config) + image := StubImage("test-image", "test-project", []string{}, 100) + sshKeys := c.Metadata["sshKeys"] + + // create our metadata + metadata, err := c.createInstanceMetadata(image, "") + + assert.True(t, err == nil, "Metadata creation should have succeeded.") + + // ensure the ssh metadata hasn't changed + assert.Equal(t, metadata["sshKeys"], sshKeys, "Instance metadata should not have been modified") +} diff --git a/builder/googlecompute/step_create_ssh_key_test.go b/builder/googlecompute/step_create_ssh_key_test.go index 304d6f1fb..94504ac7f 100644 --- a/builder/googlecompute/step_create_ssh_key_test.go +++ b/builder/googlecompute/step_create_ssh_key_test.go @@ -12,6 +12,23 @@ func TestStepCreateSSHKey_impl(t *testing.T) { var _ multistep.Step = new(StepCreateSSHKey) } +func TestStepCreateSSHKey_privateKey(t *testing.T) { + state := testState(t) + step := new(StepCreateSSHKey) + step.PrivateKeyFile = "test-fixtures/fake-key" + defer step.Cleanup(state) + + // run the step + if action := step.Run(state); action != multistep.ActionContinue { + t.Fatalf("bad action: %#v", action) + } + + // Verify that we have a public/private key + if _, ok := state.GetOk("ssh_private_key"); !ok { + t.Fatal("should have key") + } +} + func TestStepCreateSSHKey(t *testing.T) { state := testState(t) step := new(StepCreateSSHKey) diff --git a/builder/googlecompute/test-fixtures/fake-key b/builder/googlecompute/test-fixtures/fake-key new file mode 100644 index 000000000..e2f2e87e0 --- /dev/null +++ b/builder/googlecompute/test-fixtures/fake-key @@ -0,0 +1,27 @@ +-----BEGIN RSA PRIVATE KEY----- +AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA +AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA +AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA +AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA +AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA +AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA +AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA +AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA +AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA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