Skip to content

Commit

Permalink
Merge pull request #16 from one2nc/test_cases
Browse files Browse the repository at this point in the history
Add Test Cases
  • Loading branch information
chinmay185 authored Apr 6, 2023
2 parents aeae019 + e957f41 commit 332cfe3
Show file tree
Hide file tree
Showing 46 changed files with 1,533 additions and 18 deletions.
8 changes: 7 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ GO_FLAGS ?=
NAME := cloudlens
OUTPUT_BIN ?= execs/${NAME}
PACKAGE := github.com/one2nc/$(NAME)
VERSION = v0.1.2
VERSION = v0.1.3
GIT_REV ?= $(shell git rev-parse --short HEAD)
SOURCE_DATE_EPOCH ?= $(shell date +%s)
ifeq ($(shell uname), Darwin)
Expand All @@ -18,3 +18,9 @@ build:

run: build
./execs/cloudlens

setup:
docker-compose up -d

setup-down:
docker ps -a --format "{{.ID}} {{.Names}}" | grep cloudlens| awk '{print $$1}'| xargs docker stop | xargs docker rm -v
6 changes: 5 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ require (
github.com/derailed/tview v0.7.2
github.com/fvbommel/sortorder v1.0.2
github.com/gdamore/tcell/v2 v2.5.4
github.com/rs/zerolog v1.28.0
github.com/rs/zerolog v1.29.0
github.com/spf13/cobra v1.6.1
)

Expand Down Expand Up @@ -38,6 +38,7 @@ require (
github.com/aymanbagabas/go-osc52 v1.0.3 // indirect
github.com/aymerick/douceur v0.2.0 // indirect
github.com/cloudflare/circl v1.1.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dlclark/regexp2 v1.4.0 // indirect
github.com/fatih/color v1.14.1 // indirect
github.com/golang/protobuf v1.5.2 // indirect
Expand All @@ -48,13 +49,16 @@ require (
github.com/muesli/reflow v0.3.0 // indirect
github.com/muesli/termenv v0.13.0 // indirect
github.com/olekukonko/tablewriter v0.0.5 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/testify v1.8.2 // indirect
github.com/yuin/goldmark v1.5.2 // indirect
github.com/yuin/goldmark-emoji v1.0.1 // indirect
golang.org/x/crypto v0.7.0 // indirect
golang.org/x/net v0.8.0 // indirect
golang.org/x/oauth2 v0.6.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.28.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

require (
Expand Down
6 changes: 5 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,9 @@ github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJ
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rs/xid v1.4.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/zerolog v1.28.0 h1:MirSo27VyNi7RJYP3078AA1+Cyzd2GB66qy3aUHvsWY=
github.com/rs/zerolog v1.28.0/go.mod h1:NILgTygv/Uej1ra5XxGf82ZFSLk58MFGAUS2o6usyD0=
github.com/rs/zerolog v1.29.0 h1:Zes4hju04hjbvkVkOhdl2HpZa+0PmVwigmo8XoORE5w=
github.com/rs/zerolog v1.29.0/go.mod h1:NILgTygv/Uej1ra5XxGf82ZFSLk58MFGAUS2o6usyD0=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/sahilm/fuzzy v0.1.0 h1:FzWGaw2Opqyu+794ZQ9SYifWv2EIXpwP4q8dY1kDAwI=
github.com/sahilm/fuzzy v0.1.0/go.mod h1:VFvziUEIMCrT6A6tw2RFIXPXXmzXbOsSHF0DOI8ZK9Y=
Expand All @@ -161,10 +162,13 @@ github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
github.com/yuin/goldmark v1.5.2 h1:ALmeCk/px5FSm1MAcFBAsVKZjDuMVj8Tm7FFIlMJnqU=
Expand Down
19 changes: 19 additions & 0 deletions internal/aws/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"log"
"os"
"strings"

Expand Down Expand Up @@ -123,3 +124,21 @@ func GetProfiles() (profiles []string, err error) {
}
return ret, nil
}

func GetLocalstackCfg(region string) (awsV2.Config, error) {
customResolver := awsV2.EndpointResolverFunc(func(service, region string) (awsV2.Endpoint, error) {
return awsV2.Endpoint{
URL: "http://localhost:4566",
SigningRegion: region,
}, nil
})

awsLSCfg, err := config.LoadDefaultConfig(context.TODO(),
config.WithRegion(region),
config.WithEndpointResolver(customResolver),
)
if err != nil {
log.Fatalf("Cannot load the AWS configs: %s", err)
}
return awsLSCfg, nil
}
67 changes: 67 additions & 0 deletions internal/aws/ec2_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package aws

import (
"context"
"fmt"
"strconv"
"testing"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/ec2"
"github.com/aws/aws-sdk-go-v2/service/ec2/types"
)

type Ec2API interface {
GetEc2Instances(ctx context.Context, params *ec2.DescribeInstancesInput) (*ec2.DescribeInstancesOutput, error)
}

func GetEc2InstancesTest(ctx context.Context, api Ec2API) (*ec2.DescribeInstancesOutput, error) {
object, err := api.GetEc2Instances(ctx, &ec2.DescribeInstancesInput{})
if err != nil {
return nil, err
}
return object, nil
}

type mockGetEc2Instances func(ctx context.Context, params *ec2.DescribeInstancesInput) (*ec2.DescribeInstancesOutput, error)

func (m mockGetEc2Instances) GetEc2Instances(ctx context.Context, params *ec2.DescribeInstancesInput) (*ec2.DescribeInstancesOutput, error) {
return m(ctx, params)
}

func TestGetec2Instances(t *testing.T) {
cases := []struct {
client func(t *testing.T) Ec2API
expect ec2.DescribeInstancesOutput
}{
{
client: func(t *testing.T) Ec2API {
return mockGetEc2Instances(func(ctx context.Context, params *ec2.DescribeInstancesInput) (*ec2.DescribeInstancesOutput, error) {
t.Helper()
return &ec2.DescribeInstancesOutput{Reservations: []types.Reservation{{Instances: []types.Instance{{InstanceId: aws.String("ec2-instance-1"), InstanceType: types.InstanceType(*aws.String("t2.micro"))}}}}}, nil

})
},
expect: ec2.DescribeInstancesOutput{Reservations: []types.Reservation{{Instances: []types.Instance{{InstanceId: aws.String("ec2-instance-1"), InstanceType: types.InstanceType(*aws.String("t2.micro"))}}}}},
},
}

for i, tt := range cases {
t.Run(strconv.Itoa(i), func(t *testing.T) {
ctx := context.TODO()
got, err := GetEc2InstancesTest(ctx, tt.client(t))
if err != nil {
t.Fatalf("expect no error, got %v", err)
}
for i := 0; i < len(got.Reservations); i++ {
for j := 0; j < len(got.Reservations[i].Instances); j++ {
fmt.Println("got:", *got.Reservations[i].Instances[j].InstanceId)
fmt.Println("expect:", *tt.expect.Reservations[i].Instances[j].InstanceId)
if *got.Reservations[i].Instances[j].InstanceId != *tt.expect.Reservations[i].Instances[j].InstanceId {
t.Errorf("expect %v, got %v", *tt.expect.Reservations[i].Instances[j].InstanceId, *got.Reservations[i].Instances[j].InstanceId)
}
}
}
})
}
}
126 changes: 126 additions & 0 deletions internal/aws/iam_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
package aws

import (
"context"
"fmt"
"strconv"
"testing"
"time"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/iam"
"github.com/aws/aws-sdk-go-v2/service/iam/types"
)

type IamAPI interface {
GetIamUsers(ctx context.Context, params iam.ListUsersInput) (*iam.ListUsersOutput, error)
GetIamGroups(ctx context.Context, params iam.ListGroupsInput) (*iam.ListGroupsOutput, error)
}

func GetAllIamUsersTest(ctx context.Context, api IamAPI) (*iam.ListUsersOutput, error) {
object, err := api.GetIamUsers(ctx, iam.ListUsersInput{})
if err != nil {
return nil, err
}
return object, nil
}

func GetAllIamGroupsTest(ctx context.Context, api IamAPI) (*iam.ListGroupsOutput, error) {
object, err := api.GetIamGroups(ctx, iam.ListGroupsInput{})
if err != nil {
return nil, err
}
return object, nil
}

type mockGetIamUsersAPI func(ctx context.Context, params iam.ListUsersInput) (*iam.ListUsersOutput, error)
type mockGetIamGroupsAPI func(ctx context.Context, params iam.ListGroupsInput) (*iam.ListGroupsOutput, error)

func (m mockGetIamUsersAPI) GetIamUsers(ctx context.Context, params iam.ListUsersInput) (*iam.ListUsersOutput, error) {
return m(ctx, params)
}
func (m mockGetIamUsersAPI) GetIamGroups(ctx context.Context, params iam.ListGroupsInput) (*iam.ListGroupsOutput, error) {
return nil, nil
}
func (m mockGetIamGroupsAPI) GetIamUsers(ctx context.Context, params iam.ListUsersInput) (*iam.ListUsersOutput, error) {
return nil, nil
}
func (m mockGetIamGroupsAPI) GetIamGroups(ctx context.Context, params iam.ListGroupsInput) (*iam.ListGroupsOutput, error) {
return m(ctx, params)
}

func TestIamUsers(t *testing.T) {
cases := []struct {
client func(t *testing.T) IamAPI
expect iam.ListUsersOutput
}{
{
client: func(t *testing.T) IamAPI {
return mockGetIamUsersAPI(func(ctx context.Context, params iam.ListUsersInput) (*iam.ListUsersOutput, error) {
t.Helper()
var usrArr []types.User
usr := types.User{Arn: aws.String("arn:aws:iam:000000000000:user/Erdman"), UserId: aws.String("vyt1qsgh"), UserName: aws.String("Erdman"), CreateDate: aws.Time(time.Now())}
usrArr = append(usrArr, usr)
return &iam.ListUsersOutput{Users: usrArr}, nil
})
},
expect: iam.ListUsersOutput{Users: []types.User{{Arn: aws.String("arn:aws:iam:000000000000:user/Erdman"), UserId: aws.String("vyt1qsgh"), UserName: aws.String("Erdman"), CreateDate: aws.Time(time.Now())}}},
},
}

for i, tt := range cases {
t.Run(strconv.Itoa(i), func(t *testing.T) {
ctx := context.TODO()
got, err := GetAllIamUsersTest(ctx, tt.client(t))
if err != nil {
t.Fatalf("expect no error, got %v", err)
}
for i := 0; i < len(got.Users); i++ {
fmt.Println("got:", *got.Users[i].UserId)
fmt.Println("expect:", *tt.expect.Users[i].UserId)

if *got.Users[i].UserId != *tt.expect.Users[i].UserId {
t.Errorf("expect %v, got %v", *tt.expect.Users[i].UserId, *got.Users[i].UserId)
}
}
})
}
}

func TestIamGroups(t *testing.T) {
cases := []struct {
client func(t *testing.T) IamAPI
expect iam.ListGroupsOutput
}{
{
client: func(t *testing.T) IamAPI {
return mockGetIamGroupsAPI(func(ctx context.Context, params iam.ListGroupsInput) (*iam.ListGroupsOutput, error) {
t.Helper()
var grpArr []types.Group
grp := types.Group{Arn: aws.String("arn:aws:iam:000000000000:group/Erdman"), GroupId: aws.String("ibaciunsoinonioucqnoiu"), GroupName: aws.String("Erdman-Group"), CreateDate: aws.Time(time.Now())}
grpArr = append(grpArr, grp)
return &iam.ListGroupsOutput{Groups: grpArr}, nil
})
},
expect: iam.ListGroupsOutput{Groups: []types.Group{{Arn: aws.String("arn:aws:iam:000000000000:group/Erdman"), GroupId: aws.String("ibaciunsoinonioucqnoiu"), GroupName: aws.String("Erdman-Group"), CreateDate: aws.Time(time.Now())}}},
},
}

for i, tt := range cases {
t.Run(strconv.Itoa(i), func(t *testing.T) {
ctx := context.TODO()
got, err := GetAllIamGroupsTest(ctx, tt.client(t))
if err != nil {
t.Fatalf("expect no error, got %v", err)
}
for i := 0; i < len(got.Groups); i++ {
fmt.Println("got:", *got.Groups[i].GroupId)
fmt.Println("expect:", *tt.expect.Groups[i].GroupId)

if *got.Groups[i].GroupId != *tt.expect.Groups[i].GroupId {
t.Errorf("expect %v, got %v", *tt.expect.Groups[i].GroupId, *got.Groups[i].GroupId)
}
}
})
}
}
81 changes: 81 additions & 0 deletions internal/aws/lambda_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package aws

import (
"context"
"fmt"
"strconv"
"testing"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/lambda"
"github.com/aws/aws-sdk-go-v2/service/lambda/types"
)

type LabmdaFunctionsAPI interface {
GetLambdaFunctions(ctx context.Context, params *lambda.ListFunctionsInput) (*lambda.ListFunctionsOutput, error)
}

func GetAllLambdaFunctionsTest(ctx context.Context, api LabmdaFunctionsAPI) (*lambda.ListFunctionsOutput, error) {
object, err := api.GetLambdaFunctions(ctx, &lambda.ListFunctionsInput{})
if err != nil {
return nil, err
}
return object, nil
}

type mockGetAllLambdaFunctionsAPI func(ctx context.Context, params *lambda.ListFunctionsInput) (*lambda.ListFunctionsOutput, error)

func (m mockGetAllLambdaFunctionsAPI) GetLambdaFunctions(ctx context.Context, params *lambda.ListFunctionsInput) (*lambda.ListFunctionsOutput, error) {
return m(ctx, params)
}

func TestGetAllLambdaFunctions(t *testing.T) {
cases := []struct {
client func(t *testing.T) LabmdaFunctionsAPI
expect lambda.ListFunctionsOutput
region string
}{
{
client: func(t *testing.T) LabmdaFunctionsAPI {
return mockGetAllLambdaFunctionsAPI(func(ctx context.Context, params *lambda.ListFunctionsInput) (*lambda.ListFunctionsOutput, error) {
t.Helper()
funcArr := []types.FunctionConfiguration{{FunctionName: aws.String("lambda-func-1"), Role: aws.String("aen:aws:iam:000000000000:role/Role"), FunctionArn: aws.String("arn:aws:lambda:us-east-1:0000000000000:function:lambda-func-1")}}
return &lambda.ListFunctionsOutput{Functions: funcArr}, nil
})
},
expect: lambda.ListFunctionsOutput{Functions: []types.FunctionConfiguration{{FunctionName: aws.String("lambda-func-1"), Role: aws.String("aen:aws:iam:000000000000:role/Role"), FunctionArn: aws.String("arn:aws:lambda:us-east-1:0000000000000:function:lambda-func-1")}}},
region: "us-east-1",
},
{
client: func(t *testing.T) LabmdaFunctionsAPI {
return mockGetAllLambdaFunctionsAPI(func(ctx context.Context, params *lambda.ListFunctionsInput) (*lambda.ListFunctionsOutput, error) {
t.Helper()
funcArr := []types.FunctionConfiguration{{FunctionName: aws.String("lambda-func-2"), Role: aws.String("aen:aws:iam:000000000000:role/Role"), FunctionArn: aws.String("arn:aws:lambda:us-east-2:0000000000000:function:lambda-func-2")}, {FunctionName: aws.String("lambda-func-3"), Role: aws.String("aen:aws:iam:000000000000:role/Role"), FunctionArn: aws.String("arn:aws:lambda:us-east-2:0000000000000:function:lambda-func-3")}}
return &lambda.ListFunctionsOutput{Functions: funcArr}, nil
})
},
expect: lambda.ListFunctionsOutput{Functions: []types.FunctionConfiguration{{FunctionName: aws.String("lambda-func-2"), Role: aws.String("aen:aws:iam:000000000000:role/Role"), FunctionArn: aws.String("arn:aws:lambda:us-east-2:0000000000000:function:lambda-func-2")}, {FunctionName: aws.String("lambda-func-3"), Role: aws.String("aen:aws:iam:000000000000:role/Role"), FunctionArn: aws.String("arn:aws:lambda:us-east-2:0000000000000:function:lambda-func-3")}}},
region: "us-east-2",
},
}

for i, tt := range cases {
t.Run(strconv.Itoa(i), func(t *testing.T) {
ctx := context.TODO()
got, err := GetAllLambdaFunctionsTest(ctx, tt.client(t))
if err != nil {
t.Fatalf("expect no error, got %v", err)
}
if tt.region == "us-east-1" || tt.region == "us-east-2" {
for i := 0; i < len(got.Functions); i++ {
fmt.Println("got:", *got.Functions[i].FunctionName)
fmt.Println("expect:", *tt.expect.Functions[i].FunctionName)

if *got.Functions[i].FunctionName != *tt.expect.Functions[i].FunctionName {
t.Errorf("expect %v, got %v", tt.expect.Functions[i].FunctionName, got.Functions[i].FunctionName)
}
}
}
})
}
}
Loading

0 comments on commit 332cfe3

Please sign in to comment.