-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from one2nc/test_cases
Add Test Cases
- Loading branch information
Showing
46 changed files
with
1,533 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.