2022-03-22 11:03:15 -04:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"testing"
|
|
|
|
|
2022-06-08 02:17:52 -04:00
|
|
|
"github.com/edgelesssys/constellation/internal/cloud/cloudtypes"
|
2022-03-22 11:03:15 -04:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestCreateVirtualNetwork(t *testing.T) {
|
|
|
|
someErr := errors.New("failed")
|
|
|
|
testCases := map[string]struct {
|
|
|
|
networksAPI networksAPI
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr bool
|
2022-03-22 11:03:15 -04:00
|
|
|
}{
|
|
|
|
"successful create": {
|
|
|
|
networksAPI: stubNetworksAPI{},
|
|
|
|
},
|
|
|
|
"failed to get response from successful create": {
|
2022-07-27 16:02:33 -04:00
|
|
|
networksAPI: stubNetworksAPI{pollErr: someErr},
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"failed create": {
|
|
|
|
networksAPI: stubNetworksAPI{createErr: 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)
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
client := Client{
|
|
|
|
resourceGroup: "resource-group",
|
|
|
|
location: "location",
|
|
|
|
name: "name",
|
|
|
|
uid: "uid",
|
|
|
|
networksAPI: tc.networksAPI,
|
2022-06-29 09:26:29 -04:00
|
|
|
workers: make(cloudtypes.Instances),
|
|
|
|
controlPlanes: make(cloudtypes.Instances),
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
2022-04-26 10:54:05 -04:00
|
|
|
if tc.wantErr {
|
2022-03-22 11:03:15 -04:00
|
|
|
assert.Error(client.CreateVirtualNetwork(ctx))
|
|
|
|
} else {
|
|
|
|
assert.NoError(client.CreateVirtualNetwork(ctx))
|
|
|
|
assert.NotEmpty(client.subnetID)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCreateSecurityGroup(t *testing.T) {
|
|
|
|
someErr := errors.New("failed")
|
|
|
|
testNetworkSecurityGroupInput := NetworkSecurityGroupInput{
|
|
|
|
Ingress: cloudtypes.Firewall{
|
|
|
|
{
|
|
|
|
Name: "test-1",
|
|
|
|
Description: "test-1 description",
|
|
|
|
Protocol: "tcp",
|
|
|
|
IPRange: "192.0.2.0/24",
|
2022-04-26 11:09:03 -04:00
|
|
|
FromPort: 9000,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "test-2",
|
|
|
|
Description: "test-2 description",
|
|
|
|
Protocol: "udp",
|
|
|
|
IPRange: "192.0.2.0/24",
|
2022-04-26 11:09:03 -04:00
|
|
|
FromPort: 51820,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
Egress: cloudtypes.Firewall{},
|
|
|
|
}
|
|
|
|
|
|
|
|
testCases := map[string]struct {
|
|
|
|
networkSecurityGroupsAPI networkSecurityGroupsAPI
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr bool
|
2022-03-22 11:03:15 -04:00
|
|
|
}{
|
|
|
|
"successful create": {
|
|
|
|
networkSecurityGroupsAPI: stubNetworkSecurityGroupsAPI{},
|
|
|
|
},
|
|
|
|
"failed to get response from successful create": {
|
2022-07-27 16:02:33 -04:00
|
|
|
networkSecurityGroupsAPI: stubNetworkSecurityGroupsAPI{pollErr: someErr},
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"failed create": {
|
|
|
|
networkSecurityGroupsAPI: stubNetworkSecurityGroupsAPI{createErr: 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)
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
client := Client{
|
|
|
|
resourceGroup: "resource-group",
|
|
|
|
location: "location",
|
|
|
|
name: "name",
|
|
|
|
uid: "uid",
|
2022-06-29 09:26:29 -04:00
|
|
|
workers: make(cloudtypes.Instances),
|
|
|
|
controlPlanes: make(cloudtypes.Instances),
|
2022-03-22 11:03:15 -04:00
|
|
|
networkSecurityGroupsAPI: tc.networkSecurityGroupsAPI,
|
|
|
|
}
|
|
|
|
|
2022-04-26 10:54:05 -04:00
|
|
|
if tc.wantErr {
|
2022-03-22 11:03:15 -04:00
|
|
|
assert.Error(client.CreateSecurityGroup(ctx, testNetworkSecurityGroupInput))
|
|
|
|
} else {
|
|
|
|
assert.NoError(client.CreateSecurityGroup(ctx, testNetworkSecurityGroupInput))
|
|
|
|
assert.Equal("network-security-group-id", client.networkSecurityGroup)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: deprecate as soon as scale sets are available.
|
|
|
|
func TestCreateNIC(t *testing.T) {
|
|
|
|
someErr := errors.New("failed")
|
|
|
|
|
|
|
|
testCases := map[string]struct {
|
|
|
|
networkInterfacesAPI networkInterfacesAPI
|
|
|
|
name string
|
|
|
|
publicIPAddressID string
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr bool
|
2022-03-22 11:03:15 -04:00
|
|
|
}{
|
|
|
|
"successful create": {
|
|
|
|
networkInterfacesAPI: stubNetworkInterfacesAPI{},
|
|
|
|
name: "nic-name",
|
|
|
|
publicIPAddressID: "pubIP-id",
|
|
|
|
},
|
|
|
|
"failed to get response from successful create": {
|
2022-07-27 16:02:33 -04:00
|
|
|
networkInterfacesAPI: stubNetworkInterfacesAPI{pollErr: someErr},
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"failed create": {
|
|
|
|
networkInterfacesAPI: stubNetworkInterfacesAPI{createErr: 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)
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
client := Client{
|
|
|
|
resourceGroup: "resource-group",
|
|
|
|
location: "location",
|
|
|
|
name: "name",
|
|
|
|
uid: "uid",
|
2022-06-29 09:26:29 -04:00
|
|
|
workers: make(cloudtypes.Instances),
|
|
|
|
controlPlanes: make(cloudtypes.Instances),
|
2022-03-22 11:03:15 -04:00
|
|
|
networkInterfacesAPI: tc.networkInterfacesAPI,
|
|
|
|
}
|
|
|
|
|
|
|
|
ip, id, err := client.createNIC(ctx, tc.name, tc.publicIPAddressID)
|
2022-04-26 10:54:05 -04:00
|
|
|
if tc.wantErr {
|
2022-03-22 11:03:15 -04:00
|
|
|
assert.Error(err)
|
|
|
|
} else {
|
|
|
|
assert.NoError(err)
|
|
|
|
assert.NotEmpty(ip)
|
|
|
|
assert.NotEmpty(id)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCreatePublicIPAddress(t *testing.T) {
|
|
|
|
someErr := errors.New("failed")
|
|
|
|
|
|
|
|
testCases := map[string]struct {
|
|
|
|
publicIPAddressesAPI publicIPAddressesAPI
|
|
|
|
name string
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr bool
|
2022-03-22 11:03:15 -04:00
|
|
|
}{
|
|
|
|
"successful create": {
|
|
|
|
publicIPAddressesAPI: stubPublicIPAddressesAPI{},
|
|
|
|
name: "nic-name",
|
|
|
|
},
|
|
|
|
"failed to get response from successful create": {
|
2022-07-27 16:02:33 -04:00
|
|
|
publicIPAddressesAPI: stubPublicIPAddressesAPI{pollErr: someErr},
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"failed create": {
|
|
|
|
publicIPAddressesAPI: stubPublicIPAddressesAPI{createErr: 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)
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
client := Client{
|
|
|
|
resourceGroup: "resource-group",
|
|
|
|
location: "location",
|
|
|
|
name: "name",
|
|
|
|
uid: "uid",
|
2022-06-29 09:26:29 -04:00
|
|
|
workers: make(cloudtypes.Instances),
|
|
|
|
controlPlanes: make(cloudtypes.Instances),
|
2022-03-22 11:03:15 -04:00
|
|
|
publicIPAddressesAPI: tc.publicIPAddressesAPI,
|
|
|
|
}
|
|
|
|
|
|
|
|
id, err := client.createPublicIPAddress(ctx, tc.name)
|
2022-04-26 10:54:05 -04:00
|
|
|
if tc.wantErr {
|
2022-03-22 11:03:15 -04:00
|
|
|
assert.Error(err)
|
|
|
|
} else {
|
|
|
|
assert.NoError(err)
|
|
|
|
assert.NotEmpty(id)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2022-05-24 04:04:42 -04:00
|
|
|
|
|
|
|
func TestCreateExternalLoadBalancer(t *testing.T) {
|
|
|
|
someErr := errors.New("failed")
|
|
|
|
|
|
|
|
testCases := map[string]struct {
|
|
|
|
publicIPAddressesAPI publicIPAddressesAPI
|
|
|
|
loadBalancersAPI loadBalancersAPI
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
"successful create": {
|
2022-07-27 16:02:33 -04:00
|
|
|
publicIPAddressesAPI: stubPublicIPAddressesAPI{},
|
2022-05-24 04:04:42 -04:00
|
|
|
loadBalancersAPI: stubLoadBalancersAPI{},
|
|
|
|
},
|
|
|
|
"failed to get response from successful create": {
|
2022-07-27 16:02:33 -04:00
|
|
|
loadBalancersAPI: stubLoadBalancersAPI{pollErr: someErr},
|
2022-05-24 04:04:42 -04:00
|
|
|
publicIPAddressesAPI: stubPublicIPAddressesAPI{},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"failed create": {
|
|
|
|
loadBalancersAPI: stubLoadBalancersAPI{createErr: someErr},
|
|
|
|
publicIPAddressesAPI: stubPublicIPAddressesAPI{},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"cannot create public IP": {
|
|
|
|
publicIPAddressesAPI: stubPublicIPAddressesAPI{createErr: someErr},
|
|
|
|
loadBalancersAPI: stubLoadBalancersAPI{},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
client := Client{
|
|
|
|
resourceGroup: "resource-group",
|
|
|
|
location: "location",
|
|
|
|
name: "name",
|
|
|
|
uid: "uid",
|
2022-06-29 09:26:29 -04:00
|
|
|
workers: make(cloudtypes.Instances),
|
|
|
|
controlPlanes: make(cloudtypes.Instances),
|
2022-05-24 04:04:42 -04:00
|
|
|
loadBalancersAPI: tc.loadBalancersAPI,
|
|
|
|
publicIPAddressesAPI: tc.publicIPAddressesAPI,
|
|
|
|
}
|
|
|
|
|
|
|
|
err := client.CreateExternalLoadBalancer(ctx)
|
|
|
|
if tc.wantErr {
|
|
|
|
assert.Error(err)
|
|
|
|
} else {
|
|
|
|
assert.NoError(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|