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 client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
2022-08-01 08:58:23 -04:00
|
|
|
"net/http"
|
2022-03-22 11:03:15 -04:00
|
|
|
"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"
|
2022-08-01 08:58:23 -04:00
|
|
|
"google.golang.org/api/googleapi"
|
2022-03-22 11:03:15 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestCreateVPCs(t *testing.T) {
|
|
|
|
someErr := errors.New("failed")
|
|
|
|
|
|
|
|
testCases := map[string]struct {
|
|
|
|
operationGlobalAPI operationGlobalAPI
|
|
|
|
operationRegionAPI operationRegionAPI
|
|
|
|
networksAPI networksAPI
|
|
|
|
subnetworksAPI subnetworksAPI
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr bool
|
2022-03-22 11:03:15 -04:00
|
|
|
}{
|
|
|
|
"successful create": {
|
|
|
|
operationGlobalAPI: stubOperationGlobalAPI{},
|
|
|
|
operationRegionAPI: stubOperationRegionAPI{},
|
|
|
|
networksAPI: stubNetworksAPI{},
|
|
|
|
subnetworksAPI: stubSubnetworksAPI{},
|
|
|
|
},
|
|
|
|
"failed wait global op": {
|
|
|
|
operationGlobalAPI: stubOperationGlobalAPI{waitErr: someErr},
|
|
|
|
operationRegionAPI: stubOperationRegionAPI{},
|
|
|
|
networksAPI: stubNetworksAPI{},
|
|
|
|
subnetworksAPI: stubSubnetworksAPI{},
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"failed wait region op": {
|
|
|
|
operationGlobalAPI: stubOperationGlobalAPI{},
|
|
|
|
operationRegionAPI: stubOperationRegionAPI{waitErr: someErr},
|
|
|
|
networksAPI: stubNetworksAPI{},
|
|
|
|
subnetworksAPI: stubSubnetworksAPI{},
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"failed insert networks": {
|
|
|
|
operationGlobalAPI: stubOperationGlobalAPI{},
|
|
|
|
operationRegionAPI: stubOperationRegionAPI{},
|
|
|
|
networksAPI: stubNetworksAPI{insertErr: someErr},
|
|
|
|
subnetworksAPI: stubSubnetworksAPI{},
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"failed insert subnetworks": {
|
|
|
|
operationGlobalAPI: stubOperationGlobalAPI{},
|
|
|
|
operationRegionAPI: stubOperationRegionAPI{},
|
|
|
|
networksAPI: stubNetworksAPI{},
|
|
|
|
subnetworksAPI: stubSubnetworksAPI{insertErr: 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{
|
|
|
|
project: "project",
|
|
|
|
zone: "zone",
|
|
|
|
name: "name",
|
|
|
|
uid: "uid",
|
|
|
|
operationGlobalAPI: tc.operationGlobalAPI,
|
|
|
|
operationRegionAPI: tc.operationRegionAPI,
|
|
|
|
networksAPI: tc.networksAPI,
|
|
|
|
subnetworksAPI: tc.subnetworksAPI,
|
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-05-16 12:54:25 -04:00
|
|
|
assert.Error(client.CreateVPCs(ctx))
|
2022-03-22 11:03:15 -04:00
|
|
|
} else {
|
2022-05-16 12:54:25 -04:00
|
|
|
assert.NoError(client.CreateVPCs(ctx))
|
2022-03-22 11:03:15 -04:00
|
|
|
assert.NotNil(client.network)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTerminateVPCs(t *testing.T) {
|
|
|
|
someErr := errors.New("failed")
|
2022-08-01 08:58:23 -04:00
|
|
|
notFoundErr := &googleapi.Error{Code: http.StatusNotFound}
|
|
|
|
|
2022-03-22 11:03:15 -04:00
|
|
|
testCases := map[string]struct {
|
|
|
|
operationGlobalAPI operationGlobalAPI
|
|
|
|
operationRegionAPI operationRegionAPI
|
|
|
|
networksAPI networksAPI
|
|
|
|
subnetworksAPI subnetworksAPI
|
|
|
|
firewalls []string
|
2022-04-26 07:22:57 -04:00
|
|
|
subnetwork string
|
2022-08-01 08:58:23 -04:00
|
|
|
network string
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr bool
|
2022-03-22 11:03:15 -04:00
|
|
|
}{
|
|
|
|
"successful terminate": {
|
|
|
|
operationGlobalAPI: stubOperationGlobalAPI{},
|
|
|
|
operationRegionAPI: stubOperationRegionAPI{},
|
|
|
|
networksAPI: stubNetworksAPI{},
|
|
|
|
subnetworksAPI: stubSubnetworksAPI{},
|
2022-04-26 07:22:57 -04:00
|
|
|
subnetwork: "subnetwork-id-1",
|
2022-08-01 08:58:23 -04:00
|
|
|
network: "network-id-1",
|
2022-04-26 07:22:57 -04:00
|
|
|
},
|
|
|
|
"subnetwork empty": {
|
|
|
|
operationGlobalAPI: stubOperationGlobalAPI{},
|
|
|
|
operationRegionAPI: stubOperationRegionAPI{},
|
|
|
|
networksAPI: stubNetworksAPI{},
|
|
|
|
subnetworksAPI: stubSubnetworksAPI{},
|
|
|
|
subnetwork: "",
|
2022-08-01 08:58:23 -04:00
|
|
|
network: "network-id-1",
|
|
|
|
},
|
|
|
|
"network empty": {
|
|
|
|
operationGlobalAPI: stubOperationGlobalAPI{},
|
|
|
|
operationRegionAPI: stubOperationRegionAPI{},
|
|
|
|
networksAPI: stubNetworksAPI{},
|
|
|
|
subnetworksAPI: stubSubnetworksAPI{},
|
|
|
|
subnetwork: "subnetwork-id-1",
|
|
|
|
network: "",
|
|
|
|
},
|
|
|
|
"subnetwork not found": {
|
|
|
|
operationGlobalAPI: stubOperationGlobalAPI{},
|
|
|
|
operationRegionAPI: stubOperationRegionAPI{},
|
|
|
|
networksAPI: stubNetworksAPI{},
|
|
|
|
subnetworksAPI: stubSubnetworksAPI{deleteErr: notFoundErr},
|
|
|
|
subnetwork: "subnetwork-id-1",
|
|
|
|
network: "network-id-1",
|
|
|
|
},
|
|
|
|
"network not found": {
|
|
|
|
operationGlobalAPI: stubOperationGlobalAPI{},
|
|
|
|
operationRegionAPI: stubOperationRegionAPI{},
|
|
|
|
networksAPI: stubNetworksAPI{deleteErr: notFoundErr},
|
|
|
|
subnetworksAPI: stubSubnetworksAPI{},
|
|
|
|
subnetwork: "subnetwork-id-1",
|
|
|
|
network: "network-id-1",
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"failed wait global op": {
|
|
|
|
operationGlobalAPI: stubOperationGlobalAPI{waitErr: someErr},
|
|
|
|
operationRegionAPI: stubOperationRegionAPI{},
|
|
|
|
networksAPI: stubNetworksAPI{},
|
|
|
|
subnetworksAPI: stubSubnetworksAPI{},
|
2022-04-26 07:22:57 -04:00
|
|
|
subnetwork: "subnetwork-id-1",
|
2022-08-01 08:58:23 -04:00
|
|
|
network: "network-id-1",
|
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"failed delete networks": {
|
|
|
|
operationGlobalAPI: stubOperationGlobalAPI{},
|
|
|
|
operationRegionAPI: stubOperationRegionAPI{},
|
|
|
|
networksAPI: stubNetworksAPI{deleteErr: someErr},
|
|
|
|
subnetworksAPI: stubSubnetworksAPI{},
|
2022-04-26 07:22:57 -04:00
|
|
|
subnetwork: "subnetwork-id-1",
|
2022-08-01 08:58:23 -04:00
|
|
|
network: "network-id-1",
|
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"failed delete subnetworks": {
|
|
|
|
operationGlobalAPI: stubOperationGlobalAPI{},
|
|
|
|
operationRegionAPI: stubOperationRegionAPI{},
|
|
|
|
networksAPI: stubNetworksAPI{},
|
|
|
|
subnetworksAPI: stubSubnetworksAPI{deleteErr: someErr},
|
2022-04-26 07:22:57 -04:00
|
|
|
subnetwork: "subnetwork-id-1",
|
2022-08-01 08:58:23 -04:00
|
|
|
network: "network-id-1",
|
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"must delete firewalls first": {
|
|
|
|
firewalls: []string{"firewall-1", "firewall-2"},
|
|
|
|
operationRegionAPI: stubOperationRegionAPI{},
|
|
|
|
operationGlobalAPI: stubOperationGlobalAPI{},
|
|
|
|
networksAPI: stubNetworksAPI{},
|
|
|
|
subnetworksAPI: stubSubnetworksAPI{},
|
2022-04-26 07:22:57 -04:00
|
|
|
subnetwork: "subnetwork-id-1",
|
2022-08-01 08:58:23 -04:00
|
|
|
network: "network-id-1",
|
|
|
|
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{
|
|
|
|
project: "project",
|
|
|
|
zone: "zone",
|
|
|
|
name: "name",
|
|
|
|
uid: "uid",
|
|
|
|
operationGlobalAPI: tc.operationGlobalAPI,
|
|
|
|
operationRegionAPI: tc.operationRegionAPI,
|
|
|
|
networksAPI: tc.networksAPI,
|
|
|
|
subnetworksAPI: tc.subnetworksAPI,
|
|
|
|
firewalls: tc.firewalls,
|
2022-08-01 08:58:23 -04:00
|
|
|
network: tc.network,
|
2022-04-26 07:22:57 -04:00
|
|
|
subnetwork: tc.subnetwork,
|
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.TerminateVPCs(ctx))
|
|
|
|
} else {
|
|
|
|
assert.NoError(client.TerminateVPCs(ctx))
|
|
|
|
assert.Empty(client.network)
|
2022-08-01 08:58:23 -04:00
|
|
|
assert.Empty(client.subnetwork)
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCreateFirewall(t *testing.T) {
|
|
|
|
someErr := errors.New("failed")
|
|
|
|
testFirewallInput := FirewallInput{
|
|
|
|
Ingress: cloudtypes.Firewall{
|
|
|
|
cloudtypes.FirewallRule{
|
|
|
|
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
|
|
|
},
|
|
|
|
cloudtypes.FirewallRule{
|
|
|
|
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 {
|
|
|
|
network string
|
|
|
|
operationGlobalAPI operationGlobalAPI
|
|
|
|
firewallsAPI firewallsAPI
|
|
|
|
firewallInput FirewallInput
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr bool
|
2022-03-22 11:03:15 -04:00
|
|
|
}{
|
|
|
|
"successful create": {
|
|
|
|
network: "network",
|
|
|
|
operationGlobalAPI: stubOperationGlobalAPI{},
|
|
|
|
firewallsAPI: stubFirewallsAPI{},
|
|
|
|
},
|
|
|
|
"failed wait global op": {
|
|
|
|
network: "network",
|
|
|
|
operationGlobalAPI: stubOperationGlobalAPI{waitErr: someErr},
|
|
|
|
firewallsAPI: stubFirewallsAPI{},
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"failed insert networks": {
|
|
|
|
network: "network",
|
|
|
|
operationGlobalAPI: stubOperationGlobalAPI{},
|
|
|
|
firewallsAPI: stubFirewallsAPI{insertErr: someErr},
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"no network set": {
|
|
|
|
operationGlobalAPI: stubOperationGlobalAPI{},
|
|
|
|
firewallsAPI: stubFirewallsAPI{},
|
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{
|
|
|
|
project: "project",
|
|
|
|
zone: "zone",
|
|
|
|
name: "name",
|
|
|
|
uid: "uid",
|
|
|
|
network: tc.network,
|
|
|
|
operationGlobalAPI: tc.operationGlobalAPI,
|
|
|
|
firewallsAPI: tc.firewallsAPI,
|
|
|
|
}
|
|
|
|
|
2022-04-26 10:54:05 -04:00
|
|
|
if tc.wantErr {
|
2022-03-22 11:03:15 -04:00
|
|
|
assert.Error(client.CreateFirewall(ctx, testFirewallInput))
|
|
|
|
} else {
|
|
|
|
assert.NoError(client.CreateFirewall(ctx, testFirewallInput))
|
|
|
|
assert.ElementsMatch([]string{"test-1", "test-2"}, client.firewalls)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTerminateFirewall(t *testing.T) {
|
|
|
|
someErr := errors.New("failed")
|
2022-08-01 08:58:23 -04:00
|
|
|
notFoundErr := &googleapi.Error{Code: http.StatusNotFound}
|
2022-03-22 11:03:15 -04:00
|
|
|
|
|
|
|
testCases := map[string]struct {
|
|
|
|
operationGlobalAPI operationGlobalAPI
|
|
|
|
firewallsAPI firewallsAPI
|
|
|
|
firewalls []string
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr bool
|
2022-03-22 11:03:15 -04:00
|
|
|
}{
|
|
|
|
"successful terminate": {
|
|
|
|
operationGlobalAPI: stubOperationGlobalAPI{},
|
|
|
|
firewallsAPI: stubFirewallsAPI{},
|
|
|
|
firewalls: []string{"firewall-1", "firewall-2"},
|
|
|
|
},
|
|
|
|
"successful terminate when no firewall exists": {
|
|
|
|
operationGlobalAPI: stubOperationGlobalAPI{},
|
|
|
|
firewallsAPI: stubFirewallsAPI{},
|
|
|
|
firewalls: []string{},
|
|
|
|
},
|
2022-08-01 08:58:23 -04:00
|
|
|
"successful terminate when firewall not found": {
|
|
|
|
operationGlobalAPI: stubOperationGlobalAPI{},
|
|
|
|
firewallsAPI: stubFirewallsAPI{deleteErr: notFoundErr},
|
|
|
|
firewalls: []string{"firewall-1", "firewall-2"},
|
|
|
|
},
|
2022-03-22 11:03:15 -04:00
|
|
|
"failed to wait on global operation": {
|
|
|
|
operationGlobalAPI: stubOperationGlobalAPI{waitErr: someErr},
|
|
|
|
firewallsAPI: stubFirewallsAPI{},
|
|
|
|
firewalls: []string{"firewall-1", "firewall-2"},
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"failed to delete firewalls": {
|
|
|
|
operationGlobalAPI: stubOperationGlobalAPI{},
|
|
|
|
firewallsAPI: stubFirewallsAPI{deleteErr: someErr},
|
|
|
|
firewalls: []string{"firewall-1", "firewall-2"},
|
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{
|
|
|
|
project: "project",
|
|
|
|
zone: "zone",
|
|
|
|
name: "name",
|
|
|
|
uid: "uid",
|
|
|
|
firewalls: tc.firewalls,
|
|
|
|
operationGlobalAPI: tc.operationGlobalAPI,
|
|
|
|
firewallsAPI: tc.firewallsAPI,
|
|
|
|
}
|
|
|
|
|
2022-04-26 10:54:05 -04:00
|
|
|
if tc.wantErr {
|
2022-03-22 11:03:15 -04:00
|
|
|
assert.Error(client.TerminateFirewall(ctx))
|
|
|
|
} else {
|
|
|
|
assert.NoError(client.TerminateFirewall(ctx))
|
|
|
|
assert.Empty(client.firewalls)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|