@ -1,6 +1,7 @@
package globalref
import (
"sort"
"testing"
"github.com/google/go-cmp/cmp"
@ -94,3 +95,96 @@ func TestAnalyzerContributingResources(t *testing.T) {
} )
}
}
func TestAnalyzerContributingResourceAttrs ( t * testing . T ) {
azr := testAnalyzer ( t , "contributing-resources" )
tests := map [ string ] struct {
StartRefs func ( ) [ ] Reference
WantAttrs [ ] string
} {
"root output 'network'" : {
func ( ) [ ] Reference {
return azr . ReferencesFromOutputValue (
addrs . OutputValue { Name : "network" } . Absolute ( addrs . RootModuleInstance ) ,
)
} ,
[ ] string {
` data.test_thing.environment.any.base_cidr_block ` ,
` data.test_thing.environment.any.subnet_count ` ,
` module.network.test_thing.subnet ` ,
` module.network.test_thing.vpc.string ` ,
} ,
} ,
"root output 'c10s_url'" : {
func ( ) [ ] Reference {
return azr . ReferencesFromOutputValue (
addrs . OutputValue { Name : "c10s_url" } . Absolute ( addrs . RootModuleInstance ) ,
)
} ,
[ ] string {
` data.test_thing.environment.any.base_cidr_block ` ,
` data.test_thing.environment.any.subnet_count ` ,
` module.compute.test_thing.load_balancer.string ` ,
` module.network.test_thing.subnet ` ,
` module.network.test_thing.vpc.string ` ,
} ,
} ,
"module.compute.test_thing.load_balancer" : {
func ( ) [ ] Reference {
return azr . ReferencesFromResourceInstance (
addrs . Resource {
Mode : addrs . ManagedResourceMode ,
Type : "test_thing" ,
Name : "load_balancer" ,
} . Instance ( addrs . NoKey ) . Absolute ( addrs . RootModuleInstance . Child ( "compute" , addrs . NoKey ) ) ,
)
} ,
[ ] string {
` data.test_thing.environment.any.base_cidr_block ` ,
` data.test_thing.environment.any.subnet_count ` ,
` module.compute.test_thing.controller ` ,
` module.network.test_thing.subnet ` ,
` module.network.test_thing.vpc.string ` ,
} ,
} ,
"data.test_thing.environment" : {
func ( ) [ ] Reference {
return azr . ReferencesFromResourceInstance (
addrs . Resource {
Mode : addrs . DataResourceMode ,
Type : "test_thing" ,
Name : "environment" ,
} . Instance ( addrs . NoKey ) . Absolute ( addrs . RootModuleInstance ) ,
)
} ,
[ ] string {
// Nothing! This one only refers to an input variable.
} ,
} ,
}
for name , test := range tests {
t . Run ( name , func ( t * testing . T ) {
startRefs := test . StartRefs ( )
refs := azr . ContributingResourceReferences ( startRefs ... )
want := test . WantAttrs
got := make ( [ ] string , len ( refs ) )
for i , ref := range refs {
resAttr , ok := ref . ResourceAttr ( )
if ! ok {
t . Errorf ( "%s is not a resource attr reference" , resAttr . DebugString ( ) )
continue
}
got [ i ] = resAttr . DebugString ( )
}
sort . Strings ( got )
if diff := cmp . Diff ( want , got ) ; diff != "" {
t . Errorf ( "wrong addresses\n%s" , diff )
}
} )
}
}