2022-03-28 06:24:41 -04:00
|
|
|
package azure
|
|
|
|
|
|
|
|
import (
|
2022-05-24 04:04:42 -04:00
|
|
|
"context"
|
|
|
|
"errors"
|
2022-03-28 06:24:41 -04:00
|
|
|
"testing"
|
|
|
|
|
2022-06-29 09:26:29 -04:00
|
|
|
"github.com/edgelesssys/constellation/bootstrapper/internal/kubernetes/k8sapi/resources"
|
2022-06-28 10:08:05 -04:00
|
|
|
"github.com/edgelesssys/constellation/internal/cloud/metadata"
|
2022-07-22 09:05:04 -04:00
|
|
|
"github.com/edgelesssys/constellation/internal/versions"
|
2022-03-28 06:24:41 -04:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
k8s "k8s.io/api/core/v1"
|
|
|
|
meta "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestSecrets(t *testing.T) {
|
2022-05-24 04:04:42 -04:00
|
|
|
someErr := errors.New("some error")
|
2022-03-28 06:24:41 -04:00
|
|
|
testCases := map[string]struct {
|
2022-06-28 10:08:05 -04:00
|
|
|
providerID string
|
2022-05-24 04:04:42 -04:00
|
|
|
metadata ccmMetadata
|
2022-03-28 06:24:41 -04:00
|
|
|
cloudServiceAccountURI string
|
2022-04-26 10:54:05 -04:00
|
|
|
wantSecrets resources.Secrets
|
|
|
|
wantErr bool
|
2022-03-28 06:24:41 -04:00
|
|
|
}{
|
2022-03-29 07:28:23 -04:00
|
|
|
"Secrets works for scale sets": {
|
2022-06-28 10:08:05 -04:00
|
|
|
providerID: "azure:///subscriptions/subscription-id/resourceGroups/resource-group/providers/Microsoft.Compute/virtualMachineScaleSets/scale-set-name/virtualMachines/instance-id",
|
2022-03-29 11:31:18 -04:00
|
|
|
cloudServiceAccountURI: "serviceaccount://azure?tenant_id=tenant-id&client_id=client-id&client_secret=client-secret&location=location",
|
2022-05-24 04:04:42 -04:00
|
|
|
metadata: &ccmMetadataStub{loadBalancerName: "load-balancer-name", networkSecurityGroupName: "network-security-group-name"},
|
2022-04-26 10:54:05 -04:00
|
|
|
wantSecrets: resources.Secrets{
|
2022-03-29 07:28:23 -04:00
|
|
|
&k8s.Secret{
|
|
|
|
TypeMeta: meta.TypeMeta{
|
|
|
|
Kind: "Secret",
|
|
|
|
APIVersion: "v1",
|
|
|
|
},
|
|
|
|
ObjectMeta: meta.ObjectMeta{
|
|
|
|
Name: "azureconfig",
|
|
|
|
Namespace: "kube-system",
|
|
|
|
},
|
|
|
|
Data: map[string][]byte{
|
2022-05-24 04:04:42 -04:00
|
|
|
"azure.json": []byte(`{"cloud":"AzurePublicCloud","tenantId":"tenant-id","subscriptionId":"subscription-id","resourceGroup":"resource-group","location":"location","securityGroupName":"network-security-group-name","loadBalancerName":"load-balancer-name","loadBalancerSku":"standard","useInstanceMetadata":true,"vmType":"vmss","aadClientId":"client-id","aadClientSecret":"client-secret"}`),
|
2022-03-28 06:24:41 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-05-24 04:04:42 -04:00
|
|
|
"cannot get load balancer Name": {
|
2022-06-28 10:08:05 -04:00
|
|
|
providerID: "azure:///subscriptions/subscription-id/resourceGroups/resource-group/providers/Microsoft.Compute/virtualMachineScaleSets/scale-set-name/virtualMachines/instance-id",
|
2022-05-24 04:04:42 -04:00
|
|
|
cloudServiceAccountURI: "serviceaccount://azure?tenant_id=tenant-id&client_id=client-id&client_secret=client-secret&location=location",
|
|
|
|
metadata: &ccmMetadataStub{getLoadBalancerNameErr: someErr},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"cannot get network security group name": {
|
2022-06-28 10:08:05 -04:00
|
|
|
providerID: "azure:///subscriptions/subscription-id/resourceGroups/resource-group/providers/Microsoft.Compute/virtualMachineScaleSets/scale-set-name/virtualMachines/instance-id",
|
2022-05-24 04:04:42 -04:00
|
|
|
cloudServiceAccountURI: "serviceaccount://azure?tenant_id=tenant-id&client_id=client-id&client_secret=client-secret&location=location",
|
|
|
|
metadata: &ccmMetadataStub{getNetworkSecurityGroupNameErr: someErr},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
2022-03-28 06:24:41 -04:00
|
|
|
"invalid providerID fails": {
|
2022-06-28 10:08:05 -04:00
|
|
|
providerID: "invalid",
|
|
|
|
metadata: &ccmMetadataStub{},
|
|
|
|
wantErr: true,
|
2022-03-28 06:24:41 -04:00
|
|
|
},
|
|
|
|
"invalid cloudServiceAccountURI fails": {
|
2022-06-28 10:08:05 -04:00
|
|
|
providerID: "azure:///subscriptions/subscription-id/resourceGroups/resource-group/providers/Microsoft.Compute/virtualMachines/instance-name",
|
2022-05-24 04:04:42 -04:00
|
|
|
metadata: &ccmMetadataStub{},
|
2022-03-28 06:24:41 -04:00
|
|
|
cloudServiceAccountURI: "invalid",
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr: true,
|
2022-03-28 06:24:41 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
require := require.New(t)
|
|
|
|
|
2022-05-24 04:04:42 -04:00
|
|
|
cloud := NewCloudControllerManager(tc.metadata)
|
2022-06-28 10:08:05 -04:00
|
|
|
secrets, err := cloud.Secrets(context.Background(), tc.providerID, tc.cloudServiceAccountURI)
|
2022-04-26 10:54:05 -04:00
|
|
|
if tc.wantErr {
|
2022-03-28 06:24:41 -04:00
|
|
|
assert.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
require.NoError(err)
|
2022-04-26 10:54:05 -04:00
|
|
|
assert.Equal(tc.wantSecrets, secrets)
|
2022-03-28 06:24:41 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTrivialCCMFunctions(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
cloud := CloudControllerManager{}
|
|
|
|
|
2022-07-22 09:05:04 -04:00
|
|
|
assert.NotEmpty(cloud.Image(versions.Latest))
|
2022-03-28 06:24:41 -04:00
|
|
|
assert.NotEmpty(cloud.Path())
|
|
|
|
assert.NotEmpty(cloud.Name())
|
|
|
|
assert.NotEmpty(cloud.ExtraArgs())
|
2022-06-28 10:08:05 -04:00
|
|
|
assert.Empty(cloud.ConfigMaps(metadata.InstanceMetadata{}))
|
2022-03-28 06:24:41 -04:00
|
|
|
assert.NotEmpty(cloud.Volumes())
|
|
|
|
assert.NotEmpty(cloud.VolumeMounts())
|
|
|
|
assert.Empty(cloud.Env())
|
|
|
|
assert.True(cloud.Supported())
|
|
|
|
}
|
2022-05-24 04:04:42 -04:00
|
|
|
|
|
|
|
type ccmMetadataStub struct {
|
|
|
|
networkSecurityGroupName string
|
|
|
|
loadBalancerName string
|
|
|
|
|
|
|
|
getNetworkSecurityGroupNameErr error
|
|
|
|
getLoadBalancerNameErr error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ccmMetadataStub) GetNetworkSecurityGroupName(ctx context.Context) (string, error) {
|
|
|
|
return c.networkSecurityGroupName, c.getNetworkSecurityGroupNameErr
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ccmMetadataStub) GetLoadBalancerName(ctx context.Context) (string, error) {
|
|
|
|
return c.loadBalancerName, c.getLoadBalancerNameErr
|
|
|
|
}
|