constellation/cli/internal/cloudcmd/clients_test.go

228 lines
6.9 KiB
Go
Raw Normal View History

/*
Copyright (c) Edgeless Systems GmbH
SPDX-License-Identifier: AGPL-3.0-only
*/
2022-04-13 07:01:38 -04:00
package cloudcmd
import (
"context"
"strconv"
"testing"
2022-04-13 07:01:38 -04:00
2022-09-21 07:47:57 -04:00
azurecl "github.com/edgelesssys/constellation/v2/cli/internal/azure/client"
2022-09-27 03:22:29 -04:00
"github.com/edgelesssys/constellation/v2/cli/internal/terraform"
2022-09-21 07:47:57 -04:00
"github.com/edgelesssys/constellation/v2/internal/azureshared"
"github.com/edgelesssys/constellation/v2/internal/cloud/cloudprovider"
"github.com/edgelesssys/constellation/v2/internal/cloud/cloudtypes"
"github.com/edgelesssys/constellation/v2/internal/state"
"go.uber.org/goleak"
2022-04-13 07:01:38 -04:00
)
func TestMain(m *testing.M) {
goleak.VerifyTestMain(m,
// https://github.com/census-instrumentation/opencensus-go/issues/1262
goleak.IgnoreTopFunction("go.opencensus.io/stats/view.(*worker).start"),
)
}
2022-04-13 07:01:38 -04:00
type fakeAzureClient struct {
workers cloudtypes.Instances
controlPlanes cloudtypes.Instances
2022-04-13 07:01:38 -04:00
resourceGroup string
name string
uid string
location string
subscriptionID string
tenantID string
subnetID string
2022-05-24 04:04:42 -04:00
loadBalancerName string
controlPlaneScaleSet string
workerScaleSet string
2022-04-13 07:01:38 -04:00
networkSecurityGroup string
adAppObjectID string
}
2022-08-01 06:35:35 -04:00
func (c *fakeAzureClient) GetState() state.ConstellationState {
return state.ConstellationState{
CloudProvider: cloudprovider.Azure.String(),
2022-07-29 02:10:51 -04:00
AzureWorkerInstances: c.workers,
AzureControlPlaneInstances: c.controlPlanes,
Name: c.name,
UID: c.uid,
AzureResourceGroup: c.resourceGroup,
AzureLocation: c.location,
AzureSubscription: c.subscriptionID,
AzureTenant: c.tenantID,
AzureSubnet: c.subnetID,
AzureNetworkSecurityGroup: c.networkSecurityGroup,
2022-07-29 02:10:51 -04:00
AzureWorkerScaleSet: c.workerScaleSet,
AzureControlPlaneScaleSet: c.controlPlaneScaleSet,
AzureADAppObjectID: c.adAppObjectID,
2022-04-13 07:01:38 -04:00
}
}
2022-08-01 06:35:35 -04:00
func (c *fakeAzureClient) SetState(stat state.ConstellationState) {
2022-07-29 02:10:51 -04:00
c.workers = stat.AzureWorkerInstances
c.controlPlanes = stat.AzureControlPlaneInstances
2022-04-13 07:01:38 -04:00
c.name = stat.Name
c.uid = stat.UID
c.resourceGroup = stat.AzureResourceGroup
c.location = stat.AzureLocation
c.subscriptionID = stat.AzureSubscription
c.tenantID = stat.AzureTenant
c.subnetID = stat.AzureSubnet
c.networkSecurityGroup = stat.AzureNetworkSecurityGroup
2022-07-29 02:10:51 -04:00
c.workerScaleSet = stat.AzureWorkerScaleSet
c.controlPlaneScaleSet = stat.AzureControlPlaneScaleSet
2022-04-13 07:01:38 -04:00
c.adAppObjectID = stat.AzureADAppObjectID
}
func (c *fakeAzureClient) CreateApplicationInsight(ctx context.Context) error {
return nil
}
2022-04-13 07:01:38 -04:00
func (c *fakeAzureClient) CreateVirtualNetwork(ctx context.Context) error {
c.subnetID = "subnet"
return nil
}
func (c *fakeAzureClient) CreateExternalLoadBalancer(ctx context.Context, isDebugCluster bool) error {
2022-05-24 04:04:42 -04:00
c.loadBalancerName = "loadBalancer"
return nil
}
2022-04-13 07:01:38 -04:00
func (c *fakeAzureClient) CreateSecurityGroup(ctx context.Context, input azurecl.NetworkSecurityGroupInput) error {
c.networkSecurityGroup = "network-security-group"
return nil
}
func (c *fakeAzureClient) CreateInstances(ctx context.Context, input azurecl.CreateInstancesInput) error {
c.controlPlaneScaleSet = "controlplanes-scale-set"
c.workerScaleSet = "workers-scale-set"
c.workers = make(cloudtypes.Instances)
for i := 0; i < input.CountWorkers; i++ {
2022-04-13 07:01:38 -04:00
id := "id-" + strconv.Itoa(i)
c.workers[id] = cloudtypes.Instance{PublicIP: "192.0.2.1", PrivateIP: "192.0.2.1"}
2022-04-13 07:01:38 -04:00
}
c.controlPlanes = make(cloudtypes.Instances)
for i := 0; i < input.CountControlPlanes; i++ {
2022-04-13 07:01:38 -04:00
id := "id-" + strconv.Itoa(i)
c.controlPlanes[id] = cloudtypes.Instance{PublicIP: "192.0.2.1", PrivateIP: "192.0.2.1"}
2022-04-13 07:01:38 -04:00
}
return nil
}
func (c *fakeAzureClient) CreateServicePrincipal(ctx context.Context) (string, error) {
c.adAppObjectID = "00000000-0000-0000-0000-000000000001"
2022-06-07 10:27:55 -04:00
return azureshared.ApplicationCredentials{
2022-08-29 08:18:05 -04:00
AppClientID: "client-id",
ClientSecretValue: "client-secret",
2022-06-07 10:27:55 -04:00
}.ToCloudServiceAccountURI(), nil
2022-04-13 07:01:38 -04:00
}
func (c *fakeAzureClient) TerminateResourceGroupResources(ctx context.Context) error {
// TODO(katexochen)
2022-04-13 07:01:38 -04:00
return nil
}
func (c *fakeAzureClient) TerminateServicePrincipal(ctx context.Context) error {
if c.adAppObjectID == "" {
return nil
}
c.adAppObjectID = ""
return nil
}
type stubAzureClient struct {
terminateResourceGroupResourcesCalled bool
terminateServicePrincipalCalled bool
2022-04-13 07:01:38 -04:00
createApplicationInsightErr error
createVirtualNetworkErr error
createSecurityGroupErr error
createLoadBalancerErr error
createInstancesErr error
createServicePrincipalErr error
terminateResourceGroupResourcesErr error
terminateServicePrincipalErr error
2022-04-13 07:01:38 -04:00
}
2022-08-01 06:35:35 -04:00
func (c *stubAzureClient) GetState() state.ConstellationState {
return state.ConstellationState{}
2022-04-13 07:01:38 -04:00
}
2022-08-01 06:35:35 -04:00
func (c *stubAzureClient) SetState(state.ConstellationState) {
2022-04-13 07:01:38 -04:00
}
func (c *stubAzureClient) CreateExternalLoadBalancer(ctx context.Context, isDebugCluster bool) error {
2022-05-24 04:04:42 -04:00
return c.createLoadBalancerErr
}
func (c *stubAzureClient) CreateApplicationInsight(ctx context.Context) error {
return c.createApplicationInsightErr
}
2022-04-13 07:01:38 -04:00
func (c *stubAzureClient) CreateVirtualNetwork(ctx context.Context) error {
return c.createVirtualNetworkErr
}
func (c *stubAzureClient) CreateSecurityGroup(ctx context.Context, input azurecl.NetworkSecurityGroupInput) error {
return c.createSecurityGroupErr
}
func (c *stubAzureClient) CreateInstances(ctx context.Context, input azurecl.CreateInstancesInput) error {
return c.createInstancesErr
}
func (c *stubAzureClient) CreateServicePrincipal(ctx context.Context) (string, error) {
2022-06-07 10:27:55 -04:00
return azureshared.ApplicationCredentials{
2022-08-29 08:18:05 -04:00
AppClientID: "00000000-0000-0000-0000-000000000000",
ClientSecretValue: "secret",
2022-06-07 10:27:55 -04:00
}.ToCloudServiceAccountURI(), c.createServicePrincipalErr
2022-04-13 07:01:38 -04:00
}
func (c *stubAzureClient) TerminateResourceGroupResources(ctx context.Context) error {
c.terminateResourceGroupResourcesCalled = true
return c.terminateResourceGroupResourcesErr
2022-04-13 07:01:38 -04:00
}
func (c *stubAzureClient) TerminateServicePrincipal(ctx context.Context) error {
c.terminateServicePrincipalCalled = true
return c.terminateServicePrincipalErr
}
2022-09-27 03:22:29 -04:00
type stubTerraformClient struct {
state state.ConstellationState
cleanUpWorkspaceCalled bool
removeInstallerCalled bool
destroyClusterCalled bool
createClusterErr error
destroyClusterErr error
cleanUpWorkspaceErr error
2022-06-09 16:26:36 -04:00
}
2022-09-27 03:22:29 -04:00
func (c *stubTerraformClient) GetState() state.ConstellationState {
return c.state
2022-04-13 07:01:38 -04:00
}
2022-09-27 03:22:29 -04:00
func (c *stubTerraformClient) CreateCluster(ctx context.Context, name string, input terraform.Variables) error {
return c.createClusterErr
2022-04-13 07:01:38 -04:00
}
2022-09-27 03:22:29 -04:00
func (c *stubTerraformClient) DestroyCluster(ctx context.Context) error {
c.destroyClusterCalled = true
return c.destroyClusterErr
2022-04-13 07:01:38 -04:00
}
2022-09-27 03:22:29 -04:00
func (c *stubTerraformClient) CleanUpWorkspace() error {
c.cleanUpWorkspaceCalled = true
return c.cleanUpWorkspaceErr
2022-06-09 16:26:36 -04:00
}
2022-09-27 03:22:29 -04:00
func (c *stubTerraformClient) RemoveInstaller() {
c.removeInstallerCalled = true
2022-04-13 07:01:38 -04:00
}