Skip to content

Commit

Permalink
test: assert read network resources are as expected (#412)
Browse files Browse the repository at this point in the history
  • Loading branch information
goncalo-rodrigues authored Aug 18, 2022
1 parent 30a3818 commit 16de6ac
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 18 deletions.
2 changes: 1 addition & 1 deletion resources/output/kubernetes_service/aws_eks.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type VpcConfig struct {
}

type KubernetesNetworkConfig struct {
ServiceIpv4Cidr string `hcl:"service_ipv4_cidr" json:"service_ipv_4_cidr"`
ServiceIpv4Cidr string `hcl:"service_ipv4_cidr" json:"service_ipv4_cidr"`
}

type CertificateAuthority struct {
Expand Down
2 changes: 1 addition & 1 deletion resources/output/kubernetes_service/gcp_gke.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ type GoogleContainerClusterAuth struct {

type GoogleContainerClusterIpAllocationPolicy struct {
//ClusterIpv4CidrBlock string `hcl:"cluster_ipv_4_cidr_block"`
ServicesIpv4CidrBlock string `hcl:"services_ipv4_cidr_block"`
ServicesIpv4CidrBlock string `hcl:"services_ipv4_cidr_block" json:"services_ipv4_cidr_block"`
}
2 changes: 1 addition & 1 deletion resources/types/gcp/virtual_machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (r GcpVirtualMachine) FromState(state *output.TfState) (*resourcespb.Virtua
out.PublicSshKey = ""
} else {
// remove the expiration timestamp (https://cloud.google.com/compute/docs/connect/add-ssh-keys#gcloud_1)
out.PublicSshKey = strings.TrimSpace(strings.SplitN(userAndKey[1], " google-ssh ", 2)[0])
out.PublicSshKey = strings.SplitN(userAndKey[1], " google-ssh ", 2)[0]
}

if r.Args.GeneratePublicIp {
Expand Down
44 changes: 44 additions & 0 deletions test/e2e/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/multycloud/multy/api/proto/commonpb"
"github.com/multycloud/multy/api/proto/resourcespb"
"github.com/multycloud/multy/api/services"
"github.com/stretchr/testify/assert"
"google.golang.org/protobuf/proto"
"testing"
)
Expand All @@ -34,6 +35,13 @@ func createNetworkWithInternetAccess(t *testing.T, ctx context.Context, location
}
cleanup(t, ctx, server.VnService, vn)

assert.Equal(t, createVnRequest.GetResource().GetCommonParameters().GetLocation(), vn.GetCommonParameters().GetLocation())
assert.Equal(t, createVnRequest.GetResource().GetCommonParameters().GetCloudProvider(), vn.GetCommonParameters().GetCloudProvider())
assert.Nil(t, vn.GetCommonParameters().GetResourceStatus())

assert.Equal(t, createVnRequest.GetResource().GetName(), vn.GetName())
assert.Equal(t, createVnRequest.GetResource().GetCidrBlock(), vn.GetCidrBlock())

createPublicSubnetRequest := &resourcespb.CreateSubnetRequest{Resource: &resourcespb.SubnetArgs{
Name: prefix + "-test-public-subnet",
CidrBlock: publicSubnetCidr,
Expand All @@ -46,6 +54,12 @@ func createNetworkWithInternetAccess(t *testing.T, ctx context.Context, location
}
cleanup(t, ctx, server.SubnetService, publicSubnet)

assert.Nil(t, publicSubnet.GetCommonParameters().GetResourceStatus())

assert.Equal(t, createPublicSubnetRequest.GetResource().GetName(), publicSubnet.GetName())
assert.Equal(t, createPublicSubnetRequest.GetResource().GetCidrBlock(), publicSubnet.GetCidrBlock())
assert.Equal(t, createPublicSubnetRequest.GetResource().GetVirtualNetworkId(), publicSubnet.GetVirtualNetworkId())

createRtRequest := &resourcespb.CreateRouteTableRequest{Resource: &resourcespb.RouteTableArgs{
Name: prefix + "-test-rt",
VirtualNetworkId: vn.CommonParameters.ResourceId,
Expand All @@ -63,6 +77,14 @@ func createNetworkWithInternetAccess(t *testing.T, ctx context.Context, location
}
cleanup(t, ctx, server.RouteTableService, rt)

assert.Nil(t, rt.GetCommonParameters().GetResourceStatus())

assert.Equal(t, createRtRequest.GetResource().GetName(), rt.GetName())
assert.Len(t, rt.GetRoutes(), 1)
assert.Equal(t, createRtRequest.GetResource().GetRoutes()[0].GetCidrBlock(), rt.GetRoutes()[0].GetCidrBlock())
assert.Equal(t, createRtRequest.GetResource().GetRoutes()[0].GetDestination(), rt.GetRoutes()[0].GetDestination())
assert.Equal(t, createRtRequest.GetResource().GetVirtualNetworkId(), rt.GetVirtualNetworkId())

createRtaRequest := &resourcespb.CreateRouteTableAssociationRequest{Resource: &resourcespb.RouteTableAssociationArgs{
SubnetId: publicSubnet.CommonParameters.ResourceId,
RouteTableId: rt.CommonParameters.ResourceId,
Expand All @@ -74,6 +96,11 @@ func createNetworkWithInternetAccess(t *testing.T, ctx context.Context, location
}
cleanup(t, ctx, server.RouteTableAssociationService, rta)

assert.Nil(t, rta.GetCommonParameters().GetResourceStatus())

assert.Equal(t, createRtaRequest.GetResource().GetSubnetId(), rta.GetSubnetId())
assert.Equal(t, createRtaRequest.GetResource().GetRouteTableId(), rta.GetRouteTableId())

createNsgRequest := &resourcespb.CreateNetworkSecurityGroupRequest{Resource: &resourcespb.NetworkSecurityGroupArgs{
CommonParameters: &commonpb.ResourceCommonArgs{
Location: location,
Expand Down Expand Up @@ -118,6 +145,23 @@ func createNetworkWithInternetAccess(t *testing.T, ctx context.Context, location
}
cleanup(t, ctx, server.NetworkSecurityGroupService, nsg)

assert.Equal(t, createNsgRequest.GetResource().GetCommonParameters().GetLocation(), nsg.GetCommonParameters().GetLocation())
assert.Equal(t, createNsgRequest.GetResource().GetCommonParameters().GetCloudProvider(), nsg.GetCommonParameters().GetCloudProvider())
assert.Nil(t, nsg.GetCommonParameters().GetResourceStatus())

assert.Equal(t, createNsgRequest.GetResource().GetName(), nsg.GetName())
assert.Equal(t, createNsgRequest.GetResource().GetVirtualNetworkId(), nsg.GetVirtualNetworkId())
assert.Len(t, nsg.GetRules(), len(createNsgRequest.GetResource().GetRules()))
if len(nsg.GetRules()) == len(createNsgRequest.GetResource().GetRules()) {
for i, rule := range createNsgRequest.GetResource().GetRules() {
assert.Equal(t, rule.GetCidrBlock(), nsg.GetRules()[i].GetCidrBlock())
assert.Equal(t, rule.GetDirection(), nsg.GetRules()[i].GetDirection())
assert.Equal(t, rule.GetPortRange().GetFrom(), nsg.GetRules()[i].GetPortRange().GetFrom())
assert.Equal(t, rule.GetPortRange().GetTo(), nsg.GetRules()[i].GetPortRange().GetTo())
assert.Equal(t, rule.GetPriority(), nsg.GetRules()[i].GetPriority())
}
}

return publicSubnet, nsg
}

Expand Down
18 changes: 18 additions & 0 deletions test/e2e/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,24 @@ func testDatabase(t *testing.T, cloud commonpb.CloudProvider) {
}
cleanup(t, ctx, server.DatabaseService, db)

readDb, err := server.DatabaseService.Read(ctx, &resourcespb.ReadDatabaseRequest{ResourceId: db.CommonParameters.ResourceId})
if err != nil {
t.Fatalf("unable to read db, %s", err)
}

assert.Equal(t, createDbRequest.GetResource().GetCommonParameters().GetLocation(), readDb.GetCommonParameters().GetLocation())
assert.Equal(t, createDbRequest.GetResource().GetCommonParameters().GetCloudProvider(), readDb.GetCommonParameters().GetCloudProvider())
assert.Nil(t, readDb.GetCommonParameters().GetResourceStatus())

assert.Equal(t, createDbRequest.GetResource().GetName(), readDb.GetName())
assert.Equal(t, createDbRequest.GetResource().GetSubnetId(), readDb.GetSubnetId())
assert.Equal(t, createDbRequest.GetResource().GetEngine(), readDb.GetEngine())
assert.Equal(t, createDbRequest.GetResource().GetEngineVersion(), readDb.GetEngineVersion())
assert.Equal(t, createDbRequest.GetResource().GetStorageGb(), readDb.GetStorageGb())
assert.Equal(t, createDbRequest.GetResource().GetSize(), readDb.GetSize())
assert.Equal(t, createDbRequest.GetResource().GetUsername(), readDb.GetUsername())
assert.Equal(t, createDbRequest.GetResource().GetPassword(), readDb.GetPassword())

time.Sleep(5 * time.Second)

out, err := exec.Command("mysql", "-h", db.Host, "-P", "3306", "-u", db.ConnectionUsername, "--password="+db.Password, "-e", "select 12+34;").CombinedOutput()
Expand Down
7 changes: 7 additions & 0 deletions test/e2e/public_ip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"context"
"github.com/multycloud/multy/api/proto/commonpb"
"github.com/multycloud/multy/api/proto/resourcespb"
"github.com/stretchr/testify/assert"
"golang.org/x/crypto/ssh"
"testing"
"time"
Expand All @@ -33,6 +34,12 @@ func testPublicIp(t *testing.T, ctx context.Context, vm *resourcespb.VirtualMach
}
cleanup(t, ctx, server.PublicIpService, pip)

assert.Equal(t, createPipRequest.GetResource().GetCommonParameters().GetLocation(), pip.GetCommonParameters().GetLocation())
assert.Equal(t, createPipRequest.GetResource().GetCommonParameters().GetCloudProvider(), pip.GetCommonParameters().GetCloudProvider())
assert.Nil(t, pip.GetCommonParameters().GetResourceStatus())

assert.Equal(t, createPipRequest.GetResource().GetName(), pip.GetName())

updateReq := getNoopUpdate(vm)
updateReq.Resource.GeneratePublicIp = false
updateReq.Resource.PublicIpId = pip.CommonParameters.ResourceId
Expand Down
22 changes: 22 additions & 0 deletions test/e2e/vault_secret_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,17 @@ func testVaultSecret(t *testing.T, cloud commonpb.CloudProvider) {
}
cleanup(t, ctx, server.VaultService, vault)

readVault, err := server.VaultService.Read(ctx, &resourcespb.ReadVaultRequest{ResourceId: vault.CommonParameters.ResourceId})
if err != nil {
t.Fatalf("unable to read vault, %s", err)
}

assert.Equal(t, createVaultRequest.GetResource().GetCommonParameters().GetLocation(), readVault.GetCommonParameters().GetLocation())
assert.Equal(t, createVaultRequest.GetResource().GetCommonParameters().GetCloudProvider(), readVault.GetCommonParameters().GetCloudProvider())
assert.Nil(t, readVault.GetCommonParameters().GetResourceStatus())

assert.Equal(t, createVaultRequest.GetResource().GetName(), readVault.GetName())

createVaultSecretRequest := &resourcespb.CreateVaultSecretRequest{Resource: &resourcespb.VaultSecretArgs{
Name: "test-secret",
Value: "test-value",
Expand All @@ -52,6 +63,17 @@ func testVaultSecret(t *testing.T, cloud commonpb.CloudProvider) {
}
cleanup(t, ctx, server.VaultSecretService, vaultSecret)

readVaultSecret, err := server.VaultSecretService.Read(ctx, &resourcespb.ReadVaultSecretRequest{ResourceId: vaultSecret.CommonParameters.ResourceId})
if err != nil {
t.Fatalf("unable to read vault secret, %s", err)
}

assert.Nil(t, readVaultSecret.GetCommonParameters().GetResourceStatus())

assert.Equal(t, createVaultSecretRequest.GetResource().GetName(), readVaultSecret.GetName())
assert.Equal(t, createVaultSecretRequest.GetResource().GetValue(), readVaultSecret.GetValue())
assert.Equal(t, createVaultSecretRequest.GetResource().GetVaultId(), readVaultSecret.GetVaultId())

t.Run("vault-access-policy-reader", func(t *testing.T) {
testReaderAccess(t, ctx, cloud, vm, config, vault, vaultSecret)
})
Expand Down
30 changes: 15 additions & 15 deletions test/e2e/virtual_machine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,22 +120,22 @@ sudo echo "hello world" > /tmp/test.txt`)),
t.Fatalf("unable to read virtual machine, %s", err)
}

assert.Equal(t, readVm.GetCommonParameters().GetLocation(), createVmRequest.GetResource().GetCommonParameters().GetLocation())
assert.Equal(t, readVm.GetCommonParameters().GetCloudProvider(), createVmRequest.GetResource().GetCommonParameters().GetCloudProvider())
assert.Equal(t, createVmRequest.GetResource().GetCommonParameters().GetLocation(), readVm.GetCommonParameters().GetLocation())
assert.Equal(t, createVmRequest.GetResource().GetCommonParameters().GetCloudProvider(), readVm.GetCommonParameters().GetCloudProvider())
assert.Nil(t, readVm.GetCommonParameters().GetResourceStatus())

assert.Equal(t, readVm.GetName(), createVmRequest.GetResource().GetName())
assert.Equal(t, readVm.GetVmSize(), createVmRequest.GetResource().GetVmSize())
assert.Equal(t, readVm.GetPublicIpId(), createVmRequest.GetResource().GetPublicIpId())
assert.Equal(t, readVm.GetSubnetId(), createVmRequest.GetResource().GetSubnetId())
//assert.Equal(t, readVm.GetAvailabilityZone(), createVmRequest.GetResource().GetAvailabilityZone())
assert.Equal(t, readVm.GetGeneratePublicIp(), createVmRequest.GetResource().GetGeneratePublicIp())
assert.Equal(t, readVm.GetPublicSshKey(), createVmRequest.GetResource().GetPublicSshKey())
assert.Equal(t, readVm.GetUserDataBase64(), createVmRequest.GetResource().GetUserDataBase64())
assert.Equal(t, readVm.GetNetworkInterfaceIds(), createVmRequest.GetResource().GetNetworkInterfaceIds())
assert.Equal(t, readVm.GetNetworkSecurityGroupIds(), createVmRequest.GetResource().GetNetworkSecurityGroupIds())
assert.Equal(t, readVm.GetImageReference().GetOs(), createVmRequest.GetResource().GetImageReference().GetOs())
assert.Equal(t, readVm.GetImageReference().GetVersion(), createVmRequest.GetResource().GetImageReference().GetVersion())
assert.Equal(t, createVmRequest.GetResource().GetName(), readVm.GetName())
assert.Equal(t, createVmRequest.GetResource().GetVmSize(), readVm.GetVmSize())
assert.Equal(t, createVmRequest.GetResource().GetPublicIpId(), readVm.GetPublicIpId())
assert.Equal(t, createVmRequest.GetResource().GetSubnetId(), readVm.GetSubnetId())
//assert.Equal(t, createVmRequest.GetResource().GetAvailabilityZone(), readVm.GetAvailabilityZone())
assert.Equal(t, createVmRequest.GetResource().GetGeneratePublicIp(), readVm.GetGeneratePublicIp())
assert.Equal(t, createVmRequest.GetResource().GetPublicSshKey(), readVm.GetPublicSshKey())
assert.Equal(t, createVmRequest.GetResource().GetUserDataBase64(), readVm.GetUserDataBase64())
assert.Equal(t, createVmRequest.GetResource().GetNetworkInterfaceIds(), readVm.GetNetworkInterfaceIds())
assert.Equal(t, createVmRequest.GetResource().GetNetworkSecurityGroupIds(), readVm.GetNetworkSecurityGroupIds())
assert.Equal(t, createVmRequest.GetResource().GetImageReference().GetOs(), readVm.GetImageReference().GetOs())
assert.Equal(t, createVmRequest.GetResource().GetImageReference().GetVersion(), readVm.GetImageReference().GetVersion())

return vm, nsg
}
Expand Down Expand Up @@ -202,7 +202,7 @@ func makeSSHKeyPair() (string, string, error) {
var pubKeyBuf strings.Builder
pubKeyBuf.Write(ssh.MarshalAuthorizedKey(pub))

return pubKeyBuf.String(), privKeyBuf.String(), nil
return strings.Trim(pubKeyBuf.String(), "\n"), privKeyBuf.String(), nil
}

func signerFromPem(pemBytes []byte) (ssh.Signer, error) {
Expand Down

0 comments on commit 16de6ac

Please sign in to comment.