From 470fdfa0eb00d79aac8967d423b2fbd8a7dddf45 Mon Sep 17 00:00:00 2001 From: Vuong Date: Sat, 16 Nov 2024 12:02:02 +0800 Subject: [PATCH] [Internal] make `ApplyAndExpectData` work with nested set --- qa/testing.go | 8 +++++--- qa/testing_test.go | 47 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 3 deletions(-) diff --git a/qa/testing.go b/qa/testing.go index 045c100c1f..366c8c6786 100644 --- a/qa/testing.go +++ b/qa/testing.go @@ -11,6 +11,7 @@ import ( "net/http" "net/http/httptest" "os" + "reflect" "regexp" "sort" "strings" @@ -390,9 +391,10 @@ func (f ResourceFixture) ApplyAndExpectData(t *testing.T, data map[string]any) { if k == "id" { assert.Equal(t, expected, d.Id()) } else if that, ok := d.Get(k).(*schema.Set); ok { - this := expected.([]string) - assert.Equal(t, len(this), that.Len(), "set has different length") - for _, item := range this { + this := reflect.ValueOf(expected) + assert.Equal(t, this.Len(), that.Len(), "set has different length") + for i := 0; i < this.Len(); i++ { + item := this.Index(i).Interface() assert.True(t, that.Contains(item), "set does not contain %s", item) } } else { diff --git a/qa/testing_test.go b/qa/testing_test.go index b68d5899e1..68acbe3159 100644 --- a/qa/testing_test.go +++ b/qa/testing_test.go @@ -93,6 +93,22 @@ var noopContextResource = common.Resource{ Type: schema.TypeBool, Required: true, }, + "nested": { + Type: schema.TypeSet, + Optional: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "tags": { + Type: schema.TypeMap, + Optional: true, + Elem: &schema.Schema{ + Type: schema.TypeString, + Optional: true, + }, + }, + }, + }, + }, }, Read: noopContext, Create: noopContext, @@ -218,6 +234,37 @@ func TestResourceFixture_ApplyAndExpectData(t *testing.T) { }.ApplyAndExpectData(t, map[string]any{"id": "x", "dummy": true, "trigger": "now"}) } +func TestResourceFixture_ApplyAndExpectDataSet(t *testing.T) { + ResourceFixture{ + CommandMock: func(commandStr string) common.CommandResults { + return common.CommandResults{ + ResultType: "text", + Data: "yes", + } + }, + Azure: true, + Resource: noopContextResource, + ID: "x", + Delete: true, + HCL: ` + dummy = true + trigger = "now" + nested { + tags { + env = "prod" + } + } + `, + }.ApplyAndExpectData(t, + map[string]any{ + "id": "x", + "dummy": true, + "trigger": "now", + "nested": []any{map[string]any{"tags": map[string]any{"env": "prod"}}}, + }, + ) +} + func TestResourceFixture_InstanceState(t *testing.T) { ResourceFixture{ Resource: noopContextResource,