2022-09-05 03:06:08 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-04-13 07:01:38 -04:00
|
|
|
package cloudcmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"errors"
|
2022-10-05 03:11:30 -04:00
|
|
|
"runtime"
|
2022-04-13 07:01:38 -04:00
|
|
|
"testing"
|
|
|
|
|
2022-09-21 07:47:57 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/cloud/cloudprovider"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/config"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/state"
|
2022-04-13 07:01:38 -04:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestCreator(t *testing.T) {
|
2022-10-05 03:11:30 -04:00
|
|
|
failOnNonAMD64 := (runtime.GOARCH != "amd64") || (runtime.GOOS != "linux")
|
|
|
|
|
2022-04-13 07:01:38 -04:00
|
|
|
wantGCPState := state.ConstellationState{
|
2022-09-27 03:22:29 -04:00
|
|
|
CloudProvider: cloudprovider.GCP.String(),
|
|
|
|
LoadBalancerIP: "192.0.2.1",
|
2022-04-13 07:01:38 -04:00
|
|
|
}
|
|
|
|
|
2022-10-05 03:11:30 -04:00
|
|
|
wantQEMUState := state.ConstellationState{
|
|
|
|
CloudProvider: cloudprovider.QEMU.String(),
|
|
|
|
LoadBalancerIP: "192.0.2.1",
|
|
|
|
}
|
|
|
|
|
2022-04-13 07:01:38 -04:00
|
|
|
someErr := errors.New("failed")
|
|
|
|
|
|
|
|
testCases := map[string]struct {
|
2022-10-06 05:52:19 -04:00
|
|
|
tfClient terraformClient
|
|
|
|
newTfClientErr error
|
|
|
|
libvirt *stubLibvirtRunner
|
|
|
|
provider cloudprovider.Provider
|
|
|
|
config *config.Config
|
|
|
|
wantState state.ConstellationState
|
|
|
|
wantErr bool
|
|
|
|
wantRollback bool // Use only together with stubClients.
|
2022-04-13 07:01:38 -04:00
|
|
|
}{
|
|
|
|
"gcp": {
|
2022-09-27 03:22:29 -04:00
|
|
|
tfClient: &stubTerraformClient{state: wantGCPState},
|
2022-04-13 07:01:38 -04:00
|
|
|
provider: cloudprovider.GCP,
|
|
|
|
config: config.Default(),
|
|
|
|
wantState: wantGCPState,
|
|
|
|
},
|
2022-10-05 03:11:30 -04:00
|
|
|
"gcp newTerraformClient error": {
|
2022-09-27 03:22:29 -04:00
|
|
|
newTfClientErr: someErr,
|
|
|
|
provider: cloudprovider.GCP,
|
|
|
|
config: config.Default(),
|
|
|
|
wantErr: true,
|
2022-04-13 07:01:38 -04:00
|
|
|
},
|
2022-09-27 03:22:29 -04:00
|
|
|
"gcp create cluster error": {
|
|
|
|
tfClient: &stubTerraformClient{createClusterErr: someErr},
|
2022-06-09 16:26:36 -04:00
|
|
|
provider: cloudprovider.GCP,
|
|
|
|
config: config.Default(),
|
|
|
|
wantErr: true,
|
|
|
|
wantRollback: true,
|
|
|
|
},
|
2022-10-05 03:11:30 -04:00
|
|
|
"qemu": {
|
|
|
|
tfClient: &stubTerraformClient{state: wantQEMUState},
|
|
|
|
libvirt: &stubLibvirtRunner{},
|
|
|
|
provider: cloudprovider.QEMU,
|
|
|
|
config: config.Default(),
|
|
|
|
wantState: wantQEMUState,
|
|
|
|
wantErr: failOnNonAMD64,
|
|
|
|
},
|
|
|
|
"qemu newTerraformClient error": {
|
|
|
|
newTfClientErr: someErr,
|
|
|
|
libvirt: &stubLibvirtRunner{},
|
|
|
|
provider: cloudprovider.QEMU,
|
|
|
|
config: config.Default(),
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"qemu create cluster error": {
|
|
|
|
tfClient: &stubTerraformClient{createClusterErr: someErr},
|
|
|
|
libvirt: &stubLibvirtRunner{},
|
|
|
|
provider: cloudprovider.QEMU,
|
|
|
|
config: config.Default(),
|
|
|
|
wantErr: true,
|
|
|
|
wantRollback: !failOnNonAMD64, // if we run on non-AMD64/linux, we don't get to a point where rollback is needed
|
|
|
|
},
|
|
|
|
"qemu start libvirt error": {
|
|
|
|
tfClient: &stubTerraformClient{state: wantQEMUState},
|
|
|
|
libvirt: &stubLibvirtRunner{startErr: someErr},
|
|
|
|
provider: cloudprovider.QEMU,
|
|
|
|
config: config.Default(),
|
|
|
|
wantErr: true,
|
|
|
|
wantRollback: !failOnNonAMD64,
|
|
|
|
},
|
2022-04-13 07:01:38 -04:00
|
|
|
"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{},
|
2022-09-27 03:22:29 -04:00
|
|
|
newTerraformClient: func(ctx context.Context, provider cloudprovider.Provider) (terraformClient, error) {
|
|
|
|
return tc.tfClient, tc.newTfClientErr
|
2022-04-13 07:01:38 -04:00
|
|
|
},
|
2022-10-05 03:11:30 -04:00
|
|
|
newLibvirtRunner: func() libvirtRunner {
|
|
|
|
return tc.libvirt
|
|
|
|
},
|
2022-04-13 07:01:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
state, err := creator.Create(context.Background(), tc.provider, tc.config, "name", "type", 2, 3)
|
|
|
|
|
|
|
|
if tc.wantErr {
|
|
|
|
assert.Error(err)
|
|
|
|
if tc.wantRollback {
|
2022-10-06 05:52:19 -04:00
|
|
|
cl := tc.tfClient.(*stubTerraformClient)
|
|
|
|
assert.True(cl.destroyClusterCalled)
|
|
|
|
assert.True(cl.cleanUpWorkspaceCalled)
|
|
|
|
if tc.provider == cloudprovider.QEMU {
|
2022-10-05 03:11:30 -04:00
|
|
|
assert.True(tc.libvirt.stopCalled)
|
2022-04-13 07:01:38 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
assert.NoError(err)
|
|
|
|
assert.Equal(tc.wantState, state)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|