|
1 | 1 | # Testing
|
2 | 2 |
|
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. |
4 | 5 |
|
5 |
| -## Unit Tests |
| 6 | +Generally speaking, the tests are split into two categories: |
6 | 7 |
|
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: |
9 | 51 |
|
10 | 52 | ```bash
|
11 |
| -make generate |
| 53 | +make test-integration |
12 | 54 | ```
|
13 | 55 |
|
14 |
| -Which is just a wrapper around `go generate ./...`. |
| 56 | +#### Adding Additional Integration Tests |
15 | 57 |
|
16 |
| -To run the unit tests, simply run: |
| 58 | +To add another integration test, you will need to do the following: |
17 | 59 |
|
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 |
0 commit comments