2022-04-13 07:01:38 -04:00
|
|
|
package cloudcmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"testing"
|
|
|
|
|
2022-06-07 05:08:44 -04:00
|
|
|
"github.com/edgelesssys/constellation/internal/cloud/cloudprovider"
|
2022-06-08 02:17:52 -04:00
|
|
|
"github.com/edgelesssys/constellation/internal/cloud/cloudtypes"
|
2022-04-13 07:01:38 -04:00
|
|
|
"github.com/edgelesssys/constellation/internal/config"
|
|
|
|
"github.com/edgelesssys/constellation/internal/state"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestCreator(t *testing.T) {
|
|
|
|
wantGCPState := state.ConstellationState{
|
|
|
|
CloudProvider: cloudprovider.GCP.String(),
|
|
|
|
GCPProject: "project",
|
2022-07-29 02:10:51 -04:00
|
|
|
GCPControlPlaneInstances: cloudtypes.Instances{
|
2022-04-13 07:01:38 -04:00
|
|
|
"id-0": {PrivateIP: "192.0.2.1", PublicIP: "192.0.2.1"},
|
|
|
|
"id-1": {PrivateIP: "192.0.2.1", PublicIP: "192.0.2.1"},
|
|
|
|
},
|
2022-07-29 02:10:51 -04:00
|
|
|
GCPWorkerInstances: cloudtypes.Instances{
|
2022-04-13 07:01:38 -04:00
|
|
|
"id-0": {PrivateIP: "192.0.2.1", PublicIP: "192.0.2.1"},
|
|
|
|
"id-1": {PrivateIP: "192.0.2.1", PublicIP: "192.0.2.1"},
|
|
|
|
"id-2": {PrivateIP: "192.0.2.1", PublicIP: "192.0.2.1"},
|
|
|
|
},
|
2022-06-29 09:26:29 -04:00
|
|
|
GCPWorkerInstanceGroup: "workers-group",
|
|
|
|
GCPControlPlaneInstanceGroup: "controlplane-group",
|
|
|
|
GCPWorkerInstanceTemplate: "worker-template",
|
|
|
|
GCPControlPlaneInstanceTemplate: "controlplane-template",
|
|
|
|
GCPNetwork: "network",
|
|
|
|
GCPSubnetwork: "subnetwork",
|
2022-08-01 10:51:34 -04:00
|
|
|
GCPLoadbalancers: []string{"kube-lb", "boot-lb", "verify-lb"},
|
2022-05-24 04:04:42 -04:00
|
|
|
GCPFirewalls: []string{
|
2022-07-15 05:53:14 -04:00
|
|
|
"bootstrapper", "ssh", "nodeport", "kubernetes",
|
2022-05-24 04:04:42 -04:00
|
|
|
"allow-cluster-internal-tcp", "allow-cluster-internal-udp", "allow-cluster-internal-icmp",
|
|
|
|
"allow-node-internal-tcp", "allow-node-internal-udp", "allow-node-internal-icmp",
|
|
|
|
},
|
2022-04-13 07:01:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
wantAzureState := state.ConstellationState{
|
|
|
|
CloudProvider: cloudprovider.Azure.String(),
|
2022-07-29 02:10:51 -04:00
|
|
|
AzureControlPlaneInstances: cloudtypes.Instances{
|
2022-04-13 07:01:38 -04:00
|
|
|
"id-0": {PrivateIP: "192.0.2.1", PublicIP: "192.0.2.1"},
|
|
|
|
"id-1": {PrivateIP: "192.0.2.1", PublicIP: "192.0.2.1"},
|
|
|
|
},
|
2022-07-29 02:10:51 -04:00
|
|
|
AzureWorkerInstances: cloudtypes.Instances{
|
2022-04-13 07:01:38 -04:00
|
|
|
"id-0": {PrivateIP: "192.0.2.1", PublicIP: "192.0.2.1"},
|
|
|
|
"id-1": {PrivateIP: "192.0.2.1", PublicIP: "192.0.2.1"},
|
|
|
|
"id-2": {PrivateIP: "192.0.2.1", PublicIP: "192.0.2.1"},
|
|
|
|
},
|
2022-07-29 02:10:51 -04:00
|
|
|
AzureSubnet: "subnet",
|
|
|
|
AzureNetworkSecurityGroup: "network-security-group",
|
|
|
|
AzureWorkerScaleSet: "workers-scale-set",
|
|
|
|
AzureControlPlaneScaleSet: "controlplanes-scale-set",
|
2022-04-13 07:01:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
someErr := errors.New("failed")
|
|
|
|
|
|
|
|
testCases := map[string]struct {
|
|
|
|
gcpclient gcpclient
|
|
|
|
newGCPClientErr error
|
|
|
|
azureclient azureclient
|
|
|
|
newAzureClientErr error
|
2022-04-13 09:09:33 -04:00
|
|
|
provider cloudprovider.Provider
|
2022-04-13 07:01:38 -04:00
|
|
|
config *config.Config
|
|
|
|
wantState state.ConstellationState
|
|
|
|
wantErr bool
|
|
|
|
wantRollback bool // Use only together with stubClients.
|
|
|
|
}{
|
|
|
|
"gcp": {
|
|
|
|
gcpclient: &fakeGcpClient{project: "project"},
|
|
|
|
provider: cloudprovider.GCP,
|
|
|
|
config: config.Default(),
|
|
|
|
wantState: wantGCPState,
|
|
|
|
},
|
|
|
|
"gcp newGCPClient error": {
|
|
|
|
newGCPClientErr: someErr,
|
|
|
|
provider: cloudprovider.GCP,
|
|
|
|
config: config.Default(),
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"gcp CreateVPCs error": {
|
|
|
|
gcpclient: &stubGcpClient{createVPCsErr: someErr},
|
|
|
|
provider: cloudprovider.GCP,
|
|
|
|
config: config.Default(),
|
|
|
|
wantErr: true,
|
|
|
|
wantRollback: true,
|
|
|
|
},
|
|
|
|
"gcp CreateFirewall error": {
|
|
|
|
gcpclient: &stubGcpClient{createFirewallErr: someErr},
|
|
|
|
provider: cloudprovider.GCP,
|
|
|
|
config: config.Default(),
|
|
|
|
wantErr: true,
|
|
|
|
wantRollback: true,
|
|
|
|
},
|
|
|
|
"gcp CreateInstances error": {
|
|
|
|
gcpclient: &stubGcpClient{createInstancesErr: someErr},
|
|
|
|
provider: cloudprovider.GCP,
|
|
|
|
config: config.Default(),
|
|
|
|
wantErr: true,
|
|
|
|
wantRollback: true,
|
|
|
|
},
|
2022-06-09 16:26:36 -04:00
|
|
|
"gcp CreateLoadBalancer error": {
|
|
|
|
gcpclient: &stubGcpClient{createLoadBalancerErr: someErr},
|
|
|
|
provider: cloudprovider.GCP,
|
|
|
|
config: config.Default(),
|
|
|
|
wantErr: true,
|
|
|
|
wantRollback: true,
|
|
|
|
},
|
2022-04-13 07:01:38 -04:00
|
|
|
"azure": {
|
|
|
|
azureclient: &fakeAzureClient{},
|
|
|
|
provider: cloudprovider.Azure,
|
|
|
|
config: config.Default(),
|
|
|
|
wantState: wantAzureState,
|
|
|
|
},
|
|
|
|
"azure newAzureClient error": {
|
|
|
|
newAzureClientErr: someErr,
|
|
|
|
provider: cloudprovider.Azure,
|
|
|
|
config: config.Default(),
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"azure CreateVirtualNetwork error": {
|
|
|
|
azureclient: &stubAzureClient{createVirtualNetworkErr: someErr},
|
|
|
|
provider: cloudprovider.Azure,
|
|
|
|
config: config.Default(),
|
|
|
|
wantErr: true,
|
|
|
|
wantRollback: true,
|
|
|
|
},
|
|
|
|
"azure CreateSecurityGroup error": {
|
|
|
|
azureclient: &stubAzureClient{createSecurityGroupErr: someErr},
|
|
|
|
provider: cloudprovider.Azure,
|
|
|
|
config: config.Default(),
|
|
|
|
wantErr: true,
|
|
|
|
wantRollback: true,
|
|
|
|
},
|
|
|
|
"azure CreateInstances error": {
|
|
|
|
azureclient: &stubAzureClient{createInstancesErr: someErr},
|
|
|
|
provider: cloudprovider.Azure,
|
|
|
|
config: config.Default(),
|
|
|
|
wantErr: true,
|
|
|
|
wantRollback: true,
|
|
|
|
},
|
|
|
|
"unknown provider": {
|
|
|
|
provider: cloudprovider.Unknown,
|
|
|
|
config: config.Default(),
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
creator := &Creator{
|
|
|
|
out: &bytes.Buffer{},
|
|
|
|
newGCPClient: func(ctx context.Context, project, zone, region, name string) (gcpclient, error) {
|
|
|
|
return tc.gcpclient, tc.newGCPClientErr
|
|
|
|
},
|
2022-08-25 09:12:08 -04:00
|
|
|
newAzureClient: func(subscriptionID, tenantID, name, location, resourceGroup string) (azureclient, error) {
|
2022-04-13 07:01:38 -04:00
|
|
|
return tc.azureclient, tc.newAzureClientErr
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
state, err := creator.Create(context.Background(), tc.provider, tc.config, "name", "type", 2, 3)
|
|
|
|
|
|
|
|
if tc.wantErr {
|
|
|
|
assert.Error(err)
|
|
|
|
if tc.wantRollback {
|
|
|
|
switch tc.provider {
|
|
|
|
case cloudprovider.GCP:
|
|
|
|
cl := tc.gcpclient.(*stubGcpClient)
|
|
|
|
assert.True(cl.terminateFirewallCalled)
|
|
|
|
assert.True(cl.terminateInstancesCalled)
|
|
|
|
assert.True(cl.terminateVPCsCalled)
|
|
|
|
assert.True(cl.closeCalled)
|
|
|
|
case cloudprovider.Azure:
|
|
|
|
cl := tc.azureclient.(*stubAzureClient)
|
2022-08-25 09:12:08 -04:00
|
|
|
assert.True(cl.terminateResourceGroupResourcesCalled)
|
2022-04-13 07:01:38 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
assert.NoError(err)
|
|
|
|
assert.Equal(tc.wantState, state)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|