2024-04-02 15:47:49 -04:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
|
|
|
|
|
package plugin
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
"cloud.google.com/go/compute/apiv1/computepb"
|
|
|
|
|
pb "github.com/hashicorp/boundary/sdk/pbs/plugin"
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestInstanceToHost(t *testing.T) {
|
|
|
|
|
exampleId := "https://www.googleapis.com/compute/v1/projects/test-project/zones/us-central1-a/instances/test-instance"
|
|
|
|
|
examplePrivateIp := "10.0.0.1"
|
|
|
|
|
examplePrivateIp2 := "10.0.0.2"
|
|
|
|
|
examplePublicIp := "1.1.1.1"
|
|
|
|
|
examplePublicIp2 := "1.1.1.2"
|
|
|
|
|
exampleIPv6 := "some::fake::address"
|
2024-08-06 11:51:00 -04:00
|
|
|
exampleName := "test-instance"
|
2024-04-02 15:47:49 -04:00
|
|
|
|
|
|
|
|
cases := []struct {
|
|
|
|
|
name string
|
|
|
|
|
instance *computepb.Instance
|
|
|
|
|
expected *pb.ListHostsResponseHost
|
|
|
|
|
expectedErr string
|
|
|
|
|
}{
|
|
|
|
|
{
|
2024-08-06 11:51:00 -04:00
|
|
|
name: "missing instance self-link",
|
|
|
|
|
instance: &computepb.Instance{
|
|
|
|
|
Name: &exampleName,
|
|
|
|
|
},
|
2024-04-02 15:47:49 -04:00
|
|
|
expectedErr: "response integrity error: missing instance self-link",
|
|
|
|
|
},
|
2024-08-06 11:51:00 -04:00
|
|
|
{
|
|
|
|
|
name: "missing instance name",
|
|
|
|
|
instance: &computepb.Instance{
|
|
|
|
|
SelfLink: &exampleId,
|
|
|
|
|
},
|
|
|
|
|
expectedErr: "response integrity error: missing instance name",
|
|
|
|
|
},
|
2024-04-02 15:47:49 -04:00
|
|
|
{
|
|
|
|
|
name: "good, single private IP with public IP address",
|
|
|
|
|
instance: &computepb.Instance{
|
|
|
|
|
SelfLink: &exampleId,
|
2024-08-06 11:51:00 -04:00
|
|
|
Name: &exampleName,
|
2024-04-02 15:47:49 -04:00
|
|
|
NetworkInterfaces: []*computepb.NetworkInterface{
|
|
|
|
|
{
|
|
|
|
|
NetworkIP: &examplePrivateIp,
|
|
|
|
|
AccessConfigs: []*computepb.AccessConfig{
|
|
|
|
|
{
|
|
|
|
|
NatIP: &examplePublicIp,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
expected: &pb.ListHostsResponseHost{
|
2024-08-06 11:51:00 -04:00
|
|
|
ExternalId: exampleId,
|
|
|
|
|
ExternalName: exampleName,
|
|
|
|
|
IpAddresses: []string{examplePrivateIp, examplePublicIp},
|
2024-04-02 15:47:49 -04:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "good, single private IP address",
|
|
|
|
|
instance: &computepb.Instance{
|
|
|
|
|
SelfLink: &exampleId,
|
2024-08-06 11:51:00 -04:00
|
|
|
Name: &exampleName,
|
2024-04-02 15:47:49 -04:00
|
|
|
NetworkInterfaces: []*computepb.NetworkInterface{
|
|
|
|
|
{
|
|
|
|
|
NetworkIP: &examplePrivateIp,
|
|
|
|
|
AccessConfigs: []*computepb.AccessConfig{},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
expected: &pb.ListHostsResponseHost{
|
2024-08-06 11:51:00 -04:00
|
|
|
ExternalId: exampleId,
|
|
|
|
|
ExternalName: exampleName,
|
|
|
|
|
IpAddresses: []string{examplePrivateIp},
|
2024-04-02 15:47:49 -04:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "good, multiple interfaces",
|
|
|
|
|
instance: &computepb.Instance{
|
|
|
|
|
SelfLink: &exampleId,
|
2024-08-06 11:51:00 -04:00
|
|
|
Name: &exampleName,
|
2024-04-02 15:47:49 -04:00
|
|
|
NetworkInterfaces: []*computepb.NetworkInterface{
|
|
|
|
|
{
|
|
|
|
|
NetworkIP: &examplePrivateIp,
|
|
|
|
|
AccessConfigs: []*computepb.AccessConfig{
|
|
|
|
|
{
|
|
|
|
|
NatIP: &examplePublicIp,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
NetworkIP: &examplePrivateIp2,
|
|
|
|
|
AccessConfigs: []*computepb.AccessConfig{
|
|
|
|
|
{
|
|
|
|
|
NatIP: &examplePublicIp2,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
expected: &pb.ListHostsResponseHost{
|
2024-08-06 11:51:00 -04:00
|
|
|
ExternalId: exampleId,
|
|
|
|
|
ExternalName: exampleName,
|
|
|
|
|
IpAddresses: []string{examplePrivateIp, examplePublicIp, examplePrivateIp2, examplePublicIp2},
|
2024-04-02 15:47:49 -04:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "good, single private IP address with IPv6",
|
|
|
|
|
instance: &computepb.Instance{
|
|
|
|
|
SelfLink: &exampleId,
|
2024-08-06 11:51:00 -04:00
|
|
|
Name: &exampleName,
|
2024-04-02 15:47:49 -04:00
|
|
|
NetworkInterfaces: []*computepb.NetworkInterface{
|
|
|
|
|
{
|
|
|
|
|
NetworkIP: &examplePrivateIp,
|
|
|
|
|
Ipv6Address: &exampleIPv6,
|
|
|
|
|
AccessConfigs: []*computepb.AccessConfig{},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
expected: &pb.ListHostsResponseHost{
|
2024-08-06 11:51:00 -04:00
|
|
|
ExternalId: exampleId,
|
|
|
|
|
ExternalName: exampleName,
|
|
|
|
|
IpAddresses: []string{examplePrivateIp, exampleIPv6},
|
2024-04-02 15:47:49 -04:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
|
|
|
|
tc := tc
|
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
|
require := require.New(t)
|
|
|
|
|
actual, err := instanceToHost(tc.instance)
|
|
|
|
|
if tc.expectedErr != "" {
|
|
|
|
|
require.EqualError(err, tc.expectedErr)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
require.NoError(err)
|
|
|
|
|
require.Equal(tc.expected, actual)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|