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

     1  package test
     2  
     3  import (
     4  	"regexp"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/hashicorp/terraform/helper/resource"
     9  )
    10  
    11  // an empty config should be ok, because no deprecated/removed fields are set.
    12  func TestResourceList_changed(t *testing.T) {
    13  	resource.UnitTest(t, resource.TestCase{
    14  		Providers:    testAccProviders,
    15  		CheckDestroy: testAccCheckResourceDestroy,
    16  		Steps: []resource.TestStep{
    17  			resource.TestStep{
    18  				Config: strings.TrimSpace(`
    19  resource "test_resource_list" "foo" {
    20  	list_block {
    21  		string = "a"
    22  		int = 1
    23  	}
    24  }
    25  				`),
    26  				Check: resource.ComposeTestCheckFunc(
    27  					resource.TestCheckResourceAttr(
    28  						"test_resource_list.foo", "list_block.#", "1",
    29  					),
    30  					resource.TestCheckResourceAttr(
    31  						"test_resource_list.foo", "list_block.0.string", "a",
    32  					),
    33  					resource.TestCheckResourceAttr(
    34  						"test_resource_list.foo", "list_block.0.int", "1",
    35  					),
    36  				),
    37  			},
    38  			resource.TestStep{
    39  				Config: strings.TrimSpace(`
    40  resource "test_resource_list" "foo" {
    41  	list_block {
    42  		string = "a"
    43  		int = 1
    44  	}
    45  
    46  	list_block {
    47  		string = "b"
    48  		int = 2
    49  	}
    50  }
    51  				`),
    52  				Check: resource.ComposeTestCheckFunc(
    53  					resource.TestCheckResourceAttr(
    54  						"test_resource_list.foo", "list_block.#", "2",
    55  					),
    56  					resource.TestCheckResourceAttr(
    57  						"test_resource_list.foo", "list_block.0.string", "a",
    58  					),
    59  					resource.TestCheckResourceAttr(
    60  						"test_resource_list.foo", "list_block.0.int", "1",
    61  					),
    62  					resource.TestCheckResourceAttr(
    63  						"test_resource_list.foo", "list_block.1.string", "b",
    64  					),
    65  					resource.TestCheckResourceAttr(
    66  						"test_resource_list.foo", "list_block.1.int", "2",
    67  					),
    68  				),
    69  			},
    70  			resource.TestStep{
    71  				Config: strings.TrimSpace(`
    72  resource "test_resource_list" "foo" {
    73  	list_block {
    74  		string = "a"
    75  		int = 1
    76  	}
    77  
    78  	list_block {
    79  		string = "c"
    80  		int = 2
    81  	}
    82  }
    83  				`),
    84  				Check: resource.ComposeTestCheckFunc(
    85  					resource.TestCheckResourceAttr(
    86  						"test_resource_list.foo", "list_block.#", "2",
    87  					),
    88  					resource.TestCheckResourceAttr(
    89  						"test_resource_list.foo", "list_block.0.string", "a",
    90  					),
    91  					resource.TestCheckResourceAttr(
    92  						"test_resource_list.foo", "list_block.0.int", "1",
    93  					),
    94  					resource.TestCheckResourceAttr(
    95  						"test_resource_list.foo", "list_block.1.string", "c",
    96  					),
    97  					resource.TestCheckResourceAttr(
    98  						"test_resource_list.foo", "list_block.1.int", "2",
    99  					),
   100  				),
   101  			},
   102  		},
   103  	})
   104  }
   105  
   106  func TestResourceList_mapList(t *testing.T) {
   107  	resource.UnitTest(t, resource.TestCase{
   108  		Providers:    testAccProviders,
   109  		CheckDestroy: testAccCheckResourceDestroy,
   110  		Steps: []resource.TestStep{
   111  			resource.TestStep{
   112  				Config: strings.TrimSpace(`
   113  variable "map" {
   114    type = map(string)
   115    default = {}
   116  }
   117  
   118  resource "test_resource_list" "foo" {
   119  	map_list = [
   120  	  {
   121  	    a = "1"
   122  	  },
   123  	  var.map
   124  	]
   125  }
   126  				`),
   127  				Check: resource.ComposeTestCheckFunc(
   128  					resource.TestCheckResourceAttr(
   129  						"test_resource_list.foo", "map_list.1", "",
   130  					),
   131  				),
   132  			},
   133  		},
   134  	})
   135  }
   136  
   137  func TestResourceList_sublist(t *testing.T) {
   138  	resource.UnitTest(t, resource.TestCase{
   139  		Providers:    testAccProviders,
   140  		CheckDestroy: testAccCheckResourceDestroy,
   141  		Steps: []resource.TestStep{
   142  			resource.TestStep{
   143  				Config: strings.TrimSpace(`
   144  resource "test_resource_list" "foo" {
   145  	list_block {
   146  		sublist_block {
   147  			string = "a"
   148  			int = 1
   149  		}
   150  	}
   151  }
   152  				`),
   153  				Check: resource.ComposeTestCheckFunc(
   154  					resource.TestCheckResourceAttr(
   155  						"test_resource_list.foo", "list_block.0.sublist_block.#", "1",
   156  					),
   157  					resource.TestCheckResourceAttr(
   158  						"test_resource_list.foo", "list_block.0.sublist_block.0.string", "a",
   159  					),
   160  					resource.TestCheckResourceAttr(
   161  						"test_resource_list.foo", "list_block.0.sublist_block.0.int", "1",
   162  					),
   163  				),
   164  			},
   165  		},
   166  	})
   167  }
   168  
   169  func TestResourceList_interpolationChanges(t *testing.T) {
   170  	resource.UnitTest(t, resource.TestCase{
   171  		Providers:    testAccProviders,
   172  		CheckDestroy: testAccCheckResourceDestroy,
   173  		Steps: []resource.TestStep{
   174  			resource.TestStep{
   175  				Config: strings.TrimSpace(`
   176  resource "test_resource_list" "foo" {
   177  	list_block {
   178  		string = "x"
   179  	}
   180  }
   181  resource "test_resource_list" "bar" {
   182  	list_block {
   183  		string = test_resource_list.foo.id
   184  	}
   185  }
   186  				`),
   187  				Check: resource.ComposeTestCheckFunc(
   188  					resource.TestCheckResourceAttr(
   189  						"test_resource_list.foo", "list_block.0.string", "x",
   190  					),
   191  					resource.TestCheckResourceAttr(
   192  						"test_resource_list.bar", "list_block.0.string", "testId",
   193  					),
   194  				),
   195  			},
   196  			resource.TestStep{
   197  				Config: strings.TrimSpace(`
   198  resource "test_resource_list" "baz" {
   199  	list_block {
   200  		string = "x"
   201  		int = 1
   202  	}
   203  }
   204  resource "test_resource_list" "bar" {
   205  	list_block {
   206  		string = test_resource_list.baz.id
   207  		int = 3
   208  	}
   209  }
   210  				`),
   211  				Check: resource.ComposeTestCheckFunc(
   212  					resource.TestCheckResourceAttr(
   213  						"test_resource_list.baz", "list_block.0.string", "x",
   214  					),
   215  					resource.TestCheckResourceAttr(
   216  						"test_resource_list.bar", "list_block.0.string", "testId",
   217  					),
   218  				),
   219  			},
   220  		},
   221  	})
   222  }
   223  
   224  func TestResourceList_removedForcesNew(t *testing.T) {
   225  	resource.UnitTest(t, resource.TestCase{
   226  		Providers:    testAccProviders,
   227  		CheckDestroy: testAccCheckResourceDestroy,
   228  		Steps: []resource.TestStep{
   229  			resource.TestStep{
   230  				Config: strings.TrimSpace(`
   231  resource "test_resource_list" "foo" {
   232  	list_block {
   233  		force_new = "ok"
   234  	}
   235  }
   236  				`),
   237  				Check: resource.ComposeTestCheckFunc(
   238  					resource.TestCheckResourceAttr(
   239  						"test_resource_list.foo", "list_block.0.force_new", "ok",
   240  					),
   241  				),
   242  			},
   243  			resource.TestStep{
   244  				Config: strings.TrimSpace(`
   245  resource "test_resource_list" "foo" {
   246  }
   247  				`),
   248  				Check: resource.ComposeTestCheckFunc(),
   249  			},
   250  		},
   251  	})
   252  }
   253  
   254  func TestResourceList_emptyStrings(t *testing.T) {
   255  	resource.UnitTest(t, resource.TestCase{
   256  		Providers:    testAccProviders,
   257  		CheckDestroy: testAccCheckResourceDestroy,
   258  		Steps: []resource.TestStep{
   259  			resource.TestStep{
   260  				Config: strings.TrimSpace(`
   261  resource "test_resource_list" "foo" {
   262    list_block {
   263      sublist = ["a", ""]
   264    }
   265  
   266    list_block {
   267      sublist = [""]
   268    }
   269  
   270    list_block {
   271      sublist = ["", "c", ""]
   272    }
   273  }
   274  				`),
   275  				Check: resource.ComposeTestCheckFunc(
   276  					resource.TestCheckResourceAttr("test_resource_list.foo", "list_block.0.sublist.0", "a"),
   277  					resource.TestCheckResourceAttr("test_resource_list.foo", "list_block.0.sublist.1", ""),
   278  					resource.TestCheckResourceAttr("test_resource_list.foo", "list_block.1.sublist.0", ""),
   279  					resource.TestCheckResourceAttr("test_resource_list.foo", "list_block.2.sublist.0", ""),
   280  					resource.TestCheckResourceAttr("test_resource_list.foo", "list_block.2.sublist.1", "c"),
   281  					resource.TestCheckResourceAttr("test_resource_list.foo", "list_block.2.sublist.2", ""),
   282  				),
   283  			},
   284  			resource.TestStep{
   285  				Config: strings.TrimSpace(`
   286  resource "test_resource_list" "foo" {
   287    list_block {
   288      sublist = [""]
   289    }
   290  
   291    list_block {
   292      sublist = []
   293    }
   294  
   295    list_block {
   296      sublist = ["", "c"]
   297    }
   298  }
   299  			`),
   300  				Check: resource.ComposeTestCheckFunc(
   301  					resource.TestCheckResourceAttr("test_resource_list.foo", "list_block.0.sublist.#", "1"),
   302  					resource.TestCheckResourceAttr("test_resource_list.foo", "list_block.0.sublist.0", ""),
   303  					resource.TestCheckResourceAttr("test_resource_list.foo", "list_block.1.sublist.#", "0"),
   304  					resource.TestCheckResourceAttr("test_resource_list.foo", "list_block.2.sublist.1", "c"),
   305  					resource.TestCheckResourceAttr("test_resource_list.foo", "list_block.2.sublist.#", "2"),
   306  				),
   307  			},
   308  		},
   309  	})
   310  }
   311  
   312  func TestResourceList_addRemove(t *testing.T) {
   313  	resource.UnitTest(t, resource.TestCase{
   314  		Providers:    testAccProviders,
   315  		CheckDestroy: testAccCheckResourceDestroy,
   316  		Steps: []resource.TestStep{
   317  			resource.TestStep{
   318  				Config: strings.TrimSpace(`
   319  resource "test_resource_list" "foo" {
   320  }
   321  				`),
   322  				Check: resource.ComposeTestCheckFunc(
   323  					resource.TestCheckResourceAttr("test_resource_list.foo", "computed_list.#", "0"),
   324  					resource.TestCheckResourceAttr("test_resource_list.foo", "dependent_list.#", "0"),
   325  				),
   326  			},
   327  			resource.TestStep{
   328  				Config: strings.TrimSpace(`
   329  resource "test_resource_list" "foo" {
   330  	dependent_list {
   331  		val = "a"
   332  	}
   333  }
   334  				`),
   335  				Check: resource.ComposeTestCheckFunc(
   336  					resource.TestCheckResourceAttr("test_resource_list.foo", "computed_list.#", "1"),
   337  					resource.TestCheckResourceAttr("test_resource_list.foo", "dependent_list.#", "1"),
   338  				),
   339  			},
   340  			resource.TestStep{
   341  				Config: strings.TrimSpace(`
   342  resource "test_resource_list" "foo" {
   343  }
   344  				`),
   345  				Check: resource.ComposeTestCheckFunc(
   346  					resource.TestCheckResourceAttr("test_resource_list.foo", "computed_list.#", "0"),
   347  					resource.TestCheckResourceAttr("test_resource_list.foo", "dependent_list.#", "0"),
   348  				),
   349  			},
   350  		},
   351  	})
   352  }
   353  
   354  func TestResourceList_planUnknownInterpolation(t *testing.T) {
   355  	resource.UnitTest(t, resource.TestCase{
   356  		Providers:    testAccProviders,
   357  		CheckDestroy: testAccCheckResourceDestroy,
   358  		Steps: []resource.TestStep{
   359  			resource.TestStep{
   360  				Config: strings.TrimSpace(`
   361  resource "test_resource_list" "foo" {
   362  	list_block {
   363  		string = "x"
   364  	}
   365  }
   366  resource "test_resource_list" "bar" {
   367  	list_block {
   368  		sublist = [
   369  			test_resource_list.foo.list_block[0].string,
   370  		]
   371  	}
   372  }
   373  				`),
   374  				Check: resource.ComposeTestCheckFunc(
   375  					resource.TestCheckResourceAttr(
   376  						"test_resource_list.bar", "list_block.0.sublist.0", "x",
   377  					),
   378  				),
   379  			},
   380  			resource.TestStep{
   381  				Config: strings.TrimSpace(`
   382  resource "test_resource_list" "foo" {
   383  	list_block {
   384  		string = "x"
   385  	}
   386  	dependent_list {
   387  		val = "y"
   388  	}
   389  }
   390  resource "test_resource_list" "bar" {
   391  	list_block {
   392  		sublist = [
   393  			test_resource_list.foo.computed_list[0],
   394  		]
   395  	}
   396  }
   397  				`),
   398  				Check: resource.ComposeTestCheckFunc(
   399  					resource.TestCheckResourceAttr(
   400  						"test_resource_list.bar", "list_block.0.sublist.0", "y",
   401  					),
   402  				),
   403  			},
   404  			resource.TestStep{
   405  				Config: strings.TrimSpace(`
   406  resource "test_resource_list" "foo" {
   407  	list_block {
   408  		string = "x"
   409  	}
   410  	dependent_list {
   411  		val = "z"
   412  	}
   413  }
   414  resource "test_resource_list" "bar" {
   415  	list_block {
   416  		sublist = [
   417  			test_resource_list.foo.computed_list[0],
   418  		]
   419  	}
   420  }
   421  				`),
   422  				Check: resource.ComposeTestCheckFunc(
   423  					resource.TestCheckResourceAttr(
   424  						"test_resource_list.bar", "list_block.0.sublist.0", "z",
   425  					),
   426  				),
   427  			},
   428  		},
   429  	})
   430  }
   431  
   432  func TestResourceList_planUnknownInterpolationList(t *testing.T) {
   433  	resource.UnitTest(t, resource.TestCase{
   434  		Providers:    testAccProviders,
   435  		CheckDestroy: testAccCheckResourceDestroy,
   436  		Steps: []resource.TestStep{
   437  			resource.TestStep{
   438  				Config: strings.TrimSpace(`
   439  resource "test_resource_list" "foo" {
   440  	dependent_list {
   441  		val = "y"
   442  	}
   443  }
   444  resource "test_resource_list" "bar" {
   445  	list_block {
   446  		sublist_block_optional {
   447  			list = test_resource_list.foo.computed_list
   448  		}
   449  	}
   450  }
   451  				`),
   452  				Check: resource.ComposeTestCheckFunc(
   453  					resource.TestCheckResourceAttr(
   454  						"test_resource_list.bar", "list_block.0.sublist_block_optional.0.list.0", "y",
   455  					),
   456  				),
   457  			},
   458  			resource.TestStep{
   459  				Config: strings.TrimSpace(`
   460  resource "test_resource_list" "foo" {
   461  	dependent_list {
   462  		val = "z"
   463  	}
   464  }
   465  resource "test_resource_list" "bar" {
   466  	list_block {
   467  		sublist_block_optional {
   468  			list = test_resource_list.foo.computed_list
   469  		}
   470  	}
   471  }
   472  				`),
   473  				Check: resource.ComposeTestCheckFunc(
   474  					resource.TestCheckResourceAttr(
   475  						"test_resource_list.bar", "list_block.0.sublist_block_optional.0.list.0", "z",
   476  					),
   477  				),
   478  			},
   479  		},
   480  	})
   481  }
   482  
   483  func TestResourceList_dynamicList(t *testing.T) {
   484  	resource.UnitTest(t, resource.TestCase{
   485  		Providers:    testAccProviders,
   486  		CheckDestroy: testAccCheckResourceDestroy,
   487  		Steps: []resource.TestStep{
   488  			resource.TestStep{
   489  				Config: strings.TrimSpace(`
   490  resource "test_resource_list" "a" {
   491  	dependent_list {
   492  		val = "a"
   493  	}
   494  
   495  	dependent_list {
   496  		val = "b"
   497  	}
   498  }
   499  resource "test_resource_list" "b" {
   500  	list_block {
   501  		string = "constant"
   502  	}
   503  	dynamic "list_block" {
   504  		for_each = test_resource_list.a.computed_list
   505  		content {
   506  		  string = list_block.value
   507  		}
   508  	}
   509  }
   510  				`),
   511  				Check: resource.ComposeTestCheckFunc(),
   512  			},
   513  		},
   514  	})
   515  }
   516  
   517  func TestResourceList_dynamicMinItems(t *testing.T) {
   518  	resource.UnitTest(t, resource.TestCase{
   519  		Providers:    testAccProviders,
   520  		CheckDestroy: testAccCheckResourceDestroy,
   521  		Steps: []resource.TestStep{
   522  			resource.TestStep{
   523  				Config: strings.TrimSpace(`
   524  variable "a" {
   525    type = list(number)
   526    default = [1]
   527  }
   528  
   529  resource "test_resource_list" "b" {
   530  	dynamic "min_items" {
   531  		for_each = var.a
   532  		content {
   533  		  val = "foo"
   534  		}
   535  	}
   536  }
   537  				`),
   538  				ExpectError: regexp.MustCompile(`attribute supports 2`),
   539  			},
   540  			resource.TestStep{
   541  				Config: strings.TrimSpace(`
   542  resource "test_resource_list" "a" {
   543  	dependent_list {
   544  		val = "a"
   545  	}
   546  
   547  	dependent_list {
   548  		val = "b"
   549  	}
   550  }
   551  resource "test_resource_list" "b" {
   552  	list_block {
   553  		string = "constant"
   554  	}
   555  	dynamic "min_items" {
   556  		for_each = test_resource_list.a.computed_list
   557  		content {
   558  		  val = min_items.value
   559  		}
   560  	}
   561  }
   562  				`),
   563  			},
   564  		},
   565  	})
   566  }