2014-12-09 18:01:46 -05:00
|
|
|
package atlas
|
2014-12-09 18:00:03 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2014-12-09 18:00:03 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestPostProcessorConfigure(t *testing.T) {
|
2015-02-03 18:13:15 -05:00
|
|
|
currentEnv := os.Getenv("ATLAS_TOKEN")
|
2015-02-03 18:43:53 -05:00
|
|
|
os.Setenv("ATLAS_TOKEN", "")
|
2015-02-03 18:13:15 -05:00
|
|
|
defer os.Setenv("ATLAS_TOKEN", currentEnv)
|
|
|
|
|
2014-12-09 18:00:03 -05:00
|
|
|
var p PostProcessor
|
|
|
|
if err := p.Configure(validDefaults()); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.client == nil {
|
|
|
|
t.Fatal("should have client")
|
|
|
|
}
|
|
|
|
if p.client.Token != "" {
|
|
|
|
t.Fatal("should not have token")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPostProcessorConfigure_buildId(t *testing.T) {
|
|
|
|
defer os.Setenv(BuildEnvKey, os.Getenv(BuildEnvKey))
|
|
|
|
os.Setenv(BuildEnvKey, "5")
|
|
|
|
|
|
|
|
var p PostProcessor
|
|
|
|
if err := p.Configure(validDefaults()); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.config.buildId != 5 {
|
|
|
|
t.Fatalf("bad: %#v", p.config.buildId)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-25 14:52:21 -04:00
|
|
|
func TestPostProcessorConfigure_compileId(t *testing.T) {
|
|
|
|
defer os.Setenv(CompileEnvKey, os.Getenv(CompileEnvKey))
|
|
|
|
os.Setenv(CompileEnvKey, "5")
|
|
|
|
|
|
|
|
var p PostProcessor
|
|
|
|
if err := p.Configure(validDefaults()); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.config.compileId != 5 {
|
|
|
|
t.Fatalf("bad: %#v", p.config.compileId)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-09 18:00:03 -05:00
|
|
|
func TestPostProcessorMetadata(t *testing.T) {
|
|
|
|
var p PostProcessor
|
|
|
|
if err := p.Configure(validDefaults()); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
artifact := new(packer.MockArtifact)
|
|
|
|
metadata := p.metadata(artifact)
|
|
|
|
if len(metadata) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", metadata)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPostProcessorMetadata_artifact(t *testing.T) {
|
|
|
|
config := validDefaults()
|
|
|
|
config["metadata"] = map[string]string{
|
|
|
|
"foo": "bar",
|
|
|
|
}
|
|
|
|
|
|
|
|
var p PostProcessor
|
|
|
|
if err := p.Configure(config); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
artifact := new(packer.MockArtifact)
|
|
|
|
artifact.StateValues = map[string]interface{}{
|
|
|
|
ArtifactStateMetadata: map[interface{}]interface{}{
|
|
|
|
"bar": "baz",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
metadata := p.metadata(artifact)
|
|
|
|
expected := map[string]string{
|
|
|
|
"foo": "bar",
|
|
|
|
"bar": "baz",
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(metadata, expected) {
|
|
|
|
t.Fatalf("bad: %#v", metadata)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPostProcessorMetadata_config(t *testing.T) {
|
|
|
|
config := validDefaults()
|
|
|
|
config["metadata"] = map[string]string{
|
|
|
|
"foo": "bar",
|
|
|
|
}
|
|
|
|
|
|
|
|
var p PostProcessor
|
|
|
|
if err := p.Configure(config); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
artifact := new(packer.MockArtifact)
|
|
|
|
metadata := p.metadata(artifact)
|
|
|
|
expected := map[string]string{
|
|
|
|
"foo": "bar",
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(metadata, expected) {
|
|
|
|
t.Fatalf("bad: %#v", metadata)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPostProcessorType(t *testing.T) {
|
|
|
|
var p PostProcessor
|
|
|
|
if err := p.Configure(validDefaults()); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
artifact := new(packer.MockArtifact)
|
|
|
|
actual := p.artifactType(artifact)
|
|
|
|
if actual != "foo" {
|
|
|
|
t.Fatalf("bad: %#v", actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPostProcessorType_artifact(t *testing.T) {
|
|
|
|
var p PostProcessor
|
|
|
|
if err := p.Configure(validDefaults()); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
artifact := new(packer.MockArtifact)
|
|
|
|
artifact.StateValues = map[string]interface{}{
|
|
|
|
ArtifactStateType: "bar",
|
|
|
|
}
|
|
|
|
actual := p.artifactType(artifact)
|
|
|
|
if actual != "bar" {
|
|
|
|
t.Fatalf("bad: %#v", actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func validDefaults() map[string]interface{} {
|
|
|
|
return map[string]interface{}{
|
|
|
|
"artifact": "mitchellh/test",
|
|
|
|
"artifact_type": "foo",
|
|
|
|
"test": true,
|
|
|
|
}
|
|
|
|
}
|