2023-06-19 07:02:01 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
|
|
|
package terraform
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2023-06-22 10:53:40 -04:00
|
|
|
"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
|
2023-07-21 04:04:29 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/constants"
|
2023-06-28 08:42:34 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/role"
|
2023-06-19 07:02:01 -04:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAWSClusterVariables(t *testing.T) {
|
|
|
|
vars := AWSClusterVariables{
|
2023-06-23 11:19:43 -04:00
|
|
|
Name: "cluster-name",
|
|
|
|
NodeGroups: map[string]AWSNodeGroup{
|
2023-07-21 04:04:29 -04:00
|
|
|
constants.ControlPlaneDefault: {
|
2023-06-23 11:19:43 -04:00
|
|
|
Role: role.ControlPlane.TFString(),
|
|
|
|
StateDiskSizeGB: 30,
|
2023-08-02 04:41:26 -04:00
|
|
|
InitialCount: 1,
|
2023-06-23 11:19:43 -04:00
|
|
|
Zone: "eu-central-1b",
|
|
|
|
InstanceType: "x1.foo",
|
|
|
|
DiskType: "foodisk",
|
|
|
|
},
|
2023-07-21 04:04:29 -04:00
|
|
|
constants.WorkerDefault: {
|
2023-06-23 11:19:43 -04:00
|
|
|
Role: role.Worker.TFString(),
|
|
|
|
StateDiskSizeGB: 30,
|
2023-08-02 04:41:26 -04:00
|
|
|
InitialCount: 2,
|
2023-06-23 11:19:43 -04:00
|
|
|
Zone: "eu-central-1c",
|
|
|
|
InstanceType: "x1.bar",
|
|
|
|
DiskType: "bardisk",
|
|
|
|
},
|
2023-06-19 07:02:01 -04:00
|
|
|
},
|
|
|
|
Region: "eu-central-1",
|
|
|
|
Zone: "eu-central-1a",
|
|
|
|
AMIImageID: "ami-0123456789abcdef",
|
|
|
|
IAMProfileControlPlane: "arn:aws:iam::123456789012:instance-profile/cluster-name-controlplane",
|
|
|
|
IAMProfileWorkerNodes: "arn:aws:iam::123456789012:instance-profile/cluster-name-worker",
|
|
|
|
Debug: true,
|
|
|
|
EnableSNP: true,
|
2023-07-21 10:43:51 -04:00
|
|
|
CustomEndpoint: "example.com",
|
2023-06-19 07:02:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// test that the variables are correctly rendered
|
2023-06-23 11:19:43 -04:00
|
|
|
want := `name = "cluster-name"
|
|
|
|
region = "eu-central-1"
|
|
|
|
zone = "eu-central-1a"
|
|
|
|
ami = "ami-0123456789abcdef"
|
2023-06-19 07:02:01 -04:00
|
|
|
iam_instance_profile_control_plane = "arn:aws:iam::123456789012:instance-profile/cluster-name-controlplane"
|
2023-06-23 11:19:43 -04:00
|
|
|
iam_instance_profile_worker_nodes = "arn:aws:iam::123456789012:instance-profile/cluster-name-worker"
|
|
|
|
debug = true
|
|
|
|
enable_snp = true
|
|
|
|
node_groups = {
|
|
|
|
control_plane_default = {
|
|
|
|
disk_size = 30
|
|
|
|
disk_type = "foodisk"
|
|
|
|
initial_count = 1
|
|
|
|
instance_type = "x1.foo"
|
|
|
|
role = "control-plane"
|
|
|
|
zone = "eu-central-1b"
|
|
|
|
}
|
|
|
|
worker_default = {
|
|
|
|
disk_size = 30
|
|
|
|
disk_type = "bardisk"
|
|
|
|
initial_count = 2
|
|
|
|
instance_type = "x1.bar"
|
|
|
|
role = "worker"
|
|
|
|
zone = "eu-central-1c"
|
|
|
|
}
|
|
|
|
}
|
2023-10-17 09:46:15 -04:00
|
|
|
custom_endpoint = "example.com"
|
|
|
|
internal_load_balancer = false
|
2023-06-19 07:02:01 -04:00
|
|
|
`
|
|
|
|
got := vars.String()
|
|
|
|
assert.Equal(t, want, got)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAWSIAMVariables(t *testing.T) {
|
|
|
|
vars := AWSIAMVariables{
|
|
|
|
Region: "eu-central-1",
|
|
|
|
Prefix: "my-prefix",
|
|
|
|
}
|
|
|
|
|
|
|
|
// test that the variables are correctly rendered
|
2023-08-23 04:35:42 -04:00
|
|
|
want := `region = "eu-central-1"
|
|
|
|
name_prefix = "my-prefix"
|
2023-06-19 07:02:01 -04:00
|
|
|
`
|
|
|
|
got := vars.String()
|
|
|
|
assert.Equal(t, want, got)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGCPClusterVariables(t *testing.T) {
|
|
|
|
vars := GCPClusterVariables{
|
|
|
|
Name: "cluster-name",
|
|
|
|
Project: "my-project",
|
|
|
|
Region: "eu-central-1",
|
|
|
|
Zone: "eu-central-1a",
|
|
|
|
ImageID: "image-0123456789abcdef",
|
|
|
|
Debug: true,
|
|
|
|
NodeGroups: map[string]GCPNodeGroup{
|
2023-07-21 04:04:29 -04:00
|
|
|
constants.ControlPlaneDefault: {
|
2023-06-23 06:08:30 -04:00
|
|
|
Role: "control-plane",
|
2023-06-19 07:02:01 -04:00
|
|
|
StateDiskSizeGB: 30,
|
2023-08-02 04:41:26 -04:00
|
|
|
InitialCount: 1,
|
2023-06-19 07:02:01 -04:00
|
|
|
Zone: "eu-central-1a",
|
|
|
|
InstanceType: "n2d-standard-4",
|
|
|
|
DiskType: "pd-ssd",
|
|
|
|
},
|
2023-07-21 04:04:29 -04:00
|
|
|
constants.WorkerDefault: {
|
2023-06-23 06:08:30 -04:00
|
|
|
Role: "worker",
|
2023-06-19 07:02:01 -04:00
|
|
|
StateDiskSizeGB: 10,
|
2023-08-02 04:41:26 -04:00
|
|
|
InitialCount: 1,
|
2023-06-19 07:02:01 -04:00
|
|
|
Zone: "eu-central-1b",
|
|
|
|
InstanceType: "n2d-standard-8",
|
|
|
|
DiskType: "pd-ssd",
|
|
|
|
},
|
|
|
|
},
|
2023-07-21 10:43:51 -04:00
|
|
|
CustomEndpoint: "example.com",
|
2023-06-19 07:02:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// test that the variables are correctly rendered
|
|
|
|
want := `name = "cluster-name"
|
|
|
|
project = "my-project"
|
|
|
|
region = "eu-central-1"
|
|
|
|
zone = "eu-central-1a"
|
|
|
|
image_id = "image-0123456789abcdef"
|
|
|
|
debug = true
|
|
|
|
node_groups = {
|
|
|
|
control_plane_default = {
|
|
|
|
disk_size = 30
|
|
|
|
disk_type = "pd-ssd"
|
|
|
|
initial_count = 1
|
|
|
|
instance_type = "n2d-standard-4"
|
2023-06-23 06:08:30 -04:00
|
|
|
role = "control-plane"
|
2023-06-19 07:02:01 -04:00
|
|
|
zone = "eu-central-1a"
|
|
|
|
}
|
|
|
|
worker_default = {
|
|
|
|
disk_size = 10
|
|
|
|
disk_type = "pd-ssd"
|
|
|
|
initial_count = 1
|
|
|
|
instance_type = "n2d-standard-8"
|
2023-06-23 06:08:30 -04:00
|
|
|
role = "worker"
|
2023-06-19 07:02:01 -04:00
|
|
|
zone = "eu-central-1b"
|
|
|
|
}
|
|
|
|
}
|
2023-10-17 09:46:15 -04:00
|
|
|
custom_endpoint = "example.com"
|
|
|
|
internal_load_balancer = false
|
2023-06-19 07:02:01 -04:00
|
|
|
`
|
|
|
|
got := vars.String()
|
|
|
|
assert.Equal(t, want, got)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGCPIAMVariables(t *testing.T) {
|
|
|
|
vars := GCPIAMVariables{
|
|
|
|
Project: "my-project",
|
|
|
|
Region: "eu-central-1",
|
|
|
|
Zone: "eu-central-1a",
|
|
|
|
ServiceAccountID: "my-service-account",
|
|
|
|
}
|
|
|
|
|
|
|
|
// test that the variables are correctly rendered
|
2023-08-23 04:35:42 -04:00
|
|
|
want := `project_id = "my-project"
|
|
|
|
region = "eu-central-1"
|
|
|
|
zone = "eu-central-1a"
|
2023-06-19 07:02:01 -04:00
|
|
|
service_account_id = "my-service-account"
|
|
|
|
`
|
|
|
|
got := vars.String()
|
|
|
|
assert.Equal(t, want, got)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAzureClusterVariables(t *testing.T) {
|
|
|
|
vars := AzureClusterVariables{
|
2023-06-22 10:53:40 -04:00
|
|
|
Name: "cluster-name",
|
|
|
|
NodeGroups: map[string]AzureNodeGroup{
|
2023-07-21 04:04:29 -04:00
|
|
|
constants.ControlPlaneDefault: {
|
2023-06-30 04:53:00 -04:00
|
|
|
Role: "ControlPlane",
|
2023-08-02 04:41:26 -04:00
|
|
|
InitialCount: 1,
|
2023-06-30 04:53:00 -04:00
|
|
|
InstanceType: "Standard_D2s_v3",
|
|
|
|
DiskType: "StandardSSD_LRS",
|
|
|
|
DiskSizeGB: 100,
|
2023-06-22 10:53:40 -04:00
|
|
|
},
|
2023-06-19 07:02:01 -04:00
|
|
|
},
|
2023-06-22 10:53:40 -04:00
|
|
|
ConfidentialVM: to.Ptr(true),
|
2023-06-19 07:02:01 -04:00
|
|
|
ResourceGroup: "my-resource-group",
|
|
|
|
UserAssignedIdentity: "my-user-assigned-identity",
|
|
|
|
ImageID: "image-0123456789abcdef",
|
2023-06-22 10:53:40 -04:00
|
|
|
CreateMAA: to.Ptr(true),
|
|
|
|
Debug: to.Ptr(true),
|
|
|
|
Location: "eu-central-1",
|
2023-07-21 10:43:51 -04:00
|
|
|
CustomEndpoint: "example.com",
|
2023-06-19 07:02:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// test that the variables are correctly rendered
|
2023-06-22 10:53:40 -04:00
|
|
|
want := `name = "cluster-name"
|
|
|
|
image_id = "image-0123456789abcdef"
|
|
|
|
create_maa = true
|
|
|
|
debug = true
|
|
|
|
resource_group = "my-resource-group"
|
|
|
|
location = "eu-central-1"
|
2023-06-19 07:02:01 -04:00
|
|
|
user_assigned_identity = "my-user-assigned-identity"
|
2023-06-22 10:53:40 -04:00
|
|
|
confidential_vm = true
|
|
|
|
node_groups = {
|
|
|
|
control_plane_default = {
|
2023-06-30 04:53:00 -04:00
|
|
|
disk_size = 100
|
|
|
|
disk_type = "StandardSSD_LRS"
|
|
|
|
initial_count = 1
|
|
|
|
instance_type = "Standard_D2s_v3"
|
|
|
|
role = "ControlPlane"
|
|
|
|
zones = null
|
2023-06-22 10:53:40 -04:00
|
|
|
}
|
|
|
|
}
|
2023-10-17 09:46:15 -04:00
|
|
|
custom_endpoint = "example.com"
|
|
|
|
internal_load_balancer = false
|
2023-06-19 07:02:01 -04:00
|
|
|
`
|
|
|
|
got := vars.String()
|
|
|
|
assert.Equal(t, want, got)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAzureIAMVariables(t *testing.T) {
|
|
|
|
vars := AzureIAMVariables{
|
|
|
|
Region: "eu-central-1",
|
|
|
|
ServicePrincipal: "my-service-principal",
|
|
|
|
ResourceGroup: "my-resource-group",
|
|
|
|
}
|
|
|
|
|
|
|
|
// test that the variables are correctly rendered
|
2023-08-23 04:35:42 -04:00
|
|
|
want := `region = "eu-central-1"
|
|
|
|
service_principal_name = "my-service-principal"
|
|
|
|
resource_group_name = "my-resource-group"
|
2023-06-19 07:02:01 -04:00
|
|
|
`
|
|
|
|
got := vars.String()
|
|
|
|
assert.Equal(t, want, got)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestOpenStackClusterVariables(t *testing.T) {
|
|
|
|
vars := OpenStackClusterVariables{
|
2023-07-03 10:33:00 -04:00
|
|
|
Name: "cluster-name",
|
|
|
|
Cloud: toPtr("my-cloud"),
|
2023-06-19 07:02:01 -04:00
|
|
|
FloatingIPPoolID: "fip-pool-0123456789abcdef",
|
|
|
|
ImageURL: "https://example.com/image.raw",
|
|
|
|
DirectDownload: true,
|
|
|
|
OpenstackUserDomainName: "my-user-domain",
|
|
|
|
OpenstackUsername: "my-username",
|
|
|
|
OpenstackPassword: "my-password",
|
|
|
|
Debug: true,
|
2023-07-03 10:33:00 -04:00
|
|
|
NodeGroups: map[string]OpenStackNodeGroup{
|
2023-07-21 04:04:29 -04:00
|
|
|
constants.ControlPlaneDefault: {
|
2023-07-03 10:33:00 -04:00
|
|
|
Role: "control-plane",
|
2023-08-02 04:41:26 -04:00
|
|
|
InitialCount: 1,
|
|
|
|
FlavorID: "flavor-0123456789abcdef",
|
2023-07-03 10:33:00 -04:00
|
|
|
Zone: "az-01",
|
|
|
|
StateDiskType: "performance-8",
|
|
|
|
StateDiskSizeGB: 30,
|
|
|
|
},
|
|
|
|
},
|
2023-07-21 10:43:51 -04:00
|
|
|
CustomEndpoint: "example.com",
|
2023-06-19 07:02:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// test that the variables are correctly rendered
|
|
|
|
want := `name = "cluster-name"
|
2023-07-03 10:33:00 -04:00
|
|
|
node_groups = {
|
|
|
|
control_plane_default = {
|
2023-08-02 04:41:26 -04:00
|
|
|
flavor_id = "flavor-0123456789abcdef"
|
2023-07-03 10:33:00 -04:00
|
|
|
initial_count = 1
|
|
|
|
role = "control-plane"
|
|
|
|
state_disk_size = 30
|
|
|
|
state_disk_type = "performance-8"
|
|
|
|
zone = "az-01"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cloud = "my-cloud"
|
|
|
|
floating_ip_pool_id = "fip-pool-0123456789abcdef"
|
|
|
|
image_url = "https://example.com/image.raw"
|
|
|
|
direct_download = true
|
2023-06-19 07:02:01 -04:00
|
|
|
openstack_user_domain_name = "my-user-domain"
|
2023-07-03 10:33:00 -04:00
|
|
|
openstack_username = "my-username"
|
|
|
|
openstack_password = "my-password"
|
|
|
|
debug = true
|
2023-07-21 10:43:51 -04:00
|
|
|
custom_endpoint = "example.com"
|
2023-10-17 09:46:15 -04:00
|
|
|
internal_load_balancer = false
|
2023-06-19 07:02:01 -04:00
|
|
|
`
|
|
|
|
got := vars.String()
|
|
|
|
assert.Equal(t, want, got)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestQEMUClusterVariables(t *testing.T) {
|
2023-06-28 08:42:34 -04:00
|
|
|
vars := &QEMUVariables{
|
|
|
|
Name: "cluster-name",
|
|
|
|
NodeGroups: map[string]QEMUNodeGroup{
|
|
|
|
"control-plane": {
|
2023-06-30 04:53:00 -04:00
|
|
|
Role: role.ControlPlane.TFString(),
|
2023-08-02 04:41:26 -04:00
|
|
|
InitialCount: 1,
|
2023-06-30 04:53:00 -04:00
|
|
|
DiskSize: 30,
|
|
|
|
CPUCount: 4,
|
|
|
|
MemorySize: 8192,
|
2023-06-28 08:42:34 -04:00
|
|
|
},
|
2023-06-19 07:02:01 -04:00
|
|
|
},
|
2023-06-28 08:42:34 -04:00
|
|
|
Machine: "q35",
|
2023-06-19 07:02:01 -04:00
|
|
|
LibvirtURI: "qemu:///system",
|
|
|
|
LibvirtSocketPath: "/var/run/libvirt/libvirt-sock",
|
|
|
|
BootMode: "uefi",
|
|
|
|
ImagePath: "/var/lib/libvirt/images/cluster-name.qcow2",
|
|
|
|
ImageFormat: "raw",
|
|
|
|
MetadataAPIImage: "example.com/metadata-api:latest",
|
|
|
|
MetadataLibvirtURI: "qemu:///system",
|
|
|
|
NVRAM: "production",
|
2023-06-28 08:42:34 -04:00
|
|
|
InitrdPath: toPtr("/var/lib/libvirt/images/cluster-name-initrd"),
|
|
|
|
KernelCmdline: toPtr("console=ttyS0,115200n8"),
|
2023-07-21 10:43:51 -04:00
|
|
|
CustomEndpoint: "example.com",
|
2023-06-19 07:02:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// test that the variables are correctly rendered
|
|
|
|
want := `name = "cluster-name"
|
2023-06-28 08:42:34 -04:00
|
|
|
node_groups = {
|
|
|
|
control-plane = {
|
2023-06-30 04:53:00 -04:00
|
|
|
disk_size = 30
|
|
|
|
initial_count = 1
|
|
|
|
memory = 8192
|
|
|
|
role = "control-plane"
|
|
|
|
vcpus = 4
|
2023-06-28 08:42:34 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
machine = "q35"
|
|
|
|
libvirt_uri = "qemu:///system"
|
|
|
|
libvirt_socket_path = "/var/run/libvirt/libvirt-sock"
|
2023-06-19 07:02:01 -04:00
|
|
|
constellation_boot_mode = "uefi"
|
2023-06-28 08:42:34 -04:00
|
|
|
constellation_os_image = "/var/lib/libvirt/images/cluster-name.qcow2"
|
|
|
|
image_format = "raw"
|
|
|
|
metadata_api_image = "example.com/metadata-api:latest"
|
|
|
|
metadata_libvirt_uri = "qemu:///system"
|
2023-09-18 06:18:22 -04:00
|
|
|
nvram = "/usr/share/OVMF/OVMF_VARS.fd"
|
2023-06-28 08:42:34 -04:00
|
|
|
constellation_initrd = "/var/lib/libvirt/images/cluster-name-initrd"
|
|
|
|
constellation_cmdline = "console=ttyS0,115200n8"
|
2023-07-21 10:43:51 -04:00
|
|
|
custom_endpoint = "example.com"
|
2023-10-17 09:46:15 -04:00
|
|
|
internal_load_balancer = false
|
2023-06-19 07:02:01 -04:00
|
|
|
`
|
|
|
|
got := vars.String()
|
|
|
|
assert.Equal(t, want, got)
|
|
|
|
}
|
2023-08-23 04:35:42 -04:00
|
|
|
|
|
|
|
func TestVariablesFromBytes(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
awsVars := AWSIAMVariables{
|
|
|
|
Region: "test",
|
|
|
|
}
|
|
|
|
var loadedAWSVars AWSIAMVariables
|
|
|
|
err := VariablesFromBytes([]byte(awsVars.String()), &loadedAWSVars)
|
|
|
|
assert.NoError(err)
|
|
|
|
assert.Equal(awsVars, loadedAWSVars)
|
|
|
|
|
|
|
|
azureVars := AzureIAMVariables{
|
|
|
|
Region: "test",
|
|
|
|
}
|
|
|
|
var loadedAzureVars AzureIAMVariables
|
|
|
|
err = VariablesFromBytes([]byte(azureVars.String()), &loadedAzureVars)
|
|
|
|
assert.NoError(err)
|
|
|
|
assert.Equal(azureVars, loadedAzureVars)
|
|
|
|
|
|
|
|
gcpVars := GCPIAMVariables{
|
|
|
|
Region: "test",
|
|
|
|
}
|
|
|
|
var loadedGCPVars GCPIAMVariables
|
|
|
|
err = VariablesFromBytes([]byte(gcpVars.String()), &loadedGCPVars)
|
|
|
|
assert.NoError(err)
|
|
|
|
assert.Equal(gcpVars, loadedGCPVars)
|
|
|
|
|
|
|
|
err = VariablesFromBytes([]byte("invalid"), &loadedGCPVars)
|
|
|
|
assert.Error(err)
|
|
|
|
}
|