2013-07-20 23:08:41 -04:00
|
|
|
package common
|
2013-05-22 01:28:41 -04:00
|
|
|
|
|
|
|
import (
|
2014-12-02 21:22:26 -05:00
|
|
|
"reflect"
|
2013-05-22 01:28:41 -04:00
|
|
|
"testing"
|
2014-12-02 21:22:26 -05:00
|
|
|
|
2020-11-19 15:17:11 -05:00
|
|
|
packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer"
|
2013-05-22 01:28:41 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestArtifact_Impl(t *testing.T) {
|
2020-11-19 15:17:11 -05:00
|
|
|
var _ packersdk.Artifact = new(Artifact)
|
2013-05-22 01:28:41 -04:00
|
|
|
}
|
|
|
|
|
2013-06-26 20:40:21 -04:00
|
|
|
func TestArtifactId(t *testing.T) {
|
|
|
|
expected := `east:foo,west:bar`
|
|
|
|
|
|
|
|
amis := make(map[string]string)
|
|
|
|
amis["east"] = "foo"
|
|
|
|
amis["west"] = "bar"
|
|
|
|
|
2013-07-22 01:46:11 -04:00
|
|
|
a := &Artifact{
|
|
|
|
Amis: amis,
|
|
|
|
}
|
|
|
|
|
2013-06-26 20:40:21 -04:00
|
|
|
result := a.Id()
|
2013-10-16 22:29:53 -04:00
|
|
|
if result != expected {
|
|
|
|
t.Fatalf("bad: %s", result)
|
|
|
|
}
|
2013-06-26 20:40:21 -04:00
|
|
|
}
|
|
|
|
|
2014-12-02 21:22:26 -05:00
|
|
|
func TestArtifactState_atlasMetadata(t *testing.T) {
|
|
|
|
a := &Artifact{
|
|
|
|
Amis: map[string]string{
|
|
|
|
"east": "foo",
|
|
|
|
"west": "bar",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := a.State("atlas.artifact.metadata")
|
|
|
|
expected := map[string]string{
|
|
|
|
"region.east": "foo",
|
|
|
|
"region.west": "bar",
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
|
|
|
t.Fatalf("bad: %#v", actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-22 01:28:41 -04:00
|
|
|
func TestArtifactString(t *testing.T) {
|
|
|
|
expected := `AMIs were created:
|
|
|
|
east: foo
|
2017-08-24 14:57:12 -04:00
|
|
|
west: bar
|
|
|
|
`
|
2013-05-22 01:28:41 -04:00
|
|
|
|
|
|
|
amis := make(map[string]string)
|
|
|
|
amis["east"] = "foo"
|
|
|
|
amis["west"] = "bar"
|
|
|
|
|
2013-07-22 01:46:11 -04:00
|
|
|
a := &Artifact{Amis: amis}
|
2013-05-22 01:28:41 -04:00
|
|
|
result := a.String()
|
2013-10-16 22:29:53 -04:00
|
|
|
if result != expected {
|
|
|
|
t.Fatalf("bad: %s", result)
|
|
|
|
}
|
2013-05-22 01:28:41 -04:00
|
|
|
}
|
2020-01-16 06:04:03 -05:00
|
|
|
|
|
|
|
func TestArtifactState(t *testing.T) {
|
|
|
|
expectedData := "this is the data"
|
|
|
|
artifact := &Artifact{
|
|
|
|
StateData: map[string]interface{}{"state_data": expectedData},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Valid state
|
|
|
|
result := artifact.State("state_data")
|
|
|
|
if result != expectedData {
|
|
|
|
t.Fatalf("Bad: State data was %s instead of %s", result, expectedData)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Invalid state
|
|
|
|
result = artifact.State("invalid_key")
|
|
|
|
if result != nil {
|
|
|
|
t.Fatalf("Bad: State should be nil for invalid state data name")
|
|
|
|
}
|
2020-01-30 05:27:58 -05:00
|
|
|
|
|
|
|
// Nil StateData should not fail and should return nil
|
|
|
|
artifact = &Artifact{}
|
|
|
|
result = artifact.State("key")
|
|
|
|
if result != nil {
|
|
|
|
t.Fatalf("Bad: State should be nil for nil StateData")
|
|
|
|
}
|
2020-01-16 06:04:03 -05:00
|
|
|
}
|