diff --git a/internal/provider/exec_test_data_source.go b/internal/provider/exec_test_data_source.go index c00ad4d..8db4420 100644 --- a/internal/provider/exec_test_data_source.go +++ b/internal/provider/exec_test_data_source.go @@ -2,6 +2,8 @@ package provider import ( "context" + "crypto/md5" + "encoding/hex" "errors" "fmt" "net" @@ -185,13 +187,25 @@ func (d *ExecTestDataSource) Read(ctx context.Context, req datasource.ReadReques } cmd := exec.CommandContext(ctx, "sh", "-c", data.Script.ValueString()) + go func() { + select { + case <-ctx.Done(): + if err := cmd.Process.Kill(); err != nil { + tflog.Error(ctx, "failed to kill process", map[string]interface{}{"error": err}) + } + case <-time.After(time.Duration(timeout) * time.Second): + if err := cmd.Process.Kill(); err != nil { + tflog.Error(ctx, "failed to kill process", map[string]interface{}{"error": err}) + } + } + }() cmd.Env = env cmd.Dir = data.WorkingDir.ValueString() fullout, err := cmd.CombinedOutput() data.Output = types.StringValue("") // always empty. data.TestedRef = data.Digest - data.Id = data.Digest + data.Id = types.StringValue(md5str(data.Script.ValueString()) + data.Digest.ValueString()) data.ExitCode = types.Int64Value(int64(cmd.ProcessState.ExitCode())) if errors.Is(ctx.Err(), context.DeadlineExceeded) { @@ -210,6 +224,12 @@ func (d *ExecTestDataSource) Read(ctx context.Context, req datasource.ReadReques resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) } +func md5str(s string) string { + h := md5.New() + h.Write([]byte(s)) + return hex.EncodeToString(h.Sum(nil)) +} + type positiveIntValidator struct{} func (positiveIntValidator) MarkdownDescription(context.Context) string { return "positive integer" } diff --git a/internal/provider/exec_test_data_source_test.go b/internal/provider/exec_test_data_source_test.go index 360eec7..aef6e23 100644 --- a/internal/provider/exec_test_data_source_test.go +++ b/internal/provider/exec_test_data_source_test.go @@ -33,7 +33,7 @@ func TestAccExecTestDataSource(t *testing.T) { }`, d.String()), Check: resource.ComposeAggregateTestCheckFunc( resource.TestCheckResourceAttr("data.oci_exec_test.test", "digest", fmt.Sprintf("cgr.dev/chainguard/wolfi-base@%s", d.String())), - resource.TestCheckResourceAttr("data.oci_exec_test.test", "id", fmt.Sprintf("cgr.dev/chainguard/wolfi-base@%s", d.String())), + resource.TestMatchResourceAttr("data.oci_exec_test.test", "id", regexp.MustCompile(".*cgr.dev/chainguard/wolfi-base@"+d.String())), resource.TestCheckResourceAttr("data.oci_exec_test.test", "exit_code", "0"), resource.TestCheckResourceAttr("data.oci_exec_test.test", "output", ""), ), @@ -54,7 +54,7 @@ func TestAccExecTestDataSource(t *testing.T) { }`, d.String()), Check: resource.ComposeAggregateTestCheckFunc( resource.TestCheckResourceAttr("data.oci_exec_test.env", "digest", fmt.Sprintf("cgr.dev/chainguard/wolfi-base@%s", d.String())), - resource.TestCheckResourceAttr("data.oci_exec_test.env", "id", fmt.Sprintf("cgr.dev/chainguard/wolfi-base@%s", d.String())), + resource.TestMatchResourceAttr("data.oci_exec_test.env", "id", regexp.MustCompile(".*cgr.dev/chainguard/wolfi-base@"+d.String())), resource.TestCheckResourceAttr("data.oci_exec_test.env", "exit_code", "0"), resource.TestCheckResourceAttr("data.oci_exec_test.env", "output", ""), ), @@ -83,7 +83,7 @@ func TestAccExecTestDataSource(t *testing.T) { }`, d.String()), Check: resource.ComposeAggregateTestCheckFunc( resource.TestCheckResourceAttr("data.oci_exec_test.working_dir", "digest", fmt.Sprintf("cgr.dev/chainguard/wolfi-base@%s", d.String())), - resource.TestCheckResourceAttr("data.oci_exec_test.working_dir", "id", fmt.Sprintf("cgr.dev/chainguard/wolfi-base@%s", d.String())), + resource.TestMatchResourceAttr("data.oci_exec_test.working_dir", "id", regexp.MustCompile(".*cgr.dev/chainguard/wolfi-base@"+d.String())), resource.TestCheckResourceAttr("data.oci_exec_test.working_dir", "exit_code", "0"), resource.TestCheckResourceAttr("data.oci_exec_test.working_dir", "output", ""), ), @@ -131,7 +131,7 @@ func TestAccExecTestDataSource_FreePort(t *testing.T) { `, i, d.String()) checks = append(checks, resource.TestCheckResourceAttr(fmt.Sprintf("data.oci_exec_test.freeport-%d", i), "digest", fmt.Sprintf("cgr.dev/chainguard/wolfi-base@%s", d.String())), - resource.TestCheckResourceAttr(fmt.Sprintf("data.oci_exec_test.freeport-%d", i), "id", fmt.Sprintf("cgr.dev/chainguard/wolfi-base@%s", d.String())), + resource.TestMatchResourceAttr(fmt.Sprintf("data.oci_exec_test.freeport-%d", i), "id", regexp.MustCompile(".*cgr.dev/chainguard/wolfi-base@"+d.String())), ) }