Skip to content
This repository has been archived by the owner on Jun 21, 2024. It is now read-only.

Commit

Permalink
Merge pull request #193 from displague/test-recorder-init
Browse files Browse the repository at this point in the history
Introduces the govcr test recorder
  • Loading branch information
displague authored Sep 14, 2020
2 parents d5467e7 + fef420b commit 75dd10f
Show file tree
Hide file tree
Showing 33 changed files with 473 additions and 272 deletions.
46 changes: 39 additions & 7 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,13 @@ related API documentation will benefit users.

### Linters

golangci-lint is used to verify that the style of the code remains consistent.

`make lint` can be used to verify style before creating a pull request.
`golangci-lint` is used to verify that the style of the code remains consistent.

Before committing, it's a good idea to run `goimports -w .`.
([goimports](https://pkg.go.dev/golang.org/x/tools/cmd/goimports?tab=doc))
([goimports](https://pkg.go.dev/golang.org/x/tools/cmd/goimports?tab=doc)) and
`gofmt -w *.go`. ([gofmt](https://golang.org/cmd/gofmt/))

`make lint` can be used to verify style before creating a pull request.

## Building and Testing

Expand All @@ -62,9 +63,10 @@ make test BUILD=local

### Acceptance Tests

If you want to run tests against the actual Packet API, you must set envvar
`PACKET_TEST_ACTUAL_API` to non-empty string for the `go test`. The device tests
wait for the device creation, so it's best to run a few in parallel.
If you want to run tests against the actual Packet API, you must set the
environment variable `PACKET_TEST_ACTUAL_API` to a non-empty string and set
`PACKNGO_TEST_RECORDER` to `disabled`. The device tests wait for the device
creation, so it's best to run a few in parallel.

To run a particular test, you can do

Expand All @@ -79,6 +81,36 @@ string, for example:
PACKNGO_DEBUG=1 PACKNGO_TEST_ACTUAL_API=1 go test -v -run=TestAccVolumeUpdate
```

### Test Fixtures

By default, `go test ./...` will skip most of the tests unless
`PACKNGO_TEST_ACTUAL_API` is non-empty.

With the `PACKNGO_TEST_ACTUAL_API` environment variable set, tests will be run
against the Packet API, creating real infrastructure and incurring costs.

The `PACKNGO_TEST_RECORDER` variable can be used to record and playback API
responses to test code changes without the delay and costs of making actual API
calls. When unset, `PACKNGO_TEST_RECORDER` acts as though it was set to
`disabled`. This is the default behavior. This default behavior may change in
the future once fixtures are available for all tests.

When `PACKNGO_TEST_RECORDER` is set to `play`, tests will playback API responses
from recorded HTTP response fixtures. This is idea for refactoring and making
changes to request and response handling without introducing changes to the data
sent or received by the Packet API.

When adding support for new end-points, recorded test sessions should be added.
Record the HTTP interactions to fixtures by setting the environment variable
`PACKNGO_TEST_RECORDER` to `record`.

The fixtures are automatically named according to the test they were run from.
They are placed in `fixtures/`. The API token used during authentication is
automatically removed from these fixtures. Nonetheless, caution should be
exercised before committing any fixtures into the project. Account details
includes API tokens, contact, and payment details could easily be leaked by
committing fixtures that haven't been thoroughly reviewed.

### Automation (CI/CD)

Today, Drone tests pull requests using tests defined in
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ func main() {
listProjectsAndUsers(loMembersOut)
}
```

</details>

## Contributing
Expand Down
6 changes: 4 additions & 2 deletions apikeys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ func TestAccAPIKeyListProject(t *testing.T) {
func TestAccAPIKeyListUser(t *testing.T) {
skipUnlessAcceptanceTestsAllowed(t)
t.Parallel()
c := setup(t)
c, stopRecord := setup(t)
defer stopRecord()

nKeys := 10

Expand Down Expand Up @@ -95,7 +96,8 @@ func TestAccAPIKeyListUser(t *testing.T) {
func TestAccAPIKeyCreateUser(t *testing.T) {
skipUnlessAcceptanceTestsAllowed(t)
t.Parallel()
c := setup(t)
c, stopRecord := setup(t)
defer stopRecord()
req := APIKeyCreateRequest{
Description: "PACKNGO_TEST_KEY_DELETE_ME-" + randString8(),
ReadOnly: true,
Expand Down
2 changes: 1 addition & 1 deletion batches_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func TestAccInstanceBatches(t *testing.T) {
}

for _, d := range batch.Devices {
waitDeviceActive(d.ID, c)
waitDeviceActive(t, c, d.ID)
}

_, err = c.Batches.Delete(batchID, true)
Expand Down
7 changes: 2 additions & 5 deletions bgp_neighbors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@ func createBGPDevice(t *testing.T, c *Client, projectID string) *Device {
t.Fatal(err)
}

d, err = waitDeviceActive(d.ID, c)
if err != nil {
t.Fatal(err)
}
d = waitDeviceActive(t, c, d.ID)

aTrue := true
_, _, err = c.BGPSessions.Create(d.ID,
Expand Down Expand Up @@ -57,7 +54,7 @@ func TestAccBGPNeighbors(t *testing.T) {

d := createBGPDevice(t, c, projectID)

defer c.Devices.Delete(d.ID, false)
defer deleteDevice(t, c, d.ID, false)

bgpNeighbors, _, err := c.Devices.ListBGPNeighbors(d.ID, nil)
if err != nil {
Expand Down
8 changes: 2 additions & 6 deletions bgp_sessions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,9 @@ func TestAccBGPSession(t *testing.T) {
if err != nil {
t.Fatal(err)
}
defer deleteDevice(t, c, d.ID, false)

d, err = waitDeviceActive(d.ID, c)
if err != nil {
t.Fatal(err)
}
d = waitDeviceActive(t, c, d.ID)

aTrue := true

Expand Down Expand Up @@ -111,6 +109,4 @@ func TestAccBGPSession(t *testing.T) {
if err == nil {
t.Fatal("Session not deleted")
}

c.Devices.Delete(d.ID, false)
}
3 changes: 2 additions & 1 deletion capacities_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import (

func TestAccCheckCapacity(t *testing.T) {
skipUnlessAcceptanceTestsAllowed(t)
c := setup(t)
c, stopRecord := setup(t)
defer stopRecord()

input := &CapacityInput{
[]ServerInfo{
Expand Down
Loading

0 comments on commit 75dd10f

Please sign in to comment.