Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integration tests #171

Merged
merged 7 commits into from
Aug 31, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,7 @@ required = [
[[override]]
name = "github.com/gophercloud/gophercloud"
revision = "8183543f90d1aef267a5ecc209f2e0715b355acb"

[[constraint]]
branch = "master"
name = "github.com/dlespiau/kube-test-harness"
13 changes: 13 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,19 @@ test:
@go test -v -covermode=count -coverprofile=coverage.out ./pkg/... ./cmd/...
@test -z $(COVERALLS_TOKEN) || goveralls -coverprofile=coverage.out -service=circle-ci

.PHONY: integration-test-dev
integration-test-dev: build

This comment was marked as abuse.

@go test -tags integration -v -timeout 21m ./tests/integration/... \
-args \
-eksctl.cluster=integration-test-dev \
-eksctl.create=false \
-eksctl.delete=false \
-eksctl.kubeconfig=$(HOME)/.kube/eksctl/clusters/integration-test-dev

.PHONY: integration-test
integration-test: build
@go test -tags integration -v -timeout 21m ./tests/integration/...

.PHONY: generated
generate:
@go generate ./pkg/eks ./pkg/eks/mocks
Expand Down
50 changes: 50 additions & 0 deletions pkg/testutils/aws/cloudformation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package aws

import (
"fmt"
"strings"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/cloudformation"
)

const (
errorMerssageTemplate = "Stack with id %s does not exist"
)

// StackExists checks to see if a CloudFormation stack exists
func StackExists(stackName string, session *session.Session) (bool, error) {
cfn := cloudformation.New(session)

input := &cloudformation.ListStackResourcesInput{
StackName: aws.String(stackName),
}
_, err := cfn.ListStackResources(input)

if err != nil {
// Check if its a not found error
errorMessage := fmt.Sprintf(errorMerssageTemplate, stackName)
if !strings.Contains(err.Error(), errorMessage) {
return false, err
}

return false, nil
}

return true, nil
}

// DeleteStack deletes a cloudformation stack
func DeleteStack(stackName string, session *session.Session) error {
cfn := cloudformation.New(session)

input := &cloudformation.DeleteStackInput{
StackName: aws.String(stackName),
}

_, err := cfn.DeleteStack(input)

return err

}
42 changes: 42 additions & 0 deletions pkg/testutils/aws/eks.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package aws

import (
"strings"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
awseks "github.com/aws/aws-sdk-go/service/eks"
)

// EksClusterExists checks if an EKS cluster exists in AWS
func EksClusterExists(clusterName string, session *session.Session) (bool, error) {
eks := awseks.New(session)

input := &awseks.DescribeClusterInput{
Name: aws.String(clusterName),
}
_, err := eks.DescribeCluster(input)

if err != nil {
// Check if its a not found error: ResourceNotFoundException
if !strings.Contains(err.Error(), awseks.ErrCodeResourceNotFoundException) {
return false, err
}

return false, nil
}

return true, nil
}

// EksClusterDelete deletes a EKS cluster with a given name
func EksClusterDelete(clusterName string, session *session.Session) error {
eks := awseks.New(session)

input := &awseks.DeleteClusterInput{
Name: aws.String(clusterName),
}

_, err := eks.DeleteCluster(input)
return err
}
15 changes: 15 additions & 0 deletions pkg/testutils/aws/session.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package aws

import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
)

func NewSession(region string) *session.Session {
config := aws.NewConfig()
config = config.WithRegion(region)
opts := session.Options{
Config: *config,
}
return session.Must(session.NewSessionWithOptions(opts))
}
52 changes: 52 additions & 0 deletions pkg/testutils/matchers/have_cfn_stack_matcher.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package matchers

import (
"errors"
"fmt"
"reflect"

"github.com/aws/aws-sdk-go/aws/session"
"github.com/onsi/gomega/types"
"github.com/weaveworks/eksctl/pkg/testutils/aws"
)

const (
errorMerssageTemplate = "Stack with id %s does not exist"
)

// HaveCfnStack returns a GoMega matcher that will check for the existence of an cloudformatioin stack
func HaveCfnStack(expectedStackName string) types.GomegaMatcher {
return &haveCfnStackMatcher{expectedStackName: expectedStackName}
}

type haveCfnStackMatcher struct {
expectedStackName string
stackNotFound bool
}

func (m *haveCfnStackMatcher) Match(actual interface{}) (success bool, err error) {
if actual == nil {
return false, errors.New("input is nil")
}

if reflect.TypeOf(actual).String() != "*session.Session" {
return false, errors.New("not a AWS session")
}

found, err := aws.StackExists(m.expectedStackName, actual.(*session.Session))

if err != nil {
return false, err
}

m.stackNotFound = !found
return found, nil
}

func (m *haveCfnStackMatcher) FailureMessage(actual interface{}) (message string) {
return fmt.Sprintf("Expected to find a Cloudformation stack named %s but it wasn't found", m.expectedStackName)
}

func (m *haveCfnStackMatcher) NegatedFailureMessage(_ interface{}) (message string) {
return fmt.Sprintf("Expected NOT to find a Cloudformation stack named %s but it found", m.expectedStackName)
}
97 changes: 97 additions & 0 deletions pkg/testutils/matchers/have_eks_cluster_matcher.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package matchers

import (
"errors"
"fmt"
"reflect"
"strings"

"github.com/onsi/gomega/types"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
awseks "github.com/aws/aws-sdk-go/service/eks"
)

// HaveEksCluster returns a GoMega matcher that will check for the existence of an EKS cluster
func HaveEksCluster(expectedName string, expectedStatus string, expectedversion string) types.GomegaMatcher {
return &haveEksClusterMatcher{expectedName: expectedName, expectedStatus: expectedStatus, expectedVersion: expectedversion}
}

type haveEksClusterMatcher struct {
expectedName string
expectedStatus string
expectedVersion string

clusterNotFound bool
versionMismatch bool
statusMismatch bool

actualVersion string
actualStatus string

region string
}

func (m *haveEksClusterMatcher) Match(actual interface{}) (success bool, err error) {
if actual == nil {
return false, errors.New("input is nil")
}

if reflect.TypeOf(actual).String() != "*session.Session" {
return false, errors.New("not a AWS session")
}

eks := awseks.New(actual.(*session.Session))

input := &awseks.DescribeClusterInput{
Name: aws.String(m.expectedName),
}
output, err := eks.DescribeCluster(input)

if err != nil {
// Check if its a not found error: ResourceNotFoundException
if !strings.Contains(err.Error(), awseks.ErrCodeResourceNotFoundException) {
return false, err
}

m.clusterNotFound = true
return false, nil
}

m.actualStatus = *output.Cluster.Status
if m.actualStatus != m.expectedStatus {
m.statusMismatch = true
return false, nil
}

m.actualVersion = *output.Cluster.Version
if m.actualVersion != m.expectedVersion {
m.versionMismatch = true
return false, nil
}

return true, nil
}

func (m *haveEksClusterMatcher) FailureMessage(actual interface{}) (message string) {
if m.statusMismatch {
return fmt.Sprintf("Expected EKS cluster status: %s to equal actual EKS cluster status: %s", m.expectedStatus, m.actualStatus)
}
if m.versionMismatch {
return fmt.Sprintf("Expected EKS cluster version: %s to equal actual EKS cluster version: %s", m.expectedVersion, m.actualVersion)
}

return fmt.Sprintf("Expected to find a cluster named %s but it wasn't found", m.expectedName)
}

func (m *haveEksClusterMatcher) NegatedFailureMessage(_ interface{}) (message string) {
if m.statusMismatch {
return fmt.Sprintf("Expected EKS cluster status: %s NOT to equal actual EKS cluster status: %s", m.expectedStatus, m.actualStatus)
}
if m.versionMismatch {
return fmt.Sprintf("Expected EKS cluster version: %s NOT to equal actual EKS cluster version: %s", m.expectedVersion, m.actualVersion)
}

return fmt.Sprintf("Expected NOT to find a cluster named %s but it found", m.expectedName)
}
35 changes: 35 additions & 0 deletions tests/integration/cleanup.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package integration

import (
"fmt"

"github.com/weaveworks/eksctl/pkg/testutils/aws"
)

func CleanupAws(clusterName string, region string) {
session := aws.NewSession(region)

if found, _ := aws.EksClusterExists(clusterName, session); found {
aws.EksClusterDelete(clusterName, session)
}

stackName := fmt.Sprintf("EKS-%s-DefaultNodeGroup", clusterName)
if found, _ := aws.StackExists(stackName, session); found {
aws.DeleteStack(stackName, session)
}

stackName = fmt.Sprintf("EKS-%s-VPC", clusterName)
if found, _ := aws.StackExists(stackName, session); found {
aws.DeleteStack(stackName, session)
}

stackName = fmt.Sprintf("EKS-%s-ControlPlane", clusterName)
if found, _ := aws.StackExists(stackName, session); found {
aws.DeleteStack(stackName, session)
}

stackName = fmt.Sprintf("EKS-%s-ServiceRole", clusterName)
if found, _ := aws.StackExists(stackName, session); found {
aws.DeleteStack(stackName, session)
}
}
Loading