github.com/rstandt/terraform@v0.12.32-0.20230710220336-b1063613405c/builtin/providers/test/resource_defaults_test.go (about)

     1  package test
     2  
     3  import (
     4  	"strings"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/terraform/helper/resource"
     8  )
     9  
    10  func TestResourceDefaults_basic(t *testing.T) {
    11  	resource.UnitTest(t, resource.TestCase{
    12  		Providers:    testAccProviders,
    13  		CheckDestroy: testAccCheckResourceDestroy,
    14  		Steps: []resource.TestStep{
    15  			resource.TestStep{
    16  				Config: strings.TrimSpace(`
    17  resource "test_resource_defaults" "foo" {
    18  }
    19  				`),
    20  				Check: resource.ComposeTestCheckFunc(
    21  					resource.TestCheckResourceAttr(
    22  						"test_resource_defaults.foo", "default_string", "default string",
    23  					),
    24  					resource.TestCheckResourceAttr(
    25  						"test_resource_defaults.foo", "default_bool", "1",
    26  					),
    27  					resource.TestCheckNoResourceAttr(
    28  						"test_resource_defaults.foo", "nested.#",
    29  					),
    30  				),
    31  			},
    32  		},
    33  	})
    34  }
    35  
    36  func TestResourceDefaults_change(t *testing.T) {
    37  	resource.UnitTest(t, resource.TestCase{
    38  		Providers:    testAccProviders,
    39  		CheckDestroy: testAccCheckResourceDestroy,
    40  		Steps: []resource.TestStep{
    41  			{
    42  				Config: strings.TrimSpace(`
    43  resource "test_resource_defaults" "foo" {
    44  }
    45  				`),
    46  				Check: resource.ComposeTestCheckFunc(
    47  					resource.TestCheckResourceAttr(
    48  						"test_resource_defaults.foo", "default_string", "default string",
    49  					),
    50  					resource.TestCheckResourceAttr(
    51  						"test_resource_defaults.foo", "default_bool", "1",
    52  					),
    53  					resource.TestCheckNoResourceAttr(
    54  						"test_resource_defaults.foo", "nested.#",
    55  					),
    56  				),
    57  			},
    58  			{
    59  				Config: strings.TrimSpace(`
    60  resource "test_resource_defaults" "foo" {
    61  	default_string = "new"
    62  	default_bool = false
    63  	nested {
    64  		optional = "nested"
    65  	}
    66  }
    67  				`),
    68  				Check: resource.ComposeTestCheckFunc(
    69  					resource.TestCheckResourceAttr(
    70  						"test_resource_defaults.foo", "default_string", "new",
    71  					),
    72  					resource.TestCheckResourceAttr(
    73  						"test_resource_defaults.foo", "default_bool", "false",
    74  					),
    75  					resource.TestCheckResourceAttr(
    76  						"test_resource_defaults.foo", "nested.#", "1",
    77  					),
    78  					resource.TestCheckResourceAttr(
    79  						"test_resource_defaults.foo", "nested.2950978312.optional", "nested",
    80  					),
    81  					resource.TestCheckResourceAttr(
    82  						"test_resource_defaults.foo", "nested.2950978312.string", "default nested",
    83  					),
    84  				),
    85  			},
    86  			{
    87  				Config: strings.TrimSpace(`
    88  resource "test_resource_defaults" "foo" {
    89  	default_string = "new"
    90  	default_bool = false
    91  	nested {
    92  		optional = "nested"
    93  		string = "new"
    94  	}
    95  }
    96  				`),
    97  				Check: resource.ComposeTestCheckFunc(
    98  					resource.TestCheckResourceAttr(
    99  						"test_resource_defaults.foo", "default_string", "new",
   100  					),
   101  					resource.TestCheckResourceAttr(
   102  						"test_resource_defaults.foo", "default_bool", "false",
   103  					),
   104  					resource.TestCheckResourceAttr(
   105  						"test_resource_defaults.foo", "nested.#", "1",
   106  					),
   107  					resource.TestCheckResourceAttr(
   108  						"test_resource_defaults.foo", "nested.782850362.optional", "nested",
   109  					),
   110  					resource.TestCheckResourceAttr(
   111  						"test_resource_defaults.foo", "nested.782850362.string", "new",
   112  					),
   113  				),
   114  			},
   115  		},
   116  	})
   117  }
   118  
   119  func TestResourceDefaults_inSet(t *testing.T) {
   120  	resource.UnitTest(t, resource.TestCase{
   121  		Providers:    testAccProviders,
   122  		CheckDestroy: testAccCheckResourceDestroy,
   123  		Steps: []resource.TestStep{
   124  			resource.TestStep{
   125  				Config: strings.TrimSpace(`
   126  resource "test_resource_defaults" "foo" {
   127  	nested {
   128  		optional = "val"
   129  	}
   130  }
   131  				`),
   132  				Check: resource.ComposeTestCheckFunc(
   133  					resource.TestCheckResourceAttr(
   134  						"test_resource_defaults.foo", "default_string", "default string",
   135  					),
   136  					resource.TestCheckResourceAttr(
   137  						"test_resource_defaults.foo", "default_bool", "1",
   138  					),
   139  					resource.TestCheckResourceAttr(
   140  						"test_resource_defaults.foo", "nested.2826070548.optional", "val",
   141  					),
   142  					resource.TestCheckResourceAttr(
   143  						"test_resource_defaults.foo", "nested.2826070548.string", "default nested",
   144  					),
   145  				),
   146  			},
   147  		},
   148  	})
   149  }
   150  
   151  func TestResourceDefaults_import(t *testing.T) {
   152  	// FIXME: The ReadResource after ImportResourceState sin't returning the
   153  	// complete state, yet the later refresh does.
   154  	return
   155  
   156  	resource.UnitTest(t, resource.TestCase{
   157  		Providers:    testAccProviders,
   158  		CheckDestroy: testAccCheckResourceDestroy,
   159  		Steps: []resource.TestStep{
   160  			resource.TestStep{
   161  				Config: strings.TrimSpace(`
   162  resource "test_resource_defaults" "foo" {
   163  	nested {
   164  		optional = "val"
   165  	}
   166  }
   167  				`),
   168  			},
   169  			{
   170  				ImportState:       true,
   171  				ImportStateVerify: true,
   172  				ResourceName:      "test_resource_defaults.foo",
   173  			},
   174  		},
   175  	})
   176  }
   177  
   178  func TestDefaults_emptyString(t *testing.T) {
   179  	config := `
   180  resource "test_resource_defaults" "test" {
   181    default_string = ""
   182  }
   183  `
   184  	resource.UnitTest(t, resource.TestCase{
   185  		Providers: testAccProviders,
   186  		Steps: []resource.TestStep{
   187  			{
   188  				Config: config,
   189  				Check: resource.ComposeTestCheckFunc(
   190  					resource.TestCheckResourceAttr("test_resource_defaults.test", "default_string", ""),
   191  				),
   192  			},
   193  		},
   194  	})
   195  }