2022-09-05 03:06:08 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
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-10-24 10:58:21 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/cloud"
|
2022-09-21 07:47:57 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/cloud/metadata"
|
2022-10-06 06:14:41 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/role"
|
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-10-06 06:14:41 -04:00
|
|
|
Role: role.Worker,
|
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-08-29 05:54:30 -04:00
|
|
|
imdsAPI: &stubIMDSAPI{resourceGroup: "resourceGroup"},
|
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
|
|
|
},
|
2022-08-29 05:54:30 -04:00
|
|
|
"imds resource group fails": {
|
|
|
|
imdsAPI: &stubIMDSAPI{resourceGroupErr: errors.New("failed")},
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"listScaleSetVMs fails": {
|
2022-08-29 05:54:30 -04:00
|
|
|
imdsAPI: &stubIMDSAPI{resourceGroup: "resourceGroup"},
|
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-10-06 06:14:41 -04:00
|
|
|
Role: role.Worker,
|
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": {
|
2022-08-29 05:54:30 -04:00
|
|
|
imdsAPI: &stubIMDSAPI{providerID: "/subscriptions/subscription-id/resourceGroups/resource-group/providers/Microsoft.Compute/virtualMachineScaleSets/scale-set-name/virtualMachines/instance-id"},
|
2022-03-22 11:03:15 -04:00
|
|
|
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-08-29 05:54:30 -04:00
|
|
|
imdsAPI: &stubIMDSAPI{providerIDErr: errors.New("failed")},
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"GetInstance fails": {
|
2022-08-29 05:54:30 -04:00
|
|
|
imdsAPI: &stubIMDSAPI{providerID: wantScaleSetInstance.ProviderID},
|
2022-06-29 10:17:23 -04:00
|
|
|
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-08-29 05:54:30 -04:00
|
|
|
imdsAPI: &stubIMDSAPI{resourceGroup: "resourceGroup"},
|
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-08-29 05:54:30 -04:00
|
|
|
imdsAPI: &stubIMDSAPI{resourceGroup: "resourceGroup"},
|
2022-07-27 16:02:33 -04:00
|
|
|
securityGroupsAPI: &stubSecurityGroupsAPI{
|
|
|
|
pager: &stubSecurityGroupsClientListPager{},
|
|
|
|
},
|
|
|
|
wantErr: true,
|
2022-05-24 04:04:42 -04:00
|
|
|
},
|
|
|
|
"missing name in security group struct": {
|
2022-08-29 05:54:30 -04:00
|
|
|
imdsAPI: &stubIMDSAPI{resourceGroup: "resourceGroup"},
|
2022-07-27 16:02:33 -04:00
|
|
|
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-08-29 05:54:30 -04:00
|
|
|
imdsAPI: &stubIMDSAPI{resourceGroup: "resourceGroup"},
|
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-08-29 05:54:30 -04:00
|
|
|
imdsAPI: &stubIMDSAPI{resourceGroup: "resourceGroup"},
|
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-08-29 05:54:30 -04:00
|
|
|
imdsAPI: &stubIMDSAPI{resourceGroup: "resourceGroup"},
|
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,
|
|
|
|
}
|
2022-10-24 18:49:58 -04:00
|
|
|
subnetworkCIDR, err := metadata.getSubnetworkCIDR(context.Background())
|
2022-05-24 04:04:42 -04:00
|
|
|
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-08-29 05:54:30 -04:00
|
|
|
imdsAPI: &stubIMDSAPI{resourceGroup: "resourceGroup"},
|
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-08-29 05:54:30 -04:00
|
|
|
imdsAPI: &stubIMDSAPI{resourceGroup: "resourceGroup"},
|
2022-07-27 16:02:33 -04:00
|
|
|
loadBalancerAPI: &stubLoadBalancersAPI{
|
|
|
|
pager: &stubLoadBalancersClientListPager{list: []armnetwork.LoadBalancer{{}}},
|
|
|
|
},
|
|
|
|
wantErr: true,
|
2022-05-24 04:04:42 -04:00
|
|
|
},
|
|
|
|
"invalid missing name": {
|
2022-08-29 05:54:30 -04:00
|
|
|
imdsAPI: &stubIMDSAPI{resourceGroup: "resourceGroup"},
|
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)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-01 10:51:34 -04:00
|
|
|
func TestGetLoadBalancerEndpoint(t *testing.T) {
|
2022-05-24 04:04:42 -04:00
|
|
|
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
|
|
|
|
}{
|
2022-08-01 10:51:34 -04:00
|
|
|
"GetLoadBalancerEndpoint works": {
|
2022-08-29 05:54:30 -04:00
|
|
|
imdsAPI: &stubIMDSAPI{resourceGroup: "resourceGroup"},
|
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-08-29 05:54:30 -04:00
|
|
|
imdsAPI: &stubIMDSAPI{resourceGroup: "resourceGroup"},
|
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-08-29 05:54:30 -04:00
|
|
|
imdsAPI: &stubIMDSAPI{resourceGroup: "resourceGroup"},
|
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-08-29 05:54:30 -04:00
|
|
|
imdsAPI: &stubIMDSAPI{resourceGroup: "resourceGroup"},
|
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-08-29 05:54:30 -04:00
|
|
|
imdsAPI: &stubIMDSAPI{resourceGroup: "resourceGroup"},
|
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-08-29 05:54:30 -04:00
|
|
|
imdsAPI: &stubIMDSAPI{resourceGroup: "resourceGroup"},
|
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,
|
|
|
|
}
|
2022-08-01 10:51:34 -04:00
|
|
|
loadbalancerName, err := metadata.GetLoadBalancerEndpoint(context.Background())
|
2022-05-24 04:04:42 -04:00
|
|
|
if tc.wantErr {
|
|
|
|
assert.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
require.NoError(err)
|
|
|
|
assert.Equal(tc.wantIP, loadbalancerName)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-22 11:03:15 -04:00
|
|
|
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-08-29 05:54:30 -04:00
|
|
|
imdsAPI: &stubIMDSAPI{providerID: "provider-id"},
|
|
|
|
wantProviderID: "azure://provider-id",
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
2022-08-29 05:54:30 -04:00
|
|
|
"imds providerID fails": {
|
|
|
|
imdsAPI: &stubIMDSAPI{providerIDErr: errors.New("failed")},
|
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)
|
|
|
|
|
|
|
|
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
|
|
|
|
}{
|
2022-08-29 05:54:30 -04:00
|
|
|
"success": {
|
|
|
|
imdsAPI: &stubIMDSAPI{uid: "uid"},
|
2022-07-29 10:30:24 -04:00
|
|
|
wantUID: "uid",
|
|
|
|
},
|
2022-08-29 05:54:30 -04:00
|
|
|
"imds uid error": {
|
|
|
|
imdsAPI: &stubIMDSAPI{uidErr: errors.New("failed")},
|
2022-07-29 10:30:24 -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,
|
|
|
|
}
|
|
|
|
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 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-10-06 06:14:41 -04:00
|
|
|
Tags: map[string]*string{
|
2022-10-24 10:58:21 -04:00
|
|
|
cloud.TagUID: to.Ptr("uid"),
|
|
|
|
cloud.TagRole: to.Ptr("worker"),
|
2022-10-06 06:14:41 -04:00
|
|
|
},
|
2022-07-27 16:02:33 -04:00
|
|
|
}},
|
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-10-06 06:14:41 -04:00
|
|
|
Tags: map[string]*string{
|
2022-10-24 10:58:21 -04:00
|
|
|
cloud.TagUID: to.Ptr("uid"),
|
|
|
|
cloud.TagRole: to.Ptr("worker"),
|
2022-10-06 06:14:41 -04:00
|
|
|
},
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
2022-07-27 16:02:33 -04:00
|
|
|
pager: &stubVirtualMachineScaleSetVMPager{
|
2022-10-06 06:14:41 -04:00
|
|
|
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-10-06 06:14:41 -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-10-06 06:14:41 -04:00
|
|
|
Tags: map[string]*string{
|
2022-10-24 10:58:21 -04:00
|
|
|
cloud.TagUID: to.Ptr("uid"),
|
|
|
|
cloud.TagRole: to.Ptr("worker"),
|
2022-10-06 06:14:41 -04:00
|
|
|
},
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
2022-10-06 06:14:41 -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{}
|
|
|
|
}
|