chore: add test for MetadataFiles option

Signed-off-by: Pratyush singhal <psinghal20@gmail.com>
This commit is contained in:
Pratyush singhal 2019-06-11 17:45:30 +05:30
parent 99a3e9cf0a
commit 4a369b4ef1
2 changed files with 34 additions and 1 deletions

View File

@ -431,6 +431,7 @@ func testConfig(t *testing.T) (config map[string]interface{}, tempAccountFile st
"image_licenses": []string{
"test-license",
},
"metadata_files": map[string]string{},
"zone": "us-east1-a",
}
@ -484,6 +485,21 @@ func testAccountFile(t *testing.T) string {
return tf.Name()
}
const testMetadataFileContent = `testMetadata`
func testMetadataFile(t *testing.T) string {
tf, err := ioutil.TempFile("", "packer")
if err != nil {
t.Fatalf("err: %s", err)
}
defer tf.Close()
if _, err := tf.Write([]byte(testMetadataFileContent)); err != nil {
t.Fatalf("err: %s", err)
}
return tf.Name()
}
// This is just some dummy data that doesn't actually work (it was revoked
// a long time ago).
const testAccountContent = `{}`

View File

@ -325,3 +325,20 @@ func TestCreateInstanceMetadata_noPublicKey(t *testing.T) {
// ensure the ssh metadata hasn't changed
assert.Equal(t, metadata["sshKeys"], sshKeys, "Instance metadata should not have been modified")
}
func TestCreateInstanceMetadata_metadataFile(t *testing.T) {
state := testState(t)
c := state.Get("config").(*Config)
image := StubImage("test-image", "test-project", []string{}, 100)
content := testMetadataFileContent
fileName := testMetadataFile(t)
c.MetadataFiles["user-data"] = fileName
// create our metadata
metadata, err := c.createInstanceMetadata(image, "", state)
assert.True(t, err == nil, "Metadata creation should have succeeded.")
// ensure the user-data key in metadata is updated with file content
assert.Equal(t, metadata["user-data"], content, "user-data field of the instance metadata should have been updated.")
}