2022-03-22 11:03:15 -04:00
|
|
|
package azure
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
|
2022-07-27 16:02:33 -04:00
|
|
|
armcomputev2 "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/compute/armcompute/v2"
|
2022-03-22 11:03:15 -04:00
|
|
|
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/network/armnetwork"
|
2022-06-28 10:08:05 -04:00
|
|
|
"github.com/edgelesssys/constellation/internal/cloud/metadata"
|
2022-03-22 11:03:15 -04:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestList(t *testing.T) {
|
2022-06-28 10:08:05 -04:00
|
|
|
wantInstances := []metadata.InstanceMetadata{
|
2022-03-22 11:03:15 -04:00
|
|
|
{
|
|
|
|
Name: "scale-set-name-instance-id",
|
|
|
|
ProviderID: "azure:///subscriptions/subscription-id/resourceGroups/resource-group/providers/Microsoft.Compute/virtualMachineScaleSets/scale-set-name/virtualMachines/instance-id",
|
2022-08-04 05:08:20 -04:00
|
|
|
VPCIP: "192.0.2.0",
|
2022-03-22 11:03:15 -04:00
|
|
|
SSHKeys: map[string][]string{"user": {"key-data"}},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
testCases := map[string]struct {
|
|
|
|
imdsAPI imdsAPI
|
|
|
|
networkInterfacesAPI networkInterfacesAPI
|
|
|
|
scaleSetsAPI scaleSetsAPI
|
|
|
|
virtualMachineScaleSetVMsAPI virtualMachineScaleSetVMsAPI
|
|
|
|
tagsAPI tagsAPI
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr bool
|
2022-06-28 10:08:05 -04:00
|
|
|
wantInstances []metadata.InstanceMetadata
|
2022-03-22 11:03:15 -04:00
|
|
|
}{
|
|
|
|
"List works": {
|
2022-06-29 10:17:23 -04:00
|
|
|
imdsAPI: newScaleSetIMDSStub(),
|
2022-03-22 11:03:15 -04:00
|
|
|
networkInterfacesAPI: newNetworkInterfacesStub(),
|
|
|
|
scaleSetsAPI: newScaleSetsStub(),
|
|
|
|
virtualMachineScaleSetVMsAPI: newVirtualMachineScaleSetsVMsStub(),
|
|
|
|
tagsAPI: newTagsStub(),
|
2022-04-26 10:54:05 -04:00
|
|
|
wantInstances: wantInstances,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"providerID cannot be retrieved": {
|
2022-04-26 10:54:05 -04:00
|
|
|
imdsAPI: &stubIMDSAPI{retrieveErr: errors.New("imds err")},
|
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"providerID cannot be parsed": {
|
2022-04-26 10:54:05 -04:00
|
|
|
imdsAPI: newInvalidIMDSStub(),
|
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"listScaleSetVMs fails": {
|
2022-06-29 10:17:23 -04:00
|
|
|
imdsAPI: newScaleSetIMDSStub(),
|
2022-03-22 11:03:15 -04:00
|
|
|
networkInterfacesAPI: newNetworkInterfacesStub(),
|
|
|
|
scaleSetsAPI: newScaleSetsStub(),
|
|
|
|
virtualMachineScaleSetVMsAPI: newFailingListsVirtualMachineScaleSetsVMsStub(),
|
|
|
|
tagsAPI: newTagsStub(),
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
require := require.New(t)
|
|
|
|
|
2022-06-28 10:08:05 -04:00
|
|
|
azureMetadata := Metadata{
|
2022-03-22 11:03:15 -04:00
|
|
|
imdsAPI: tc.imdsAPI,
|
|
|
|
networkInterfacesAPI: tc.networkInterfacesAPI,
|
|
|
|
scaleSetsAPI: tc.scaleSetsAPI,
|
|
|
|
virtualMachineScaleSetVMsAPI: tc.virtualMachineScaleSetVMsAPI,
|
|
|
|
tagsAPI: tc.tagsAPI,
|
|
|
|
}
|
2022-06-28 10:08:05 -04:00
|
|
|
instances, err := azureMetadata.List(context.Background())
|
2022-03-22 11:03:15 -04:00
|
|
|
|
2022-04-26 10:54:05 -04:00
|
|
|
if tc.wantErr {
|
2022-03-22 11:03:15 -04:00
|
|
|
assert.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
require.NoError(err)
|
2022-04-26 10:54:05 -04:00
|
|
|
assert.ElementsMatch(tc.wantInstances, instances)
|
2022-03-22 11:03:15 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSelf(t *testing.T) {
|
2022-06-28 10:08:05 -04:00
|
|
|
wantScaleSetInstance := metadata.InstanceMetadata{
|
2022-03-22 11:03:15 -04:00
|
|
|
Name: "scale-set-name-instance-id",
|
|
|
|
ProviderID: "azure:///subscriptions/subscription-id/resourceGroups/resource-group/providers/Microsoft.Compute/virtualMachineScaleSets/scale-set-name/virtualMachines/instance-id",
|
2022-08-04 05:08:20 -04:00
|
|
|
VPCIP: "192.0.2.0",
|
2022-03-22 11:03:15 -04:00
|
|
|
SSHKeys: map[string][]string{"user": {"key-data"}},
|
|
|
|
}
|
|
|
|
testCases := map[string]struct {
|
|
|
|
imdsAPI imdsAPI
|
|
|
|
networkInterfacesAPI networkInterfacesAPI
|
|
|
|
virtualMachineScaleSetVMsAPI virtualMachineScaleSetVMsAPI
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr bool
|
2022-06-28 10:08:05 -04:00
|
|
|
wantInstance metadata.InstanceMetadata
|
2022-03-22 11:03:15 -04:00
|
|
|
}{
|
|
|
|
"self for scale set instance works": {
|
|
|
|
imdsAPI: newScaleSetIMDSStub(),
|
|
|
|
networkInterfacesAPI: newNetworkInterfacesStub(),
|
|
|
|
virtualMachineScaleSetVMsAPI: newVirtualMachineScaleSetsVMsStub(),
|
2022-04-26 10:54:05 -04:00
|
|
|
wantInstance: wantScaleSetInstance,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"providerID cannot be retrieved": {
|
2022-04-26 10:54:05 -04:00
|
|
|
imdsAPI: &stubIMDSAPI{retrieveErr: errors.New("imds err")},
|
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"GetInstance fails": {
|
2022-06-29 10:17:23 -04:00
|
|
|
imdsAPI: newScaleSetIMDSStub(),
|
|
|
|
virtualMachineScaleSetVMsAPI: &stubVirtualMachineScaleSetVMsAPI{getErr: errors.New("failed")},
|
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
metadata := Metadata{
|
|
|
|
imdsAPI: tc.imdsAPI,
|
|
|
|
networkInterfacesAPI: tc.networkInterfacesAPI,
|
|
|
|
virtualMachineScaleSetVMsAPI: tc.virtualMachineScaleSetVMsAPI,
|
|
|
|
}
|
|
|
|
instance, err := metadata.Self(context.Background())
|
|
|
|
|
2022-04-26 10:54:05 -04:00
|
|
|
if tc.wantErr {
|
2022-03-22 11:03:15 -04:00
|
|
|
assert.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
require.NoError(err)
|
2022-04-26 10:54:05 -04:00
|
|
|
assert.Equal(tc.wantInstance, instance)
|
2022-03-22 11:03:15 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-24 04:04:42 -04:00
|
|
|
func TestGetNetworkSecurityGroupName(t *testing.T) {
|
|
|
|
name := "network-security-group-name"
|
|
|
|
testCases := map[string]struct {
|
|
|
|
securityGroupsAPI securityGroupsAPI
|
|
|
|
imdsAPI imdsAPI
|
|
|
|
wantName string
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
"GetNetworkSecurityGroupName works": {
|
2022-06-29 10:17:23 -04:00
|
|
|
imdsAPI: newScaleSetIMDSStub(),
|
2022-05-24 04:04:42 -04:00
|
|
|
securityGroupsAPI: &stubSecurityGroupsAPI{
|
2022-07-27 16:02:33 -04:00
|
|
|
pager: &stubSecurityGroupsClientListPager{
|
|
|
|
list: []armnetwork.SecurityGroup{{Name: to.Ptr(name)}},
|
2022-05-24 04:04:42 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
wantName: name,
|
|
|
|
},
|
|
|
|
"no security group": {
|
2022-07-27 16:02:33 -04:00
|
|
|
imdsAPI: newScaleSetIMDSStub(),
|
|
|
|
securityGroupsAPI: &stubSecurityGroupsAPI{
|
|
|
|
pager: &stubSecurityGroupsClientListPager{},
|
|
|
|
},
|
|
|
|
wantErr: true,
|
2022-05-24 04:04:42 -04:00
|
|
|
},
|
|
|
|
"missing name in security group struct": {
|
2022-07-27 16:02:33 -04:00
|
|
|
imdsAPI: newScaleSetIMDSStub(),
|
|
|
|
securityGroupsAPI: &stubSecurityGroupsAPI{
|
|
|
|
pager: &stubSecurityGroupsClientListPager{
|
|
|
|
list: []armnetwork.SecurityGroup{{}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
wantErr: true,
|
2022-05-24 04:04:42 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
metadata := Metadata{
|
|
|
|
imdsAPI: tc.imdsAPI,
|
|
|
|
securityGroupsAPI: tc.securityGroupsAPI,
|
|
|
|
}
|
|
|
|
name, err := metadata.GetNetworkSecurityGroupName(context.Background())
|
|
|
|
if tc.wantErr {
|
|
|
|
assert.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
require.NoError(err)
|
|
|
|
assert.Equal(tc.wantName, name)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetSubnetworkCIDR(t *testing.T) {
|
|
|
|
subnetworkCIDR := "192.0.2.0/24"
|
|
|
|
name := "name"
|
|
|
|
testCases := map[string]struct {
|
|
|
|
virtualNetworksAPI virtualNetworksAPI
|
|
|
|
imdsAPI imdsAPI
|
|
|
|
wantNetworkCIDR string
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
"GetSubnetworkCIDR works": {
|
2022-06-29 10:17:23 -04:00
|
|
|
imdsAPI: newScaleSetIMDSStub(),
|
2022-07-27 16:02:33 -04:00
|
|
|
virtualNetworksAPI: &stubVirtualNetworksAPI{
|
|
|
|
pager: &stubVirtualNetworksClientListPager{
|
|
|
|
list: []armnetwork.VirtualNetwork{{
|
|
|
|
Name: to.Ptr(name),
|
2022-05-24 04:04:42 -04:00
|
|
|
Properties: &armnetwork.VirtualNetworkPropertiesFormat{
|
|
|
|
Subnets: []*armnetwork.Subnet{
|
2022-07-27 16:02:33 -04:00
|
|
|
{Properties: &armnetwork.SubnetPropertiesFormat{AddressPrefix: to.Ptr(subnetworkCIDR)}},
|
2022-05-24 04:04:42 -04:00
|
|
|
},
|
|
|
|
},
|
2022-07-27 16:02:33 -04:00
|
|
|
}},
|
2022-05-24 04:04:42 -04:00
|
|
|
},
|
2022-07-27 16:02:33 -04:00
|
|
|
},
|
2022-05-24 04:04:42 -04:00
|
|
|
wantNetworkCIDR: subnetworkCIDR,
|
|
|
|
},
|
|
|
|
"no virtual networks found": {
|
2022-06-29 10:17:23 -04:00
|
|
|
imdsAPI: newScaleSetIMDSStub(),
|
2022-07-27 16:02:33 -04:00
|
|
|
virtualNetworksAPI: &stubVirtualNetworksAPI{
|
|
|
|
pager: &stubVirtualNetworksClientListPager{},
|
|
|
|
},
|
2022-05-24 04:04:42 -04:00
|
|
|
wantErr: true,
|
|
|
|
wantNetworkCIDR: subnetworkCIDR,
|
|
|
|
},
|
|
|
|
"malformed network struct": {
|
2022-06-29 10:17:23 -04:00
|
|
|
imdsAPI: newScaleSetIMDSStub(),
|
2022-07-27 16:02:33 -04:00
|
|
|
virtualNetworksAPI: &stubVirtualNetworksAPI{
|
|
|
|
pager: &stubVirtualNetworksClientListPager{list: []armnetwork.VirtualNetwork{{}}},
|
|
|
|
},
|
2022-05-24 04:04:42 -04:00
|
|
|
wantErr: true,
|
|
|
|
wantNetworkCIDR: subnetworkCIDR,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
metadata := Metadata{
|
|
|
|
imdsAPI: tc.imdsAPI,
|
|
|
|
virtualNetworksAPI: tc.virtualNetworksAPI,
|
|
|
|
}
|
|
|
|
subnetworkCIDR, err := metadata.GetSubnetworkCIDR(context.Background())
|
|
|
|
if tc.wantErr {
|
|
|
|
assert.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
require.NoError(err)
|
|
|
|
assert.Equal(tc.wantNetworkCIDR, subnetworkCIDR)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetLoadBalancerName(t *testing.T) {
|
|
|
|
loadBalancerName := "load-balancer-name"
|
|
|
|
testCases := map[string]struct {
|
|
|
|
loadBalancerAPI loadBalancerAPI
|
|
|
|
imdsAPI imdsAPI
|
|
|
|
wantName string
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
"GetLoadBalancerName works": {
|
2022-06-29 10:17:23 -04:00
|
|
|
imdsAPI: newScaleSetIMDSStub(),
|
2022-05-24 04:04:42 -04:00
|
|
|
loadBalancerAPI: &stubLoadBalancersAPI{
|
2022-07-27 16:02:33 -04:00
|
|
|
pager: &stubLoadBalancersClientListPager{
|
|
|
|
list: []armnetwork.LoadBalancer{{
|
|
|
|
Name: to.Ptr(loadBalancerName),
|
|
|
|
Properties: &armnetwork.LoadBalancerPropertiesFormat{},
|
|
|
|
}},
|
2022-05-24 04:04:42 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
wantName: loadBalancerName,
|
|
|
|
},
|
|
|
|
"invalid load balancer struct": {
|
2022-07-27 16:02:33 -04:00
|
|
|
imdsAPI: newScaleSetIMDSStub(),
|
|
|
|
loadBalancerAPI: &stubLoadBalancersAPI{
|
|
|
|
pager: &stubLoadBalancersClientListPager{list: []armnetwork.LoadBalancer{{}}},
|
|
|
|
},
|
|
|
|
wantErr: true,
|
2022-05-24 04:04:42 -04:00
|
|
|
},
|
|
|
|
"invalid missing name": {
|
2022-06-29 10:17:23 -04:00
|
|
|
imdsAPI: newScaleSetIMDSStub(),
|
2022-07-27 16:02:33 -04:00
|
|
|
loadBalancerAPI: &stubLoadBalancersAPI{
|
|
|
|
pager: &stubLoadBalancersClientListPager{list: []armnetwork.LoadBalancer{{
|
|
|
|
Properties: &armnetwork.LoadBalancerPropertiesFormat{},
|
|
|
|
}}},
|
|
|
|
},
|
2022-05-24 04:04:42 -04:00
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
metadata := Metadata{
|
|
|
|
imdsAPI: tc.imdsAPI,
|
|
|
|
loadBalancerAPI: tc.loadBalancerAPI,
|
|
|
|
}
|
|
|
|
loadbalancerName, err := metadata.GetLoadBalancerName(context.Background())
|
|
|
|
if tc.wantErr {
|
|
|
|
assert.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
require.NoError(err)
|
|
|
|
assert.Equal(tc.wantName, loadbalancerName)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetLoadBalancerIP(t *testing.T) {
|
|
|
|
loadBalancerName := "load-balancer-name"
|
|
|
|
publicIP := "192.0.2.1"
|
|
|
|
correctPublicIPID := "/subscriptions/subscription/resourceGroups/resourceGroup/providers/Microsoft.Network/publicIPAddresses/pubIPName"
|
|
|
|
someErr := errors.New("some error")
|
|
|
|
testCases := map[string]struct {
|
|
|
|
loadBalancerAPI loadBalancerAPI
|
|
|
|
publicIPAddressesAPI publicIPAddressesAPI
|
|
|
|
imdsAPI imdsAPI
|
|
|
|
wantIP string
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
"GetLoadBalancerIP works": {
|
2022-06-29 10:17:23 -04:00
|
|
|
imdsAPI: newScaleSetIMDSStub(),
|
2022-05-24 04:04:42 -04:00
|
|
|
loadBalancerAPI: &stubLoadBalancersAPI{
|
2022-07-27 16:02:33 -04:00
|
|
|
pager: &stubLoadBalancersClientListPager{
|
|
|
|
list: []armnetwork.LoadBalancer{{
|
|
|
|
Name: to.Ptr(loadBalancerName),
|
|
|
|
Properties: &armnetwork.LoadBalancerPropertiesFormat{
|
|
|
|
FrontendIPConfigurations: []*armnetwork.FrontendIPConfiguration{
|
|
|
|
{
|
|
|
|
Properties: &armnetwork.FrontendIPConfigurationPropertiesFormat{
|
|
|
|
PublicIPAddress: &armnetwork.PublicIPAddress{ID: &correctPublicIPID},
|
2022-05-24 04:04:42 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-07-27 16:02:33 -04:00
|
|
|
}},
|
2022-05-24 04:04:42 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
publicIPAddressesAPI: &stubPublicIPAddressesAPI{getResponse: armnetwork.PublicIPAddressesClientGetResponse{
|
2022-07-27 16:02:33 -04:00
|
|
|
PublicIPAddress: armnetwork.PublicIPAddress{
|
|
|
|
Properties: &armnetwork.PublicIPAddressPropertiesFormat{
|
|
|
|
IPAddress: &publicIP,
|
2022-05-24 04:04:42 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}},
|
|
|
|
wantIP: publicIP,
|
|
|
|
},
|
|
|
|
"no load balancer": {
|
2022-06-29 10:17:23 -04:00
|
|
|
imdsAPI: newScaleSetIMDSStub(),
|
2022-05-24 04:04:42 -04:00
|
|
|
loadBalancerAPI: &stubLoadBalancersAPI{
|
2022-07-27 16:02:33 -04:00
|
|
|
pager: &stubLoadBalancersClientListPager{},
|
2022-05-24 04:04:42 -04:00
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"load balancer missing public IP reference": {
|
2022-06-29 10:17:23 -04:00
|
|
|
imdsAPI: newScaleSetIMDSStub(),
|
2022-05-24 04:04:42 -04:00
|
|
|
loadBalancerAPI: &stubLoadBalancersAPI{
|
2022-07-27 16:02:33 -04:00
|
|
|
pager: &stubLoadBalancersClientListPager{
|
|
|
|
list: []armnetwork.LoadBalancer{{
|
|
|
|
Name: to.Ptr(loadBalancerName),
|
|
|
|
Properties: &armnetwork.LoadBalancerPropertiesFormat{
|
|
|
|
FrontendIPConfigurations: []*armnetwork.FrontendIPConfiguration{},
|
2022-05-24 04:04:42 -04:00
|
|
|
},
|
2022-07-27 16:02:33 -04:00
|
|
|
}},
|
2022-05-24 04:04:42 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"public IP reference has wrong format": {
|
2022-06-29 10:17:23 -04:00
|
|
|
imdsAPI: newScaleSetIMDSStub(),
|
2022-05-24 04:04:42 -04:00
|
|
|
loadBalancerAPI: &stubLoadBalancersAPI{
|
2022-07-27 16:02:33 -04:00
|
|
|
pager: &stubLoadBalancersClientListPager{
|
|
|
|
list: []armnetwork.LoadBalancer{{
|
|
|
|
Name: to.Ptr(loadBalancerName),
|
|
|
|
Properties: &armnetwork.LoadBalancerPropertiesFormat{
|
|
|
|
FrontendIPConfigurations: []*armnetwork.FrontendIPConfiguration{
|
|
|
|
{
|
|
|
|
Properties: &armnetwork.FrontendIPConfigurationPropertiesFormat{
|
|
|
|
PublicIPAddress: &armnetwork.PublicIPAddress{
|
|
|
|
ID: to.Ptr("wrong-format"),
|
2022-05-24 04:04:42 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-07-27 16:02:33 -04:00
|
|
|
}},
|
2022-05-24 04:04:42 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"no public IP address found": {
|
2022-06-29 10:17:23 -04:00
|
|
|
imdsAPI: newScaleSetIMDSStub(),
|
2022-05-24 04:04:42 -04:00
|
|
|
loadBalancerAPI: &stubLoadBalancersAPI{
|
2022-07-27 16:02:33 -04:00
|
|
|
pager: &stubLoadBalancersClientListPager{
|
|
|
|
list: []armnetwork.LoadBalancer{{
|
|
|
|
Name: to.Ptr(loadBalancerName),
|
|
|
|
Properties: &armnetwork.LoadBalancerPropertiesFormat{
|
|
|
|
FrontendIPConfigurations: []*armnetwork.FrontendIPConfiguration{
|
|
|
|
{
|
|
|
|
Properties: &armnetwork.FrontendIPConfigurationPropertiesFormat{
|
|
|
|
PublicIPAddress: &armnetwork.PublicIPAddress{ID: &correctPublicIPID},
|
2022-05-24 04:04:42 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-07-27 16:02:33 -04:00
|
|
|
}},
|
2022-05-24 04:04:42 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
publicIPAddressesAPI: &stubPublicIPAddressesAPI{getErr: someErr},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"found public IP has no address field": {
|
2022-06-29 10:17:23 -04:00
|
|
|
imdsAPI: newScaleSetIMDSStub(),
|
2022-05-24 04:04:42 -04:00
|
|
|
loadBalancerAPI: &stubLoadBalancersAPI{
|
2022-07-27 16:02:33 -04:00
|
|
|
pager: &stubLoadBalancersClientListPager{
|
|
|
|
list: []armnetwork.LoadBalancer{{
|
|
|
|
Name: to.Ptr(loadBalancerName),
|
|
|
|
Properties: &armnetwork.LoadBalancerPropertiesFormat{
|
|
|
|
FrontendIPConfigurations: []*armnetwork.FrontendIPConfiguration{
|
|
|
|
{
|
|
|
|
Properties: &armnetwork.FrontendIPConfigurationPropertiesFormat{
|
|
|
|
PublicIPAddress: &armnetwork.PublicIPAddress{ID: &correctPublicIPID},
|
2022-05-24 04:04:42 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-07-27 16:02:33 -04:00
|
|
|
}},
|
2022-05-24 04:04:42 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
publicIPAddressesAPI: &stubPublicIPAddressesAPI{getResponse: armnetwork.PublicIPAddressesClientGetResponse{
|
2022-07-27 16:02:33 -04:00
|
|
|
PublicIPAddress: armnetwork.PublicIPAddress{
|
|
|
|
Properties: &armnetwork.PublicIPAddressPropertiesFormat{},
|
2022-05-24 04:04:42 -04:00
|
|
|
},
|
|
|
|
}},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
metadata := Metadata{
|
|
|
|
imdsAPI: tc.imdsAPI,
|
|
|
|
loadBalancerAPI: tc.loadBalancerAPI,
|
|
|
|
publicIPAddressesAPI: tc.publicIPAddressesAPI,
|
|
|
|
}
|
|
|
|
loadbalancerName, err := metadata.GetLoadBalancerIP(context.Background())
|
|
|
|
if tc.wantErr {
|
|
|
|
assert.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
require.NoError(err)
|
|
|
|
assert.Equal(tc.wantIP, loadbalancerName)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-22 11:03:15 -04:00
|
|
|
func TestMetadataSupported(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
metadata := Metadata{}
|
|
|
|
assert.True(metadata.Supported())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestProviderID(t *testing.T) {
|
|
|
|
testCases := map[string]struct {
|
2022-04-26 10:54:05 -04:00
|
|
|
imdsAPI imdsAPI
|
|
|
|
wantErr bool
|
|
|
|
wantProviderID string
|
2022-03-22 11:03:15 -04:00
|
|
|
}{
|
|
|
|
"providerID for scale set instance works": {
|
2022-04-26 10:54:05 -04:00
|
|
|
imdsAPI: newScaleSetIMDSStub(),
|
|
|
|
wantProviderID: "azure:///subscriptions/subscription-id/resourceGroups/resource-group/providers/Microsoft.Compute/virtualMachineScaleSets/scale-set-name/virtualMachines/instance-id",
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"imds retrieval fails": {
|
2022-04-26 10:54:05 -04:00
|
|
|
imdsAPI: &stubIMDSAPI{retrieveErr: errors.New("imds err")},
|
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
metadata := Metadata{
|
|
|
|
imdsAPI: tc.imdsAPI,
|
|
|
|
}
|
|
|
|
providerID, err := metadata.providerID(context.Background())
|
|
|
|
|
2022-04-26 10:54:05 -04:00
|
|
|
if tc.wantErr {
|
2022-03-22 11:03:15 -04:00
|
|
|
assert.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
require.NoError(err)
|
2022-04-26 10:54:05 -04:00
|
|
|
assert.Equal(tc.wantProviderID, providerID)
|
2022-03-22 11:03:15 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-29 10:30:24 -04:00
|
|
|
func TestUID(t *testing.T) {
|
|
|
|
testCases := map[string]struct {
|
|
|
|
imdsAPI imdsAPI
|
|
|
|
wantErr bool
|
|
|
|
wantUID string
|
|
|
|
}{
|
|
|
|
"uid extraction from providerID works": {
|
|
|
|
imdsAPI: &stubIMDSAPI{
|
|
|
|
res: metadataResponse{Compute: struct {
|
|
|
|
ResourceID string `json:"resourceId,omitempty"`
|
|
|
|
}{"/subscriptions/subscription-id/resourceGroups/basename-uid/providers/Microsoft.Compute/virtualMachineScaleSets/scale-set-name/virtualMachines/instance-id"}},
|
|
|
|
},
|
|
|
|
wantUID: "uid",
|
|
|
|
},
|
|
|
|
"providerID does not contain uid": {
|
|
|
|
imdsAPI: &stubIMDSAPI{
|
|
|
|
res: metadataResponse{Compute: struct {
|
|
|
|
ResourceID string `json:"resourceId,omitempty"`
|
|
|
|
}{"/subscriptions/subscription-id/resourceGroups/invalid/providers/Microsoft.Compute/virtualMachineScaleSets/scale-set-name/virtualMachines/instance-id"}},
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"providerID is invalid": {
|
|
|
|
imdsAPI: newInvalidIMDSStub(),
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"imds retrieval fails": {
|
|
|
|
imdsAPI: &stubIMDSAPI{retrieveErr: errors.New("imds err")},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
metadata := Metadata{
|
|
|
|
imdsAPI: tc.imdsAPI,
|
|
|
|
}
|
|
|
|
uid, err := metadata.UID(context.Background())
|
|
|
|
|
|
|
|
if tc.wantErr {
|
|
|
|
assert.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
require.NoError(err)
|
|
|
|
assert.Equal(tc.wantUID, uid)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-22 11:03:15 -04:00
|
|
|
func TestExtractInstanceTags(t *testing.T) {
|
|
|
|
testCases := map[string]struct {
|
2022-04-26 10:54:05 -04:00
|
|
|
in map[string]*string
|
|
|
|
wantTags map[string]string
|
2022-03-22 11:03:15 -04:00
|
|
|
}{
|
|
|
|
"tags are extracted": {
|
2022-07-27 16:02:33 -04:00
|
|
|
in: map[string]*string{"key": to.Ptr("value")},
|
2022-04-26 10:54:05 -04:00
|
|
|
wantTags: map[string]string{"key": "value"},
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"nil values are skipped": {
|
2022-04-26 10:54:05 -04:00
|
|
|
in: map[string]*string{"key": nil},
|
|
|
|
wantTags: map[string]string{},
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
tags := extractInstanceTags(tc.in)
|
|
|
|
|
2022-04-26 10:54:05 -04:00
|
|
|
assert.Equal(tc.wantTags, tags)
|
2022-03-22 11:03:15 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestExtractSSHKeys(t *testing.T) {
|
|
|
|
testCases := map[string]struct {
|
2022-07-27 16:02:33 -04:00
|
|
|
in armcomputev2.SSHConfiguration
|
2022-04-26 10:54:05 -04:00
|
|
|
wantKeys map[string][]string
|
2022-03-22 11:03:15 -04:00
|
|
|
}{
|
|
|
|
"ssh key is extracted": {
|
2022-07-27 16:02:33 -04:00
|
|
|
in: armcomputev2.SSHConfiguration{
|
|
|
|
PublicKeys: []*armcomputev2.SSHPublicKey{
|
2022-03-22 11:03:15 -04:00
|
|
|
{
|
2022-07-27 16:02:33 -04:00
|
|
|
KeyData: to.Ptr("key-data"),
|
|
|
|
Path: to.Ptr("/home/user/.ssh/authorized_keys"),
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-04-26 10:54:05 -04:00
|
|
|
wantKeys: map[string][]string{"user": {"key-data"}},
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"invalid path is skipped": {
|
2022-07-27 16:02:33 -04:00
|
|
|
in: armcomputev2.SSHConfiguration{
|
|
|
|
PublicKeys: []*armcomputev2.SSHPublicKey{
|
2022-03-22 11:03:15 -04:00
|
|
|
{
|
2022-07-27 16:02:33 -04:00
|
|
|
KeyData: to.Ptr("key-data"),
|
|
|
|
Path: to.Ptr("invalid-path"),
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-04-26 10:54:05 -04:00
|
|
|
wantKeys: map[string][]string{},
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"key data is nil": {
|
2022-07-27 16:02:33 -04:00
|
|
|
in: armcomputev2.SSHConfiguration{
|
|
|
|
PublicKeys: []*armcomputev2.SSHPublicKey{
|
2022-03-22 11:03:15 -04:00
|
|
|
{
|
2022-07-27 16:02:33 -04:00
|
|
|
Path: to.Ptr("/home/user/.ssh/authorized_keys"),
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-04-26 10:54:05 -04:00
|
|
|
wantKeys: map[string][]string{},
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"path is nil": {
|
2022-07-27 16:02:33 -04:00
|
|
|
in: armcomputev2.SSHConfiguration{
|
|
|
|
PublicKeys: []*armcomputev2.SSHPublicKey{
|
2022-03-22 11:03:15 -04:00
|
|
|
{
|
2022-07-27 16:02:33 -04:00
|
|
|
KeyData: to.Ptr("key-data"),
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-04-26 10:54:05 -04:00
|
|
|
wantKeys: map[string][]string{},
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"public keys are nil": {
|
2022-07-27 16:02:33 -04:00
|
|
|
in: armcomputev2.SSHConfiguration{},
|
2022-04-26 10:54:05 -04:00
|
|
|
wantKeys: map[string][]string{},
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
keys := extractSSHKeys(tc.in)
|
|
|
|
|
2022-04-26 10:54:05 -04:00
|
|
|
assert.Equal(tc.wantKeys, keys)
|
2022-03-22 11:03:15 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func newScaleSetIMDSStub() *stubIMDSAPI {
|
|
|
|
return &stubIMDSAPI{
|
|
|
|
res: metadataResponse{Compute: struct {
|
|
|
|
ResourceID string `json:"resourceId,omitempty"`
|
|
|
|
}{"/subscriptions/subscription-id/resourceGroups/resource-group/providers/Microsoft.Compute/virtualMachineScaleSets/scale-set-name/virtualMachines/instance-id"}},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func newInvalidIMDSStub() *stubIMDSAPI {
|
|
|
|
return &stubIMDSAPI{
|
|
|
|
res: metadataResponse{Compute: struct {
|
|
|
|
ResourceID string `json:"resourceId,omitempty"`
|
|
|
|
}{"invalid-resource-id"}},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func newNetworkInterfacesStub() *stubNetworkInterfacesAPI {
|
|
|
|
return &stubNetworkInterfacesAPI{
|
|
|
|
getInterface: armnetwork.Interface{
|
2022-07-27 16:02:33 -04:00
|
|
|
Name: to.Ptr("interface-name"),
|
2022-03-22 11:03:15 -04:00
|
|
|
Properties: &armnetwork.InterfacePropertiesFormat{
|
|
|
|
IPConfigurations: []*armnetwork.InterfaceIPConfiguration{
|
|
|
|
{
|
|
|
|
Properties: &armnetwork.InterfaceIPConfigurationPropertiesFormat{
|
2022-07-27 16:02:33 -04:00
|
|
|
PrivateIPAddress: to.Ptr("192.0.2.0"),
|
|
|
|
Primary: to.Ptr(true),
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func newScaleSetsStub() *stubScaleSetsAPI {
|
|
|
|
return &stubScaleSetsAPI{
|
2022-07-27 16:02:33 -04:00
|
|
|
pager: &stubVirtualMachineScaleSetsClientListPager{
|
|
|
|
list: []armcomputev2.VirtualMachineScaleSet{{
|
|
|
|
Name: to.Ptr("scale-set-name"),
|
|
|
|
}},
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func newVirtualMachineScaleSetsVMsStub() *stubVirtualMachineScaleSetVMsAPI {
|
|
|
|
return &stubVirtualMachineScaleSetVMsAPI{
|
2022-07-27 16:02:33 -04:00
|
|
|
getVM: armcomputev2.VirtualMachineScaleSetVM{
|
|
|
|
Name: to.Ptr("scale-set-name_instance-id"),
|
|
|
|
InstanceID: to.Ptr("instance-id"),
|
|
|
|
ID: to.Ptr("/subscriptions/subscription-id/resourceGroups/resource-group/providers/Microsoft.Compute/virtualMachineScaleSets/scale-set-name/virtualMachines/instance-id"),
|
|
|
|
Properties: &armcomputev2.VirtualMachineScaleSetVMProperties{
|
|
|
|
NetworkProfile: &armcomputev2.NetworkProfile{
|
|
|
|
NetworkInterfaces: []*armcomputev2.NetworkInterfaceReference{
|
2022-03-22 11:03:15 -04:00
|
|
|
{
|
2022-07-27 16:02:33 -04:00
|
|
|
ID: to.Ptr("/subscriptions/subscription-id/resourceGroups/resource-group/providers/Microsoft.Compute/virtualMachineScaleSets/scale-set-name/virtualMachines/instance-id/networkInterfaces/interface-name"),
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-07-27 16:02:33 -04:00
|
|
|
OSProfile: &armcomputev2.OSProfile{
|
|
|
|
ComputerName: to.Ptr("scale-set-name-instance-id"),
|
|
|
|
LinuxConfiguration: &armcomputev2.LinuxConfiguration{
|
|
|
|
SSH: &armcomputev2.SSHConfiguration{
|
|
|
|
PublicKeys: []*armcomputev2.SSHPublicKey{
|
2022-03-22 11:03:15 -04:00
|
|
|
{
|
2022-07-27 16:02:33 -04:00
|
|
|
KeyData: to.Ptr("key-data"),
|
|
|
|
Path: to.Ptr("/home/user/.ssh/authorized_keys"),
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-07-27 16:02:33 -04:00
|
|
|
pager: &stubVirtualMachineScaleSetVMPager{
|
|
|
|
list: []armcomputev2.VirtualMachineScaleSetVM{{
|
|
|
|
Name: to.Ptr("scale-set-name_instance-id"),
|
|
|
|
InstanceID: to.Ptr("instance-id"),
|
|
|
|
ID: to.Ptr("/subscriptions/subscription-id/resourceGroups/resource-group/providers/Microsoft.Compute/virtualMachineScaleSets/scale-set-name/virtualMachines/instance-id"),
|
|
|
|
Properties: &armcomputev2.VirtualMachineScaleSetVMProperties{
|
|
|
|
NetworkProfile: &armcomputev2.NetworkProfile{
|
|
|
|
NetworkInterfaces: []*armcomputev2.NetworkInterfaceReference{
|
|
|
|
{
|
|
|
|
ID: to.Ptr("/subscriptions/subscription-id/resourceGroups/resource-group/providers/Microsoft.Compute/virtualMachineScaleSets/scale-set-name/virtualMachines/instance-id/networkInterfaces/interface-name"),
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
},
|
2022-07-27 16:02:33 -04:00
|
|
|
},
|
|
|
|
OSProfile: &armcomputev2.OSProfile{
|
|
|
|
ComputerName: to.Ptr("scale-set-name-instance-id"),
|
|
|
|
LinuxConfiguration: &armcomputev2.LinuxConfiguration{
|
|
|
|
SSH: &armcomputev2.SSHConfiguration{
|
|
|
|
PublicKeys: []*armcomputev2.SSHPublicKey{
|
|
|
|
{
|
|
|
|
KeyData: to.Ptr("key-data"),
|
|
|
|
Path: to.Ptr("/home/user/.ssh/authorized_keys"),
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-07-27 16:02:33 -04:00
|
|
|
}},
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func newFailingListsVirtualMachineScaleSetsVMsStub() *stubVirtualMachineScaleSetVMsAPI {
|
|
|
|
return &stubVirtualMachineScaleSetVMsAPI{
|
2022-07-27 16:02:33 -04:00
|
|
|
pager: &stubVirtualMachineScaleSetVMPager{
|
|
|
|
list: []armcomputev2.VirtualMachineScaleSetVM{{
|
|
|
|
InstanceID: to.Ptr("invalid-instance-id"),
|
|
|
|
}},
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func newTagsStub() *stubTagsAPI {
|
|
|
|
return &stubTagsAPI{}
|
|
|
|
}
|