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 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-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"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestCreateInstances(t *testing.T) {
|
|
|
|
someErr := errors.New("failed")
|
|
|
|
testCases := map[string]struct {
|
|
|
|
publicIPAddressesAPI publicIPAddressesAPI
|
|
|
|
networkInterfacesAPI networkInterfacesAPI
|
|
|
|
scaleSetsAPI scaleSetsAPI
|
|
|
|
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-09-06 04:05:51 -04:00
|
|
|
getResponse: armcomputev2.VirtualMachineScaleSet{
|
|
|
|
Identity: &armcomputev2.VirtualMachineScaleSetIdentity{PrincipalID: to.Ptr("principal-id")}, SKU: &armcomputev2.SKU{Capacity: to.Ptr[int64](0)},
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
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-08-25 09:24:31 -04:00
|
|
|
ConfidentialVM: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
"error when creating scale set": {
|
|
|
|
publicIPAddressesAPI: stubPublicIPAddressesAPI{},
|
|
|
|
networkInterfacesAPI: stubNetworkInterfacesAPI{},
|
|
|
|
scaleSetsAPI: stubScaleSetsAPI{createErr: someErr},
|
|
|
|
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-08-25 09:24:31 -04:00
|
|
|
ConfidentialVM: true,
|
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-08-24 05:31:43 -04:00
|
|
|
scaleSetsAPI: stubScaleSetsAPI{getErr: someErr},
|
2022-03-22 11:03:15 -04:00
|
|
|
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-08-25 09:24:31 -04:00
|
|
|
ConfidentialVM: true,
|
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 private IPs": {
|
|
|
|
publicIPAddressesAPI: stubPublicIPAddressesAPI{},
|
|
|
|
networkInterfacesAPI: stubNetworkInterfacesAPI{getErr: someErr},
|
|
|
|
scaleSetsAPI: stubScaleSetsAPI{},
|
|
|
|
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-08-25 09:24:31 -04:00
|
|
|
ConfidentialVM: true,
|
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)
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
client := Client{
|
|
|
|
location: "location",
|
|
|
|
name: "name",
|
|
|
|
uid: "uid",
|
|
|
|
resourceGroup: "name",
|
|
|
|
publicIPAddressesAPI: tc.publicIPAddressesAPI,
|
|
|
|
networkInterfacesAPI: tc.networkInterfacesAPI,
|
|
|
|
scaleSetsAPI: tc.scaleSetsAPI,
|
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
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|