Skip to content

Commit 0b11ace

Browse files
committed
Merge pull request hashicorp#4788 from hashicorp/phinze/skip-remote-test-option
tests: allow opt-out of remote tests via env var
2 parents 3f74382 + 6bafa74 commit 0b11ace

4 files changed

Lines changed: 44 additions & 7 deletions

File tree

‎helper/acctest/remotetests.go‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package acctest
2+
3+
import (
4+
"net/http"
5+
"os"
6+
"testing"
7+
)
8+
9+
// SkipRemoteTestsEnvVar is an environment variable that can be set by a user
10+
// running the tests in an environment with limited network connectivity. By
11+
// default, tests requiring internet connectivity make an effort to skip if no
12+
// internet is available, but in some cases the smoke test will pass even
13+
// though the test should still be skipped.
14+
const SkipRemoteTestsEnvVar = "TF_SKIP_REMOTE_TESTS"
15+
16+
// RemoteTestPrecheck is meant to be run by any unit test that requires
17+
// outbound internet connectivity. The test will be skipped if it's
18+
// unavailable.
19+
func RemoteTestPrecheck(t *testing.T) {
20+
if os.Getenv(SkipRemoteTestsEnvVar) != "" {
21+
t.Skipf("skipping test, %s was set", SkipRemoteTestsEnvVar)
22+
}
23+
24+
if _, err := http.Get("http://google.com"); err != nil {
25+
t.Skipf("skipping, internet seems to not be available: %s", err)
26+
}
27+
}

‎state/remote/atlas_test.go‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"testing"
1010
"time"
1111

12+
"github.com/hashicorp/terraform/helper/acctest"
1213
"github.com/hashicorp/terraform/terraform"
1314
)
1415

@@ -17,9 +18,7 @@ func TestAtlasClient_impl(t *testing.T) {
1718
}
1819

1920
func TestAtlasClient(t *testing.T) {
20-
if _, err := http.Get("http://google.com"); err != nil {
21-
t.Skipf("skipping, internet seems to not be available: %s", err)
22-
}
21+
acctest.RemoteTestPrecheck(t)
2322

2423
token := os.Getenv("ATLAS_TOKEN")
2524
if token == "" {

‎state/remote/consul_test.go‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,18 @@ package remote
22

33
import (
44
"fmt"
5-
"net/http"
65
"testing"
76
"time"
7+
8+
"github.com/hashicorp/terraform/helper/acctest"
89
)
910

1011
func TestConsulClient_impl(t *testing.T) {
1112
var _ Client = new(ConsulClient)
1213
}
1314

1415
func TestConsulClient(t *testing.T) {
15-
if _, err := http.Get("http://google.com"); err != nil {
16-
t.Skipf("skipping, internet seems to not be available: %s", err)
17-
}
16+
acctest.RemoteTestPrecheck(t)
1817

1918
client, err := consulFactory(map[string]string{
2019
"address": "demo.consul.io:80",

‎website/source/docs/configuration/environment-variables.html.md‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,15 @@ export TF_VAR_ami=ami-049d8641
6262
```
6363

6464
For more on how to use `TF_VAR_name` in context, check out the section on [Variable Configuration](/docs/configuration/variables.html).
65+
66+
## TF_SKIP_REMOTE_TESTS
67+
68+
This can be set prior to running the unit tests to opt-out of any tests
69+
requiring remote network connectivity. The unit tests make an attempt to
70+
automatically detect when connectivity is unavailable and skip the relevant
71+
tests, but by setting this variable you can force these tests to be skipped.
72+
73+
```
74+
export TF_SKIP_REMOTE_TESTS=1
75+
make test
76+
```

0 commit comments

Comments
 (0)