2022-03-22 11:03:15 -04:00
|
|
|
package gcp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"testing"
|
|
|
|
|
2022-06-28 10:08:05 -04:00
|
|
|
"github.com/edgelesssys/constellation/internal/cloud/metadata"
|
2022-03-22 11:03:15 -04:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestList(t *testing.T) {
|
|
|
|
err := errors.New("some err")
|
|
|
|
uid := "1234"
|
2022-06-28 10:08:05 -04:00
|
|
|
instancesGenerator := func() *[]metadata.InstanceMetadata {
|
|
|
|
return &[]metadata.InstanceMetadata{
|
2022-03-22 11:03:15 -04:00
|
|
|
{
|
|
|
|
Name: "someInstance",
|
|
|
|
ProviderID: "gce://someProject/someZone/someInstance",
|
2022-08-04 05:08:20 -04:00
|
|
|
VPCIP: "192.0.2.0",
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
testCases := map[string]struct {
|
|
|
|
client stubGCPClient
|
2022-06-28 10:08:05 -04:00
|
|
|
instancesGenerator func() *[]metadata.InstanceMetadata
|
|
|
|
instancesMutator func(*[]metadata.InstanceMetadata)
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr bool
|
2022-06-28 10:08:05 -04:00
|
|
|
wantInstances []metadata.InstanceMetadata
|
2022-03-22 11:03:15 -04:00
|
|
|
}{
|
|
|
|
"retrieve works": {
|
|
|
|
client: stubGCPClient{
|
|
|
|
projectID: "someProjectID",
|
|
|
|
zone: "someZone",
|
|
|
|
retrieveInstanceMetadaValues: map[string]string{
|
|
|
|
"constellation-uid": uid,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
instancesGenerator: instancesGenerator,
|
2022-06-28 10:08:05 -04:00
|
|
|
wantInstances: []metadata.InstanceMetadata{
|
2022-03-22 11:03:15 -04:00
|
|
|
{
|
|
|
|
Name: "someInstance",
|
|
|
|
ProviderID: "gce://someProject/someZone/someInstance",
|
2022-08-04 05:08:20 -04:00
|
|
|
VPCIP: "192.0.2.0",
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"retrieve error is detected": {
|
|
|
|
client: stubGCPClient{
|
|
|
|
projectID: "someProjectID",
|
|
|
|
zone: "someZone",
|
|
|
|
retrieveInstanceMetadaValues: map[string]string{
|
|
|
|
"constellation-uid": uid,
|
|
|
|
},
|
|
|
|
retrieveInstancesErr: err,
|
|
|
|
},
|
|
|
|
instancesGenerator: instancesGenerator,
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"project metadata retrieval error is detected": {
|
|
|
|
client: stubGCPClient{
|
|
|
|
retrieveProjectIDErr: err,
|
|
|
|
},
|
|
|
|
instancesGenerator: instancesGenerator,
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"zone retrieval error is detected": {
|
|
|
|
client: stubGCPClient{
|
|
|
|
retrieveZoneErr: err,
|
|
|
|
},
|
|
|
|
instancesGenerator: instancesGenerator,
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
tc.client.retrieveInstancesValues = *tc.instancesGenerator()
|
|
|
|
if tc.instancesMutator != nil {
|
|
|
|
tc.instancesMutator(&tc.client.retrieveInstancesValues)
|
|
|
|
}
|
|
|
|
metadata := New(&tc.client)
|
|
|
|
instances, err := metadata.List(context.Background())
|
|
|
|
|
2022-04-26 10:54:05 -04:00
|
|
|
if tc.wantErr {
|
2022-03-22 11:03:15 -04:00
|
|
|
assert.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
require.NoError(err)
|
2022-04-26 10:54:05 -04:00
|
|
|
assert.ElementsMatch(tc.wantInstances, instances)
|
2022-03-22 11:03:15 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSelf(t *testing.T) {
|
|
|
|
err := errors.New("some err")
|
|
|
|
uid := "1234"
|
|
|
|
|
|
|
|
testCases := map[string]struct {
|
2022-04-26 10:54:05 -04:00
|
|
|
client stubGCPClient
|
|
|
|
wantErr bool
|
2022-06-28 10:08:05 -04:00
|
|
|
wantInstance metadata.InstanceMetadata
|
2022-03-22 11:03:15 -04:00
|
|
|
}{
|
|
|
|
"retrieve works": {
|
|
|
|
client: stubGCPClient{
|
|
|
|
projectID: "someProjectID",
|
|
|
|
zone: "someZone",
|
2022-06-28 10:08:05 -04:00
|
|
|
retrieveInstanceValue: metadata.InstanceMetadata{
|
2022-03-22 11:03:15 -04:00
|
|
|
Name: "someInstance",
|
|
|
|
ProviderID: "gce://someProject/someZone/someInstance",
|
2022-08-04 05:08:20 -04:00
|
|
|
VPCIP: "192.0.2.0",
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
},
|
2022-06-28 10:08:05 -04:00
|
|
|
wantInstance: metadata.InstanceMetadata{
|
2022-03-22 11:03:15 -04:00
|
|
|
Name: "someInstance",
|
|
|
|
ProviderID: "gce://someProject/someZone/someInstance",
|
2022-08-04 05:08:20 -04:00
|
|
|
VPCIP: "192.0.2.0",
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
"retrieve error is detected": {
|
|
|
|
client: stubGCPClient{
|
|
|
|
projectID: "someProjectID",
|
|
|
|
zone: "someZone",
|
|
|
|
retrieveInstanceMetadaValues: map[string]string{
|
|
|
|
"constellation-uid": uid,
|
|
|
|
},
|
|
|
|
retrieveInstanceErr: err,
|
|
|
|
},
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"project id retrieval error is detected": {
|
|
|
|
client: stubGCPClient{
|
|
|
|
retrieveProjectIDErr: err,
|
|
|
|
},
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"zone retrieval error is detected": {
|
|
|
|
client: stubGCPClient{
|
|
|
|
retrieveZoneErr: err,
|
|
|
|
},
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"instance name retrieval error is detected": {
|
|
|
|
client: stubGCPClient{
|
|
|
|
retrieveInstanceNameErr: err,
|
|
|
|
},
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
cloud := New(&tc.client)
|
|
|
|
instance, err := cloud.Self(context.Background())
|
|
|
|
|
2022-04-26 10:54:05 -04:00
|
|
|
if tc.wantErr {
|
2022-03-22 11:03:15 -04:00
|
|
|
assert.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
require.NoError(err)
|
2022-04-26 10:54:05 -04:00
|
|
|
assert.Equal(tc.wantInstance, instance)
|
2022-03-22 11:03:15 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetInstance(t *testing.T) {
|
|
|
|
err := errors.New("some err")
|
|
|
|
|
|
|
|
testCases := map[string]struct {
|
2022-04-26 10:54:05 -04:00
|
|
|
providerID string
|
|
|
|
client stubGCPClient
|
|
|
|
wantErr bool
|
2022-06-28 10:08:05 -04:00
|
|
|
wantInstance metadata.InstanceMetadata
|
2022-03-22 11:03:15 -04:00
|
|
|
}{
|
|
|
|
"retrieve works": {
|
|
|
|
providerID: "gce://someProject/someZone/someInstance",
|
|
|
|
client: stubGCPClient{
|
2022-06-28 10:08:05 -04:00
|
|
|
retrieveInstanceValue: metadata.InstanceMetadata{
|
2022-03-22 11:03:15 -04:00
|
|
|
Name: "someInstance",
|
|
|
|
ProviderID: "gce://someProject/someZone/someInstance",
|
2022-08-04 05:08:20 -04:00
|
|
|
VPCIP: "192.0.2.0",
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
},
|
2022-06-28 10:08:05 -04:00
|
|
|
wantInstance: metadata.InstanceMetadata{
|
2022-03-22 11:03:15 -04:00
|
|
|
Name: "someInstance",
|
|
|
|
ProviderID: "gce://someProject/someZone/someInstance",
|
2022-08-04 05:08:20 -04:00
|
|
|
VPCIP: "192.0.2.0",
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
"retrieve error is detected": {
|
|
|
|
providerID: "gce://someProject/someZone/someInstance",
|
|
|
|
client: stubGCPClient{
|
|
|
|
retrieveInstanceErr: err,
|
|
|
|
},
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"malformed providerID with too many fields is detected": {
|
|
|
|
providerID: "gce://someProject/someZone/someInstance/tooMany/fields",
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"malformed providerID with too few fields is detected": {
|
|
|
|
providerID: "gce://someProject",
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
cloud := New(&tc.client)
|
|
|
|
instance, err := cloud.GetInstance(context.Background(), tc.providerID)
|
|
|
|
|
2022-04-26 10:54:05 -04:00
|
|
|
if tc.wantErr {
|
2022-03-22 11:03:15 -04:00
|
|
|
assert.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
require.NoError(err)
|
2022-04-26 10:54:05 -04:00
|
|
|
assert.Equal(tc.wantInstance, instance)
|
2022-03-22 11:03:15 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type stubGCPClient struct {
|
2022-07-29 10:30:24 -04:00
|
|
|
retrieveUIDValue string
|
|
|
|
retrieveUIDErr error
|
2022-06-28 10:08:05 -04:00
|
|
|
retrieveInstanceValue metadata.InstanceMetadata
|
2022-03-22 11:03:15 -04:00
|
|
|
retrieveInstanceErr error
|
2022-06-28 10:08:05 -04:00
|
|
|
retrieveInstancesValues []metadata.InstanceMetadata
|
2022-03-22 11:03:15 -04:00
|
|
|
retrieveInstancesErr error
|
|
|
|
retrieveInstanceMetadaValues map[string]string
|
|
|
|
retrieveInstanceMetadataErr error
|
2022-05-24 04:04:42 -04:00
|
|
|
retrieveSubentworkAliasErr error
|
2022-03-22 11:03:15 -04:00
|
|
|
projectID string
|
|
|
|
zone string
|
|
|
|
instanceName string
|
2022-06-09 16:26:36 -04:00
|
|
|
loadBalancerIP string
|
2022-03-22 11:03:15 -04:00
|
|
|
retrieveProjectIDErr error
|
|
|
|
retrieveZoneErr error
|
|
|
|
retrieveInstanceNameErr error
|
|
|
|
setInstanceMetadataErr error
|
|
|
|
unsetInstanceMetadataErr error
|
2022-06-09 16:26:36 -04:00
|
|
|
retrieveLoadBalancerErr error
|
2022-03-22 11:03:15 -04:00
|
|
|
|
|
|
|
instanceMetadataProjects []string
|
|
|
|
instanceMetadataZones []string
|
|
|
|
instanceMetadataInstanceNames []string
|
|
|
|
instanceMetadataKeys []string
|
|
|
|
instanceMetadataValues []string
|
|
|
|
|
|
|
|
unsetMetadataProjects []string
|
|
|
|
unsetMetadataZones []string
|
|
|
|
unsetMetadataInstanceNames []string
|
|
|
|
unsetMetadataKeys []string
|
|
|
|
}
|
|
|
|
|
2022-06-28 10:08:05 -04:00
|
|
|
func (s *stubGCPClient) RetrieveInstances(ctx context.Context, project, zone string) ([]metadata.InstanceMetadata, error) {
|
2022-03-22 11:03:15 -04:00
|
|
|
return s.retrieveInstancesValues, s.retrieveInstancesErr
|
|
|
|
}
|
|
|
|
|
2022-06-28 10:08:05 -04:00
|
|
|
func (s *stubGCPClient) RetrieveInstance(ctx context.Context, project, zone string, instanceName string) (metadata.InstanceMetadata, error) {
|
2022-03-22 11:03:15 -04:00
|
|
|
return s.retrieveInstanceValue, s.retrieveInstanceErr
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stubGCPClient) RetrieveInstanceMetadata(attr string) (string, error) {
|
|
|
|
return s.retrieveInstanceMetadaValues[attr], s.retrieveInstanceMetadataErr
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stubGCPClient) RetrieveProjectID() (string, error) {
|
|
|
|
return s.projectID, s.retrieveProjectIDErr
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stubGCPClient) RetrieveZone() (string, error) {
|
|
|
|
return s.zone, s.retrieveZoneErr
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stubGCPClient) RetrieveInstanceName() (string, error) {
|
|
|
|
return s.instanceName, s.retrieveInstanceNameErr
|
|
|
|
}
|
|
|
|
|
2022-06-09 16:26:36 -04:00
|
|
|
func (s *stubGCPClient) RetrieveLoadBalancerIP(ctx context.Context, project, zone string) (string, error) {
|
|
|
|
return s.loadBalancerIP, s.retrieveLoadBalancerErr
|
|
|
|
}
|
|
|
|
|
2022-07-29 10:30:24 -04:00
|
|
|
func (s *stubGCPClient) UID() (string, error) {
|
|
|
|
return s.retrieveUIDValue, s.retrieveUIDErr
|
|
|
|
}
|
|
|
|
|
2022-03-22 11:03:15 -04:00
|
|
|
func (s *stubGCPClient) SetInstanceMetadata(ctx context.Context, project, zone, instanceName, key, value string) error {
|
|
|
|
s.instanceMetadataProjects = append(s.instanceMetadataProjects, project)
|
|
|
|
s.instanceMetadataZones = append(s.instanceMetadataZones, zone)
|
|
|
|
s.instanceMetadataInstanceNames = append(s.instanceMetadataInstanceNames, instanceName)
|
|
|
|
s.instanceMetadataKeys = append(s.instanceMetadataKeys, key)
|
|
|
|
s.instanceMetadataValues = append(s.instanceMetadataValues, value)
|
|
|
|
|
|
|
|
return s.setInstanceMetadataErr
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stubGCPClient) UnsetInstanceMetadata(ctx context.Context, project, zone, instanceName, key string) error {
|
|
|
|
s.unsetMetadataProjects = append(s.unsetMetadataProjects, project)
|
|
|
|
s.unsetMetadataZones = append(s.unsetMetadataZones, zone)
|
|
|
|
s.unsetMetadataInstanceNames = append(s.unsetMetadataInstanceNames, instanceName)
|
|
|
|
s.unsetMetadataKeys = append(s.unsetMetadataKeys, key)
|
|
|
|
|
|
|
|
return s.unsetInstanceMetadataErr
|
|
|
|
}
|
2022-05-24 04:04:42 -04:00
|
|
|
|
|
|
|
func (s *stubGCPClient) RetrieveSubnetworkAliasCIDR(ctx context.Context, project, zone, instanceName string) (string, error) {
|
|
|
|
return "", s.retrieveSubentworkAliasErr
|
|
|
|
}
|