|
| 1 | +// Copyright (c) HashiCorp, Inc. |
| 2 | +// SPDX-License-Identifier: MPL-2.0 |
| 3 | + |
| 4 | +package statecheck_test |
| 5 | + |
| 6 | +import ( |
| 7 | + "fmt" |
| 8 | + "regexp" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 12 | + |
| 13 | + r "github.com/hashicorp/terraform-plugin-testing/helper/resource" |
| 14 | + "github.com/hashicorp/terraform-plugin-testing/statecheck" |
| 15 | + "github.com/hashicorp/terraform-plugin-testing/terraform" |
| 16 | + "github.com/hashicorp/terraform-plugin-testing/tfjsonpath" |
| 17 | +) |
| 18 | + |
| 19 | +func TestExtractBoolValue_CheckState_Basic(t *testing.T) { |
| 20 | + t.Parallel() |
| 21 | + |
| 22 | + var targetVar bool |
| 23 | + |
| 24 | + r.Test(t, r.TestCase{ |
| 25 | + ProviderFactories: map[string]func() (*schema.Provider, error){ |
| 26 | + "test": func() (*schema.Provider, error) { //nolint:unparam // required signature |
| 27 | + return testProvider(), nil |
| 28 | + }, |
| 29 | + }, |
| 30 | + Steps: []r.TestStep{ |
| 31 | + { |
| 32 | + Config: `resource "test_resource" "one" { |
| 33 | + bool_attribute = false |
| 34 | + } |
| 35 | + `, |
| 36 | + ConfigStateChecks: []statecheck.StateCheck{ |
| 37 | + statecheck.ExtractBoolValue( |
| 38 | + "test_resource.one", |
| 39 | + tfjsonpath.New("bool_attribute"), |
| 40 | + &targetVar, |
| 41 | + ), |
| 42 | + }, |
| 43 | + Check: testAccAssertBoolEquals(false, targetVar), |
| 44 | + }, |
| 45 | + }, |
| 46 | + }) |
| 47 | +} |
| 48 | + |
| 49 | +func TestExtractBoolValue_CheckState_KnownValueWrongType(t *testing.T) { |
| 50 | + t.Parallel() |
| 51 | + |
| 52 | + var targetVar bool |
| 53 | + |
| 54 | + r.Test(t, r.TestCase{ |
| 55 | + ProviderFactories: map[string]func() (*schema.Provider, error){ |
| 56 | + "test": func() (*schema.Provider, error) { //nolint:unparam // required signature |
| 57 | + return testProvider(), nil |
| 58 | + }, |
| 59 | + }, |
| 60 | + Steps: []r.TestStep{ |
| 61 | + { |
| 62 | + Config: `resource "test_resource" "one" { |
| 63 | + float_attribute = 1.23 |
| 64 | + } |
| 65 | + `, |
| 66 | + ConfigStateChecks: []statecheck.StateCheck{ |
| 67 | + statecheck.ExtractBoolValue( |
| 68 | + "test_resource.one", |
| 69 | + tfjsonpath.New("float_attribute"), |
| 70 | + &targetVar, |
| 71 | + ), |
| 72 | + }, |
| 73 | + ExpectError: regexp.MustCompile(`invalid type for attribute \'float_attribute\' in \'test_resource\.one\'. Expected: bool, Got: json\.Number`), |
| 74 | + }, |
| 75 | + }, |
| 76 | + }) |
| 77 | +} |
| 78 | + |
| 79 | +func TestExtractBoolValue_CheckState_Null(t *testing.T) { |
| 80 | + t.Parallel() |
| 81 | + |
| 82 | + var targetVar bool |
| 83 | + |
| 84 | + r.Test(t, r.TestCase{ |
| 85 | + ProviderFactories: map[string]func() (*schema.Provider, error){ |
| 86 | + "test": func() (*schema.Provider, error) { //nolint:unparam // required signature |
| 87 | + return testProvider(), nil |
| 88 | + }, |
| 89 | + }, |
| 90 | + Steps: []r.TestStep{ |
| 91 | + { |
| 92 | + Config: `resource "test_resource" "one" { |
| 93 | + bool_attribute = null |
| 94 | + } |
| 95 | + `, |
| 96 | + ConfigStateChecks: []statecheck.StateCheck{ |
| 97 | + statecheck.ExtractBoolValue( |
| 98 | + "test_resource.one", |
| 99 | + tfjsonpath.New("bool_attribute"), |
| 100 | + &targetVar, |
| 101 | + ), |
| 102 | + }, |
| 103 | + ExpectError: regexp.MustCompile(`invalid type for attribute \'float_attribute\' in \'test_resource\.one\'. Expected: bool, Got: json\.Number`), |
| 104 | + }, |
| 105 | + }, |
| 106 | + }) |
| 107 | +} |
| 108 | + |
| 109 | +// testAccAssertBoolEquals compares the expected and target bool values. |
| 110 | +func testAccAssertBoolEquals(expected bool, targetVar bool) r.TestCheckFunc { |
| 111 | + return func(s *terraform.State) error { |
| 112 | + if targetVar != expected { |
| 113 | + return fmt.Errorf("expected targetVar to be %v, got %v", expected, targetVar) |
| 114 | + } |
| 115 | + |
| 116 | + return nil |
| 117 | + } |
| 118 | +} |
0 commit comments