2014-06-16 15:53:37 -04:00
|
|
|
package vagrantcloud
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2019-08-16 10:42:56 -04:00
|
|
|
"context"
|
2018-10-24 09:39:09 -04:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2017-06-07 13:40:31 -04:00
|
|
|
"os"
|
2019-08-16 10:42:56 -04:00
|
|
|
"strings"
|
2014-06-16 15:53:37 -04:00
|
|
|
"testing"
|
2016-07-30 20:10:16 -04:00
|
|
|
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2014-06-16 15:53:37 -04:00
|
|
|
)
|
|
|
|
|
2014-06-25 10:56:09 -04:00
|
|
|
func testGoodConfig() map[string]interface{} {
|
|
|
|
return map[string]interface{}{
|
|
|
|
"access_token": "foo",
|
|
|
|
"version_description": "bar",
|
|
|
|
"box_tag": "hashicorp/precise64",
|
|
|
|
"version": "0.5",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testBadConfig() map[string]interface{} {
|
|
|
|
return map[string]interface{}{
|
|
|
|
"access_token": "foo",
|
|
|
|
"box_tag": "baz",
|
|
|
|
"version_description": "bar",
|
|
|
|
}
|
2014-06-16 15:53:37 -04:00
|
|
|
}
|
|
|
|
|
2018-10-24 09:39:09 -04:00
|
|
|
func newSecureServer(token string, handler http.HandlerFunc) *httptest.Server {
|
|
|
|
token = fmt.Sprintf("Bearer %s", token)
|
|
|
|
return httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
|
|
|
|
if req.Header.Get("authorization") != token {
|
|
|
|
http.Error(rw, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if handler != nil {
|
|
|
|
handler(rw, req)
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
2019-03-05 08:37:37 -05:00
|
|
|
func newSelfSignedSslServer(token string, handler http.HandlerFunc) *httptest.Server {
|
|
|
|
token = fmt.Sprintf("Bearer %s", token)
|
|
|
|
return httptest.NewTLSServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
|
|
|
|
if req.Header.Get("authorization") != token {
|
|
|
|
http.Error(rw, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if handler != nil {
|
|
|
|
handler(rw, req)
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPostProcessor_Insecure_Ssl(t *testing.T) {
|
|
|
|
var p PostProcessor
|
|
|
|
server := newSelfSignedSslServer("foo", nil)
|
|
|
|
defer server.Close()
|
|
|
|
|
|
|
|
config := testGoodConfig()
|
|
|
|
config["vagrant_cloud_url"] = server.URL
|
|
|
|
config["insecure_skip_tls_verify"] = true
|
|
|
|
if err := p.Configure(config); err != nil {
|
2019-03-05 08:39:50 -05:00
|
|
|
t.Fatalf("Expected TLS to skip certificate validation: %s", err)
|
2019-03-05 08:37:37 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-07 13:40:31 -04:00
|
|
|
func TestPostProcessor_Configure_fromVagrantEnv(t *testing.T) {
|
|
|
|
var p PostProcessor
|
|
|
|
config := testGoodConfig()
|
2018-10-24 09:39:09 -04:00
|
|
|
server := newSecureServer("bar", nil)
|
|
|
|
defer server.Close()
|
|
|
|
config["vagrant_cloud_url"] = server.URL
|
2017-06-07 13:40:31 -04:00
|
|
|
config["access_token"] = ""
|
|
|
|
os.Setenv("VAGRANT_CLOUD_TOKEN", "bar")
|
|
|
|
defer func() {
|
|
|
|
os.Setenv("VAGRANT_CLOUD_TOKEN", "")
|
|
|
|
}()
|
|
|
|
|
|
|
|
if err := p.Configure(config); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.config.AccessToken != "bar" {
|
|
|
|
t.Fatalf("Expected to get token from VAGRANT_CLOUD_TOKEN env var. Got '%s' instead",
|
|
|
|
p.config.AccessToken)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPostProcessor_Configure_fromAtlasEnv(t *testing.T) {
|
|
|
|
var p PostProcessor
|
|
|
|
config := testGoodConfig()
|
|
|
|
config["access_token"] = ""
|
2018-10-24 09:39:09 -04:00
|
|
|
server := newSecureServer("foo", nil)
|
|
|
|
defer server.Close()
|
|
|
|
config["vagrant_cloud_url"] = server.URL
|
2017-06-07 13:40:31 -04:00
|
|
|
os.Setenv("ATLAS_TOKEN", "foo")
|
|
|
|
defer func() {
|
|
|
|
os.Setenv("ATLAS_TOKEN", "")
|
|
|
|
}()
|
|
|
|
|
|
|
|
if err := p.Configure(config); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.config.AccessToken != "foo" {
|
|
|
|
t.Fatalf("Expected to get token from ATLAS_TOKEN env var. Got '%s' instead",
|
|
|
|
p.config.AccessToken)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !p.warnAtlasToken {
|
|
|
|
t.Fatal("Expected warn flag to be set when getting token from atlas env var.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-25 10:56:09 -04:00
|
|
|
func TestPostProcessor_Configure_Good(t *testing.T) {
|
2018-10-24 09:39:09 -04:00
|
|
|
config := testGoodConfig()
|
|
|
|
server := newSecureServer("foo", nil)
|
|
|
|
defer server.Close()
|
|
|
|
config["vagrant_cloud_url"] = server.URL
|
2014-06-16 15:53:37 -04:00
|
|
|
var p PostProcessor
|
2018-10-24 09:39:09 -04:00
|
|
|
if err := p.Configure(config); err != nil {
|
2014-06-16 15:53:37 -04:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2014-06-25 10:56:09 -04:00
|
|
|
}
|
2014-06-16 15:53:37 -04:00
|
|
|
|
2014-06-25 10:56:09 -04:00
|
|
|
func TestPostProcessor_Configure_Bad(t *testing.T) {
|
2018-10-24 09:39:09 -04:00
|
|
|
config := testBadConfig()
|
|
|
|
server := newSecureServer("foo", nil)
|
|
|
|
defer server.Close()
|
|
|
|
config["vagrant_cloud_url"] = server.URL
|
2014-06-25 10:56:09 -04:00
|
|
|
var p PostProcessor
|
2018-10-24 09:39:09 -04:00
|
|
|
if err := p.Configure(config); err == nil {
|
2014-06-25 10:56:09 -04:00
|
|
|
t.Fatalf("should have err")
|
|
|
|
}
|
2014-06-16 15:53:37 -04:00
|
|
|
}
|
|
|
|
|
2019-08-16 10:42:56 -04:00
|
|
|
func TestPostProcessor_PostProcess_checkArtifactType(t *testing.T) {
|
|
|
|
artifact := &packer.MockArtifact{
|
|
|
|
BuilderIdValue: "invalid.builder",
|
|
|
|
}
|
|
|
|
|
|
|
|
config := testGoodConfig()
|
|
|
|
server := newSecureServer("foo", nil)
|
|
|
|
defer server.Close()
|
|
|
|
config["vagrant_cloud_url"] = server.URL
|
|
|
|
var p PostProcessor
|
|
|
|
|
|
|
|
p.Configure(config)
|
|
|
|
_, _, _, err := p.PostProcess(context.Background(), testUi(), artifact)
|
|
|
|
if !strings.Contains(err.Error(), "Unknown artifact type") {
|
|
|
|
t.Fatalf("Should error with message 'Unknown artifact type...' with BuilderId: %s", artifact.BuilderIdValue)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-16 15:53:37 -04:00
|
|
|
func testUi() *packer.BasicUi {
|
|
|
|
return &packer.BasicUi{
|
|
|
|
Reader: new(bytes.Buffer),
|
|
|
|
Writer: new(bytes.Buffer),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPostProcessor_ImplementsPostProcessor(t *testing.T) {
|
|
|
|
var _ packer.PostProcessor = new(PostProcessor)
|
|
|
|
}
|
|
|
|
|
2016-07-30 20:10:16 -04:00
|
|
|
func TestProviderFromBuilderName(t *testing.T) {
|
2014-06-16 15:53:37 -04:00
|
|
|
if providerFromBuilderName("foobar") != "foobar" {
|
|
|
|
t.Fatal("should copy unknown provider")
|
|
|
|
}
|
|
|
|
|
|
|
|
if providerFromBuilderName("vmware") != "vmware_desktop" {
|
|
|
|
t.Fatal("should convert provider")
|
|
|
|
}
|
|
|
|
}
|