2022-09-05 03:06:08 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-03-22 11:03:15 -04:00
|
|
|
package gcp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
compute "cloud.google.com/go/compute/apiv1"
|
2022-06-28 10:08:05 -04:00
|
|
|
"github.com/edgelesssys/constellation/internal/cloud/metadata"
|
2022-08-26 05:42:40 -04:00
|
|
|
"github.com/edgelesssys/constellation/internal/role"
|
2022-03-22 11:03:15 -04:00
|
|
|
gax "github.com/googleapis/gax-go/v2"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"go.uber.org/goleak"
|
|
|
|
"google.golang.org/api/iterator"
|
|
|
|
computepb "google.golang.org/genproto/googleapis/cloud/compute/v1"
|
|
|
|
"google.golang.org/protobuf/proto"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestMain(m *testing.M) {
|
|
|
|
goleak.VerifyTestMain(m,
|
|
|
|
// https://github.com/census-instrumentation/opencensus-go/issues/1262
|
|
|
|
goleak.IgnoreTopFunction("go.opencensus.io/stats/view.(*worker).start"),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRetrieveInstances(t *testing.T) {
|
|
|
|
uid := "1234"
|
|
|
|
someErr := errors.New("failed")
|
|
|
|
newTestIter := func() *stubInstanceIterator {
|
|
|
|
return &stubInstanceIterator{
|
|
|
|
instances: []*computepb.Instance{
|
|
|
|
{
|
|
|
|
Name: proto.String("someInstance"),
|
|
|
|
Metadata: &computepb.Metadata{
|
|
|
|
Items: []*computepb.Items{
|
|
|
|
{
|
|
|
|
Key: proto.String("ssh-keys"),
|
|
|
|
Value: proto.String("bob:ssh-rsa bobskey"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Key: proto.String("key-2"),
|
|
|
|
Value: proto.String("value-2"),
|
|
|
|
},
|
|
|
|
{
|
2022-06-28 10:08:05 -04:00
|
|
|
Key: proto.String(constellationUIDMetadataKey),
|
2022-03-22 11:03:15 -04:00
|
|
|
Value: proto.String(uid),
|
|
|
|
},
|
|
|
|
{
|
2022-06-28 10:08:05 -04:00
|
|
|
Key: proto.String(roleMetadataKey),
|
2022-06-29 09:26:29 -04:00
|
|
|
Value: proto.String(role.ControlPlane.String()),
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
NetworkInterfaces: []*computepb.NetworkInterface{
|
2022-05-24 04:04:42 -04:00
|
|
|
{
|
2022-08-04 05:08:20 -04:00
|
|
|
Name: proto.String("nic0"),
|
2022-05-24 04:04:42 -04:00
|
|
|
NetworkIP: proto.String("192.0.2.0"),
|
|
|
|
AliasIpRanges: []*computepb.AliasIpRange{{IpCidrRange: proto.String("192.0.2.0/16")}},
|
|
|
|
AccessConfigs: []*computepb.AccessConfig{{NatIP: proto.String("192.0.2.1")}},
|
|
|
|
},
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
testCases := map[string]struct {
|
|
|
|
client stubInstancesClient
|
|
|
|
metadata stubMetadataClient
|
|
|
|
instanceIter *stubInstanceIterator
|
|
|
|
instanceIterMutator func(*stubInstanceIterator)
|
2022-06-28 10:08:05 -04:00
|
|
|
wantInstances []metadata.InstanceMetadata
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr bool
|
2022-03-22 11:03:15 -04:00
|
|
|
}{
|
|
|
|
"retrieve works": {
|
|
|
|
client: stubInstancesClient{},
|
|
|
|
metadata: stubMetadataClient{InstanceValue: uid},
|
|
|
|
instanceIter: newTestIter(),
|
2022-06-28 10:08:05 -04:00
|
|
|
wantInstances: []metadata.InstanceMetadata{
|
2022-03-22 11:03:15 -04:00
|
|
|
{
|
2022-05-24 04:04:42 -04:00
|
|
|
Name: "someInstance",
|
|
|
|
ProviderID: "gce://someProject/someZone/someInstance",
|
2022-06-29 09:26:29 -04:00
|
|
|
Role: role.ControlPlane,
|
2022-05-24 04:04:42 -04:00
|
|
|
AliasIPRanges: []string{"192.0.2.0/16"},
|
2022-08-04 05:08:20 -04:00
|
|
|
PublicIP: "192.0.2.1",
|
|
|
|
VPCIP: "192.0.2.0",
|
2022-05-24 04:04:42 -04:00
|
|
|
SSHKeys: map[string][]string{"bob": {"ssh-rsa bobskey"}},
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"instance name is null": {
|
|
|
|
client: stubInstancesClient{},
|
|
|
|
metadata: stubMetadataClient{InstanceValue: uid},
|
|
|
|
instanceIter: newTestIter(),
|
|
|
|
instanceIterMutator: func(sii *stubInstanceIterator) { sii.instances[0].Name = nil },
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"no instance with network ip": {
|
|
|
|
client: stubInstancesClient{},
|
|
|
|
metadata: stubMetadataClient{InstanceValue: uid},
|
|
|
|
instanceIter: newTestIter(),
|
|
|
|
instanceIterMutator: func(sii *stubInstanceIterator) { sii.instances[0].NetworkInterfaces = nil },
|
2022-06-28 10:08:05 -04:00
|
|
|
wantInstances: []metadata.InstanceMetadata{
|
2022-03-22 11:03:15 -04:00
|
|
|
{
|
2022-05-24 04:04:42 -04:00
|
|
|
Name: "someInstance",
|
|
|
|
ProviderID: "gce://someProject/someZone/someInstance",
|
2022-06-29 09:26:29 -04:00
|
|
|
Role: role.ControlPlane,
|
2022-05-24 04:04:42 -04:00
|
|
|
AliasIPRanges: []string{},
|
2022-08-04 05:08:20 -04:00
|
|
|
PublicIP: "",
|
|
|
|
VPCIP: "",
|
2022-05-24 04:04:42 -04:00
|
|
|
SSHKeys: map[string][]string{"bob": {"ssh-rsa bobskey"}},
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"network ip is nil": {
|
|
|
|
client: stubInstancesClient{},
|
|
|
|
metadata: stubMetadataClient{InstanceValue: uid},
|
|
|
|
instanceIter: newTestIter(),
|
|
|
|
instanceIterMutator: func(sii *stubInstanceIterator) { sii.instances[0].NetworkInterfaces[0].NetworkIP = nil },
|
2022-06-28 10:08:05 -04:00
|
|
|
wantInstances: []metadata.InstanceMetadata{
|
2022-03-22 11:03:15 -04:00
|
|
|
{
|
2022-05-24 04:04:42 -04:00
|
|
|
Name: "someInstance",
|
|
|
|
ProviderID: "gce://someProject/someZone/someInstance",
|
2022-06-29 09:26:29 -04:00
|
|
|
Role: role.ControlPlane,
|
2022-05-24 04:04:42 -04:00
|
|
|
AliasIPRanges: []string{"192.0.2.0/16"},
|
2022-08-04 05:08:20 -04:00
|
|
|
PublicIP: "192.0.2.1",
|
|
|
|
VPCIP: "",
|
2022-05-24 04:04:42 -04:00
|
|
|
SSHKeys: map[string][]string{"bob": {"ssh-rsa bobskey"}},
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"constellation id is not set": {
|
|
|
|
client: stubInstancesClient{},
|
|
|
|
metadata: stubMetadataClient{InstanceValue: uid},
|
|
|
|
instanceIter: newTestIter(),
|
|
|
|
instanceIterMutator: func(sii *stubInstanceIterator) { sii.instances[0].Metadata.Items[2].Key = proto.String("") },
|
2022-06-28 10:08:05 -04:00
|
|
|
wantInstances: []metadata.InstanceMetadata{},
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"constellation retrieval fails": {
|
|
|
|
client: stubInstancesClient{},
|
|
|
|
metadata: stubMetadataClient{InstanceErr: someErr},
|
|
|
|
instanceIter: newTestIter(),
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"role is not set": {
|
|
|
|
client: stubInstancesClient{},
|
|
|
|
metadata: stubMetadataClient{InstanceValue: uid},
|
|
|
|
instanceIter: newTestIter(),
|
|
|
|
instanceIterMutator: func(sii *stubInstanceIterator) { sii.instances[0].Metadata.Items[3].Key = proto.String("") },
|
2022-06-28 10:08:05 -04:00
|
|
|
wantInstances: []metadata.InstanceMetadata{
|
2022-03-22 11:03:15 -04:00
|
|
|
{
|
2022-05-24 04:04:42 -04:00
|
|
|
Name: "someInstance",
|
|
|
|
ProviderID: "gce://someProject/someZone/someInstance",
|
|
|
|
Role: role.Unknown,
|
|
|
|
AliasIPRanges: []string{"192.0.2.0/16"},
|
2022-08-04 05:08:20 -04:00
|
|
|
PublicIP: "192.0.2.1",
|
|
|
|
VPCIP: "192.0.2.0",
|
2022-05-24 04:04:42 -04:00
|
|
|
SSHKeys: map[string][]string{"bob": {"ssh-rsa bobskey"}},
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"instance iterator Next() errors": {
|
|
|
|
client: stubInstancesClient{},
|
|
|
|
metadata: stubMetadataClient{InstanceValue: uid},
|
|
|
|
instanceIter: &stubInstanceIterator{nextErr: someErr},
|
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)
|
|
|
|
|
|
|
|
if tc.instanceIterMutator != nil {
|
|
|
|
tc.instanceIterMutator(tc.instanceIter)
|
|
|
|
}
|
|
|
|
tc.client.ListInstanceIterator = tc.instanceIter
|
|
|
|
client := Client{
|
|
|
|
instanceAPI: tc.client,
|
|
|
|
metadataAPI: tc.metadata,
|
|
|
|
}
|
|
|
|
|
|
|
|
instances, err := client.RetrieveInstances(context.Background(), "someProject", "someZone")
|
|
|
|
|
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.wantInstances, instances)
|
2022-03-22 11:03:15 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRetrieveInstance(t *testing.T) {
|
|
|
|
newTestInstance := func() *computepb.Instance {
|
|
|
|
return &computepb.Instance{
|
|
|
|
Name: proto.String("someInstance"),
|
|
|
|
Metadata: &computepb.Metadata{
|
|
|
|
Items: []*computepb.Items{
|
|
|
|
{
|
|
|
|
Key: proto.String("key-1"),
|
|
|
|
Value: proto.String("value-1"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Key: proto.String("key-2"),
|
|
|
|
Value: proto.String("value-2"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
NetworkInterfaces: []*computepb.NetworkInterface{
|
2022-05-24 04:04:42 -04:00
|
|
|
{
|
2022-08-04 05:08:20 -04:00
|
|
|
Name: proto.String("nic0"),
|
2022-05-24 04:04:42 -04:00
|
|
|
NetworkIP: proto.String("192.0.2.0"),
|
|
|
|
AliasIpRanges: []*computepb.AliasIpRange{{IpCidrRange: proto.String("192.0.2.0/16")}},
|
|
|
|
AccessConfigs: []*computepb.AccessConfig{{NatIP: proto.String("192.0.2.1")}},
|
|
|
|
},
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
testCases := map[string]struct {
|
|
|
|
client stubInstancesClient
|
|
|
|
clientInstance *computepb.Instance
|
|
|
|
clientInstanceMutator func(*computepb.Instance)
|
2022-06-28 10:08:05 -04:00
|
|
|
wantInstance metadata.InstanceMetadata
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr bool
|
2022-03-22 11:03:15 -04:00
|
|
|
}{
|
|
|
|
"retrieve works": {
|
|
|
|
client: stubInstancesClient{},
|
|
|
|
clientInstance: newTestInstance(),
|
2022-06-28 10:08:05 -04:00
|
|
|
wantInstance: metadata.InstanceMetadata{
|
2022-05-24 04:04:42 -04:00
|
|
|
Name: "someInstance",
|
|
|
|
ProviderID: "gce://someProject/someZone/someInstance",
|
|
|
|
AliasIPRanges: []string{"192.0.2.0/16"},
|
2022-08-04 05:08:20 -04:00
|
|
|
PublicIP: "192.0.2.1",
|
|
|
|
VPCIP: "192.0.2.0",
|
2022-05-24 04:04:42 -04:00
|
|
|
SSHKeys: map[string][]string{},
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
"retrieve with SSH key works": {
|
|
|
|
client: stubInstancesClient{},
|
|
|
|
clientInstance: newTestInstance(),
|
|
|
|
clientInstanceMutator: func(i *computepb.Instance) {
|
|
|
|
i.Metadata.Items[0].Key = proto.String("ssh-keys")
|
|
|
|
i.Metadata.Items[0].Value = proto.String("bob:ssh-rsa bobskey")
|
|
|
|
},
|
2022-06-28 10:08:05 -04:00
|
|
|
wantInstance: metadata.InstanceMetadata{
|
2022-05-24 04:04:42 -04:00
|
|
|
Name: "someInstance",
|
|
|
|
ProviderID: "gce://someProject/someZone/someInstance",
|
|
|
|
AliasIPRanges: []string{"192.0.2.0/16"},
|
2022-08-04 05:08:20 -04:00
|
|
|
PublicIP: "192.0.2.1",
|
|
|
|
VPCIP: "192.0.2.0",
|
2022-05-24 04:04:42 -04:00
|
|
|
SSHKeys: map[string][]string{"bob": {"ssh-rsa bobskey"}},
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
"retrieve with Role works": {
|
|
|
|
client: stubInstancesClient{},
|
|
|
|
clientInstance: newTestInstance(),
|
|
|
|
clientInstanceMutator: func(i *computepb.Instance) {
|
2022-06-28 10:08:05 -04:00
|
|
|
i.Metadata.Items[0].Key = proto.String(roleMetadataKey)
|
2022-06-29 09:26:29 -04:00
|
|
|
i.Metadata.Items[0].Value = proto.String(role.ControlPlane.String())
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
2022-06-28 10:08:05 -04:00
|
|
|
wantInstance: metadata.InstanceMetadata{
|
2022-05-24 04:04:42 -04:00
|
|
|
Name: "someInstance",
|
|
|
|
ProviderID: "gce://someProject/someZone/someInstance",
|
|
|
|
AliasIPRanges: []string{"192.0.2.0/16"},
|
2022-08-04 05:08:20 -04:00
|
|
|
PublicIP: "192.0.2.1",
|
2022-06-29 09:26:29 -04:00
|
|
|
Role: role.ControlPlane,
|
2022-08-04 05:08:20 -04:00
|
|
|
VPCIP: "192.0.2.0",
|
2022-05-24 04:04:42 -04:00
|
|
|
SSHKeys: map[string][]string{},
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
"retrieve fails": {
|
|
|
|
client: stubInstancesClient{
|
|
|
|
GetErr: errors.New("retrieve error"),
|
|
|
|
},
|
|
|
|
clientInstance: nil,
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"metadata item is null": {
|
|
|
|
client: stubInstancesClient{},
|
|
|
|
clientInstance: newTestInstance(),
|
|
|
|
clientInstanceMutator: func(i *computepb.Instance) { i.Metadata.Items[0] = nil },
|
2022-06-28 10:08:05 -04:00
|
|
|
wantInstance: metadata.InstanceMetadata{
|
2022-05-24 04:04:42 -04:00
|
|
|
Name: "someInstance",
|
|
|
|
ProviderID: "gce://someProject/someZone/someInstance",
|
|
|
|
AliasIPRanges: []string{"192.0.2.0/16"},
|
2022-08-04 05:08:20 -04:00
|
|
|
PublicIP: "192.0.2.1",
|
|
|
|
VPCIP: "192.0.2.0",
|
2022-05-24 04:04:42 -04:00
|
|
|
SSHKeys: map[string][]string{},
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
"metadata key is null": {
|
|
|
|
client: stubInstancesClient{},
|
|
|
|
clientInstance: newTestInstance(),
|
|
|
|
clientInstanceMutator: func(i *computepb.Instance) { i.Metadata.Items[0].Key = nil },
|
2022-06-28 10:08:05 -04:00
|
|
|
wantInstance: metadata.InstanceMetadata{
|
2022-05-24 04:04:42 -04:00
|
|
|
Name: "someInstance",
|
|
|
|
ProviderID: "gce://someProject/someZone/someInstance",
|
|
|
|
AliasIPRanges: []string{"192.0.2.0/16"},
|
2022-08-04 05:08:20 -04:00
|
|
|
PublicIP: "192.0.2.1",
|
|
|
|
VPCIP: "192.0.2.0",
|
2022-05-24 04:04:42 -04:00
|
|
|
SSHKeys: map[string][]string{},
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
"metadata value is null": {
|
|
|
|
client: stubInstancesClient{},
|
|
|
|
clientInstance: newTestInstance(),
|
|
|
|
clientInstanceMutator: func(i *computepb.Instance) { i.Metadata.Items[0].Value = nil },
|
2022-06-28 10:08:05 -04:00
|
|
|
wantInstance: metadata.InstanceMetadata{
|
2022-05-24 04:04:42 -04:00
|
|
|
Name: "someInstance",
|
|
|
|
ProviderID: "gce://someProject/someZone/someInstance",
|
|
|
|
AliasIPRanges: []string{"192.0.2.0/16"},
|
2022-08-04 05:08:20 -04:00
|
|
|
PublicIP: "192.0.2.1",
|
|
|
|
VPCIP: "192.0.2.0",
|
2022-05-24 04:04:42 -04:00
|
|
|
SSHKeys: map[string][]string{},
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
"instance without network ip": {
|
|
|
|
client: stubInstancesClient{},
|
|
|
|
clientInstance: newTestInstance(),
|
|
|
|
clientInstanceMutator: func(i *computepb.Instance) { i.NetworkInterfaces[0] = nil },
|
2022-06-28 10:08:05 -04:00
|
|
|
wantInstance: metadata.InstanceMetadata{
|
2022-05-24 04:04:42 -04:00
|
|
|
Name: "someInstance",
|
|
|
|
ProviderID: "gce://someProject/someZone/someInstance",
|
|
|
|
AliasIPRanges: []string{},
|
2022-08-04 05:08:20 -04:00
|
|
|
PublicIP: "",
|
|
|
|
VPCIP: "",
|
2022-05-24 04:04:42 -04:00
|
|
|
SSHKeys: map[string][]string{},
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
"network ip is nil": {
|
|
|
|
client: stubInstancesClient{},
|
|
|
|
clientInstance: newTestInstance(),
|
|
|
|
clientInstanceMutator: func(i *computepb.Instance) { i.NetworkInterfaces[0].NetworkIP = nil },
|
2022-06-28 10:08:05 -04:00
|
|
|
wantInstance: metadata.InstanceMetadata{
|
2022-05-24 04:04:42 -04:00
|
|
|
Name: "someInstance",
|
|
|
|
ProviderID: "gce://someProject/someZone/someInstance",
|
|
|
|
AliasIPRanges: []string{"192.0.2.0/16"},
|
2022-08-04 05:08:20 -04:00
|
|
|
PublicIP: "192.0.2.1",
|
|
|
|
VPCIP: "",
|
2022-05-24 04:04:42 -04:00
|
|
|
SSHKeys: map[string][]string{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"network alias cidr is nil": {
|
|
|
|
client: stubInstancesClient{},
|
|
|
|
clientInstance: newTestInstance(),
|
|
|
|
clientInstanceMutator: func(i *computepb.Instance) { i.NetworkInterfaces[0].AliasIpRanges[0].IpCidrRange = nil },
|
2022-06-28 10:08:05 -04:00
|
|
|
wantInstance: metadata.InstanceMetadata{
|
2022-05-24 04:04:42 -04:00
|
|
|
Name: "someInstance",
|
|
|
|
ProviderID: "gce://someProject/someZone/someInstance",
|
|
|
|
AliasIPRanges: []string{},
|
2022-08-04 05:08:20 -04:00
|
|
|
PublicIP: "192.0.2.1",
|
|
|
|
VPCIP: "192.0.2.0",
|
2022-05-24 04:04:42 -04:00
|
|
|
SSHKeys: map[string][]string{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"network public ip is nil": {
|
|
|
|
client: stubInstancesClient{},
|
|
|
|
clientInstance: newTestInstance(),
|
|
|
|
clientInstanceMutator: func(i *computepb.Instance) { i.NetworkInterfaces[0].AccessConfigs[0].NatIP = nil },
|
2022-06-28 10:08:05 -04:00
|
|
|
wantInstance: metadata.InstanceMetadata{
|
2022-05-24 04:04:42 -04:00
|
|
|
Name: "someInstance",
|
|
|
|
ProviderID: "gce://someProject/someZone/someInstance",
|
|
|
|
AliasIPRanges: []string{"192.0.2.0/16"},
|
2022-08-04 05:08:20 -04:00
|
|
|
PublicIP: "",
|
|
|
|
VPCIP: "192.0.2.0",
|
2022-05-24 04:04:42 -04:00
|
|
|
SSHKeys: map[string][]string{},
|
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)
|
|
|
|
|
|
|
|
if tc.clientInstanceMutator != nil {
|
|
|
|
tc.clientInstanceMutator(tc.clientInstance)
|
|
|
|
}
|
|
|
|
tc.client.GetInstance = tc.clientInstance
|
|
|
|
client := Client{instanceAPI: tc.client}
|
|
|
|
|
|
|
|
instance, err := client.RetrieveInstance(context.Background(), "someProject", "someZone", "someInstance")
|
|
|
|
|
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 TestRetrieveProjectID(t *testing.T) {
|
|
|
|
someErr := errors.New("failed")
|
|
|
|
|
|
|
|
testCases := map[string]struct {
|
2022-04-26 10:54:05 -04:00
|
|
|
client stubMetadataClient
|
|
|
|
wantValue string
|
|
|
|
wantErr bool
|
2022-03-22 11:03:15 -04:00
|
|
|
}{
|
|
|
|
"retrieve works": {
|
2022-04-26 10:54:05 -04:00
|
|
|
client: stubMetadataClient{ProjectIDValue: "someProjectID"},
|
|
|
|
wantValue: "someProjectID",
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"retrieve fails": {
|
2022-04-26 10:54:05 -04:00
|
|
|
client: stubMetadataClient{ProjectIDErr: someErr},
|
|
|
|
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)
|
|
|
|
|
|
|
|
client := Client{metadataAPI: tc.client}
|
|
|
|
value, err := client.RetrieveProjectID()
|
|
|
|
|
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.wantValue, value)
|
2022-03-22 11:03:15 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRetrieveZone(t *testing.T) {
|
|
|
|
someErr := errors.New("failed")
|
|
|
|
|
|
|
|
testCases := map[string]struct {
|
2022-04-26 10:54:05 -04:00
|
|
|
client stubMetadataClient
|
|
|
|
wantValue string
|
|
|
|
wantErr bool
|
2022-03-22 11:03:15 -04:00
|
|
|
}{
|
|
|
|
"retrieve works": {
|
2022-04-26 10:54:05 -04:00
|
|
|
client: stubMetadataClient{ZoneValue: "someZone"},
|
|
|
|
wantValue: "someZone",
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"retrieve fails": {
|
2022-04-26 10:54:05 -04:00
|
|
|
client: stubMetadataClient{ZoneErr: someErr},
|
|
|
|
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)
|
|
|
|
|
|
|
|
client := Client{metadataAPI: tc.client}
|
|
|
|
value, err := client.RetrieveZone()
|
|
|
|
|
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.wantValue, value)
|
2022-03-22 11:03:15 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRetrieveInstanceName(t *testing.T) {
|
|
|
|
someErr := errors.New("failed")
|
|
|
|
|
|
|
|
testCases := map[string]struct {
|
2022-04-26 10:54:05 -04:00
|
|
|
client stubMetadataClient
|
|
|
|
wantValue string
|
|
|
|
wantErr bool
|
2022-03-22 11:03:15 -04:00
|
|
|
}{
|
|
|
|
"retrieve works": {
|
2022-04-26 10:54:05 -04:00
|
|
|
client: stubMetadataClient{InstanceNameValue: "someInstanceName"},
|
|
|
|
wantValue: "someInstanceName",
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"retrieve fails": {
|
2022-04-26 10:54:05 -04:00
|
|
|
client: stubMetadataClient{InstanceNameErr: someErr},
|
|
|
|
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)
|
|
|
|
|
|
|
|
client := Client{metadataAPI: tc.client}
|
|
|
|
value, err := client.RetrieveInstanceName()
|
|
|
|
|
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.wantValue, value)
|
2022-03-22 11:03:15 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRetrieveInstanceMetadata(t *testing.T) {
|
|
|
|
someErr := errors.New("failed")
|
|
|
|
attr := "someAttribute"
|
|
|
|
|
|
|
|
testCases := map[string]struct {
|
2022-04-26 10:54:05 -04:00
|
|
|
client stubMetadataClient
|
|
|
|
attr string
|
|
|
|
wantValue string
|
|
|
|
wantErr bool
|
2022-03-22 11:03:15 -04:00
|
|
|
}{
|
|
|
|
"retrieve works": {
|
|
|
|
client: stubMetadataClient{
|
|
|
|
InstanceValue: "someValue",
|
|
|
|
InstanceErr: nil,
|
|
|
|
},
|
2022-04-26 10:54:05 -04:00
|
|
|
wantValue: "someValue",
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"retrieve fails": {
|
|
|
|
client: stubMetadataClient{
|
|
|
|
InstanceValue: "",
|
|
|
|
InstanceErr: someErr,
|
|
|
|
},
|
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)
|
|
|
|
|
|
|
|
client := Client{metadataAPI: tc.client}
|
|
|
|
value, err := client.RetrieveInstanceMetadata(attr)
|
|
|
|
|
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.wantValue, value)
|
2022-03-22 11:03:15 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSetInstanceMetadata(t *testing.T) {
|
|
|
|
someErr := errors.New("failed")
|
|
|
|
|
|
|
|
testCases := map[string]struct {
|
2022-04-26 10:54:05 -04:00
|
|
|
client stubInstancesClient
|
|
|
|
wantErr bool
|
2022-03-22 11:03:15 -04:00
|
|
|
}{
|
|
|
|
"set works": {
|
|
|
|
client: stubInstancesClient{
|
|
|
|
GetInstance: &computepb.Instance{
|
|
|
|
Metadata: &computepb.Metadata{
|
|
|
|
Fingerprint: proto.String("someFingerprint"),
|
|
|
|
Kind: proto.String("compute#metadata"),
|
|
|
|
Items: []*computepb.Items{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"retrieve fails": {
|
|
|
|
client: stubInstancesClient{
|
|
|
|
GetErr: someErr,
|
|
|
|
},
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"retrieve returns nil": {
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"setting fails": {
|
|
|
|
client: stubInstancesClient{
|
|
|
|
GetInstance: &computepb.Instance{
|
|
|
|
Metadata: &computepb.Metadata{
|
|
|
|
Fingerprint: proto.String("someFingerprint"),
|
|
|
|
Kind: proto.String("compute#metadata"),
|
|
|
|
Items: []*computepb.Items{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
SetMetadataErr: someErr,
|
|
|
|
},
|
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)
|
|
|
|
|
|
|
|
client := Client{instanceAPI: tc.client}
|
|
|
|
err := client.SetInstanceMetadata(context.Background(), "project", "zone", "instanceName", "key", "value")
|
|
|
|
|
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)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUnsetInstanceMetadata(t *testing.T) {
|
|
|
|
someErr := errors.New("failed")
|
|
|
|
|
|
|
|
testCases := map[string]struct {
|
2022-04-26 10:54:05 -04:00
|
|
|
client stubInstancesClient
|
|
|
|
wantErr bool
|
2022-03-22 11:03:15 -04:00
|
|
|
}{
|
|
|
|
"unset works": {
|
|
|
|
client: stubInstancesClient{
|
|
|
|
GetInstance: &computepb.Instance{
|
|
|
|
Metadata: &computepb.Metadata{
|
|
|
|
Fingerprint: proto.String("someFingerprint"),
|
|
|
|
Kind: proto.String("compute#metadata"),
|
|
|
|
Items: []*computepb.Items{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"unset with existing key works": {
|
|
|
|
client: stubInstancesClient{
|
|
|
|
GetInstance: &computepb.Instance{
|
|
|
|
Metadata: &computepb.Metadata{
|
|
|
|
Fingerprint: proto.String("someFingerprint"),
|
|
|
|
Kind: proto.String("compute#metadata"),
|
|
|
|
Items: []*computepb.Items{
|
|
|
|
{
|
|
|
|
Key: proto.String("key"),
|
|
|
|
Value: proto.String("value"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"retrieve fails": {
|
2022-04-26 10:54:05 -04:00
|
|
|
client: stubInstancesClient{GetErr: someErr},
|
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"retrieve returns nil": {
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"setting fails": {
|
|
|
|
client: stubInstancesClient{
|
|
|
|
GetInstance: &computepb.Instance{
|
|
|
|
Metadata: &computepb.Metadata{
|
|
|
|
Fingerprint: proto.String("someFingerprint"),
|
|
|
|
Kind: proto.String("compute#metadata"),
|
|
|
|
Items: []*computepb.Items{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
SetMetadataErr: someErr,
|
|
|
|
},
|
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)
|
|
|
|
|
|
|
|
client := Client{instanceAPI: tc.client}
|
|
|
|
err := client.UnsetInstanceMetadata(context.Background(), "project", "zone", "instanceName", "key")
|
|
|
|
|
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-05-24 04:04:42 -04:00
|
|
|
func TestRetrieveSubnetworkAliasCIDR(t *testing.T) {
|
|
|
|
aliasCIDR := "192.0.2.1/24"
|
|
|
|
someErr := errors.New("some error")
|
|
|
|
testCases := map[string]struct {
|
|
|
|
stubInstancesClient stubInstancesClient
|
|
|
|
stubSubnetworksClient stubSubnetworksClient
|
|
|
|
wantAliasCIDR string
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
"RetrieveSubnetworkAliasCIDR works": {
|
|
|
|
stubInstancesClient: stubInstancesClient{
|
|
|
|
GetInstance: &computepb.Instance{
|
|
|
|
NetworkInterfaces: []*computepb.NetworkInterface{
|
|
|
|
{
|
|
|
|
Subnetwork: proto.String("projects/project/regions/region/subnetworks/subnetwork"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
stubSubnetworksClient: stubSubnetworksClient{
|
|
|
|
GetSubnetwork: &computepb.Subnetwork{
|
2022-08-12 04:20:19 -04:00
|
|
|
SecondaryIpRanges: []*computepb.SubnetworkSecondaryRange{
|
|
|
|
{
|
|
|
|
IpCidrRange: proto.String(aliasCIDR),
|
|
|
|
},
|
|
|
|
},
|
2022-05-24 04:04:42 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
wantAliasCIDR: aliasCIDR,
|
|
|
|
},
|
|
|
|
"instance has no network interface": {
|
|
|
|
stubInstancesClient: stubInstancesClient{
|
|
|
|
GetInstance: &computepb.Instance{
|
|
|
|
NetworkInterfaces: []*computepb.NetworkInterface{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"cannot get instance": {
|
|
|
|
stubInstancesClient: stubInstancesClient{
|
|
|
|
GetErr: someErr,
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"cannot get subnetwork": {
|
|
|
|
stubInstancesClient: stubInstancesClient{
|
|
|
|
GetInstance: &computepb.Instance{
|
|
|
|
NetworkInterfaces: []*computepb.NetworkInterface{
|
|
|
|
{
|
|
|
|
Subnetwork: proto.String("projects/project/regions/region/subnetworks/subnetwork"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
stubSubnetworksClient: stubSubnetworksClient{
|
|
|
|
GetErr: someErr,
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"subnetwork has no cidr range": {
|
|
|
|
stubInstancesClient: stubInstancesClient{
|
|
|
|
GetInstance: &computepb.Instance{
|
|
|
|
NetworkInterfaces: []*computepb.NetworkInterface{
|
|
|
|
{
|
|
|
|
Subnetwork: proto.String("projects/project/regions/region/subnetworks/subnetwork"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
stubSubnetworksClient: stubSubnetworksClient{
|
|
|
|
GetSubnetwork: &computepb.Subnetwork{},
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
client := Client{instanceAPI: tc.stubInstancesClient, subnetworkAPI: tc.stubSubnetworksClient}
|
2022-06-09 16:26:36 -04:00
|
|
|
aliasCIDR, err := client.RetrieveSubnetworkAliasCIDR(context.Background(), "project", "us-central1-a", "subnetwork")
|
2022-05-24 04:04:42 -04:00
|
|
|
|
|
|
|
if tc.wantErr {
|
|
|
|
assert.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
require.NoError(err)
|
|
|
|
assert.Equal(tc.wantAliasCIDR, aliasCIDR)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-01 10:51:34 -04:00
|
|
|
func TestRetrieveLoadBalancerEndpoint(t *testing.T) {
|
2022-06-09 16:26:36 -04:00
|
|
|
loadBalancerIP := "192.0.2.1"
|
|
|
|
uid := "uid"
|
|
|
|
someErr := errors.New("some error")
|
|
|
|
testCases := map[string]struct {
|
|
|
|
stubForwardingRulesClient stubForwardingRulesClient
|
|
|
|
stubMetadataClient stubMetadataClient
|
|
|
|
wantLoadBalancerIP string
|
|
|
|
wantErr bool
|
|
|
|
}{
|
2022-08-01 10:51:34 -04:00
|
|
|
"works": {
|
2022-06-09 16:26:36 -04:00
|
|
|
stubMetadataClient: stubMetadataClient{InstanceValue: uid},
|
|
|
|
stubForwardingRulesClient: stubForwardingRulesClient{
|
|
|
|
ForwardingRuleIterator: &stubForwardingRuleIterator{
|
|
|
|
rules: []*computepb.ForwardingRule{
|
|
|
|
{
|
|
|
|
IPAddress: proto.String(loadBalancerIP),
|
2022-08-01 10:51:34 -04:00
|
|
|
Ports: []string{"100"},
|
2022-06-09 16:26:36 -04:00
|
|
|
Labels: map[string]string{"constellation-uid": uid},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
wantLoadBalancerIP: loadBalancerIP,
|
|
|
|
},
|
2022-08-01 10:51:34 -04:00
|
|
|
"fails when no matching load balancers exists": {
|
2022-06-09 16:26:36 -04:00
|
|
|
stubMetadataClient: stubMetadataClient{InstanceValue: uid},
|
|
|
|
stubForwardingRulesClient: stubForwardingRulesClient{
|
|
|
|
ForwardingRuleIterator: &stubForwardingRuleIterator{
|
|
|
|
rules: []*computepb.ForwardingRule{
|
|
|
|
{
|
|
|
|
IPAddress: proto.String(loadBalancerIP),
|
2022-08-01 10:51:34 -04:00
|
|
|
Ports: []string{"100"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"fails when retrieving uid": {
|
|
|
|
stubMetadataClient: stubMetadataClient{InstanceErr: someErr},
|
|
|
|
stubForwardingRulesClient: stubForwardingRulesClient{
|
|
|
|
ForwardingRuleIterator: &stubForwardingRuleIterator{
|
|
|
|
rules: []*computepb.ForwardingRule{
|
|
|
|
{
|
|
|
|
IPAddress: proto.String(loadBalancerIP),
|
|
|
|
Ports: []string{"100"},
|
|
|
|
Labels: map[string]string{"constellation-uid": uid},
|
2022-06-09 16:26:36 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
2022-08-01 10:51:34 -04:00
|
|
|
"fails when answer has empty port range": {
|
2022-06-09 16:26:36 -04:00
|
|
|
stubMetadataClient: stubMetadataClient{InstanceErr: someErr},
|
|
|
|
stubForwardingRulesClient: stubForwardingRulesClient{
|
|
|
|
ForwardingRuleIterator: &stubForwardingRuleIterator{
|
|
|
|
rules: []*computepb.ForwardingRule{
|
|
|
|
{
|
|
|
|
IPAddress: proto.String(loadBalancerIP),
|
|
|
|
Labels: map[string]string{"constellation-uid": uid},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
2022-08-01 10:51:34 -04:00
|
|
|
"fails when retrieving loadbalancer IP": {
|
2022-06-09 16:26:36 -04:00
|
|
|
stubMetadataClient: stubMetadataClient{},
|
|
|
|
stubForwardingRulesClient: stubForwardingRulesClient{
|
|
|
|
ForwardingRuleIterator: &stubForwardingRuleIterator{
|
|
|
|
nextErr: someErr,
|
|
|
|
rules: []*computepb.ForwardingRule{
|
|
|
|
{
|
|
|
|
IPAddress: proto.String(loadBalancerIP),
|
2022-08-01 10:51:34 -04:00
|
|
|
Ports: []string{"100"},
|
2022-06-09 16:26:36 -04:00
|
|
|
Labels: map[string]string{"constellation-uid": uid},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
client := Client{forwardingRulesAPI: tc.stubForwardingRulesClient, metadataAPI: tc.stubMetadataClient}
|
2022-08-01 10:51:34 -04:00
|
|
|
aliasCIDR, err := client.RetrieveLoadBalancerEndpoint(context.Background(), "project", "us-central1-a")
|
2022-06-09 16:26:36 -04:00
|
|
|
|
|
|
|
if tc.wantErr {
|
|
|
|
assert.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
require.NoError(err)
|
2022-08-01 10:51:34 -04:00
|
|
|
assert.Equal(tc.wantLoadBalancerIP+":100", aliasCIDR)
|
2022-06-09 16:26:36 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-22 11:03:15 -04:00
|
|
|
func TestClose(t *testing.T) {
|
|
|
|
someErr := errors.New("failed")
|
|
|
|
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
2022-06-09 16:26:36 -04:00
|
|
|
client := Client{instanceAPI: stubInstancesClient{}, subnetworkAPI: stubSubnetworksClient{}, forwardingRulesAPI: stubForwardingRulesClient{}}
|
2022-03-22 11:03:15 -04:00
|
|
|
assert.NoError(client.Close())
|
|
|
|
|
2022-06-09 16:26:36 -04:00
|
|
|
client = Client{instanceAPI: stubInstancesClient{CloseErr: someErr}, subnetworkAPI: stubSubnetworksClient{}, forwardingRulesAPI: stubForwardingRulesClient{}}
|
|
|
|
assert.Error(client.Close())
|
|
|
|
|
|
|
|
client = Client{instanceAPI: stubInstancesClient{}, subnetworkAPI: stubSubnetworksClient{CloseErr: someErr}, forwardingRulesAPI: stubForwardingRulesClient{}}
|
2022-05-24 04:04:42 -04:00
|
|
|
assert.Error(client.Close())
|
|
|
|
|
2022-06-09 16:26:36 -04:00
|
|
|
client = Client{instanceAPI: stubInstancesClient{}, subnetworkAPI: stubSubnetworksClient{}, forwardingRulesAPI: stubForwardingRulesClient{CloseErr: someErr}}
|
2022-03-22 11:03:15 -04:00
|
|
|
assert.Error(client.Close())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFetchSSHKeys(t *testing.T) {
|
|
|
|
testCases := map[string]struct {
|
2022-04-26 10:54:05 -04:00
|
|
|
metadata map[string]string
|
|
|
|
wantKeys map[string][]string
|
2022-03-22 11:03:15 -04:00
|
|
|
}{
|
|
|
|
"fetch works": {
|
2022-04-26 10:54:05 -04:00
|
|
|
metadata: map[string]string{"ssh-keys": "bob:ssh-rsa bobskey"},
|
|
|
|
wantKeys: map[string][]string{"bob": {"ssh-rsa bobskey"}},
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"google ssh key metadata is ignored": {
|
2022-04-26 10:54:05 -04:00
|
|
|
metadata: map[string]string{"ssh-keys": "bob:ssh-rsa bobskey google-ssh {\"userName\":\"bob\",\"expireOn\":\"2021-06-14T16:59:03+0000\"}"},
|
|
|
|
wantKeys: map[string][]string{"bob": {"ssh-rsa bobskey"}},
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"ssh key format error is ignored": {
|
2022-04-26 10:54:05 -04:00
|
|
|
metadata: map[string]string{"ssh-keys": "incorrect-format"},
|
|
|
|
wantKeys: map[string][]string{},
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"ssh key format space error is ignored": {
|
2022-04-26 10:54:05 -04:00
|
|
|
metadata: map[string]string{"ssh-keys": "user:incorrect-key-format"},
|
|
|
|
wantKeys: map[string][]string{},
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"metadata field empty": {
|
2022-04-26 10:54:05 -04:00
|
|
|
metadata: map[string]string{"ssh-keys": ""},
|
|
|
|
wantKeys: map[string][]string{},
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"metadata field missing": {
|
2022-04-26 10:54:05 -04:00
|
|
|
metadata: map[string]string{},
|
|
|
|
wantKeys: map[string][]string{},
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"multiple keys": {
|
|
|
|
metadata: map[string]string{"ssh-keys": "bob:ssh-rsa bobskey\nalice:ssh-rsa alicekey"},
|
2022-04-26 10:54:05 -04:00
|
|
|
wantKeys: map[string][]string{
|
2022-03-22 11:03:15 -04:00
|
|
|
"bob": {"ssh-rsa bobskey"},
|
|
|
|
"alice": {"ssh-rsa alicekey"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
keys := extractSSHKeys(tc.metadata)
|
2022-04-26 10:54:05 -04:00
|
|
|
assert.Equal(tc.wantKeys, keys)
|
2022-03-22 11:03:15 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type stubInstanceIterator struct {
|
|
|
|
instances []*computepb.Instance
|
|
|
|
nextErr error
|
|
|
|
|
|
|
|
internalCounter int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *stubInstanceIterator) Next() (*computepb.Instance, error) {
|
|
|
|
if i.nextErr != nil {
|
|
|
|
return nil, i.nextErr
|
|
|
|
}
|
|
|
|
if i.internalCounter >= len(i.instances) {
|
|
|
|
i.internalCounter = 0
|
|
|
|
return nil, iterator.Done
|
|
|
|
}
|
|
|
|
resp := i.instances[i.internalCounter]
|
|
|
|
i.internalCounter++
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type stubInstancesClient struct {
|
|
|
|
GetInstance *computepb.Instance
|
|
|
|
GetErr error
|
|
|
|
ListInstanceIterator InstanceIterator
|
|
|
|
SetMetadataOperation *compute.Operation
|
|
|
|
SetMetadataErr error
|
|
|
|
CloseErr error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s stubInstancesClient) Get(ctx context.Context, req *computepb.GetInstanceRequest, opts ...gax.CallOption) (*computepb.Instance, error) {
|
|
|
|
return s.GetInstance, s.GetErr
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s stubInstancesClient) List(ctx context.Context, req *computepb.ListInstancesRequest, opts ...gax.CallOption) InstanceIterator {
|
|
|
|
return s.ListInstanceIterator
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s stubInstancesClient) SetMetadata(ctx context.Context, req *computepb.SetMetadataInstanceRequest, opts ...gax.CallOption) (*compute.Operation, error) {
|
|
|
|
return s.SetMetadataOperation, s.SetMetadataErr
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s stubInstancesClient) Close() error {
|
|
|
|
return s.CloseErr
|
|
|
|
}
|
|
|
|
|
2022-05-24 04:04:42 -04:00
|
|
|
type stubSubnetworksClient struct {
|
|
|
|
GetSubnetwork *computepb.Subnetwork
|
|
|
|
GetErr error
|
|
|
|
SubnetworkIterator SubnetworkIterator
|
|
|
|
CloseErr error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s stubSubnetworksClient) Get(ctx context.Context, req *computepb.GetSubnetworkRequest, opts ...gax.CallOption) (*computepb.Subnetwork, error) {
|
|
|
|
return s.GetSubnetwork, s.GetErr
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s stubSubnetworksClient) List(ctx context.Context, req *computepb.ListSubnetworksRequest, opts ...gax.CallOption) SubnetworkIterator {
|
|
|
|
return s.SubnetworkIterator
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s stubSubnetworksClient) Close() error {
|
|
|
|
return s.CloseErr
|
|
|
|
}
|
|
|
|
|
2022-06-09 16:26:36 -04:00
|
|
|
type stubForwardingRuleIterator struct {
|
|
|
|
rules []*computepb.ForwardingRule
|
|
|
|
nextErr error
|
|
|
|
|
|
|
|
internalCounter int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *stubForwardingRuleIterator) Next() (*computepb.ForwardingRule, error) {
|
|
|
|
if i.nextErr != nil {
|
|
|
|
return nil, i.nextErr
|
|
|
|
}
|
|
|
|
if i.internalCounter >= len(i.rules) {
|
|
|
|
i.internalCounter = 0
|
|
|
|
return nil, iterator.Done
|
|
|
|
}
|
|
|
|
resp := i.rules[i.internalCounter]
|
|
|
|
i.internalCounter++
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type stubForwardingRulesClient struct {
|
|
|
|
ForwardingRuleIterator ForwardingRuleIterator
|
|
|
|
GetErr error
|
|
|
|
CloseErr error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s stubForwardingRulesClient) List(ctx context.Context, req *computepb.ListForwardingRulesRequest, opts ...gax.CallOption) ForwardingRuleIterator {
|
|
|
|
return s.ForwardingRuleIterator
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s stubForwardingRulesClient) Close() error {
|
|
|
|
return s.CloseErr
|
|
|
|
}
|
|
|
|
|
2022-03-22 11:03:15 -04:00
|
|
|
type stubMetadataClient struct {
|
|
|
|
InstanceValue string
|
|
|
|
InstanceErr error
|
|
|
|
ProjectIDValue string
|
|
|
|
ProjectIDErr error
|
|
|
|
ZoneValue string
|
|
|
|
ZoneErr error
|
|
|
|
InstanceNameValue string
|
|
|
|
InstanceNameErr error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s stubMetadataClient) InstanceAttributeValue(attr string) (string, error) {
|
|
|
|
return s.InstanceValue, s.InstanceErr
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s stubMetadataClient) ProjectID() (string, error) {
|
|
|
|
return s.ProjectIDValue, s.ProjectIDErr
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s stubMetadataClient) Zone() (string, error) {
|
|
|
|
return s.ZoneValue, s.ZoneErr
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s stubMetadataClient) InstanceName() (string, error) {
|
|
|
|
return s.InstanceNameValue, s.InstanceNameErr
|
|
|
|
}
|