2022-03-22 11:03:15 -04:00
|
|
|
package azure
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
|
|
|
|
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/compute/armcompute"
|
|
|
|
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/network/armnetwork"
|
2022-06-29 09:26:29 -04:00
|
|
|
"github.com/edgelesssys/constellation/bootstrapper/role"
|
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 TestGetScaleSetVM(t *testing.T) {
|
2022-06-28 10:08:05 -04:00
|
|
|
wantInstance := 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-05-24 04:04:42 -04:00
|
|
|
PrivateIPs: []string{"192.0.2.0"},
|
2022-03-22 11:03:15 -04:00
|
|
|
SSHKeys: map[string][]string{"user": {"key-data"}},
|
|
|
|
}
|
|
|
|
testCases := map[string]struct {
|
|
|
|
providerID string
|
|
|
|
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
|
|
|
}{
|
|
|
|
"getVM for scale set instance works": {
|
|
|
|
providerID: "azure:///subscriptions/subscription-id/resourceGroups/resource-group/providers/Microsoft.Compute/virtualMachineScaleSets/scale-set-name/virtualMachines/instance-id",
|
|
|
|
networkInterfacesAPI: newNetworkInterfacesStub(),
|
|
|
|
virtualMachineScaleSetVMsAPI: newVirtualMachineScaleSetsVMsStub(),
|
2022-04-26 10:54:05 -04:00
|
|
|
wantInstance: wantInstance,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"getVM for individual instance must fail": {
|
|
|
|
providerID: "azure:///subscriptions/subscription-id/resourceGroups/resource-group/providers/Microsoft.Compute/virtualMachines/instance-name",
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"Get fails": {
|
|
|
|
providerID: "azure:///subscriptions/subscription-id/resourceGroups/resource-group/providers/Microsoft.Compute/virtualMachineScaleSets/scale-set-name/virtualMachines/instance-id",
|
|
|
|
virtualMachineScaleSetVMsAPI: newFailingGetScaleSetVirtualMachinesStub(),
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"conversion fails": {
|
|
|
|
providerID: "azure:///subscriptions/subscription-id/resourceGroups/resource-group/providers/Microsoft.Compute/virtualMachineScaleSets/scale-set-name/virtualMachines/instance-id",
|
|
|
|
virtualMachineScaleSetVMsAPI: newGetInvalidScaleSetVirtualMachinesStub(),
|
|
|
|
networkInterfacesAPI: newNetworkInterfacesStub(),
|
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{
|
|
|
|
networkInterfacesAPI: tc.networkInterfacesAPI,
|
|
|
|
virtualMachineScaleSetVMsAPI: tc.virtualMachineScaleSetVMsAPI,
|
|
|
|
}
|
|
|
|
instance, err := metadata.getScaleSetVM(context.Background(), tc.providerID)
|
|
|
|
|
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
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestListScaleSetVMs(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-05-24 04:04:42 -04:00
|
|
|
PrivateIPs: []string{"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
|
|
|
|
scaleSetsAPI scaleSetsAPI
|
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
|
|
|
}{
|
|
|
|
"listVMs works": {
|
2022-06-29 10:17:23 -04:00
|
|
|
imdsAPI: newScaleSetIMDSStub(),
|
2022-03-22 11:03:15 -04:00
|
|
|
networkInterfacesAPI: newNetworkInterfacesStub(),
|
|
|
|
virtualMachineScaleSetVMsAPI: newVirtualMachineScaleSetsVMsStub(),
|
|
|
|
scaleSetsAPI: newScaleSetsStub(),
|
2022-04-26 10:54:05 -04:00
|
|
|
wantInstances: wantInstances,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"invalid scale sets are skipped": {
|
2022-06-29 10:17:23 -04:00
|
|
|
imdsAPI: newScaleSetIMDSStub(),
|
2022-03-22 11:03:15 -04:00
|
|
|
networkInterfacesAPI: newNetworkInterfacesStub(),
|
|
|
|
virtualMachineScaleSetVMsAPI: newVirtualMachineScaleSetsVMsStub(),
|
|
|
|
scaleSetsAPI: newListContainingNilScaleSetStub(),
|
2022-04-26 10:54:05 -04:00
|
|
|
wantInstances: wantInstances,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"listVMs can return 0 VMs": {
|
2022-06-29 10:17:23 -04:00
|
|
|
imdsAPI: newScaleSetIMDSStub(),
|
2022-03-22 11:03:15 -04:00
|
|
|
networkInterfacesAPI: newNetworkInterfacesStub(),
|
|
|
|
virtualMachineScaleSetVMsAPI: &stubVirtualMachineScaleSetVMsAPI{},
|
|
|
|
scaleSetsAPI: newScaleSetsStub(),
|
2022-06-28 10:08:05 -04:00
|
|
|
wantInstances: []metadata.InstanceMetadata{},
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"can skip nil in VM list": {
|
2022-06-29 10:17:23 -04:00
|
|
|
imdsAPI: newScaleSetIMDSStub(),
|
2022-03-22 11:03:15 -04:00
|
|
|
networkInterfacesAPI: newNetworkInterfacesStub(),
|
|
|
|
virtualMachineScaleSetVMsAPI: newListContainingNilScaleSetVirtualMachinesStub(),
|
|
|
|
scaleSetsAPI: newScaleSetsStub(),
|
2022-04-26 10:54:05 -04:00
|
|
|
wantInstances: wantInstances,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"converting instance fails": {
|
2022-06-29 10:17:23 -04:00
|
|
|
imdsAPI: newScaleSetIMDSStub(),
|
2022-03-22 11:03:15 -04:00
|
|
|
networkInterfacesAPI: newNetworkInterfacesStub(),
|
|
|
|
virtualMachineScaleSetVMsAPI: newListContainingInvalidScaleSetVirtualMachinesStub(),
|
|
|
|
scaleSetsAPI: newScaleSetsStub(),
|
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,
|
|
|
|
networkInterfacesAPI: tc.networkInterfacesAPI,
|
|
|
|
virtualMachineScaleSetVMsAPI: tc.virtualMachineScaleSetVMsAPI,
|
|
|
|
scaleSetsAPI: tc.scaleSetsAPI,
|
|
|
|
}
|
|
|
|
instances, err := metadata.listScaleSetVMs(context.Background(), "resource-group")
|
|
|
|
|
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 TestConvertScaleSetVMToCoreInstance(t *testing.T) {
|
|
|
|
testCases := map[string]struct {
|
2022-05-24 04:04:42 -04:00
|
|
|
inVM armcompute.VirtualMachineScaleSetVM
|
|
|
|
inInterface []armnetwork.Interface
|
|
|
|
inPublicIPs []string
|
|
|
|
wantErr bool
|
2022-06-28 10:08:05 -04:00
|
|
|
wantInstance metadata.InstanceMetadata
|
2022-03-22 11:03:15 -04:00
|
|
|
}{
|
|
|
|
"conversion works": {
|
|
|
|
inVM: armcompute.VirtualMachineScaleSetVM{
|
|
|
|
Name: to.StringPtr("scale-set-name_instance-id"),
|
|
|
|
ID: to.StringPtr("/subscriptions/subscription-id/resourceGroups/resource-group/providers/Microsoft.Compute/virtualMachineScaleSets/scale-set-name/virtualMachines/instance-id"),
|
|
|
|
Tags: map[string]*string{"tag-key": to.StringPtr("tag-value")},
|
|
|
|
Properties: &armcompute.VirtualMachineScaleSetVMProperties{
|
|
|
|
OSProfile: &armcompute.OSProfile{
|
|
|
|
ComputerName: to.StringPtr("scale-set-name-instance-id"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-05-24 04:04:42 -04:00
|
|
|
inInterface: []armnetwork.Interface{
|
2022-03-22 11:03:15 -04:00
|
|
|
{
|
2022-05-24 04:04:42 -04:00
|
|
|
Name: to.StringPtr("scale-set-name_instance-id"),
|
|
|
|
ID: to.StringPtr("/subscriptions/subscription-id/resourceGroups/resource-group/providers/Microsoft.Network/networkInterfaces/interface-name"),
|
|
|
|
Properties: &armnetwork.InterfacePropertiesFormat{
|
|
|
|
IPConfigurations: []*armnetwork.InterfaceIPConfiguration{
|
|
|
|
{
|
|
|
|
Properties: &armnetwork.InterfaceIPConfigurationPropertiesFormat{
|
|
|
|
PrivateIPAddress: to.StringPtr("192.0.2.0"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-05-24 04:04:42 -04:00
|
|
|
inPublicIPs: []string{"192.0.2.100", "192.0.2.101"},
|
2022-06-28 10:08:05 -04:00
|
|
|
wantInstance: 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-05-24 04:04:42 -04:00
|
|
|
PrivateIPs: []string{"192.0.2.0"},
|
|
|
|
PublicIPs: []string{"192.0.2.100", "192.0.2.101"},
|
2022-03-22 11:03:15 -04:00
|
|
|
SSHKeys: map[string][]string{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"invalid instance": {
|
2022-04-26 10:54:05 -04:00
|
|
|
inVM: armcompute.VirtualMachineScaleSetVM{},
|
|
|
|
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-05-24 04:04:42 -04:00
|
|
|
instance, err := convertScaleSetVMToCoreInstance("scale-set", tc.inVM, tc.inInterface, tc.inPublicIPs)
|
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.Equal(tc.wantInstance, instance)
|
2022-03-22 11:03:15 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestExtractScaleSetVMRole(t *testing.T) {
|
|
|
|
testCases := map[string]struct {
|
2022-04-26 10:54:05 -04:00
|
|
|
scaleSet string
|
|
|
|
wantRole role.Role
|
2022-03-22 11:03:15 -04:00
|
|
|
}{
|
2022-06-29 09:26:29 -04:00
|
|
|
"bootstrapper role": {
|
2022-06-29 10:17:23 -04:00
|
|
|
scaleSet: "constellation-scale-set-controlplanes-abcd123",
|
2022-06-29 09:26:29 -04:00
|
|
|
wantRole: role.ControlPlane,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"node role": {
|
2022-06-29 10:17:23 -04:00
|
|
|
scaleSet: "constellation-scale-set-workers-abcd123",
|
2022-06-29 09:26:29 -04:00
|
|
|
wantRole: role.Worker,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"unknown role": {
|
2022-04-26 10:54:05 -04:00
|
|
|
scaleSet: "unknown",
|
|
|
|
wantRole: role.Unknown,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
role := extractScaleSetVMRole(tc.scaleSet)
|
|
|
|
|
2022-04-26 10:54:05 -04:00
|
|
|
assert.Equal(tc.wantRole, role)
|
2022-03-22 11:03:15 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func newFailingGetScaleSetVirtualMachinesStub() *stubVirtualMachineScaleSetVMsAPI {
|
|
|
|
return &stubVirtualMachineScaleSetVMsAPI{
|
|
|
|
getErr: errors.New("get err"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func newGetInvalidScaleSetVirtualMachinesStub() *stubVirtualMachineScaleSetVMsAPI {
|
|
|
|
return &stubVirtualMachineScaleSetVMsAPI{
|
|
|
|
getVM: armcompute.VirtualMachineScaleSetVM{},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func newListContainingNilScaleSetVirtualMachinesStub() *stubVirtualMachineScaleSetVMsAPI {
|
|
|
|
return &stubVirtualMachineScaleSetVMsAPI{
|
|
|
|
listPages: [][]*armcompute.VirtualMachineScaleSetVM{
|
|
|
|
{
|
|
|
|
nil,
|
|
|
|
{
|
|
|
|
Name: to.StringPtr("scale-set-name_instance-id"),
|
|
|
|
ID: to.StringPtr("/subscriptions/subscription-id/resourceGroups/resource-group/providers/Microsoft.Compute/virtualMachineScaleSets/scale-set-name/virtualMachines/instance-id"),
|
|
|
|
InstanceID: to.StringPtr("instance-id"),
|
|
|
|
Tags: map[string]*string{
|
|
|
|
"tag-key": to.StringPtr("tag-value"),
|
|
|
|
},
|
|
|
|
Properties: &armcompute.VirtualMachineScaleSetVMProperties{
|
|
|
|
NetworkProfile: &armcompute.NetworkProfile{
|
|
|
|
NetworkInterfaces: []*armcompute.NetworkInterfaceReference{
|
|
|
|
{
|
|
|
|
ID: to.StringPtr("/subscriptions/subscription-id/resourceGroups/resource-group/providers/Microsoft.Compute/virtualMachineScaleSets/scale-set-name/virtualMachines/instance-id/networkInterfaces/interface-name"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
OSProfile: &armcompute.OSProfile{
|
|
|
|
ComputerName: to.StringPtr("scale-set-name-instance-id"),
|
|
|
|
LinuxConfiguration: &armcompute.LinuxConfiguration{
|
|
|
|
SSH: &armcompute.SSHConfiguration{
|
|
|
|
PublicKeys: []*armcompute.SSHPublicKey{
|
|
|
|
{
|
|
|
|
KeyData: to.StringPtr("key-data"),
|
|
|
|
Path: to.StringPtr("/home/user/.ssh/authorized_keys"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func newListContainingInvalidScaleSetVirtualMachinesStub() *stubVirtualMachineScaleSetVMsAPI {
|
|
|
|
return &stubVirtualMachineScaleSetVMsAPI{
|
|
|
|
listPages: [][]*armcompute.VirtualMachineScaleSetVM{
|
|
|
|
{
|
|
|
|
{
|
|
|
|
Name: nil,
|
|
|
|
ID: nil,
|
|
|
|
InstanceID: to.StringPtr("instance-id"),
|
|
|
|
Properties: &armcompute.VirtualMachineScaleSetVMProperties{
|
|
|
|
OSProfile: &armcompute.OSProfile{
|
|
|
|
ComputerName: nil,
|
|
|
|
},
|
|
|
|
NetworkProfile: &armcompute.NetworkProfile{
|
|
|
|
NetworkInterfaces: []*armcompute.NetworkInterfaceReference{
|
|
|
|
{
|
|
|
|
ID: to.StringPtr("/subscriptions/subscription-id/resourceGroups/resource-group/providers/Microsoft.Compute/virtualMachineScaleSets/scale-set-name/virtualMachines/instance-id/networkInterfaces/interface-name"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func newListContainingNilScaleSetStub() *stubScaleSetsAPI {
|
|
|
|
return &stubScaleSetsAPI{
|
|
|
|
listPages: [][]*armcompute.VirtualMachineScaleSet{
|
|
|
|
{
|
|
|
|
nil,
|
|
|
|
{Name: to.StringPtr("scale-set-name")},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|