github.phpd.cn/hashicorp/packer@v1.3.2/builder/azure/arm/resource_resolver_test.go (about) 1 package arm 2 3 import ( 4 "testing" 5 ) 6 7 func TestResourceResolverIgnoresEmptyVirtualNetworkName(t *testing.T) { 8 c, _, _ := newConfig(getArmBuilderConfiguration(), getPackerConfiguration()) 9 if c.VirtualNetworkName != "" { 10 t.Fatalf("Expected VirtualNetworkName to be empty by default") 11 } 12 13 sut := newTestResourceResolver() 14 sut.findVirtualNetworkResourceGroup = nil // assert that this is not even called 15 sut.Resolve(c) 16 17 if c.VirtualNetworkName != "" { 18 t.Fatalf("Expected VirtualNetworkName to be empty") 19 } 20 if c.VirtualNetworkResourceGroupName != "" { 21 t.Fatalf("Expected VirtualNetworkResourceGroupName to be empty") 22 } 23 } 24 25 // If the user fully specified the virtual network name and resource group then 26 // there is no need to do a lookup. 27 func TestResourceResolverIgnoresSetVirtualNetwork(t *testing.T) { 28 c, _, _ := newConfig(getArmBuilderConfiguration(), getPackerConfiguration()) 29 c.VirtualNetworkName = "--virtual-network-name--" 30 c.VirtualNetworkResourceGroupName = "--virtual-network-resource-group-name--" 31 c.VirtualNetworkSubnetName = "--virtual-network-subnet-name--" 32 33 sut := newTestResourceResolver() 34 sut.findVirtualNetworkResourceGroup = nil // assert that this is not even called 35 sut.findVirtualNetworkSubnet = nil // assert that this is not even called 36 sut.Resolve(c) 37 38 if c.VirtualNetworkName != "--virtual-network-name--" { 39 t.Fatalf("Expected VirtualNetworkName to be --virtual-network-name--") 40 } 41 if c.VirtualNetworkResourceGroupName != "--virtual-network-resource-group-name--" { 42 t.Fatalf("Expected VirtualNetworkResourceGroupName to be --virtual-network-resource-group-name--") 43 } 44 if c.VirtualNetworkSubnetName != "--virtual-network-subnet-name--" { 45 t.Fatalf("Expected VirtualNetworkSubnetName to be --virtual-network-subnet-name--") 46 } 47 } 48 49 // If the user set virtual network name then the code should resolve virtual network 50 // resource group name. 51 func TestResourceResolverSetVirtualNetworkResourceGroupName(t *testing.T) { 52 c, _, _ := newConfig(getArmBuilderConfiguration(), getPackerConfiguration()) 53 c.VirtualNetworkName = "--virtual-network-name--" 54 55 sut := newTestResourceResolver() 56 sut.Resolve(c) 57 58 if c.VirtualNetworkResourceGroupName != "findVirtualNetworkResourceGroup is mocked" { 59 t.Fatalf("Expected VirtualNetworkResourceGroupName to be 'findVirtualNetworkResourceGroup is mocked'") 60 } 61 if c.VirtualNetworkSubnetName != "findVirtualNetworkSubnet is mocked" { 62 t.Fatalf("Expected findVirtualNetworkSubnet to be 'findVirtualNetworkSubnet is mocked'") 63 } 64 } 65 66 func newTestResourceResolver() resourceResolver { 67 return resourceResolver{ 68 client: nil, 69 findVirtualNetworkResourceGroup: func(*AzureClient, string) (string, error) { 70 return "findVirtualNetworkResourceGroup is mocked", nil 71 }, 72 findVirtualNetworkSubnet: func(*AzureClient, string, string) (string, error) { 73 return "findVirtualNetworkSubnet is mocked", nil 74 }, 75 } 76 }