Skip to content

Commit

Permalink
Add outputs command and test
Browse files Browse the repository at this point in the history
  • Loading branch information
josh- authored and aidansteele committed Jun 17, 2020
1 parent fb02646 commit 22e85ab
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 0 deletions.
51 changes: 51 additions & 0 deletions cmd/outputs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright © 2017 Aidan Steele <[email protected]>
//
// 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 (
"github.com/aws/aws-sdk-go/service/cloudformation"
"github.com/aws/aws-sdk-go/service/sts"
"github.com/glassechidna/stackit/cmd/honey"
"github.com/glassechidna/stackit/pkg/stackit"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

var outputsCmd = &cobra.Command{
Use: "outputs",
Short: "Prints a given stack's Outputs",
Run: func(cmd *cobra.Command, args []string) {
region := viper.GetString("region")
profile := viper.GetString("profile")
stackName := viper.GetString("stack-name")

sess := awsSession(profile, region)
sit := stackit.NewStackit(cloudformation.New(sess), sts.New(sess))

ctx, end := honey.RootContext()
defer end()

stack, _ := sit.Describe(ctx, stackName)
if stack == nil {
return
}

sit.PrintOutputs(ctx, *stack.StackId, cmd.OutOrStdout())
},
}

func init() {
RootCmd.AddCommand(outputsCmd)
}
56 changes: 56 additions & 0 deletions cmd/outputs_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package cmd

import (
"bytes"
"io"
"os"
"regexp"
"testing"

"github.com/stretchr/testify/assert"
)

func TestOutputs(t *testing.T) {
if testing.Short() {
t.Skip("skip e2e tests in short mode")
}

buf := &bytes.Buffer{}

t.Run("up", func(t *testing.T) {
RootCmd.SetArgs([]string{
"up",
"--stack-name", "test-stack-s3",
"--template", "../sample/s3.yml",
})
_ = RootCmd.Execute()
})

buf.Reset()

t.Run("outputs", func(t *testing.T) {
RootCmd.SetArgs([]string{
"outputs",
"--stack-name", "test-stack-s3",
})

buf := &bytes.Buffer{}
out := io.MultiWriter(buf, os.Stderr)
RootCmd.SetOutput(out)

_ = RootCmd.Execute()

assert.Regexp(t, regexp.MustCompile(`\{
"S3BucketName": "test-stack-s3-s3bucket-[a-z0-9]*"
\}
`), buf.String())
})

t.Run("down", func(t *testing.T) {
RootCmd.SetArgs([]string{
"down",
"--stack-name", "test-stack-s3",
})
_ = RootCmd.Execute()
})
}
8 changes: 8 additions & 0 deletions sample/s3.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
AWSTemplateFormatVersion: 2010-09-09
Resources:
S3Bucket:
Type: AWS::S3::Bucket
Outputs:
S3BucketName:
Value: !Ref S3Bucket
Description: S3 Bucket name

0 comments on commit 22e85ab

Please sign in to comment.