Skip to content

Commit

Permalink
use setup functions in tests to avoid magic number requirements durin…
Browse files Browse the repository at this point in the history
…g testing

Closes #32
  • Loading branch information
displague committed Sep 9, 2018
1 parent ff1dbed commit 03b2c58
Show file tree
Hide file tree
Showing 15 changed files with 1,871 additions and 136 deletions.
12 changes: 2 additions & 10 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,9 @@ include .env

.PHONY: vendor example refresh-fixtures clean-fixtures

LINODE_FIXTURE_INSTANCE:=76859403
LINODE_FIXTURE_VOLUME:=6574839201

.PHONY: test
test: vendor
@LINODE_TEST_INSTANCE=$(LINODE_FIXTURE_INSTANCE) \
LINODE_TEST_VOLUME=$(LINODE_FIXTURE_VOLUME) \
LINODE_FIXTURE_MODE="play" \
@LINODE_FIXTURE_MODE="play" \
LINODE_TOKEN="awesometokenawesometokenawesometoken" \
go test $(ARGS)

Expand All @@ -31,19 +26,16 @@ refresh-fixtures: clean-fixtures fixtures
fixtures:
@echo "* Running fixtures"
@LINODE_TOKEN=$(LINODE_TOKEN) \
LINODE_TEST_INSTANCE=$(LINODE_TEST_INSTANCE) \
LINODE_TEST_VOLUME=$(LINODE_TEST_VOLUME) \
LINODE_FIXTURE_MODE="record" go test $(ARGS)
@echo "* Santizing fixtures"
@for yaml in fixtures/*yaml; do \
sed -E -i "" -e "s/$(LINODE_TOKEN)/awesometokenawesometokenawesometoken/g" \
-e "s/$(LINODE_TEST_INSTANCE)/$(LINODE_FIXTURE_INSTANCE)/g" \
-e 's/20[0-9]{2}-[01][0-9]-[0-3][0-9]T[0-2][0-9]:[0-9]{2}:[0-9]{2}/2018-01-02T03:04:05/g' \
-e 's/nb-[0-9]{1,3}-[0-9]{1,3}-[0-9]{1,3}-[0-9]{1,3}\./nb-10-20-30-40./g' \
-e 's/192\.168\.((1?[0-9][0-9]?|2[0-4][0-9]|25[0-5])\.)(1?[0-9][0-9]?|2[0-4][0-9]|25[0-5])/192.168.030.040/g' \
-e '/^192\.168/!s/((1?[0-9][0-9]?|2[0-4][0-9]|25[0-5])\.){3}(1?[0-9][0-9]?|2[0-4][0-9]|25[0-5])/010.020.030.040/g' \
-e 's/(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))/1234::5678/g' \
-e "s/$(LINODE_TEST_VOLUME)/$(LINODE_FIXTURE_VOLUME)/g" $$yaml; \
$$yaml; \
done

.PHONY: godoc
Expand Down
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,8 @@ When performing a `POST` or `PUT` request, multiple field related errors will be
Run `make test` to run the unit tests. This is the same as running `go test` except that `make test` will
execute the tests while playing back API response fixtures that were recorded during a previous development build.

`go test` can be used without the fixtures, so long as `LINODE_TEST_INSTANCE` and `LINODE_TEST_VOLUME` are set
to an instance ID and volume ID that exists on your account. The Linode instance must have a backup and a snapshot to
match the test expectations. Copy `env.sample` to `.env` and configure your persistent test settings, including an API token.
`go test` can be used without the fixtures. Copy `env.sample` to `.env` and configure your persistent test
settings, including an API token.

`go test -short` can be used to run live API tests that do not require an account token.

Expand Down
14 changes: 0 additions & 14 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ var testingMode = recorder.ModeDisabled
var debugAPI = false
var validTestAPIKey = "NOTANAPIKEY"

var TestInstanceID int
var TestVolumeID int

func init() {
if apiToken, ok := os.LookupEnv("LINODE_TOKEN"); ok {
validTestAPIKey = apiToken
Expand All @@ -42,17 +39,6 @@ func init() {
testingMode = recorder.ModeReplaying
}
}

if apiTestInstance, ok := os.LookupEnv("LINODE_TEST_INSTANCE"); ok {
TestInstanceID, _ = strconv.Atoi(apiTestInstance)
log.Printf("[INFO] LINODE_TEST_INSTANCE %d will be examined for tests", TestInstanceID)
}

if apiTestVolume, ok := os.LookupEnv("LINODE_TEST_VOLUME"); ok {
TestVolumeID, _ = strconv.Atoi(apiTestVolume)
log.Printf("[INFO] LINODE_TEST_VOLUME %d will be examined for tests", TestVolumeID)
}

}

// testRecorder returns a go-vcr recorder and an associated function that the caller must defer
Expand Down
2 changes: 0 additions & 2 deletions env.sample
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
LINODE_TOKEN=
LINODE_DEBUG=0
LINODE_TEST_INSTANCE=<FOR_RECORDING_MOCKS>
LINODE_TEST_VOLUME=<FOR_RECORDING_MOCKS>
254 changes: 238 additions & 16 deletions fixtures/TestGetInstance.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,159 @@
---
version: 1
interactions:
- request:
body: '{"region":"us-west","type":"g6-nanode-1","label":"linodego-test-instance","booted":false}'
form: {}
headers:
Accept:
- application/json
Authorization:
- Bearer awesometokenawesometokenawesometoken
Content-Type:
- application/json
User-Agent:
- linodego 0.4.0 https://github.com/linode/linodego
url: https://api.linode.com/v4/linode/instances
method: POST
response:
body: '{"region": "us-west", "alerts": {"network_in": 10, "io": 10000, "cpu":
90, "transfer_quota": 80, "network_out": 10}, "created": "2018-01-02T03:04:05",
"label": "linodego-test-instance", "updated": "2018-01-02T03:04:05", "watchdog_enabled":
true, "image": null, "id": 10157452, "group": "", "ipv4": ["010.020.030.040"],
"status": "provisioning", "hypervisor": "kvm", "ipv6": "1234::5678/64",
"type": "g6-nanode-1", "backups": {"enabled": false, "schedule": {"day": null,
"window": null}}, "specs": {"vcpus": 1, "disk": 25600, "memory": 1024, "transfer":
1000}}'
headers:
Access-Control-Allow-Credentials:
- "true"
Access-Control-Allow-Headers:
- Authorization, Origin, X-Requested-With, Content-Type, Accept, X-Filter
Access-Control-Allow-Methods:
- HEAD, GET, OPTIONS, POST, PUT, DELETE
Access-Control-Allow-Origin:
- '*'
Access-Control-Expose-Headers:
- X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Status
Cache-Control:
- private, max-age=60, s-maxage=60
Connection:
- keep-alive
Content-Length:
- "576"
Content-Security-Policy:
- default-src 'none'
Content-Type:
- application/json
Date:
- Sat, 08 Sep 2018 20:05:47 GMT
Retry-After:
- "118"
Server:
- nginx
Strict-Transport-Security:
- max-age=31536000
Vary:
- Authorization, X-Filter
X-Accepted-Oauth-Scopes:
- linodes:read_write
X-Content-Type-Options:
- nosniff
- nosniff
X-Frame-Options:
- DENY
- DENY
X-Oauth-Scopes:
- '*'
X-Ratelimit-Limit:
- "400"
X-Ratelimit-Remaining:
- "399"
X-Ratelimit-Reset:
- "1536437266"
X-Spec-Version:
- 4.0.4
X-Xss-Protection:
- 1; mode=block
status: 200 OK
code: 200
duration: ""
- request:
body: '{"label":"linodego-test-config","devices":{}}'
form: {}
headers:
Accept:
- application/json
Authorization:
- Bearer awesometokenawesometokenawesometoken
Content-Type:
- application/json
User-Agent:
- linodego 0.4.0 https://github.com/linode/linodego
url: https://api.linode.com/v4/linode/instances/10157452/configs
method: POST
response:
body: '{"virt_mode": "paravirt", "devices": {"sdg": null, "sda": null, "sdd":
null, "sdb": null, "sde": null, "sdc": null, "sdh": null, "sdf": null}, "memory_limit":
0, "comments": "", "updated": "2018-01-02T03:04:05", "run_level": "default",
"label": "linodego-test-config", "id": 11404308, "created": "2018-01-02T03:04:05",
"initrd": null, "root_device": "/dev/sda", "helpers": {"distro": true, "modules_dep":
true, "network": true, "devtmpfs_automount": true, "updatedb_disabled": true},
"kernel": "linode/latest-64bit"}'
headers:
Access-Control-Allow-Credentials:
- "true"
Access-Control-Allow-Headers:
- Authorization, Origin, X-Requested-With, Content-Type, Accept, X-Filter
Access-Control-Allow-Methods:
- HEAD, GET, OPTIONS, POST, PUT, DELETE
Access-Control-Allow-Origin:
- '*'
Access-Control-Expose-Headers:
- X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Status
Cache-Control:
- private, max-age=60, s-maxage=60
Connection:
- keep-alive
Content-Length:
- "516"
Content-Security-Policy:
- default-src 'none'
Content-Type:
- application/json
Date:
- Sat, 08 Sep 2018 20:05:47 GMT
Retry-After:
- "119"
Server:
- nginx
Strict-Transport-Security:
- max-age=31536000
Vary:
- Authorization, X-Filter
X-Accepted-Oauth-Scopes:
- linodes:read_write
X-Content-Type-Options:
- nosniff
- nosniff
X-Frame-Options:
- DENY
- DENY
X-Oauth-Scopes:
- '*'
X-Ratelimit-Limit:
- "400"
X-Ratelimit-Remaining:
- "399"
X-Ratelimit-Reset:
- "1536437267"
X-Spec-Version:
- 4.0.4
X-Xss-Protection:
- 1; mode=block
status: 200 OK
code: 200
duration: ""
- request:
body: ""
form: {}
Expand All @@ -12,18 +165,17 @@ interactions:
Content-Type:
- application/json
User-Agent:
- linodego 0.0.1 https://github.com/linode/linodego
url: https://api.linode.com/v4/linode/instances/76859403
- linodego 0.4.0 https://github.com/linode/linodego
url: https://api.linode.com/v4/linode/instances/10157452
method: GET
response:
body: '{"watchdog_enabled": true, "ipv6": "1234::5678/64",
"hypervisor": "kvm", "ipv4": ["010.020.030.040", "192.168.030.040"], "status": "running",
"label": "test-linode", "backups": {"schedule": {"window": "W16", "day": "Monday"},
"enabled": true}, "updated": "2018-01-02T03:04:05", "image": "linode/ubuntu18.04",
"created": "2018-01-02T03:04:05", "id": 76859403, "type": "g6-standard-1", "region":
"us-east", "group": "", "specs": {"vcpus": 1, "disk": 51200, "memory": 2048,
"transfer": 2000}, "alerts": {"cpu": 90, "io": 10000, "network_out": 10, "network_in":
10, "transfer_quota": 80}}'
body: '{"group": "", "backups": {"schedule": {"window": null, "day": null}, "enabled":
false}, "region": "us-west", "created": "2018-01-02T03:04:05", "label": "linodego-test-instance",
"id": 10157452, "status": "provisioning", "image": null, "specs": {"transfer":
1000, "vcpus": 1, "disk": 25600, "memory": 1024}, "updated": "2018-01-02T03:04:05",
"ipv6": "1234::5678/64", "hypervisor": "kvm", "type": "g6-nanode-1",
"alerts": {"cpu": 90, "network_in": 10, "io": 10000, "transfer_quota": 80, "network_out":
10}, "ipv4": ["010.020.030.040"], "watchdog_enabled": true}'
headers:
Access-Control-Allow-Credentials:
- "true"
Expand All @@ -41,15 +193,15 @@ interactions:
Connection:
- keep-alive
Content-Length:
- "599"
- "576"
Content-Security-Policy:
- default-src 'none'
Content-Type:
- application/json
Date:
- Tue, 03 Jul 2018 01:38:49 GMT
- Sat, 08 Sep 2018 20:05:48 GMT
Retry-After:
- "28"
- "15"
Server:
- nginx
Strict-Transport-Security:
Expand All @@ -70,11 +222,81 @@ interactions:
X-Ratelimit-Limit:
- "400"
X-Ratelimit-Remaining:
- "395"
- "398"
X-Ratelimit-Reset:
- "1536437164"
X-Spec-Version:
- 4.0.4
X-Xss-Protection:
- 1; mode=block
status: 200 OK
code: 200
duration: ""
- request:
body: ""
form: {}
headers:
Accept:
- application/json
Authorization:
- Bearer awesometokenawesometokenawesometoken
Content-Type:
- application/json
User-Agent:
- linodego 0.4.0 https://github.com/linode/linodego
url: https://api.linode.com/v4/linode/instances/10157452
method: DELETE
response:
body: '{}'
headers:
Access-Control-Allow-Credentials:
- "true"
Access-Control-Allow-Headers:
- Authorization, Origin, X-Requested-With, Content-Type, Accept, X-Filter
Access-Control-Allow-Methods:
- HEAD, GET, OPTIONS, POST, PUT, DELETE
Access-Control-Allow-Origin:
- '*'
Access-Control-Expose-Headers:
- X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Status
Cache-Control:
- private, max-age=60, s-maxage=60
Connection:
- keep-alive
Content-Length:
- "2"
Content-Security-Policy:
- default-src 'none'
Content-Type:
- application/json
Date:
- Sat, 08 Sep 2018 20:05:48 GMT
Retry-After:
- "119"
Server:
- nginx
Strict-Transport-Security:
- max-age=31536000
Vary:
- Authorization, X-Filter
X-Accepted-Oauth-Scopes:
- linodes:read_write
X-Content-Type-Options:
- nosniff
- nosniff
X-Frame-Options:
- DENY
- DENY
X-Oauth-Scopes:
- '*'
X-Ratelimit-Limit:
- "400"
X-Ratelimit-Remaining:
- "399"
X-Ratelimit-Reset:
- "1530581958"
- "1536437268"
X-Spec-Version:
- 4.0.2
- 4.0.4
X-Xss-Protection:
- 1; mode=block
status: 200 OK
Expand Down
Loading

0 comments on commit 03b2c58

Please sign in to comment.