constellation/cli/internal/cloudcmd/clients_test.go

404 lines
12 KiB
Go
Raw Normal View History

2022-04-13 07:01:38 -04:00
package cloudcmd
import (
"context"
"errors"
"strconv"
"testing"
2022-04-13 07:01:38 -04:00
2022-06-07 10:30:41 -04:00
azurecl "github.com/edgelesssys/constellation/cli/internal/azure/client"
2022-06-07 08:52:47 -04:00
gcpcl "github.com/edgelesssys/constellation/cli/internal/gcp/client"
2022-06-07 10:27:55 -04:00
"github.com/edgelesssys/constellation/internal/azureshared"
"github.com/edgelesssys/constellation/internal/cloud/cloudprovider"
"github.com/edgelesssys/constellation/internal/cloud/cloudtypes"
2022-04-13 07:01:38 -04:00
"github.com/edgelesssys/constellation/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) CreateResourceGroup(ctx context.Context) error {
c.resourceGroup = "resource-group"
return nil
}
func (c *fakeAzureClient) CreateVirtualNetwork(ctx context.Context) error {
c.subnetID = "subnet"
return nil
}
2022-05-24 04:04:42 -04:00
func (c *fakeAzureClient) CreateExternalLoadBalancer(ctx context.Context) error {
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-04-13 07:01:38 -04:00
ClientID: "client-id",
ClientSecret: "client-secret",
2022-06-07 10:27:55 -04:00
}.ToCloudServiceAccountURI(), nil
2022-04-13 07:01:38 -04:00
}
func (c *fakeAzureClient) TerminateResourceGroup(ctx context.Context) error {
if c.resourceGroup == "" {
return nil
}
c.workers = nil
c.controlPlanes = nil
2022-04-13 07:01:38 -04:00
c.resourceGroup = ""
c.subnetID = ""
c.networkSecurityGroup = ""
c.workerScaleSet = ""
c.controlPlaneScaleSet = ""
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 {
terminateResourceGroupCalled bool
terminateServicePrincipalCalled bool
createApplicationInsightErr error
2022-04-13 07:01:38 -04:00
createResourceGroupErr error
createVirtualNetworkErr error
createSecurityGroupErr error
2022-05-24 04:04:42 -04:00
createLoadBalancerErr error
2022-04-13 07:01:38 -04:00
createInstancesErr error
createServicePrincipalErr error
terminateResourceGroupErr error
terminateServicePrincipalErr error
}
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
}
2022-05-24 04:04:42 -04:00
func (c *stubAzureClient) CreateExternalLoadBalancer(ctx context.Context) error {
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) CreateResourceGroup(ctx context.Context) error {
return c.createResourceGroupErr
}
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-04-13 07:01:38 -04:00
ClientID: "00000000-0000-0000-0000-000000000000",
ClientSecret: "secret",
2022-06-07 10:27:55 -04:00
}.ToCloudServiceAccountURI(), c.createServicePrincipalErr
2022-04-13 07:01:38 -04:00
}
func (c *stubAzureClient) TerminateResourceGroup(ctx context.Context) error {
c.terminateResourceGroupCalled = true
return c.terminateResourceGroupErr
}
func (c *stubAzureClient) TerminateServicePrincipal(ctx context.Context) error {
c.terminateServicePrincipalCalled = true
return c.terminateServicePrincipalErr
}
type fakeGcpClient struct {
workers cloudtypes.Instances
controlPlanes cloudtypes.Instances
workerInstanceGroup string
controlPlaneInstanceGroup string
controlPlaneTemplate string
workerTemplate string
network string
subnetwork string
firewalls []string
project string
uid string
name string
zone string
2022-08-01 10:51:34 -04:00
loadbalancers []string
2022-04-13 07:01:38 -04:00
}
2022-08-01 06:35:35 -04:00
func (c *fakeGcpClient) GetState() state.ConstellationState {
return state.ConstellationState{
CloudProvider: cloudprovider.GCP.String(),
2022-07-29 02:10:51 -04:00
GCPWorkerInstances: c.workers,
GCPControlPlaneInstances: c.controlPlanes,
GCPWorkerInstanceGroup: c.workerInstanceGroup,
GCPControlPlaneInstanceGroup: c.controlPlaneInstanceGroup,
GCPWorkerInstanceTemplate: c.workerTemplate,
GCPControlPlaneInstanceTemplate: c.controlPlaneTemplate,
GCPNetwork: c.network,
GCPSubnetwork: c.subnetwork,
GCPFirewalls: c.firewalls,
GCPProject: c.project,
Name: c.name,
UID: c.uid,
GCPZone: c.zone,
2022-08-01 10:51:34 -04:00
GCPLoadbalancers: c.loadbalancers,
2022-04-13 07:01:38 -04:00
}
}
2022-08-01 06:35:35 -04:00
func (c *fakeGcpClient) SetState(stat state.ConstellationState) {
2022-07-29 02:10:51 -04:00
c.workers = stat.GCPWorkerInstances
c.controlPlanes = stat.GCPControlPlaneInstances
c.workerInstanceGroup = stat.GCPWorkerInstanceGroup
c.controlPlaneInstanceGroup = stat.GCPControlPlaneInstanceGroup
c.workerTemplate = stat.GCPWorkerInstanceTemplate
c.controlPlaneTemplate = stat.GCPControlPlaneInstanceTemplate
2022-04-13 07:01:38 -04:00
c.network = stat.GCPNetwork
c.subnetwork = stat.GCPSubnetwork
c.firewalls = stat.GCPFirewalls
c.project = stat.GCPProject
c.name = stat.Name
c.uid = stat.UID
c.zone = stat.GCPZone
2022-08-01 10:51:34 -04:00
c.loadbalancers = stat.GCPLoadbalancers
2022-04-13 07:01:38 -04:00
}
func (c *fakeGcpClient) CreateVPCs(ctx context.Context) error {
2022-04-13 07:01:38 -04:00
c.network = "network"
c.subnetwork = "subnetwork"
return nil
}
func (c *fakeGcpClient) CreateFirewall(ctx context.Context, input gcpcl.FirewallInput) error {
if c.network == "" {
return errors.New("client has not network")
}
for _, rule := range input.Ingress {
2022-05-24 04:04:42 -04:00
c.firewalls = append(c.firewalls, rule.Name)
2022-04-13 07:01:38 -04:00
}
return nil
}
func (c *fakeGcpClient) CreateInstances(ctx context.Context, input gcpcl.CreateInstancesInput) error {
c.controlPlaneInstanceGroup = "controlplane-group"
c.workerInstanceGroup = "workers-group"
c.workerTemplate = "worker-template"
c.controlPlaneTemplate = "controlplane-template"
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
}
2022-08-01 10:51:34 -04:00
func (c *fakeGcpClient) CreateLoadBalancers(ctx context.Context) error {
c.loadbalancers = []string{"kube-lb", "boot-lb", "verify-lb"}
2022-06-09 16:26:36 -04:00
return nil
}
2022-04-13 07:01:38 -04:00
func (c *fakeGcpClient) TerminateFirewall(ctx context.Context) error {
if len(c.firewalls) == 0 {
return nil
}
c.firewalls = nil
return nil
}
func (c *fakeGcpClient) TerminateVPCs(context.Context) error {
if len(c.firewalls) != 0 {
return errors.New("client has firewalls, which must be deleted first")
}
c.network = ""
c.subnetwork = ""
return nil
}
func (c *fakeGcpClient) TerminateInstances(context.Context) error {
c.workerTemplate = ""
c.controlPlaneTemplate = ""
c.workerInstanceGroup = ""
c.controlPlaneInstanceGroup = ""
c.workers = nil
c.controlPlanes = nil
2022-04-13 07:01:38 -04:00
return nil
}
2022-08-01 10:51:34 -04:00
func (c *fakeGcpClient) TerminateLoadBalancers(context.Context) error {
c.loadbalancers = nil
2022-06-09 16:26:36 -04:00
return nil
}
2022-04-13 07:01:38 -04:00
func (c *fakeGcpClient) Close() error {
return nil
}
type stubGcpClient struct {
2022-08-23 11:49:55 -04:00
terminateFirewallCalled bool
terminateInstancesCalled bool
terminateVPCsCalled bool
closeCalled bool
createVPCsErr error
createFirewallErr error
createInstancesErr error
createLoadBalancerErr error
terminateFirewallErr error
terminateVPCsErr error
terminateInstancesErr error
terminateLoadBalancerErr error
closeErr error
2022-04-13 07:01:38 -04:00
}
2022-08-01 06:35:35 -04:00
func (c *stubGcpClient) GetState() state.ConstellationState {
return state.ConstellationState{}
2022-04-13 07:01:38 -04:00
}
2022-08-01 06:35:35 -04:00
func (c *stubGcpClient) SetState(state.ConstellationState) {
2022-04-13 07:01:38 -04:00
}
func (c *stubGcpClient) CreateVPCs(ctx context.Context) error {
2022-04-13 07:01:38 -04:00
return c.createVPCsErr
}
func (c *stubGcpClient) CreateFirewall(ctx context.Context, input gcpcl.FirewallInput) error {
return c.createFirewallErr
}
func (c *stubGcpClient) CreateInstances(ctx context.Context, input gcpcl.CreateInstancesInput) error {
return c.createInstancesErr
}
2022-08-01 10:51:34 -04:00
func (c *stubGcpClient) CreateLoadBalancers(ctx context.Context) error {
2022-06-09 16:26:36 -04:00
return c.createLoadBalancerErr
}
2022-04-13 07:01:38 -04:00
func (c *stubGcpClient) TerminateFirewall(ctx context.Context) error {
c.terminateFirewallCalled = true
return c.terminateFirewallErr
}
func (c *stubGcpClient) TerminateVPCs(context.Context) error {
c.terminateVPCsCalled = true
return c.terminateVPCsErr
}
func (c *stubGcpClient) TerminateInstances(context.Context) error {
c.terminateInstancesCalled = true
return c.terminateInstancesErr
}
2022-08-01 10:51:34 -04:00
func (c *stubGcpClient) TerminateLoadBalancers(context.Context) error {
2022-06-09 16:26:36 -04:00
return c.terminateLoadBalancerErr
}
2022-04-13 07:01:38 -04:00
func (c *stubGcpClient) Close() error {
c.closeCalled = true
return c.closeErr
}