Skip to content

Commit

Permalink
Merge pull request #121 from richardcase/testing
Browse files Browse the repository at this point in the history
Add some tests
  • Loading branch information
errordeveloper authored Jul 18, 2018
2 parents b7c60b6 + d859ea6 commit 253542f
Show file tree
Hide file tree
Showing 249 changed files with 231,521 additions and 118 deletions.
110 changes: 109 additions & 1 deletion Gopkg.lock

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

13 changes: 13 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
required = [
"github.com/heptio/authenticator/cmd/heptio-authenticator-aws",
"github.com/vektra/mockery/cmd/mockery",
]

[[override]]
Expand Down Expand Up @@ -59,3 +60,15 @@ required = [
go-tests = true
unused-packages = true
non-go = true

[[constraint]]
branch = "master"
name = "github.com/vektra/mockery"

[[constraint]]
name = "github.com/stretchr/testify"
version = "1.2.2"

[[constraint]]
name = "github.com/onsi/ginkgo"
version = "1.5.0"
9 changes: 9 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,19 @@ test:
update-bindata:
go generate ./pkg/eks


.PHONY: install-bindata
install-bindata:
go get -u github.com/jteeuwen/go-bindata/...

.PHONY: update-mockery
update-mockery:
go generate ./pkg/eks/mocks

.PHONY: install-mockery
install-mockery:
go get -u github.com/vektra/mockery/cmd/mockery

.PHONY: eksctl-build-image
eksctl-build-image:
@-docker pull $(EKSCTL_BUILD_IMAGE)
Expand Down
74 changes: 45 additions & 29 deletions pkg/eks/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,37 @@ var DefaultWaitTimeout = 20 * time.Minute

type ClusterProvider struct {
// core fields used for config and AWS APIs
cfg *ClusterConfig
svc *providerServices

Spec *ClusterConfig
Provider Provider
// informative fields, i.e. used as outputs
iamRoleARN string
sessionCreds *credentials.Credentials
Status *ProviderStatus
}

type Provider interface {
CloudFormation() cloudformationiface.CloudFormationAPI
EKS() eksiface.EKSAPI
EC2() ec2iface.EC2API
STS() stsiface.STSAPI
}

type providerServices struct {
type ProviderServices struct {
cfn cloudformationiface.CloudFormationAPI
eks eksiface.EKSAPI
ec2 ec2iface.EC2API
sts stsiface.STSAPI
}

func (p ProviderServices) CloudFormation() cloudformationiface.CloudFormationAPI { return p.cfn }

func (p ProviderServices) EKS() eksiface.EKSAPI { return p.eks }
func (p ProviderServices) EC2() ec2iface.EC2API { return p.ec2 }
func (p ProviderServices) STS() stsiface.STSAPI { return p.sts }

type ProviderStatus struct {
iamRoleARN string
sessionCreds *credentials.Credentials
}

// simple config, to be replaced with Cluster API
type ClusterConfig struct {
Region string
Expand Down Expand Up @@ -88,49 +104,49 @@ type AddonIAM struct {
}

func New(clusterConfig *ClusterConfig) *ClusterProvider {

// Create a new session and save credentials for possible
// later re-use if overriding sessions due to custom URL
s := newSession(clusterConfig, "", nil)

c := &ClusterProvider{
cfg: clusterConfig,
svc: &providerServices{
cfn: cloudformation.New(s),
eks: eks.New(s),
ec2: ec2.New(s),
sts: sts.New(s),
},
provider := &ProviderServices{
cfn: cloudformation.New(s),
eks: eks.New(s),
ec2: ec2.New(s),
sts: sts.New(s),
}

status := &ProviderStatus{
sessionCreds: s.Config.Credentials,
}

// override sessions if any custom endpoints specified
if endpoint, ok := os.LookupEnv("AWS_CLOUDFORMATION_ENDPOINT"); ok {
logger.Debug("Setting CloudFormation endpoint to %s", endpoint)
s := newSession(clusterConfig, endpoint, c.sessionCreds)
c.svc.cfn = cloudformation.New(s)
provider.cfn = cloudformation.New(newSession(clusterConfig, endpoint, status.sessionCreds))
}
if endpoint, ok := os.LookupEnv("AWS_EKS_ENDPOINT"); ok {
logger.Debug("Setting EKS endpoint to %s", endpoint)
s := newSession(clusterConfig, endpoint, c.sessionCreds)
c.svc.eks = eks.New(s)
provider.eks = eks.New(newSession(clusterConfig, endpoint, status.sessionCreds))
}
if endpoint, ok := os.LookupEnv("AWS_EC2_ENDPOINT"); ok {
logger.Debug("Setting EC2 endpoint to %s", endpoint)
s := newSession(clusterConfig, endpoint, c.sessionCreds)
c.svc.ec2 = ec2.New(s)
provider.ec2 = ec2.New(newSession(clusterConfig, endpoint, status.sessionCreds))

}
if endpoint, ok := os.LookupEnv("AWS_STS_ENDPOINT"); ok {
logger.Debug("Setting STS endpoint to %s", endpoint)
s := newSession(clusterConfig, endpoint, c.sessionCreds)
c.svc.sts = sts.New(s)
provider.sts = sts.New(newSession(clusterConfig, endpoint, status.sessionCreds))
}

return c
return &ClusterProvider{
Spec: clusterConfig,
Provider: provider,
Status: status,
}
}

func (c *ClusterProvider) GetCredentialsEnv() ([]string, error) {
creds, err := c.sessionCreds.Get()
creds, err := c.Status.sessionCreds.Get()
if err != nil {
return nil, errors.Wrap(err, "getting effective credentials")
}
Expand All @@ -144,16 +160,16 @@ func (c *ClusterProvider) GetCredentialsEnv() ([]string, error) {
func (c *ClusterProvider) CheckAuth() error {
{
input := &sts.GetCallerIdentityInput{}
output, err := c.svc.sts.GetCallerIdentity(input)
output, err := c.Provider.STS().GetCallerIdentity(input)
if err != nil {
return errors.Wrap(err, "checking AWS STS access – cannot get role ARN for current session")
}
c.iamRoleARN = *output.Arn
logger.Debug("role ARN for the current session is %q", c.iamRoleARN)
c.Status.iamRoleARN = *output.Arn
logger.Debug("role ARN for the current session is %q", c.Status.iamRoleARN)
}
{
input := &cloudformation.ListStacksInput{}
if _, err := c.svc.cfn.ListStacks(input); err != nil {
if _, err := c.Provider.CloudFormation().ListStacks(input); err != nil {
return errors.Wrap(err, "checking AWS CloudFormation access – cannot list stacks")
}
}
Expand Down
28 changes: 28 additions & 0 deletions pkg/eks/api_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package eks_test

import (
"github.com/aws/aws-sdk-go/service/cloudformation/cloudformationiface"
"github.com/aws/aws-sdk-go/service/ec2/ec2iface"
"github.com/aws/aws-sdk-go/service/eks/eksiface"
"github.com/aws/aws-sdk-go/service/sts/stsiface"
"github.com/weaveworks/eksctl/pkg/eks/mocks"
)

type MockProvider struct {
cfn *mocks.CloudFormationAPI
eks *mocks.EKSAPI
ec2 *mocks.EC2API
sts *mocks.STSAPI
}

func (m MockProvider) CloudFormation() cloudformationiface.CloudFormationAPI { return m.cfn }
func (m MockProvider) mockCloudFormation() *mocks.CloudFormationAPI {
return m.CloudFormation().(*mocks.CloudFormationAPI)
}

func (m MockProvider) EKS() eksiface.EKSAPI { return m.eks }
func (m MockProvider) mockEKS() *mocks.EKSAPI { return m.EKS().(*mocks.EKSAPI) }
func (m MockProvider) EC2() ec2iface.EC2API { return m.ec2 }
func (m MockProvider) mockEC2() *mocks.EC2API { return m.EC2().(*mocks.EC2API) }
func (m MockProvider) STS() stsiface.STSAPI { return m.sts }
func (m MockProvider) mockSTS() *mocks.STSAPI { return m.STS().(*mocks.STSAPI) }
Loading

0 comments on commit 253542f

Please sign in to comment.