Skip to content

Commit

Permalink
first smoke test for install
Browse files Browse the repository at this point in the history
  • Loading branch information
csquared committed Sep 23, 2015
1 parent 71a5564 commit 94411a7
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 36 deletions.
5 changes: 3 additions & 2 deletions api/controllers/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ import (
"testing"

"github.com/convox/rack/api/controllers"
"github.com/convox/rack/test"
"github.com/stretchr/testify/assert"
)

// Note: these tests don't use the api helpers to ensure a naked
// client can connect

func TestNoPassword(t *testing.T) {
aws := stubAws(DescribeConvoxStackCycle("convox-test"))
aws := stubAws(test.DescribeConvoxStackCycle("convox-test"))
defer aws.Close()
defer os.Setenv("RACK", os.Getenv("RACK"))

Expand All @@ -25,7 +26,7 @@ func TestNoPassword(t *testing.T) {

func TestBasicAuth(t *testing.T) {
assert := assert.New(t)
aws := stubAws(DescribeConvoxStackCycle("convox-test"))
aws := stubAws(test.DescribeConvoxStackCycle("convox-test"))
defer aws.Close()
defer os.Setenv("PASSWORD", os.Getenv("PASSWORD"))
defer os.Setenv("RACK", os.Getenv("RACK"))
Expand Down
94 changes: 60 additions & 34 deletions cmd/convox/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/defaults"
"github.com/aws/aws-sdk-go/service/cloudformation"
"github.com/codegangsta/cli"
"github.com/convox/rack/cmd/convox/stdcli"
Expand Down Expand Up @@ -57,7 +58,7 @@ var FormationUrl = "http://convox.s3.amazonaws.com/release/%s/formation.json"
var isDevelopment = false

// https://docs.aws.amazon.com/general/latest/gr/rande.html#lambda_region
var lambdaRegions = map[string]bool{"us-east-1": true, "us-west-2": true, "eu-west-1": true, "ap-northeast-1": true}
var lambdaRegions = map[string]bool{"us-east-1": true, "us-west-2": true, "eu-west-1": true, "ap-northeast-1": true, "test": true}

func init() {
rand.Seed(time.Now().UTC().UnixNano())
Expand Down Expand Up @@ -231,6 +232,12 @@ func cmdInstall(c *cli.Context) {
TemplateURL: aws.String(formationUrl),
})

// NOTE: we start making lots of network requests here
if *defaults.DefaultConfig.Region == "test" {
fmt.Println(*res.StackId)
return
}

if err != nil {
sendMixpanelEvent(fmt.Sprintf("convox-install-error"), err.Error())

Expand Down Expand Up @@ -419,6 +426,7 @@ func waitForCompletion(stack string, CloudFormation *cloudformation.CloudFormati
var events = map[string]bool{}

func displayProgress(stack string, CloudFormation *cloudformation.CloudFormation, isDeleting bool) error {

res, err := CloudFormation.DescribeStackEvents(&cloudformation.DescribeStackEventsInput{
StackName: aws.String(stack),
})
Expand Down Expand Up @@ -583,43 +591,22 @@ func readCredentials(c *cli.Context) (creds AwsCredentials, err error) {
creds.Secret = os.Getenv("AWS_SECRET_ACCESS_KEY")
creds.Session = os.Getenv("AWS_SESSION_TOKEN")

// read credentials from credentials.csv file
// note: takes precendence over ENV
if len(c.Args()) > 0 {
credentialsCsvFileName := c.Args()[0]
credsFile, err := ioutil.ReadFile(credentialsCsvFileName)

if err != nil {
return creds, err
}

r := csv.NewReader(bytes.NewReader(credsFile))
records, err := r.ReadAll()
if err != nil {
return creds, err
}
if os.Getenv("AWS_ENDPOINT_URL") != "" {
url := os.Getenv("AWS_ENDPOINT_URL")
defaults.DefaultConfig.Endpoint = &url
}

if len(records) == 2 && len(records[1]) == 3 {
creds.Access = records[1][1]
creds.Secret = records[1][2]
}
if len(c.Args()) > 0 {
fileName := c.Args()[0]
creds, err = readCredentialsFromFile(fileName)
} else if !terminal.IsTerminal(int(os.Stdin.Fd())) {
// read from STDIN if not a terminal
stdin, err := ioutil.ReadAll(os.Stdin)
if err != nil {
return creds, err
}
var input struct {
Credentials AwsCredentials
}
err = json.Unmarshal(stdin, &input)
if err != nil {
return creds, err
}
creds = input.Credentials
creds, err = readCredentialsFromSTDIN()
}

if err != nil {
return creds, err
}

// read credentials interactively
if creds.Access == "" || creds.Secret == "" {
fmt.Println(CredentialsMessage)

Expand Down Expand Up @@ -649,3 +636,42 @@ func readCredentials(c *cli.Context) (creds AwsCredentials, err error) {

return
}

func readCredentialsFromFile(credentialsCsvFileName string) (creds AwsCredentials, err error) {
credsFile, err := ioutil.ReadFile(credentialsCsvFileName)

if err != nil {
return creds, err
}

r := csv.NewReader(bytes.NewReader(credsFile))
records, err := r.ReadAll()
if err != nil {
return creds, err
}

if len(records) == 2 && len(records[1]) == 3 {
creds.Access = records[1][1]
creds.Secret = records[1][2]
}

return
}

func readCredentialsFromSTDIN() (creds AwsCredentials, err error) {
stdin, err := ioutil.ReadAll(os.Stdin)
if err != nil {
return creds, err
}

var input struct {
Credentials AwsCredentials
}
err = json.Unmarshal(stdin, &input)

if err != nil {
return creds, err
}

return input.Credentials, err
}
47 changes: 47 additions & 0 deletions cmd/convox/install_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package main

import (
"net/http/httptest"
"testing"

"github.com/aws/aws-sdk-go/aws/defaults"
"github.com/convox/rack/api/awsutil"
"github.com/convox/rack/test"
"github.com/convox/release/version"
)

func TestConvoxInstallSTDINCredentials(t *testing.T) {
stackId := "arn:aws:cloudformation:us-east-1:123456789:stack/MyStack/aaf549a0-a413-11df-adb3-5081b3858e83"
cycles := []awsutil.Cycle{
awsutil.Cycle{
awsutil.Request{"/", "", "/./"},
awsutil.Response{200, `<CreateStackResult><StackId>` + stackId + `</StackId></CreateStackResult>`},
},
awsutil.Cycle{
awsutil.Request{"/", "", ""},
awsutil.Response{200, ""},
},
}

handler := awsutil.NewHandler(cycles)
s := httptest.NewServer(handler)
defaults.DefaultConfig.Endpoint = &s.URL

defer s.Close()

latest, _ := version.Latest()

test.Runs(t,
test.ExecRun{
Command: "convox install",
Exit: 0,
Env: map[string]string{"AWS_ENDPOINT_URL": s.URL, "AWS_REGION": "test"},
Stdin: `{"Credentials":{"AccessKeyId":"FOO","SecretAccessKey":"BAR","Expiration":"2015-09-17T14:09:41Z"}}`,
Stdout: Banner + "\nInstalling Convox (" + latest + ")...\n" + stackId + "\n",
},
)
}

func TestConvoxInstallFileCredentials(t *testing.T) {

}

0 comments on commit 94411a7

Please sign in to comment.