Skip to content

Commit 722c540

Browse files
committed
chore/docs: testing and mocks (ekristen#23)
* chore: cleanup and standardize mock generation * chore: improving docs and makefile
1 parent d662601 commit 722c540

File tree

11 files changed

+584
-167
lines changed

11 files changed

+584
-167
lines changed

Makefile

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,13 @@ docs-serve:
55
docker run --rm -it -p 8000:8000 -v ${PWD}:/docs squidfunk/mkdocs-material
66

77
docs-seed:
8-
cp README.md docs/index.md
8+
cp README.md docs/index.md
9+
10+
generate:
11+
go generate ./...
12+
13+
test:
14+
go test ./...
15+
16+
test-integration:
17+
go test ./... -tags=integration

docs/testing.md

Lines changed: 72 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,82 @@
11
# Testing
22

3-
This is not a lot of test coverage around the resources themselves. This is due to the cost
3+
This is not a lot of test coverage around the resources themselves. This is due to the cost of running the tests. However,
4+
[libnuke](https://github.com/ekristen/libnuke) is extensively tested for functionality to ensure a smooth experience.
45

5-
## Unit Tests
6+
Generally speaking, the tests are split into two categories:
67

7-
To unit test *aws-nuke*, some tests require [gomock](https://github.com/golang/mock) to run. However these mocks are
8-
included in the source code already, but they can be generated by running:
8+
1. Tool Testing
9+
2. Resource Testing
10+
11+
Furthermore, for resource testing, these are broken down into two additional categories:
12+
13+
1. Mock Tests
14+
2. Integration Tests
15+
16+
## Tool Testing
17+
18+
These are unit tests written against non resource focused code inside the `pkg/` directory.
19+
20+
## Resource Testing
21+
22+
These are unit tests written against the resources in the `resources/` directory.
23+
24+
### Mock Tests
25+
26+
These are tests where the AWS API calls are mocked out. This is done to ensure that the code is working as expected.
27+
Currently, there are only two services mocked out for testing, IAM and CloudFormation.
28+
29+
#### Adding Additional Mocks
30+
31+
To add another service to be mocked out, you will need to do the following:
32+
33+
1. Identify the service in the AWS SDK for Go
34+
2. Create a new file in the `resources/` directory called `service_mock_test.go`
35+
3. Add the following code to the file: (replace `<service>` with actual service name)
36+
```go
37+
//go:generate ../mocks/generate_mocks.sh <service> <service>iface
38+
package resources
39+
40+
// Note: empty on purpose, this file exist purely to generate mocks for the <service> service
41+
```
42+
4. Run `make generate` to generate the mocks
43+
5. Add tests to the `resources/<service>_mock_test.go` file.
44+
6. Run `make test` to ensure the tests pass
45+
7. Submit a PR with the changes
46+
47+
### Integration Tests
48+
49+
These are tests where the AWS API calls are called directly and tested against a live AWS account. These tests are
50+
behind a build flag (`-tags=integration`), so they are not run by default. To run these tests, you will need to run the following:
951

1052
```bash
11-
make generate
53+
make test-integration
1254
```
1355

14-
Which is just a wrapper around `go generate ./...`.
56+
#### Adding Additional Integration Tests
1557

16-
To run the unit tests, simply run:
58+
To add another integration test, you will need to do the following:
1759

18-
```bash
19-
make test
20-
```
60+
1. Create a new file in the `resources/` directory called `<resource>_test.go`
61+
2. Add the following code to the file: (replace `<resource>` with actual resource name)
62+
```go
63+
//go:build integration
64+
65+
package resources
66+
67+
import (
68+
"testing"
69+
70+
"github.com/aws/aws-sdk-go/aws/session"
71+
"github.com/aws/aws-sdk-go/service/<resource>"
72+
)
73+
74+
func Test_ExampleResource_Remove(t *testing.T) {
75+
// 1. write code to create resource in AWS using golang sdk
76+
// 2. stub the resource struct out that is defined in <resource>.go file
77+
// 3. call the Remove() function
78+
// 4. assert that the resource was removed
79+
}
80+
```
81+
3. Run `make test-integration` to ensure the tests pass
82+
4. Submit a PR with the changes

generate_mocks

Lines changed: 0 additions & 5 deletions
This file was deleted.

generate_test.go

Lines changed: 0 additions & 3 deletions
This file was deleted.

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@ require (
2929
github.com/russross/blackfriday/v2 v2.1.0 // indirect
3030
github.com/stevenle/topsort v0.2.0 // indirect
3131
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
32+
golang.org/x/mod v0.4.2 // indirect
3233
golang.org/x/sync v0.6.0 // indirect
3334
golang.org/x/sys v0.14.0 // indirect
35+
golang.org/x/tools v0.1.1 // indirect
36+
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
3437
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
3538
)

go.sum

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
github.com/aws/aws-sdk-go v1.49.13 h1:f4mGztsgnx2dR9r8FQYa9YW/RsKb+N7bgef4UGrOW1Y=
2-
github.com/aws/aws-sdk-go v1.49.13/go.mod h1:LF8svs817+Nz+DmiMQKTO3ubZ/6IaTpq3TjupRn3Eqk=
31
github.com/aws/aws-sdk-go v1.49.21 h1:Rl8KW6HqkwzhATwvXhyr7vD4JFUMi7oXGAw9SrxxIFY=
42
github.com/aws/aws-sdk-go v1.49.21/go.mod h1:LF8svs817+Nz+DmiMQKTO3ubZ/6IaTpq3TjupRn3Eqk=
53
github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w=
@@ -62,6 +60,7 @@ github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673/go.mod h1:N3UwUGtsr
6260
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
6361
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
6462
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
63+
golang.org/x/mod v0.4.2 h1:Gz96sIWK3OalVv/I/qNygP42zyoKp3xptRVCWRFEBvo=
6564
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
6665
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
6766
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
@@ -89,9 +88,11 @@ golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k=
8988
golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
9089
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
9190
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
91+
golang.org/x/tools v0.1.1 h1:wGiQel/hW0NnEkJUk8lbzkX2gFJU6PFxf1v5OlCfuOs=
9292
golang.org/x/tools v0.1.1/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
9393
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
9494
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
95+
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
9596
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
9697
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
9798
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=

mocks/generate_mocks.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/bin/sh
2+
3+
SERVICE=$1
4+
INTERFACE=$2
5+
6+
go run github.com/golang/mock/mockgen -source $(go list -m -mod=mod -f "{{.Dir}}" "github.com/aws/aws-sdk-go")/service/$SERVICE/$INTERFACE/interface.go -destination ../mocks/mock_$INTERFACE/mock.go

0 commit comments

Comments
 (0)