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"
|
|
|
|
"github.com/edgelesssys/constellation/coordinator/core"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestGetVM(t *testing.T) {
|
2022-04-26 10:54:05 -04:00
|
|
|
wantInstance := core.Instance{
|
2022-03-22 11:03:15 -04:00
|
|
|
Name: "instance-name",
|
|
|
|
ProviderID: "azure:///subscriptions/subscription-id/resourceGroups/resource-group/providers/Microsoft.Compute/virtualMachines/instance-name",
|
|
|
|
IPs: []string{"192.0.2.0"},
|
|
|
|
SSHKeys: map[string][]string{"user": {"key-data"}},
|
|
|
|
}
|
|
|
|
testCases := map[string]struct {
|
|
|
|
providerID string
|
|
|
|
networkInterfacesAPI networkInterfacesAPI
|
|
|
|
virtualMachinesAPI virtualMachinesAPI
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr bool
|
|
|
|
wantInstance core.Instance
|
2022-03-22 11:03:15 -04:00
|
|
|
}{
|
|
|
|
"getVM for individual instance works": {
|
|
|
|
providerID: "azure:///subscriptions/subscription-id/resourceGroups/resource-group/providers/Microsoft.Compute/virtualMachines/instance-name",
|
|
|
|
networkInterfacesAPI: newNetworkInterfacesStub(),
|
|
|
|
virtualMachinesAPI: newVirtualMachinesStub(),
|
2022-04-26 10:54:05 -04:00
|
|
|
wantInstance: wantInstance,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"getVM for scale set instance must fail": {
|
|
|
|
providerID: "azure:///subscriptions/subscription-id/resourceGroups/resource-group/providers/Microsoft.Compute/virtualMachineScaleSets/scale-set-name/virtualMachines/instance-id",
|
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/virtualMachines/instance-name",
|
|
|
|
virtualMachinesAPI: newFailingGetVirtualMachinesStub(),
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"retrieving interfaces fails": {
|
|
|
|
providerID: "azure:///subscriptions/subscription-id/resourceGroups/resource-group/providers/Microsoft.Compute/virtualMachines/instance-name",
|
|
|
|
virtualMachinesAPI: newVirtualMachinesStub(),
|
|
|
|
networkInterfacesAPI: newFailingNetworkInterfacesStub(),
|
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/virtualMachines/instance-name",
|
|
|
|
virtualMachinesAPI: newGetInvalidVirtualMachinesStub(),
|
|
|
|
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,
|
|
|
|
virtualMachinesAPI: tc.virtualMachinesAPI,
|
|
|
|
}
|
|
|
|
instance, err := metadata.getVM(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 TestListVMs(t *testing.T) {
|
2022-04-26 10:54:05 -04:00
|
|
|
wantInstances := []core.Instance{
|
2022-03-22 11:03:15 -04:00
|
|
|
{
|
|
|
|
Name: "instance-name",
|
|
|
|
ProviderID: "azure:///subscriptions/subscription-id/resourceGroups/resource-group/providers/Microsoft.Compute/virtualMachines/instance-name",
|
|
|
|
IPs: []string{"192.0.2.0"},
|
|
|
|
SSHKeys: map[string][]string{"user": {"key-data"}},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
testCases := map[string]struct {
|
|
|
|
imdsAPI imdsAPI
|
|
|
|
networkInterfacesAPI networkInterfacesAPI
|
|
|
|
virtualMachinesAPI virtualMachinesAPI
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr bool
|
|
|
|
wantInstances []core.Instance
|
2022-03-22 11:03:15 -04:00
|
|
|
}{
|
|
|
|
"listVMs works": {
|
|
|
|
imdsAPI: newIMDSStub(),
|
|
|
|
networkInterfacesAPI: newNetworkInterfacesStub(),
|
|
|
|
virtualMachinesAPI: newVirtualMachinesStub(),
|
2022-04-26 10:54:05 -04:00
|
|
|
wantInstances: wantInstances,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"listVMs can return 0 VMs": {
|
|
|
|
imdsAPI: newIMDSStub(),
|
|
|
|
networkInterfacesAPI: newNetworkInterfacesStub(),
|
|
|
|
virtualMachinesAPI: &stubVirtualMachinesAPI{},
|
2022-04-26 10:54:05 -04:00
|
|
|
wantInstances: []core.Instance{},
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"can skip nil in VM list": {
|
|
|
|
imdsAPI: newIMDSStub(),
|
|
|
|
networkInterfacesAPI: newNetworkInterfacesStub(),
|
|
|
|
virtualMachinesAPI: newListContainingNilVirtualMachinesStub(),
|
2022-04-26 10:54:05 -04:00
|
|
|
wantInstances: wantInstances,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"retrieving network interfaces fails": {
|
|
|
|
imdsAPI: newIMDSStub(),
|
|
|
|
networkInterfacesAPI: newFailingNetworkInterfacesStub(),
|
|
|
|
virtualMachinesAPI: newVirtualMachinesStub(),
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"converting instance fails": {
|
|
|
|
imdsAPI: newIMDSStub(),
|
|
|
|
networkInterfacesAPI: newNetworkInterfacesStub(),
|
|
|
|
virtualMachinesAPI: newListContainingInvalidVirtualMachinesStub(),
|
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,
|
|
|
|
virtualMachinesAPI: tc.virtualMachinesAPI,
|
|
|
|
}
|
|
|
|
instances, err := metadata.listVMs(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 TestSetTag(t *testing.T) {
|
|
|
|
testCases := map[string]struct {
|
2022-04-26 10:54:05 -04:00
|
|
|
imdsAPI imdsAPI
|
|
|
|
tagsAPI tagsAPI
|
|
|
|
wantErr bool
|
2022-03-22 11:03:15 -04:00
|
|
|
}{
|
|
|
|
"setTag works": {
|
|
|
|
imdsAPI: newIMDSStub(),
|
|
|
|
tagsAPI: newTagsStub(),
|
|
|
|
},
|
|
|
|
"retrieving resource ID fails": {
|
2022-04-26 10:54:05 -04:00
|
|
|
imdsAPI: newFailingIMDSStub(),
|
|
|
|
tagsAPI: newTagsStub(),
|
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"updating tags fails": {
|
2022-04-26 10:54:05 -04:00
|
|
|
imdsAPI: newIMDSStub(),
|
|
|
|
tagsAPI: newFailingTagsStub(),
|
|
|
|
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,
|
|
|
|
tagsAPI: tc.tagsAPI,
|
|
|
|
}
|
|
|
|
err := metadata.setTag(context.Background(), "key", "value")
|
|
|
|
|
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)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSplitVMProviderID(t *testing.T) {
|
|
|
|
testCases := map[string]struct {
|
2022-04-26 10:54:05 -04:00
|
|
|
providerID string
|
|
|
|
wantErr bool
|
|
|
|
wantSubscriptionID string
|
|
|
|
wantResourceGroup string
|
|
|
|
wantInstanceName string
|
2022-03-22 11:03:15 -04:00
|
|
|
}{
|
|
|
|
"providerID for individual instance works": {
|
2022-04-26 10:54:05 -04:00
|
|
|
providerID: "azure:///subscriptions/subscription-id/resourceGroups/resource-group/providers/Microsoft.Compute/virtualMachines/instance-name",
|
|
|
|
wantSubscriptionID: "subscription-id",
|
|
|
|
wantResourceGroup: "resource-group",
|
|
|
|
wantInstanceName: "instance-name",
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"providerID for scale set instance must fail": {
|
|
|
|
providerID: "azure:///subscriptions/subscription-id/resourceGroups/resource-group/providers/Microsoft.Compute/virtualMachineScaleSets/scale-set-name/virtualMachines/instance-id",
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"providerID is malformed": {
|
|
|
|
providerID: "malformed-provider-id",
|
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)
|
|
|
|
|
|
|
|
subscriptionID, resourceGroup, instanceName, err := splitVMProviderID(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.wantSubscriptionID, subscriptionID)
|
|
|
|
assert.Equal(tc.wantResourceGroup, resourceGroup)
|
|
|
|
assert.Equal(tc.wantInstanceName, instanceName)
|
2022-03-22 11:03:15 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestConvertVMToCoreInstance(t *testing.T) {
|
|
|
|
testCases := map[string]struct {
|
|
|
|
inVM armcompute.VirtualMachine
|
|
|
|
inInterfaceIPConfigs []*armnetwork.InterfaceIPConfiguration
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr bool
|
|
|
|
wantInstance core.Instance
|
2022-03-22 11:03:15 -04:00
|
|
|
}{
|
|
|
|
"conversion works": {
|
|
|
|
inVM: armcompute.VirtualMachine{
|
|
|
|
Name: to.StringPtr("instance-name"),
|
|
|
|
ID: to.StringPtr("/subscriptions/subscription-id/resourceGroups/resource-group/providers/Microsoft.Compute/virtualMachines/instance-name"),
|
|
|
|
Tags: map[string]*string{"tag-key": to.StringPtr("tag-value")},
|
|
|
|
Properties: &armcompute.VirtualMachineProperties{
|
|
|
|
OSProfile: &armcompute.OSProfile{
|
|
|
|
LinuxConfiguration: &armcompute.LinuxConfiguration{
|
|
|
|
SSH: &armcompute.SSHConfiguration{
|
|
|
|
PublicKeys: []*armcompute.SSHPublicKey{
|
|
|
|
{
|
|
|
|
Path: to.StringPtr("/home/user/.ssh/authorized_keys"),
|
|
|
|
KeyData: to.StringPtr("key-data"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
inInterfaceIPConfigs: []*armnetwork.InterfaceIPConfiguration{
|
|
|
|
{
|
|
|
|
Properties: &armnetwork.InterfaceIPConfigurationPropertiesFormat{
|
|
|
|
PrivateIPAddress: to.StringPtr("192.0.2.0"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-04-26 10:54:05 -04:00
|
|
|
wantInstance: core.Instance{
|
2022-03-22 11:03:15 -04:00
|
|
|
Name: "instance-name",
|
|
|
|
ProviderID: "azure:///subscriptions/subscription-id/resourceGroups/resource-group/providers/Microsoft.Compute/virtualMachines/instance-name",
|
|
|
|
IPs: []string{"192.0.2.0"},
|
|
|
|
SSHKeys: map[string][]string{"user": {"key-data"}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"conversion without SSH keys works": {
|
|
|
|
inVM: armcompute.VirtualMachine{
|
|
|
|
Name: to.StringPtr("instance-name"),
|
|
|
|
ID: to.StringPtr("/subscriptions/subscription-id/resourceGroups/resource-group/providers/Microsoft.Compute/virtualMachines/instance-name"),
|
|
|
|
Tags: map[string]*string{"tag-key": to.StringPtr("tag-value")},
|
|
|
|
},
|
|
|
|
inInterfaceIPConfigs: []*armnetwork.InterfaceIPConfiguration{
|
|
|
|
{
|
|
|
|
Properties: &armnetwork.InterfaceIPConfigurationPropertiesFormat{
|
|
|
|
PrivateIPAddress: to.StringPtr("192.0.2.0"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-04-26 10:54:05 -04:00
|
|
|
wantInstance: core.Instance{
|
2022-03-22 11:03:15 -04:00
|
|
|
Name: "instance-name",
|
|
|
|
ProviderID: "azure:///subscriptions/subscription-id/resourceGroups/resource-group/providers/Microsoft.Compute/virtualMachines/instance-name",
|
|
|
|
IPs: []string{"192.0.2.0"},
|
|
|
|
SSHKeys: map[string][]string{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"invalid instance": {
|
2022-04-26 10:54:05 -04:00
|
|
|
inVM: armcompute.VirtualMachine{},
|
|
|
|
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)
|
|
|
|
|
|
|
|
instance, err := convertVMToCoreInstance(tc.inVM, tc.inInterfaceIPConfigs)
|
|
|
|
|
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 newFailingNetworkInterfacesStub() *stubNetworkInterfacesAPI {
|
|
|
|
return &stubNetworkInterfacesAPI{
|
|
|
|
getErr: errors.New("get err"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func newGetInvalidVirtualMachinesStub() *stubVirtualMachinesAPI {
|
|
|
|
return &stubVirtualMachinesAPI{
|
|
|
|
getVM: armcompute.VirtualMachine{},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func newListContainingNilVirtualMachinesStub() *stubVirtualMachinesAPI {
|
|
|
|
return &stubVirtualMachinesAPI{
|
|
|
|
listPages: [][]*armcompute.VirtualMachine{
|
|
|
|
{
|
|
|
|
nil,
|
|
|
|
{
|
|
|
|
Name: to.StringPtr("instance-name"),
|
|
|
|
ID: to.StringPtr("/subscriptions/subscription-id/resourceGroups/resource-group/providers/Microsoft.Compute/virtualMachines/instance-name"),
|
|
|
|
Tags: map[string]*string{
|
|
|
|
"tag-key": to.StringPtr("tag-value"),
|
|
|
|
},
|
|
|
|
Properties: &armcompute.VirtualMachineProperties{
|
|
|
|
NetworkProfile: &armcompute.NetworkProfile{
|
|
|
|
NetworkInterfaces: []*armcompute.NetworkInterfaceReference{
|
|
|
|
{
|
|
|
|
ID: to.StringPtr("/subscriptions/subscription-id/resourceGroups/resource-group/providers/Microsoft.Network/networkInterfaces/interface-name"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
OSProfile: &armcompute.OSProfile{
|
|
|
|
LinuxConfiguration: &armcompute.LinuxConfiguration{
|
|
|
|
SSH: &armcompute.SSHConfiguration{
|
|
|
|
PublicKeys: []*armcompute.SSHPublicKey{
|
|
|
|
{
|
|
|
|
KeyData: to.StringPtr("key-data"),
|
|
|
|
Path: to.StringPtr("/home/user/.ssh/authorized_keys"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func newListContainingInvalidVirtualMachinesStub() *stubVirtualMachinesAPI {
|
|
|
|
return &stubVirtualMachinesAPI{
|
|
|
|
listPages: [][]*armcompute.VirtualMachine{
|
|
|
|
{
|
|
|
|
{
|
|
|
|
Name: nil,
|
|
|
|
ID: nil,
|
|
|
|
Properties: &armcompute.VirtualMachineProperties{
|
|
|
|
NetworkProfile: &armcompute.NetworkProfile{
|
|
|
|
NetworkInterfaces: []*armcompute.NetworkInterfaceReference{
|
|
|
|
{
|
|
|
|
ID: to.StringPtr("/subscriptions/subscription-id/resourceGroups/resource-group/providers/Microsoft.Network/networkInterfaces/interface-name"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
OSProfile: &armcompute.OSProfile{
|
|
|
|
LinuxConfiguration: &armcompute.LinuxConfiguration{
|
|
|
|
SSH: &armcompute.SSHConfiguration{
|
|
|
|
PublicKeys: []*armcompute.SSHPublicKey{
|
|
|
|
{
|
|
|
|
KeyData: to.StringPtr("key-data"),
|
|
|
|
Path: to.StringPtr("/home/user/.ssh/authorized_keys"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|