77 "testing"
88
99 "github.com/hashicorp/go-version"
10+ "github.com/hashicorp/terraform-plugin-testing/config"
1011 "github.com/hashicorp/terraform-plugin-testing/helper/resource"
1112 "github.com/hashicorp/terraform-plugin-testing/tfversion"
1213)
@@ -220,13 +221,63 @@ output "test" {
220221 })
221222}
222223
224+ func TestNotNullFunction_compoundValidation (t * testing.T ) {
225+ t .Parallel ()
226+ resource .UnitTest (t , resource.TestCase {
227+ TerraformVersionChecks : []tfversion.TerraformVersionCheck {
228+ tfversion .SkipBelow (version .Must (version .NewVersion (MinimalRequiredTerraformVersion ))),
229+ },
230+ ProtoV6ProviderFactories : testAccProtoV6ProviderFactories ,
231+ Steps : []resource.TestStep {
232+ {
233+ Config : `
234+ variable "ipv4_ipam_pool_id" {
235+ default = null
236+ description = "ID of the IPv4 IPAM pool to use for the VPC."
237+ type = string
238+ }
239+
240+ variable "cidr_block" {
241+ default = null
242+ description = "CIDR block for the VPC."
243+ type = string
244+
245+ validation {
246+ condition = provider::assert::cidr(var.cidr_block)
247+ error_message = "CIDR block must be a valid CIDR range."
248+ }
249+
250+ validation {
251+ condition = anytrue([
252+ provider::assert::not_null(var.cidr_block),
253+ provider::assert::not_null(var.ipv4_ipam_pool_id)
254+ ])
255+ error_message = "Exactly one of cidr_block or ipv4_ipam_pool_id must be provided."
256+ }
257+ }
258+ ` ,
259+ ConfigVariables : config.Variables {
260+ "cidr_block" : config .StringVariable ("10.0.42.0/24" ),
261+ },
262+ Check : resource .ComposeAggregateTestCheckFunc (),
263+ },
264+ },
265+ })
266+ }
267+
223268func TestNotNullFunction_falseCases (t * testing.T ) {
224269 t .Parallel ()
225270 resource .UnitTest (t , resource.TestCase {
226271 TerraformVersionChecks : []tfversion.TerraformVersionCheck {
227272 tfversion .SkipBelow (version .Must (version .NewVersion (MinimalRequiredTerraformVersion ))),
228273 },
229274 ProtoV6ProviderFactories : testAccProtoV6ProviderFactories ,
275+ ExternalProviders : map [string ]resource.ExternalProvider {
276+ "wireguard" : {
277+ Source : "OJFord/wireguard" ,
278+ VersionConstraint : "0.3.1" ,
279+ },
280+ },
230281 Steps : []resource.TestStep {
231282 {
232283 Config : `
@@ -235,6 +286,23 @@ locals {
235286}
236287output "test" {
237288 value = provider::assert::not_null(local.object)
289+ }
290+ ` ,
291+ Check : resource .ComposeAggregateTestCheckFunc (
292+ resource .TestCheckOutput ("test" , "false" ),
293+ ),
294+ },
295+ {
296+ Config : `
297+ resource "wireguard_asymmetric_key" "main" {}
298+
299+ data "wireguard_config_document" "main" {
300+ private_key = wireguard_asymmetric_key.main.private_key
301+ }
302+
303+ output "test" {
304+ // .addresses is always null in this configuration
305+ value = provider::assert::not_null(data.wireguard_config_document.main.addresses)
238306}
239307 ` ,
240308 Check : resource .ComposeAggregateTestCheckFunc (
0 commit comments