Skip to content

Commit

Permalink
tests: Initial functional tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ipmb committed Dec 14, 2020
1 parent 5d8a035 commit 4984ae1
Show file tree
Hide file tree
Showing 3 changed files with 181 additions and 11 deletions.
36 changes: 36 additions & 0 deletions .github/workflows/functional_tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: functional-tests

on:
push:

jobs:
functional-tests:
runs-on: ubuntu-latest
steps:
-
name: Checkout
uses: actions/checkout@v2
-
name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.15
-
name: Build
run: go build
-
name: Create account
run: ./apppack create account --dockerhub-username $DOCKERHUB_USERNAME --dockerhub-access-token $DOCKERHUB_ACCESS_TOKEN
env:
DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }}
DOCKERHUB_ACCESS_TOKEN: ${{ secrets.DOCKERHUB_ACCESS_TOKEN }}
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
AWS_REGION: us-east-1
-
name: Destroy account
run: yes yes | ./apppack destroy account
env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
AWS_REGION: us-east-1
25 changes: 14 additions & 11 deletions cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"fmt"
"math/rand"
"net/url"
"os"
"strings"
"time"

Expand Down Expand Up @@ -300,7 +299,13 @@ func askForMissingArgs(cmd *cobra.Command, overrideQuestions *map[string]*survey
var questions []*survey.Question
cmd.LocalFlags().VisitAll(func(flag *pflag.Flag) {
if flag.Name != "help" {
addQuestionFromFlag(flag, &questions, (*overrideQuestions)[flag.Name])
var override *survey.Question
if overrideQuestions != nil {
override = (*overrideQuestions)[flag.Name]
} else {
override = nil
}
addQuestionFromFlag(flag, &questions, override)
}
})
answers := make(map[string]interface{})
Expand Down Expand Up @@ -396,8 +401,7 @@ var accountCmd = &cobra.Command{
})

if err == nil {
printError("Account already exists")
os.Exit(1)
checkErr(fmt.Errorf("account already exists"))
}
if createChangeSet {
fmt.Println("Creating Cloudformation Change Set for account-level resources...")
Expand All @@ -409,13 +413,6 @@ var accountCmd = &cobra.Command{
{Key: aws.String("paaws:account"), Value: aws.String("true")},
{Key: aws.String("paaws"), Value: aws.String("true")},
}
_, err = ssmSvc.PutParameter(&ssm.PutParameterInput{
Name: aws.String("/paaws/account/dockerhub-username"),
Value: getArgValue(cmd, answers, "dockerhub-username"),
Type: aws.String("SecureString"),
Tags: tags,
})
checkErr(err)
_, err = ssmSvc.PutParameter(&ssm.PutParameterInput{
Name: aws.String("/paaws/account/dockerhub-access-token"),
Value: getArgValue(cmd, answers, "dockerhub-access-token"),
Expand All @@ -436,6 +433,10 @@ var accountCmd = &cobra.Command{
ParameterKey: aws.String("PaawsRoleExternalId"),
ParameterValue: aws.String(strings.Replace(uuid.New().String(), "-", "", -1)),
},
{
ParameterKey: aws.String("DockerhubUsername"),
ParameterValue: getArgValue(cmd, answers, "dockerhub-username"),
},
},
Capabilities: []*string{aws.String("CAPABILITY_IAM")},
Tags: cfnTags,
Expand All @@ -459,6 +460,8 @@ var accountCmd = &cobra.Command{
statusURL := fmt.Sprintf("https://console.aws.amazon.com/cloudformation/home#/stacks/events?stackId=%s", url.QueryEscape(*stack.Stacks[0].StackId))
if *stack.Stacks[0].StackStatus != "CREATE_COMPLETE" {
checkErr(fmt.Errorf("Stack creation Failed.\nView status at %s", statusURL))
} else {
printSuccess("AppPack account created")
}
}

Expand Down
131 changes: 131 additions & 0 deletions cmd/destroy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
/*
Copyright © 2020 NAME HERE <EMAIL ADDRESS>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cmd

import (
"encoding/json"
"fmt"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/cloudformation"
"github.com/aws/aws-sdk-go/service/ssm"
"github.com/logrusorgru/aurora"
"github.com/spf13/cobra"
)

type accountDetails struct {
StackID string `json:"stack_id"`
}

// destroyCmd represents the destroy command
var destroyCmd = &cobra.Command{
Use: "destroy",
Short: "Destroy AWS resources used by AppPack",
Long: `Destroy AWS resources used by AppPack`,
}

// destroyAccountCmd represents the destroy command
var destroyAccountCmd = &cobra.Command{
Use: "account",
Short: "Destroy AWS resources used by your AppPack account",
Long: `Destroy AWS resources used by your AppPack account`,
Run: func(cmd *cobra.Command, args []string) {
Spinner.Start()
sess := session.Must(session.NewSession())
ssmSvc := ssm.New(sess)
paramOutput, err := ssmSvc.GetParameter(&ssm.GetParameterInput{
Name: aws.String("/paaws/account"),
})
checkErr(err)
var account accountDetails
err = json.Unmarshal([]byte(*paramOutput.Parameter.Value), &account)
checkErr(err)
cfnSvc := cloudformation.New(sess)
_, err = cfnSvc.DescribeStacks(&cloudformation.DescribeStacksInput{
StackName: &account.StackID,
})
checkErr(err)
Spinner.Stop()
var confirm string
fmt.Printf("Are you sure you want to delete your AppPack Account Stack\n%s? yes/[%s]\n", aurora.Faint(account.StackID), aurora.Bold("no"))
fmt.Scanln(&confirm)
if confirm != "yes" {
checkErr(fmt.Errorf("aborting due to user input"))
}
Spinner.Start()
_, err = cfnSvc.DeleteStack(&cloudformation.DeleteStackInput{
StackName: &account.StackID,
})
checkErr(err)
err = cfnSvc.WaitUntilStackDeleteComplete(&cloudformation.DescribeStacksInput{
StackName: &account.StackID,
})
_, err1 := ssmSvc.DeleteParameter(&ssm.DeleteParameterInput{
Name: aws.String("/paaws/account/dockerhub-access-token"),
})
Spinner.Stop()
checkErr(err)
checkErr(err1)
printSuccess("AppPack account deleted")
},
}

// destroyAccountCmd represents the destroy command
var destroyClusterCmd = &cobra.Command{
Use: "cluster",
Short: "Destroy AWS resources used by the AppPack cluster",
Long: `Destroy AWS resources used by the AppPack cluster`,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
clusterName := args[0]
Spinner.Start()
sess := session.Must(session.NewSession())
stackOutput, err := stackOutputFromDDBItem(sess, fmt.Sprintf("CLUSTER#%s", clusterName))
checkErr(err)
stackID := (*stackOutput)["stack_id"]
cfnSvc := cloudformation.New(sess)
_, err = cfnSvc.DescribeStacks(&cloudformation.DescribeStacksInput{
StackName: stackID,
})
checkErr(err)
Spinner.Stop()
var confirm string
fmt.Printf("Are you sure you want to delete your AppPack Cluster %s\n%s? yes/[%s]\n", clusterName, aurora.Faint(*stackID), aurora.Bold("no"))
fmt.Scanln(&confirm)
if confirm != "yes" {
checkErr(fmt.Errorf("aborting due to user input"))
}
Spinner.Start()
_, err = cfnSvc.DeleteStack(&cloudformation.DeleteStackInput{
StackName: stackID,
})
checkErr(err)
err = cfnSvc.WaitUntilStackDeleteComplete(&cloudformation.DescribeStacksInput{
StackName: stackID,
})
checkErr(err)
Spinner.Stop()
printSuccess(fmt.Sprintf("AppPack cluster %s destroyed", clusterName))
},
}

func init() {
rootCmd.AddCommand(destroyCmd)

destroyCmd.AddCommand(destroyAccountCmd)
destroyCmd.AddCommand(destroyClusterCmd)
}

0 comments on commit 4984ae1

Please sign in to comment.