Skip to content

Commit

Permalink
chore/docs: testing and mocks (#23)
Browse files Browse the repository at this point in the history
* chore: cleanup and standardize mock generation

* chore: improving docs and makefile
  • Loading branch information
ekristen committed Jan 30, 2024
1 parent d662601 commit 722c540
Show file tree
Hide file tree
Showing 11 changed files with 584 additions and 167 deletions.
11 changes: 10 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,13 @@ docs-serve:
docker run --rm -it -p 8000:8000 -v ${PWD}:/docs squidfunk/mkdocs-material

docs-seed:
cp README.md docs/index.md
cp README.md docs/index.md

generate:
go generate ./...

test:
go test ./...

test-integration:
go test ./... -tags=integration
82 changes: 72 additions & 10 deletions docs/testing.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,82 @@
# Testing

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

## Unit Tests
Generally speaking, the tests are split into two categories:

To unit test *aws-nuke*, some tests require [gomock](https://github.com/golang/mock) to run. However these mocks are
included in the source code already, but they can be generated by running:
1. Tool Testing
2. Resource Testing

Furthermore, for resource testing, these are broken down into two additional categories:

1. Mock Tests
2. Integration Tests

## Tool Testing

These are unit tests written against non resource focused code inside the `pkg/` directory.

## Resource Testing

These are unit tests written against the resources in the `resources/` directory.

### Mock Tests

These are tests where the AWS API calls are mocked out. This is done to ensure that the code is working as expected.
Currently, there are only two services mocked out for testing, IAM and CloudFormation.

#### Adding Additional Mocks

To add another service to be mocked out, you will need to do the following:

1. Identify the service in the AWS SDK for Go
2. Create a new file in the `resources/` directory called `service_mock_test.go`
3. Add the following code to the file: (replace `<service>` with actual service name)
```go
//go:generate ../mocks/generate_mocks.sh <service> <service>iface
package resources

// Note: empty on purpose, this file exist purely to generate mocks for the <service> service
```
4. Run `make generate` to generate the mocks
5. Add tests to the `resources/<service>_mock_test.go` file.
6. Run `make test` to ensure the tests pass
7. Submit a PR with the changes

### Integration Tests

These are tests where the AWS API calls are called directly and tested against a live AWS account. These tests are
behind a build flag (`-tags=integration`), so they are not run by default. To run these tests, you will need to run the following:

```bash
make generate
make test-integration
```

Which is just a wrapper around `go generate ./...`.
#### Adding Additional Integration Tests

To run the unit tests, simply run:
To add another integration test, you will need to do the following:

```bash
make test
```
1. Create a new file in the `resources/` directory called `<resource>_test.go`
2. Add the following code to the file: (replace `<resource>` with actual resource name)
```go
//go:build integration
package resources
import (
"testing"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/<resource>"
)
func Test_ExampleResource_Remove(t *testing.T) {
// 1. write code to create resource in AWS using golang sdk
// 2. stub the resource struct out that is defined in <resource>.go file
// 3. call the Remove() function
// 4. assert that the resource was removed
}
```
3. Run `make test-integration` to ensure the tests pass
4. Submit a PR with the changes
5 changes: 0 additions & 5 deletions generate_mocks

This file was deleted.

3 changes: 0 additions & 3 deletions generate_test.go

This file was deleted.

3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ require (
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/stevenle/topsort v0.2.0 // indirect
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
golang.org/x/mod v0.4.2 // indirect
golang.org/x/sync v0.6.0 // indirect
golang.org/x/sys v0.14.0 // indirect
golang.org/x/tools v0.1.1 // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
)
5 changes: 3 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
github.com/aws/aws-sdk-go v1.49.13 h1:f4mGztsgnx2dR9r8FQYa9YW/RsKb+N7bgef4UGrOW1Y=
github.com/aws/aws-sdk-go v1.49.13/go.mod h1:LF8svs817+Nz+DmiMQKTO3ubZ/6IaTpq3TjupRn3Eqk=
github.com/aws/aws-sdk-go v1.49.21 h1:Rl8KW6HqkwzhATwvXhyr7vD4JFUMi7oXGAw9SrxxIFY=
github.com/aws/aws-sdk-go v1.49.21/go.mod h1:LF8svs817+Nz+DmiMQKTO3ubZ/6IaTpq3TjupRn3Eqk=
github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w=
Expand Down Expand Up @@ -62,6 +60,7 @@ github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673/go.mod h1:N3UwUGtsr
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/mod v0.4.2 h1:Gz96sIWK3OalVv/I/qNygP42zyoKp3xptRVCWRFEBvo=
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
Expand Down Expand Up @@ -89,9 +88,11 @@ golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k=
golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.1 h1:wGiQel/hW0NnEkJUk8lbzkX2gFJU6PFxf1v5OlCfuOs=
golang.org/x/tools v0.1.1/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
Expand Down
6 changes: 6 additions & 0 deletions mocks/generate_mocks.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/sh

SERVICE=$1
INTERFACE=$2

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
Loading

0 comments on commit 722c540

Please sign in to comment.