Skip to content

Commit e8f7e67

Browse files
committed
fix(system): improve timeout handling and test error cases
- Refined timeout loop and error handling in system datasource - Adjusted snooze logic to account for actual remaining time - Added/expanded tests for broken endpoints and ignore_errors flag - Changed test config endpoint port for improved error simulation
1 parent 5d159bb commit e8f7e67

3 files changed

Lines changed: 36 additions & 5 deletions

File tree

internal/provider/datasource/system/system.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,17 +127,36 @@ func (d *SystemDataSource) Read(ctx context.Context, req datasource.ReadRequest,
127127
return
128128
}
129129

130-
// if no timeout was specified, break immediately
130+
// if no timeout was specified, break immediately after the first check
131+
if tov <= 0 {
132+
break
133+
}
134+
131135
if time.Now().After(endTime) {
136+
if !ignoreErrors {
137+
resp.Diagnostics.AddError(
138+
common.ErrorLabel,
139+
fmt.Sprintf("ran into timeout (max %s)", timeout),
140+
)
141+
return
142+
}
132143
break
133144
}
134145

146+
waitFor := snoozeFor
147+
if remaining := time.Until(endTime); remaining < waitFor {
148+
waitFor = remaining
149+
}
150+
135151
select {
136-
case <-time.After(snoozeFor):
152+
case <-time.After(waitFor):
137153
case <-ctx.Done():
138154
return
139155
}
140156
if time.Now().After(endTime) {
157+
if ignoreErrors {
158+
break
159+
}
141160
resp.Diagnostics.AddError(
142161
common.ErrorLabel,
143162
fmt.Sprintf("ran into timeout (max %s)", timeout),

internal/provider/datasource/system/system_test.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package system_test
22

33
import (
44
"fmt"
5+
"regexp"
56
"testing"
67

78
"github.com/hashicorp/terraform-plugin-framework/providerserver"
@@ -25,12 +26,18 @@ func testAccPreCheck(t *testing.T) {
2526
func TestSystemDataSource(t *testing.T) {
2627
cfg.SkipUnlessAcc(t)
2728

29+
brokenEndpointError := regexp.MustCompile(`(?s)(ran into timeout|CML client error|connection refused|connect: cannot assign requested address)`)
30+
2831
resource.Test(t, resource.TestCase{
2932
PreCheck: func() { testAccPreCheck(t) },
3033
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
3134
Steps: []resource.TestStep{
3235
{
33-
Config: testSystemDataSourceConfig(cfg.CfgBroken, 8),
36+
Config: testSystemDataSourceConfig(cfg.CfgBroken, 2),
37+
ExpectError: brokenEndpointError,
38+
},
39+
{
40+
Config: testSystemDataSourceConfigWithIgnoreErrors(cfg.CfgBroken, 2, true),
3441
Check: resource.ComposeAggregateTestCheckFunc(
3542
resource.TestCheckOutput("bla", "false"),
3643
),
@@ -52,13 +59,18 @@ func TestSystemDataSource(t *testing.T) {
5259
}
5360

5461
func testSystemDataSourceConfig(cfg string, timeout int) string {
62+
return testSystemDataSourceConfigWithIgnoreErrors(cfg, timeout, false)
63+
}
64+
65+
func testSystemDataSourceConfigWithIgnoreErrors(cfg string, timeout int, ignoreErrors bool) string {
5566
return fmt.Sprintf(`
5667
%[1]s
5768
data "cml2_system" "test" {
5869
timeout = "%[2]ds"
70+
ignore_errors = %[3]t
5971
}
6072
output "bla" {
6173
value = data.cml2_system.test.ready
6274
}
63-
`, cfg, timeout)
75+
`, cfg, timeout, ignoreErrors)
6476
}

internal/testing/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ variable "request_headers" {
107107
}
108108
provider "cml2" {
109109
# something non-existent
110-
address = "https://127.0.0.1:5555"
110+
address = "https://127.0.0.1:1"
111111
username = var.username
112112
password = var.password
113113
token = var.token

0 commit comments

Comments
 (0)