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-10-12 11:00:59 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/cli/internal/terraform"
|
2022-09-21 07:47:57 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/cloud/cloudprovider"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/config"
|
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-10-11 06:24:33 -04:00
|
|
|
ip := "192.0.2.1"
|
2022-04-13 07:01:38 -04:00
|
|
|
someErr := errors.New("failed")
|
|
|
|
|
|
|
|
testCases := map[string]struct {
|
2022-11-15 08:00:44 -05:00
|
|
|
tfClient terraformClient
|
|
|
|
newTfClientErr error
|
|
|
|
libvirt *stubLibvirtRunner
|
|
|
|
provider cloudprovider.Provider
|
|
|
|
config *config.Config
|
2023-03-20 08:33:04 -04:00
|
|
|
policyPatcher *stubPolicyPatcher
|
2022-11-15 08:00:44 -05:00
|
|
|
wantErr bool
|
|
|
|
wantRollback bool // Use only together with stubClients.
|
|
|
|
wantTerraformRollback bool // When libvirt fails, don't call into Terraform.
|
2022-04-13 07:01:38 -04:00
|
|
|
}{
|
|
|
|
"gcp": {
|
2022-10-11 06:24:33 -04:00
|
|
|
tfClient: &stubTerraformClient{ip: ip},
|
|
|
|
provider: cloudprovider.GCP,
|
|
|
|
config: config.Default(),
|
2022-04-13 07:01:38 -04:00
|
|
|
},
|
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": {
|
2022-11-15 08:00:44 -05:00
|
|
|
tfClient: &stubTerraformClient{createClusterErr: someErr},
|
|
|
|
provider: cloudprovider.GCP,
|
|
|
|
config: config.Default(),
|
|
|
|
wantErr: true,
|
|
|
|
wantRollback: true,
|
|
|
|
wantTerraformRollback: true,
|
2022-06-09 16:26:36 -04:00
|
|
|
},
|
2023-03-20 08:33:04 -04:00
|
|
|
"azure": {
|
|
|
|
tfClient: &stubTerraformClient{ip: ip},
|
|
|
|
provider: cloudprovider.Azure,
|
|
|
|
config: config.Default(),
|
|
|
|
policyPatcher: &stubPolicyPatcher{},
|
|
|
|
},
|
|
|
|
"azure new policy patch error": {
|
|
|
|
tfClient: &stubTerraformClient{ip: ip},
|
|
|
|
provider: cloudprovider.Azure,
|
|
|
|
config: config.Default(),
|
|
|
|
policyPatcher: &stubPolicyPatcher{someErr},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"azure newTerraformClient error": {
|
|
|
|
newTfClientErr: someErr,
|
|
|
|
provider: cloudprovider.Azure,
|
|
|
|
config: config.Default(),
|
|
|
|
policyPatcher: &stubPolicyPatcher{},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"azure create cluster error": {
|
|
|
|
tfClient: &stubTerraformClient{createClusterErr: someErr},
|
|
|
|
provider: cloudprovider.Azure,
|
|
|
|
config: config.Default(),
|
|
|
|
policyPatcher: &stubPolicyPatcher{},
|
|
|
|
wantErr: true,
|
|
|
|
wantRollback: true,
|
|
|
|
wantTerraformRollback: true,
|
|
|
|
},
|
2022-10-05 03:11:30 -04:00
|
|
|
"qemu": {
|
2022-10-11 06:24:33 -04:00
|
|
|
tfClient: &stubTerraformClient{ip: ip},
|
|
|
|
libvirt: &stubLibvirtRunner{},
|
|
|
|
provider: cloudprovider.QEMU,
|
|
|
|
config: config.Default(),
|
|
|
|
wantErr: failOnNonAMD64,
|
2022-10-05 03:11:30 -04:00
|
|
|
},
|
|
|
|
"qemu newTerraformClient error": {
|
|
|
|
newTfClientErr: someErr,
|
|
|
|
libvirt: &stubLibvirtRunner{},
|
|
|
|
provider: cloudprovider.QEMU,
|
|
|
|
config: config.Default(),
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"qemu create cluster error": {
|
2022-11-15 08:00:44 -05:00
|
|
|
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
|
|
|
|
wantTerraformRollback: true,
|
2022-10-05 03:11:30 -04:00
|
|
|
},
|
|
|
|
"qemu start libvirt error": {
|
2022-11-15 08:00:44 -05:00
|
|
|
tfClient: &stubTerraformClient{ip: ip},
|
|
|
|
libvirt: &stubLibvirtRunner{startErr: someErr},
|
|
|
|
provider: cloudprovider.QEMU,
|
|
|
|
config: config.Default(),
|
|
|
|
wantRollback: !failOnNonAMD64,
|
|
|
|
wantTerraformRollback: false,
|
|
|
|
wantErr: true,
|
2022-10-05 03:11:30 -04:00
|
|
|
},
|
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-11-22 12:47:08 -05:00
|
|
|
image: &stubImageFetcher{
|
|
|
|
reference: "some-image",
|
|
|
|
},
|
2022-10-26 09:57:00 -04:00
|
|
|
newTerraformClient: func(ctx context.Context) (terraformClient, error) {
|
2022-09-27 03:22:29 -04:00
|
|
|
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-11-22 12:47:08 -05:00
|
|
|
newRawDownloader: func() rawDownloader {
|
|
|
|
return &stubRawDownloader{
|
|
|
|
destination: "some-destination",
|
|
|
|
}
|
|
|
|
},
|
2023-03-20 08:33:04 -04:00
|
|
|
policyPatcher: tc.policyPatcher,
|
2022-04-13 07:01:38 -04:00
|
|
|
}
|
|
|
|
|
2023-02-10 07:27:22 -05:00
|
|
|
idFile, err := creator.Create(context.Background(), tc.provider, tc.config, "type", 2, 3)
|
2022-04-13 07:01:38 -04:00
|
|
|
|
|
|
|
if tc.wantErr {
|
|
|
|
assert.Error(err)
|
|
|
|
if tc.wantRollback {
|
2022-10-06 05:52:19 -04:00
|
|
|
cl := tc.tfClient.(*stubTerraformClient)
|
2022-11-15 08:00:44 -05:00
|
|
|
if tc.wantTerraformRollback {
|
2023-02-13 02:42:54 -05:00
|
|
|
assert.True(cl.destroyCalled)
|
2022-11-15 08:00:44 -05:00
|
|
|
}
|
2022-10-06 05:52:19 -04:00
|
|
|
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)
|
2022-10-11 06:24:33 -04:00
|
|
|
assert.Equal(tc.provider, idFile.CloudProvider)
|
|
|
|
assert.Equal(ip, idFile.IP)
|
2022-04-13 07:01:38 -04:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2022-10-12 11:00:59 -04:00
|
|
|
|
2023-03-20 08:33:04 -04:00
|
|
|
type stubPolicyPatcher struct {
|
|
|
|
patchErr error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s stubPolicyPatcher) Patch(ctx context.Context, attestationURL string) error {
|
|
|
|
return s.patchErr
|
|
|
|
}
|
|
|
|
|
2022-10-12 11:00:59 -04:00
|
|
|
func TestNormalizeAzureURIs(t *testing.T) {
|
|
|
|
testCases := map[string]struct {
|
2022-12-07 05:48:54 -05:00
|
|
|
in terraform.AzureClusterVariables
|
|
|
|
want terraform.AzureClusterVariables
|
2022-10-12 11:00:59 -04:00
|
|
|
}{
|
|
|
|
"empty": {
|
2022-12-07 05:48:54 -05:00
|
|
|
in: terraform.AzureClusterVariables{},
|
|
|
|
want: terraform.AzureClusterVariables{},
|
2022-10-12 11:00:59 -04:00
|
|
|
},
|
|
|
|
"no change": {
|
2022-12-07 05:48:54 -05:00
|
|
|
in: terraform.AzureClusterVariables{
|
2022-10-12 11:00:59 -04:00
|
|
|
ImageID: "/communityGalleries/foo/images/constellation/versions/2.1.0",
|
|
|
|
},
|
2022-12-07 05:48:54 -05:00
|
|
|
want: terraform.AzureClusterVariables{
|
2022-10-12 11:00:59 -04:00
|
|
|
ImageID: "/communityGalleries/foo/images/constellation/versions/2.1.0",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"fix image id": {
|
2022-12-07 05:48:54 -05:00
|
|
|
in: terraform.AzureClusterVariables{
|
2022-10-12 11:00:59 -04:00
|
|
|
ImageID: "/CommunityGalleries/foo/Images/constellation/Versions/2.1.0",
|
|
|
|
},
|
2022-12-07 05:48:54 -05:00
|
|
|
want: terraform.AzureClusterVariables{
|
2022-10-12 11:00:59 -04:00
|
|
|
ImageID: "/communityGalleries/foo/images/constellation/versions/2.1.0",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"fix resource group": {
|
2022-12-07 05:48:54 -05:00
|
|
|
in: terraform.AzureClusterVariables{
|
2022-10-12 11:00:59 -04:00
|
|
|
UserAssignedIdentity: "/subscriptions/foo/resourcegroups/test/providers/Microsoft.ManagedIdentity/userAssignedIdentities/uai",
|
|
|
|
},
|
2022-12-07 05:48:54 -05:00
|
|
|
want: terraform.AzureClusterVariables{
|
2022-10-12 11:00:59 -04:00
|
|
|
UserAssignedIdentity: "/subscriptions/foo/resourceGroups/test/providers/Microsoft.ManagedIdentity/userAssignedIdentities/uai",
|
|
|
|
},
|
|
|
|
},
|
2022-10-13 11:38:38 -04:00
|
|
|
"fix arbitrary casing": {
|
2022-12-07 05:48:54 -05:00
|
|
|
in: terraform.AzureClusterVariables{
|
2022-10-13 11:38:38 -04:00
|
|
|
ImageID: "/CoMMUnitygaLLeries/foo/iMAges/constellation/vERsions/2.1.0",
|
|
|
|
UserAssignedIdentity: "/subsCRiptions/foo/resoURCegroups/test/proViDers/MICROsoft.mANAgedIdentity/USerASsignediDENtities/uai",
|
|
|
|
},
|
2022-12-07 05:48:54 -05:00
|
|
|
want: terraform.AzureClusterVariables{
|
2022-10-13 11:38:38 -04:00
|
|
|
ImageID: "/communityGalleries/foo/images/constellation/versions/2.1.0",
|
|
|
|
UserAssignedIdentity: "/subscriptions/foo/resourceGroups/test/providers/Microsoft.ManagedIdentity/userAssignedIdentities/uai",
|
|
|
|
},
|
|
|
|
},
|
2022-10-12 11:00:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
out := normalizeAzureURIs(tc.in)
|
|
|
|
assert.Equal(tc.want, out)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|