Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix waiter crash when conditions are not present #2008

Merged
merged 3 commits into from Feb 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/2008.txt
@@ -0,0 +1,3 @@
```release-note:enhancement
kubernetes_manifest: fix crash when waiting on conditions that are not yet present
```
2 changes: 1 addition & 1 deletion manifest/provider/waiter.go
Expand Up @@ -343,7 +343,7 @@ func (w *ConditionsWaiter) Wait(ctx context.Context) error {
}

if status, ok := res.Object["status"].(map[string]interface{}); ok {
if conditions := status["conditions"].([]interface{}); ok && len(conditions) > 0 {
if conditions, ok := status["conditions"].([]interface{}); ok && len(conditions) > 0 {
conditionsMet := true
for _, c := range w.conditions {
var condition map[string]tftypes.Value
Expand Down
@@ -0,0 +1,21 @@
resource "kubernetes_manifest" "test" {
manifest = {
apiVersion = "v1"
kind = "Namespace"

metadata = {
name = var.name
}
}

wait {
condition {
type = "Ready"
status = "True"
}
}

timeouts {
create = "3s"
}
}
35 changes: 35 additions & 0 deletions manifest/test/acceptance/wait_test.go
Expand Up @@ -5,6 +5,7 @@ package acceptance

import (
"context"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -185,3 +186,37 @@ func TestKubernetesManifest_WaitCondition_Pod(t *testing.T) {
"kubernetes_manifest.test.wait.0.condition.1.status": "True",
})
}

func TestKubernetesManifest_Wait_InvalidCondition(t *testing.T) {
// NOTE: this tests that specifying a condition for a resource that
// will never have one does not crash the provider

ctx := context.Background()

name := randName()

reattachInfo, err := provider.ServeTest(ctx, hclog.Default(), t)
if err != nil {
t.Errorf("Failed to create provider instance: %q", err)
}

tf := tfhelper.RequireNewWorkingDir(ctx, t)
tf.SetReattachInfo(ctx, reattachInfo)
defer func() {
tf.Destroy(ctx)
tf.Close()
k8shelper.AssertResourceDoesNotExist(t, "v1", "namespaces", name)
}()

tfvars := TFVARS{
"name": name,
}
tfconfig := loadTerraformConfig(t, "Wait/wait_for_condition_invalid.tf", tfvars)
tf.SetConfig(ctx, tfconfig)
tf.Init(ctx)

err = tf.Apply(ctx)
if err == nil || !strings.Contains(err.Error(), "Terraform timed out waiting on the operation to complete") {
t.Fatalf("Waiter should have timed out")
}
}