2022-03-22 11:03:15 -04:00
|
|
|
package client
|
|
|
|
|
|
|
|
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/resources/armresources"
|
2022-06-08 02:17:52 -04:00
|
|
|
"github.com/edgelesssys/constellation/internal/cloud/cloudtypes"
|
2022-03-22 11:03:15 -04:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestCreateResourceGroup(t *testing.T) {
|
|
|
|
someErr := errors.New("failed")
|
|
|
|
testCases := map[string]struct {
|
|
|
|
resourceGroupAPI resourceGroupAPI
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr bool
|
2022-03-22 11:03:15 -04:00
|
|
|
}{
|
|
|
|
"successful create": {
|
|
|
|
resourceGroupAPI: stubResourceGroupAPI{},
|
|
|
|
},
|
|
|
|
"failed create": {
|
|
|
|
resourceGroupAPI: stubResourceGroupAPI{createErr: someErr},
|
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)
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
client := Client{
|
|
|
|
location: "location",
|
|
|
|
name: "name",
|
|
|
|
uid: "uid",
|
|
|
|
resourceGroupAPI: tc.resourceGroupAPI,
|
2022-06-29 09:26:29 -04:00
|
|
|
workers: make(cloudtypes.Instances),
|
|
|
|
controlPlanes: make(cloudtypes.Instances),
|
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(client.CreateResourceGroup(ctx))
|
|
|
|
} else {
|
|
|
|
assert.NoError(client.CreateResourceGroup(ctx))
|
|
|
|
assert.Equal(client.name+"-"+client.uid, client.resourceGroup)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTerminateResourceGroup(t *testing.T) {
|
|
|
|
someErr := errors.New("failed")
|
|
|
|
clientWithResourceGroup := Client{
|
|
|
|
resourceGroup: "name",
|
|
|
|
location: "location",
|
|
|
|
name: "name",
|
|
|
|
uid: "uid",
|
|
|
|
subnetID: "subnet",
|
2022-06-29 09:26:29 -04:00
|
|
|
workerScaleSet: "node-scale-set",
|
|
|
|
controlPlaneScaleSet: "controlplane-scale-set",
|
|
|
|
workers: cloudtypes.Instances{
|
2022-03-22 11:03:15 -04:00
|
|
|
"0": {
|
|
|
|
PublicIP: "192.0.2.1", PrivateIP: "192.0.2.1",
|
|
|
|
},
|
|
|
|
},
|
2022-06-29 09:26:29 -04:00
|
|
|
controlPlanes: cloudtypes.Instances{
|
2022-03-22 11:03:15 -04:00
|
|
|
"0": {
|
|
|
|
PublicIP: "192.0.2.1", PrivateIP: "192.0.2.1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
testCases := map[string]struct {
|
|
|
|
resourceGroup string
|
|
|
|
resourceGroupAPI resourceGroupAPI
|
|
|
|
client Client
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr bool
|
2022-03-22 11:03:15 -04:00
|
|
|
}{
|
|
|
|
"successful terminate": {
|
|
|
|
resourceGroupAPI: stubResourceGroupAPI{},
|
|
|
|
client: clientWithResourceGroup,
|
|
|
|
},
|
|
|
|
"no resource group to terminate": {
|
|
|
|
resourceGroupAPI: stubResourceGroupAPI{},
|
|
|
|
client: Client{},
|
|
|
|
resourceGroup: "",
|
|
|
|
},
|
|
|
|
"failed terminate": {
|
|
|
|
resourceGroupAPI: stubResourceGroupAPI{terminateErr: someErr},
|
|
|
|
client: clientWithResourceGroup,
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"failed to poll terminate response": {
|
2022-07-27 16:02:33 -04:00
|
|
|
resourceGroupAPI: stubResourceGroupAPI{pollErr: someErr},
|
2022-03-22 11:03:15 -04:00
|
|
|
client: clientWithResourceGroup,
|
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)
|
|
|
|
tc.client.resourceGroupAPI = tc.resourceGroupAPI
|
|
|
|
ctx := context.Background()
|
|
|
|
|
2022-04-26 10:54:05 -04:00
|
|
|
if tc.wantErr {
|
2022-03-22 11:03:15 -04:00
|
|
|
assert.Error(tc.client.TerminateResourceGroup(ctx))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
assert.NoError(tc.client.TerminateResourceGroup(ctx))
|
|
|
|
assert.Empty(tc.client.resourceGroup)
|
|
|
|
assert.Empty(tc.client.subnetID)
|
2022-06-29 09:26:29 -04:00
|
|
|
assert.Empty(tc.client.workers)
|
|
|
|
assert.Empty(tc.client.controlPlanes)
|
|
|
|
assert.Empty(tc.client.workerScaleSet)
|
|
|
|
assert.Empty(tc.client.controlPlaneScaleSet)
|
2022-03-22 11:03:15 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCreateInstances(t *testing.T) {
|
|
|
|
someErr := errors.New("failed")
|
|
|
|
testCases := map[string]struct {
|
|
|
|
publicIPAddressesAPI publicIPAddressesAPI
|
|
|
|
networkInterfacesAPI networkInterfacesAPI
|
|
|
|
scaleSetsAPI scaleSetsAPI
|
|
|
|
resourceGroupAPI resourceGroupAPI
|
|
|
|
roleAssignmentsAPI roleAssignmentsAPI
|
|
|
|
createInstancesInput CreateInstancesInput
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr bool
|
2022-03-22 11:03:15 -04:00
|
|
|
}{
|
|
|
|
"successful create": {
|
|
|
|
publicIPAddressesAPI: stubPublicIPAddressesAPI{},
|
|
|
|
networkInterfacesAPI: stubNetworkInterfacesAPI{},
|
|
|
|
scaleSetsAPI: stubScaleSetsAPI{
|
2022-07-27 16:02:33 -04:00
|
|
|
stubResponse: armcomputev2.VirtualMachineScaleSetsClientCreateOrUpdateResponse{
|
|
|
|
VirtualMachineScaleSet: armcomputev2.VirtualMachineScaleSet{Identity: &armcomputev2.VirtualMachineScaleSetIdentity{PrincipalID: to.Ptr("principal-id")}},
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
resourceGroupAPI: newSuccessfulResourceGroupStub(),
|
|
|
|
roleAssignmentsAPI: &stubRoleAssignmentsAPI{},
|
|
|
|
createInstancesInput: CreateInstancesInput{
|
2022-06-29 09:26:29 -04:00
|
|
|
CountControlPlanes: 3,
|
|
|
|
CountWorkers: 3,
|
2022-03-22 11:03:15 -04:00
|
|
|
InstanceType: "type",
|
|
|
|
Image: "image",
|
|
|
|
UserAssingedIdentity: "identity",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"error when creating scale set": {
|
|
|
|
publicIPAddressesAPI: stubPublicIPAddressesAPI{},
|
|
|
|
networkInterfacesAPI: stubNetworkInterfacesAPI{},
|
|
|
|
scaleSetsAPI: stubScaleSetsAPI{createErr: someErr},
|
|
|
|
resourceGroupAPI: newSuccessfulResourceGroupStub(),
|
|
|
|
roleAssignmentsAPI: &stubRoleAssignmentsAPI{},
|
|
|
|
createInstancesInput: CreateInstancesInput{
|
2022-06-29 09:26:29 -04:00
|
|
|
CountControlPlanes: 3,
|
|
|
|
CountWorkers: 3,
|
2022-03-22 11:03:15 -04:00
|
|
|
InstanceType: "type",
|
|
|
|
Image: "image",
|
|
|
|
UserAssingedIdentity: "identity",
|
|
|
|
},
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"error when polling create scale set response": {
|
|
|
|
publicIPAddressesAPI: stubPublicIPAddressesAPI{},
|
|
|
|
networkInterfacesAPI: stubNetworkInterfacesAPI{},
|
2022-07-27 16:02:33 -04:00
|
|
|
scaleSetsAPI: stubScaleSetsAPI{pollErr: someErr},
|
2022-03-22 11:03:15 -04:00
|
|
|
resourceGroupAPI: newSuccessfulResourceGroupStub(),
|
|
|
|
roleAssignmentsAPI: &stubRoleAssignmentsAPI{},
|
|
|
|
createInstancesInput: CreateInstancesInput{
|
2022-06-29 09:26:29 -04:00
|
|
|
CountControlPlanes: 3,
|
|
|
|
CountWorkers: 3,
|
2022-03-22 11:03:15 -04:00
|
|
|
InstanceType: "type",
|
|
|
|
Image: "image",
|
|
|
|
UserAssingedIdentity: "identity",
|
|
|
|
},
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"error when retrieving private IPs": {
|
|
|
|
publicIPAddressesAPI: stubPublicIPAddressesAPI{},
|
|
|
|
networkInterfacesAPI: stubNetworkInterfacesAPI{getErr: someErr},
|
|
|
|
scaleSetsAPI: stubScaleSetsAPI{},
|
|
|
|
resourceGroupAPI: newSuccessfulResourceGroupStub(),
|
|
|
|
roleAssignmentsAPI: &stubRoleAssignmentsAPI{},
|
|
|
|
createInstancesInput: CreateInstancesInput{
|
2022-06-29 09:26:29 -04:00
|
|
|
CountWorkers: 3,
|
2022-03-22 11:03:15 -04:00
|
|
|
InstanceType: "type",
|
|
|
|
Image: "image",
|
|
|
|
UserAssingedIdentity: "identity",
|
|
|
|
},
|
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)
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
client := Client{
|
|
|
|
location: "location",
|
|
|
|
name: "name",
|
|
|
|
uid: "uid",
|
|
|
|
resourceGroup: "name",
|
|
|
|
publicIPAddressesAPI: tc.publicIPAddressesAPI,
|
|
|
|
networkInterfacesAPI: tc.networkInterfacesAPI,
|
|
|
|
scaleSetsAPI: tc.scaleSetsAPI,
|
|
|
|
resourceGroupAPI: tc.resourceGroupAPI,
|
|
|
|
roleAssignmentsAPI: tc.roleAssignmentsAPI,
|
2022-06-29 09:26:29 -04:00
|
|
|
workers: make(cloudtypes.Instances),
|
|
|
|
controlPlanes: make(cloudtypes.Instances),
|
2022-05-24 04:04:42 -04:00
|
|
|
loadBalancerPubIP: "lbip",
|
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(client.CreateInstances(ctx, tc.createInstancesInput))
|
|
|
|
} else {
|
|
|
|
assert.NoError(client.CreateInstances(ctx, tc.createInstancesInput))
|
2022-06-29 09:26:29 -04:00
|
|
|
assert.Equal(tc.createInstancesInput.CountControlPlanes, len(client.controlPlanes))
|
|
|
|
assert.Equal(tc.createInstancesInput.CountWorkers, len(client.workers))
|
|
|
|
assert.NotEmpty(client.workers["0"].PrivateIP)
|
|
|
|
assert.NotEmpty(client.workers["0"].PublicIP)
|
|
|
|
assert.NotEmpty(client.controlPlanes["0"].PrivateIP)
|
2022-07-08 04:59:59 -04:00
|
|
|
assert.NotEmpty(client.controlPlanes["0"].PublicIP)
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: deprecate as soon as scale sets are available.
|
|
|
|
func TestCreateInstancesVMs(t *testing.T) {
|
|
|
|
someErr := errors.New("failed")
|
|
|
|
testCases := map[string]struct {
|
|
|
|
publicIPAddressesAPI publicIPAddressesAPI
|
|
|
|
networkInterfacesAPI networkInterfacesAPI
|
|
|
|
virtualMachinesAPI virtualMachinesAPI
|
|
|
|
resourceGroupAPI resourceGroupAPI
|
|
|
|
roleAssignmentsAPI roleAssignmentsAPI
|
|
|
|
createInstancesInput CreateInstancesInput
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr bool
|
2022-03-22 11:03:15 -04:00
|
|
|
}{
|
|
|
|
"successful create": {
|
|
|
|
publicIPAddressesAPI: stubPublicIPAddressesAPI{},
|
|
|
|
networkInterfacesAPI: stubNetworkInterfacesAPI{},
|
|
|
|
virtualMachinesAPI: stubVirtualMachinesAPI{
|
2022-07-27 16:02:33 -04:00
|
|
|
stubResponse: armcomputev2.VirtualMachinesClientCreateOrUpdateResponse{VirtualMachine: armcomputev2.VirtualMachine{
|
|
|
|
Identity: &armcomputev2.VirtualMachineIdentity{PrincipalID: to.Ptr("principal-id")},
|
|
|
|
}},
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
resourceGroupAPI: newSuccessfulResourceGroupStub(),
|
|
|
|
roleAssignmentsAPI: &stubRoleAssignmentsAPI{},
|
|
|
|
createInstancesInput: CreateInstancesInput{
|
2022-06-29 09:26:29 -04:00
|
|
|
CountControlPlanes: 3,
|
|
|
|
CountWorkers: 3,
|
|
|
|
InstanceType: "type",
|
|
|
|
Image: "image",
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
"error when creating scale set": {
|
|
|
|
publicIPAddressesAPI: stubPublicIPAddressesAPI{},
|
|
|
|
networkInterfacesAPI: stubNetworkInterfacesAPI{},
|
|
|
|
virtualMachinesAPI: stubVirtualMachinesAPI{createErr: someErr},
|
|
|
|
resourceGroupAPI: newSuccessfulResourceGroupStub(),
|
|
|
|
roleAssignmentsAPI: &stubRoleAssignmentsAPI{},
|
|
|
|
createInstancesInput: CreateInstancesInput{
|
2022-06-29 09:26:29 -04:00
|
|
|
CountControlPlanes: 3,
|
|
|
|
CountWorkers: 3,
|
|
|
|
InstanceType: "type",
|
|
|
|
Image: "image",
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"error when polling create scale set response": {
|
|
|
|
publicIPAddressesAPI: stubPublicIPAddressesAPI{},
|
|
|
|
networkInterfacesAPI: stubNetworkInterfacesAPI{},
|
2022-07-27 16:02:33 -04:00
|
|
|
virtualMachinesAPI: stubVirtualMachinesAPI{pollErr: someErr},
|
2022-03-22 11:03:15 -04:00
|
|
|
resourceGroupAPI: newSuccessfulResourceGroupStub(),
|
|
|
|
roleAssignmentsAPI: &stubRoleAssignmentsAPI{},
|
|
|
|
createInstancesInput: CreateInstancesInput{
|
2022-06-29 09:26:29 -04:00
|
|
|
CountControlPlanes: 3,
|
|
|
|
CountWorkers: 3,
|
|
|
|
InstanceType: "type",
|
|
|
|
Image: "image",
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"error when creating NIC": {
|
|
|
|
publicIPAddressesAPI: stubPublicIPAddressesAPI{},
|
|
|
|
networkInterfacesAPI: stubNetworkInterfacesAPI{createErr: someErr},
|
|
|
|
virtualMachinesAPI: stubVirtualMachinesAPI{},
|
|
|
|
resourceGroupAPI: newSuccessfulResourceGroupStub(),
|
|
|
|
roleAssignmentsAPI: &stubRoleAssignmentsAPI{},
|
|
|
|
createInstancesInput: CreateInstancesInput{
|
2022-06-29 09:26:29 -04:00
|
|
|
CountControlPlanes: 3,
|
|
|
|
CountWorkers: 3,
|
|
|
|
InstanceType: "type",
|
|
|
|
Image: "image",
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"error when creating public IP": {
|
|
|
|
publicIPAddressesAPI: stubPublicIPAddressesAPI{createErr: someErr},
|
|
|
|
networkInterfacesAPI: stubNetworkInterfacesAPI{},
|
|
|
|
virtualMachinesAPI: stubVirtualMachinesAPI{},
|
|
|
|
resourceGroupAPI: newSuccessfulResourceGroupStub(),
|
|
|
|
roleAssignmentsAPI: &stubRoleAssignmentsAPI{},
|
|
|
|
createInstancesInput: CreateInstancesInput{
|
2022-06-29 09:26:29 -04:00
|
|
|
CountControlPlanes: 3,
|
|
|
|
CountWorkers: 3,
|
|
|
|
InstanceType: "type",
|
|
|
|
Image: "image",
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"error when retrieving public IP": {
|
|
|
|
publicIPAddressesAPI: stubPublicIPAddressesAPI{getErr: someErr},
|
|
|
|
networkInterfacesAPI: stubNetworkInterfacesAPI{},
|
|
|
|
virtualMachinesAPI: stubVirtualMachinesAPI{},
|
|
|
|
resourceGroupAPI: newSuccessfulResourceGroupStub(),
|
|
|
|
roleAssignmentsAPI: &stubRoleAssignmentsAPI{},
|
|
|
|
createInstancesInput: CreateInstancesInput{
|
2022-06-29 09:26:29 -04:00
|
|
|
CountControlPlanes: 3,
|
|
|
|
CountWorkers: 3,
|
|
|
|
InstanceType: "type",
|
|
|
|
Image: "image",
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
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)
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
client := Client{
|
|
|
|
location: "location",
|
|
|
|
name: "name",
|
|
|
|
uid: "uid",
|
|
|
|
resourceGroup: "name",
|
|
|
|
publicIPAddressesAPI: tc.publicIPAddressesAPI,
|
|
|
|
networkInterfacesAPI: tc.networkInterfacesAPI,
|
|
|
|
virtualMachinesAPI: tc.virtualMachinesAPI,
|
|
|
|
resourceGroupAPI: tc.resourceGroupAPI,
|
|
|
|
roleAssignmentsAPI: tc.roleAssignmentsAPI,
|
2022-06-29 09:26:29 -04:00
|
|
|
workers: make(cloudtypes.Instances),
|
|
|
|
controlPlanes: make(cloudtypes.Instances),
|
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(client.CreateInstancesVMs(ctx, tc.createInstancesInput))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
require.NoError(client.CreateInstancesVMs(ctx, tc.createInstancesInput))
|
2022-06-29 09:26:29 -04:00
|
|
|
assert.Equal(tc.createInstancesInput.CountControlPlanes, len(client.controlPlanes))
|
|
|
|
assert.Equal(tc.createInstancesInput.CountWorkers, len(client.workers))
|
|
|
|
assert.NotEmpty(client.workers["0"].PrivateIP)
|
|
|
|
assert.NotEmpty(client.workers["0"].PublicIP)
|
|
|
|
assert.NotEmpty(client.controlPlanes["0"].PrivateIP)
|
|
|
|
assert.NotEmpty(client.controlPlanes["0"].PublicIP)
|
2022-03-22 11:03:15 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func newSuccessfulResourceGroupStub() *stubResourceGroupAPI {
|
|
|
|
return &stubResourceGroupAPI{
|
|
|
|
getResourceGroup: armresources.ResourceGroup{
|
2022-07-27 16:02:33 -04:00
|
|
|
ID: to.Ptr("resource-group-id"),
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|