Skip to content

Commit 834e485

Browse files
authored
test: overriding test images or location using env vars (#851)
This allows us to reduce the pressure on some locations, or override an image if needed.
1 parent 1f05fc1 commit 834e485

File tree

3 files changed

+28
-21
lines changed

3 files changed

+28
-21
lines changed

internal/e2etests/image/data_source_test.go

+10-13
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,15 @@ import (
1212
"github.com/hetznercloud/terraform-provider-hcloud/internal/testtemplate"
1313
)
1414

15-
const TestImageName = e2etests.TestImage
16-
const TestImageID = "15512617"
17-
1815
func TestAccHcloudDataSourceImageTest(t *testing.T) {
1916
tmplMan := testtemplate.Manager{}
2017

2118
imageByName := &image.DData{
22-
ImageName: TestImageName,
19+
ImageName: e2etests.TestImage,
2320
}
2421
imageByName.SetRName("image_by_name")
2522
imageByID := &image.DData{
26-
ImageID: TestImageID,
23+
ImageID: e2etests.TestImageID,
2724
}
2825
imageByID.SetRName("image_by_id")
2926

@@ -40,12 +37,12 @@ func TestAccHcloudDataSourceImageTest(t *testing.T) {
4037

4138
Check: resource.ComposeTestCheckFunc(
4239
resource.TestCheckResourceAttr(imageByName.TFID(),
43-
"name", TestImageName),
44-
resource.TestCheckResourceAttr(imageByName.TFID(), "id", TestImageID),
40+
"name", e2etests.TestImage),
41+
resource.TestCheckResourceAttr(imageByName.TFID(), "id", e2etests.TestImageID),
4542

4643
resource.TestCheckResourceAttr(imageByID.TFID(),
47-
"name", TestImageName),
48-
resource.TestCheckResourceAttr(imageByID.TFID(), "id", TestImageID),
44+
"name", e2etests.TestImage),
45+
resource.TestCheckResourceAttr(imageByID.TFID(), "id", e2etests.TestImageID),
4946
),
5047
},
5148
},
@@ -56,7 +53,7 @@ func TestAccHcloudDataSourceImageWithFiltersTest(t *testing.T) {
5653
tmplMan := testtemplate.Manager{}
5754

5855
imageByName := &image.DData{
59-
ImageName: TestImageName,
56+
ImageName: e2etests.TestImage,
6057
Architecture: "arm",
6158
IncludeDeprecated: true,
6259
}
@@ -74,7 +71,7 @@ func TestAccHcloudDataSourceImageWithFiltersTest(t *testing.T) {
7471

7572
Check: resource.ComposeTestCheckFunc(
7673
resource.TestCheckResourceAttr(imageByName.TFID(),
77-
"name", TestImageName),
74+
"name", e2etests.TestImage),
7875
resource.TestCheckResourceAttr(imageByName.TFID(),
7976
"architecture", "arm"),
8077
),
@@ -101,8 +98,8 @@ func TestAccHcloudDataSourceImageListTest(t *testing.T) {
10198
Check: resource.ComposeTestCheckFunc(
10299
resource.TestCheckTypeSetElemNestedAttrs(allImagesSel.TFID(), "images.*",
103100
map[string]string{
104-
"name": TestImageName,
105-
"id": TestImageID,
101+
"name": e2etests.TestImage,
102+
"id": e2etests.TestImageID,
106103
},
107104
),
108105
),

internal/e2etests/server/resource_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,7 @@ func TestServerResource_PrimaryIPNetworkTests(t *testing.T) {
450450
Name: "primaryip-v4-test",
451451
Type: "ipv4",
452452
Labels: nil,
453-
Datacenter: "hel1-dc2",
453+
Datacenter: e2etests.TestDataCenter,
454454
AssigneeType: "server",
455455
AutoDelete: false,
456456
}
@@ -460,7 +460,7 @@ func TestServerResource_PrimaryIPNetworkTests(t *testing.T) {
460460
Name: "primaryip-v6-test",
461461
Type: "ipv6",
462462
Labels: nil,
463-
Datacenter: "hel1-dc2",
463+
Datacenter: e2etests.TestDataCenter,
464464
AssigneeType: "server",
465465
AutoDelete: false,
466466
}

internal/e2etests/testing.go

+16-6
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,27 @@ import (
1111
tfhcloud "github.com/hetznercloud/terraform-provider-hcloud/hcloud"
1212
)
1313

14-
const (
14+
var (
1515
// TestImage is the system image that is used in all tests
16-
TestImage = "ubuntu-20.04"
16+
TestImage = getEnv("TEST_IMAGE", "ubuntu-22.04")
17+
18+
// TestImage is the system image ID that is used in all tests
19+
TestImageID = getEnv("TEST_IMAGE_ID", "67794396")
1720

1821
// TestServerType is the default server type used in all tests
19-
TestServerType = "cx11"
22+
TestServerType = getEnv("TEST_SERVER_TYPE", "cx11")
2023

2124
// TestArchitecture is the default architecture used in all tests, should match the architecture of the TestServerType.
22-
TestArchitecture = hcloud.ArchitectureX86
25+
TestArchitecture = getEnv("TEST_ARCHITECTURE", string(hcloud.ArchitectureX86))
2326

2427
// TestLoadBalancerType is the default Load Balancer type used in all tests
2528
TestLoadBalancerType = "lb11"
2629

2730
// TestDataCenter is the default datacenter where we execute our tests.
28-
TestDataCenter = "hel1-dc2"
31+
TestDataCenter = getEnv("TEST_DATACENTER", "nbg1-dc3")
2932

3033
// TestLocationName is the default location where we execute our tests.
31-
TestLocationName = "hel1"
34+
TestLocationName = getEnv("TEST_LOCATION", "nbg1")
3235
)
3336

3437
func ProtoV6ProviderFactories() map[string]func() (tfprotov6.ProviderServer, error) {
@@ -55,3 +58,10 @@ func PreCheck(t *testing.T) func() {
5558
}
5659
}
5760
}
61+
62+
func getEnv(key, fallback string) string {
63+
if value, ok := os.LookupEnv(key); ok {
64+
return value
65+
}
66+
return fallback
67+
}

0 commit comments

Comments
 (0)