mirror of
https://github.com/edgelesssys/constellation.git
synced 2025-02-09 11:28:29 -05:00
Ref/want err from err expected (#82)
consistent naming for test values using 'want' instead of 'expect/ed'
This commit is contained in:
parent
6265b307af
commit
51068abc27
@ -9,6 +9,6 @@ import (
|
||||
func TestAutoscalingNodeGroup(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
nodeGroups := AutoscalingNodeGroup("scale-set", 0, 100)
|
||||
expectedNodeGroups := "0:100:scale-set"
|
||||
assert.Equal(expectedNodeGroups, nodeGroups)
|
||||
wantNodeGroups := "0:100:scale-set"
|
||||
assert.Equal(wantNodeGroups, nodeGroups)
|
||||
}
|
||||
|
@ -23,7 +23,7 @@ func TestCreateServicePrincipal(t *testing.T) {
|
||||
servicePrincipalsAPI servicePrincipalsAPI
|
||||
roleAssignmentsAPI roleAssignmentsAPI
|
||||
resourceGroupAPI resourceGroupAPI
|
||||
errExpected bool
|
||||
wantErr bool
|
||||
}{
|
||||
"successful create": {
|
||||
applicationsAPI: stubApplicationsAPI{},
|
||||
@ -39,14 +39,14 @@ func TestCreateServicePrincipal(t *testing.T) {
|
||||
applicationsAPI: stubApplicationsAPI{
|
||||
createErr: someErr,
|
||||
},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"failed service principal create": {
|
||||
applicationsAPI: stubApplicationsAPI{},
|
||||
servicePrincipalsAPI: stubServicePrincipalsAPI{
|
||||
createErr: someErr,
|
||||
},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"failed role assignment": {
|
||||
applicationsAPI: stubApplicationsAPI{},
|
||||
@ -59,7 +59,7 @@ func TestCreateServicePrincipal(t *testing.T) {
|
||||
ID: to.StringPtr("resource-group-id"),
|
||||
},
|
||||
},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"failed update creds": {
|
||||
applicationsAPI: stubApplicationsAPI{
|
||||
@ -72,7 +72,7 @@ func TestCreateServicePrincipal(t *testing.T) {
|
||||
ID: to.StringPtr("resource-group-id"),
|
||||
},
|
||||
},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
for name, tc := range testCases {
|
||||
@ -93,7 +93,7 @@ func TestCreateServicePrincipal(t *testing.T) {
|
||||
}
|
||||
|
||||
_, err := client.CreateServicePrincipal(ctx)
|
||||
if tc.errExpected {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
return
|
||||
}
|
||||
@ -107,7 +107,7 @@ func TestTerminateServicePrincipal(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
appObjectID string
|
||||
applicationsAPI applicationsAPI
|
||||
errExpected bool
|
||||
wantErr bool
|
||||
}{
|
||||
"successful terminate": {
|
||||
appObjectID: "object-id",
|
||||
@ -121,7 +121,7 @@ func TestTerminateServicePrincipal(t *testing.T) {
|
||||
applicationsAPI: stubApplicationsAPI{
|
||||
deleteErr: someErr,
|
||||
},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
for name, tc := range testCases {
|
||||
@ -139,7 +139,7 @@ func TestTerminateServicePrincipal(t *testing.T) {
|
||||
}
|
||||
|
||||
err := client.TerminateServicePrincipal(ctx)
|
||||
if tc.errExpected {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
return
|
||||
}
|
||||
@ -152,7 +152,7 @@ func TestCreateADApplication(t *testing.T) {
|
||||
someErr := errors.New("failed")
|
||||
testCases := map[string]struct {
|
||||
applicationsAPI applicationsAPI
|
||||
errExpected bool
|
||||
wantErr bool
|
||||
}{
|
||||
"successful create": {
|
||||
applicationsAPI: stubApplicationsAPI{},
|
||||
@ -161,7 +161,7 @@ func TestCreateADApplication(t *testing.T) {
|
||||
applicationsAPI: stubApplicationsAPI{
|
||||
createErr: someErr,
|
||||
},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"app create returns invalid appid": {
|
||||
applicationsAPI: stubApplicationsAPI{
|
||||
@ -169,7 +169,7 @@ func TestCreateADApplication(t *testing.T) {
|
||||
ObjectID: proto.String("00000000-0000-0000-0000-000000000001"),
|
||||
},
|
||||
},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"app create returns invalid objectid": {
|
||||
applicationsAPI: stubApplicationsAPI{
|
||||
@ -177,7 +177,7 @@ func TestCreateADApplication(t *testing.T) {
|
||||
AppID: proto.String("00000000-0000-0000-0000-000000000000"),
|
||||
},
|
||||
},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
for name, tc := range testCases {
|
||||
@ -193,7 +193,7 @@ func TestCreateADApplication(t *testing.T) {
|
||||
}
|
||||
|
||||
appCredentials, err := client.createADApplication(ctx)
|
||||
if tc.errExpected {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
return
|
||||
}
|
||||
@ -207,7 +207,7 @@ func TestCreateAppServicePrincipal(t *testing.T) {
|
||||
someErr := errors.New("failed")
|
||||
testCases := map[string]struct {
|
||||
servicePrincipalsAPI servicePrincipalsAPI
|
||||
errExpected bool
|
||||
wantErr bool
|
||||
}{
|
||||
"successful create": {
|
||||
servicePrincipalsAPI: stubServicePrincipalsAPI{},
|
||||
@ -216,13 +216,13 @@ func TestCreateAppServicePrincipal(t *testing.T) {
|
||||
servicePrincipalsAPI: stubServicePrincipalsAPI{
|
||||
createErr: someErr,
|
||||
},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"service principal create returns invalid objectid": {
|
||||
servicePrincipalsAPI: stubServicePrincipalsAPI{
|
||||
createServicePrincipal: &graphrbac.ServicePrincipal{},
|
||||
},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
for name, tc := range testCases {
|
||||
@ -238,7 +238,7 @@ func TestCreateAppServicePrincipal(t *testing.T) {
|
||||
}
|
||||
|
||||
_, err := client.createAppServicePrincipal(ctx, "app-id")
|
||||
if tc.errExpected {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
return
|
||||
}
|
||||
@ -252,7 +252,7 @@ func TestAssignOwnerOfResourceGroup(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
roleAssignmentsAPI roleAssignmentsAPI
|
||||
resourceGroupAPI resourceGroupAPI
|
||||
errExpected bool
|
||||
wantErr bool
|
||||
}{
|
||||
"successful assign": {
|
||||
roleAssignmentsAPI: &stubRoleAssignmentsAPI{},
|
||||
@ -271,21 +271,21 @@ func TestAssignOwnerOfResourceGroup(t *testing.T) {
|
||||
ID: to.StringPtr("resource-group-id"),
|
||||
},
|
||||
},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"failed resource group get": {
|
||||
roleAssignmentsAPI: &stubRoleAssignmentsAPI{},
|
||||
resourceGroupAPI: stubResourceGroupAPI{
|
||||
getErr: someErr,
|
||||
},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"resource group get returns invalid id": {
|
||||
roleAssignmentsAPI: &stubRoleAssignmentsAPI{},
|
||||
resourceGroupAPI: stubResourceGroupAPI{
|
||||
getResourceGroup: armresources.ResourceGroup{},
|
||||
},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"create returns PrincipalNotFound the first time": {
|
||||
roleAssignmentsAPI: &stubRoleAssignmentsAPI{
|
||||
@ -313,7 +313,7 @@ func TestAssignOwnerOfResourceGroup(t *testing.T) {
|
||||
ID: to.StringPtr("resource-group-id"),
|
||||
},
|
||||
},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"create service error code is unknown": {
|
||||
roleAssignmentsAPI: &stubRoleAssignmentsAPI{
|
||||
@ -331,7 +331,7 @@ func TestAssignOwnerOfResourceGroup(t *testing.T) {
|
||||
ID: to.StringPtr("resource-group-id"),
|
||||
},
|
||||
},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
for name, tc := range testCases {
|
||||
@ -350,7 +350,7 @@ func TestAssignOwnerOfResourceGroup(t *testing.T) {
|
||||
}
|
||||
|
||||
err := client.assignResourceGroupRole(ctx, "principal-id", "role-definition-id")
|
||||
if tc.errExpected {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
return
|
||||
}
|
||||
|
@ -12,8 +12,8 @@ import (
|
||||
|
||||
func TestSetGetState(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
state state.ConstellationState
|
||||
errExpected bool
|
||||
state state.ConstellationState
|
||||
wantErr bool
|
||||
}{
|
||||
"valid state": {
|
||||
state: state.ConstellationState{
|
||||
@ -62,7 +62,7 @@ func TestSetGetState(t *testing.T) {
|
||||
AzureNodesScaleSet: "node-scale-set",
|
||||
AzureCoordinatorsScaleSet: "coordinator-scale-set",
|
||||
},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"missing coordinator": {
|
||||
state: state.ConstellationState{
|
||||
@ -84,7 +84,7 @@ func TestSetGetState(t *testing.T) {
|
||||
AzureNodesScaleSet: "node-scale-set",
|
||||
AzureCoordinatorsScaleSet: "coordinator-scale-set",
|
||||
},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"missing name": {
|
||||
state: state.ConstellationState{
|
||||
@ -111,7 +111,7 @@ func TestSetGetState(t *testing.T) {
|
||||
AzureNodesScaleSet: "node-scale-set",
|
||||
AzureCoordinatorsScaleSet: "coordinator-scale-set",
|
||||
},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"missing uid": {
|
||||
state: state.ConstellationState{
|
||||
@ -138,7 +138,7 @@ func TestSetGetState(t *testing.T) {
|
||||
AzureNodesScaleSet: "node-scale-set",
|
||||
AzureCoordinatorsScaleSet: "coordinator-scale-set",
|
||||
},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"missing resource group": {
|
||||
state: state.ConstellationState{
|
||||
@ -165,7 +165,7 @@ func TestSetGetState(t *testing.T) {
|
||||
AzureNodesScaleSet: "node-scale-set",
|
||||
AzureCoordinatorsScaleSet: "coordinator-scale-set",
|
||||
},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"missing location": {
|
||||
state: state.ConstellationState{
|
||||
@ -192,7 +192,7 @@ func TestSetGetState(t *testing.T) {
|
||||
AzureNodesScaleSet: "node-scale-set",
|
||||
AzureCoordinatorsScaleSet: "coordinator-scale-set",
|
||||
},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"missing subscription": {
|
||||
state: state.ConstellationState{
|
||||
@ -219,7 +219,7 @@ func TestSetGetState(t *testing.T) {
|
||||
AzureNodesScaleSet: "node-scale-set",
|
||||
AzureCoordinatorsScaleSet: "coordinator-scale-set",
|
||||
},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"missing tenant": {
|
||||
state: state.ConstellationState{
|
||||
@ -246,7 +246,7 @@ func TestSetGetState(t *testing.T) {
|
||||
AzureNodesScaleSet: "node-scale-set",
|
||||
AzureCoordinatorsScaleSet: "coordinator-scale-set",
|
||||
},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"missing subnet": {
|
||||
state: state.ConstellationState{
|
||||
@ -273,7 +273,7 @@ func TestSetGetState(t *testing.T) {
|
||||
AzureNodesScaleSet: "node-scale-set",
|
||||
AzureCoordinatorsScaleSet: "coordinator-scale-set",
|
||||
},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"missing network security group": {
|
||||
state: state.ConstellationState{
|
||||
@ -300,7 +300,7 @@ func TestSetGetState(t *testing.T) {
|
||||
AzureNodesScaleSet: "node-scale-set",
|
||||
AzureCoordinatorsScaleSet: "coordinator-scale-set",
|
||||
},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"missing node scale set": {
|
||||
state: state.ConstellationState{
|
||||
@ -327,7 +327,7 @@ func TestSetGetState(t *testing.T) {
|
||||
AzureNetworkSecurityGroup: "network-security-group",
|
||||
AzureCoordinatorsScaleSet: "coordinator-scale-set",
|
||||
},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"missing coordinator scale set": {
|
||||
state: state.ConstellationState{
|
||||
@ -354,7 +354,7 @@ func TestSetGetState(t *testing.T) {
|
||||
AzureNetworkSecurityGroup: "network-security-group",
|
||||
AzureNodesScaleSet: "node-scale-set",
|
||||
},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -364,7 +364,7 @@ func TestSetGetState(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
client := Client{}
|
||||
if tc.errExpected {
|
||||
if tc.wantErr {
|
||||
assert.Error(client.SetState(tc.state))
|
||||
} else {
|
||||
assert.NoError(client.SetState(tc.state))
|
||||
@ -404,7 +404,7 @@ func TestSetGetState(t *testing.T) {
|
||||
nodesScaleSet: tc.state.AzureNodesScaleSet,
|
||||
coordinatorsScaleSet: tc.state.AzureCoordinatorsScaleSet,
|
||||
}
|
||||
if tc.errExpected {
|
||||
if tc.wantErr {
|
||||
_, err := client.GetState()
|
||||
assert.Error(err)
|
||||
} else {
|
||||
|
@ -17,14 +17,14 @@ func TestCreateResourceGroup(t *testing.T) {
|
||||
someErr := errors.New("failed")
|
||||
testCases := map[string]struct {
|
||||
resourceGroupAPI resourceGroupAPI
|
||||
errExpected bool
|
||||
wantErr bool
|
||||
}{
|
||||
"successful create": {
|
||||
resourceGroupAPI: stubResourceGroupAPI{},
|
||||
},
|
||||
"failed create": {
|
||||
resourceGroupAPI: stubResourceGroupAPI{createErr: someErr},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
for name, tc := range testCases {
|
||||
@ -42,7 +42,7 @@ func TestCreateResourceGroup(t *testing.T) {
|
||||
coordinators: make(azure.Instances),
|
||||
}
|
||||
|
||||
if tc.errExpected {
|
||||
if tc.wantErr {
|
||||
assert.Error(client.CreateResourceGroup(ctx))
|
||||
} else {
|
||||
assert.NoError(client.CreateResourceGroup(ctx))
|
||||
@ -77,7 +77,7 @@ func TestTerminateResourceGroup(t *testing.T) {
|
||||
resourceGroup string
|
||||
resourceGroupAPI resourceGroupAPI
|
||||
client Client
|
||||
errExpected bool
|
||||
wantErr bool
|
||||
}{
|
||||
"successful terminate": {
|
||||
resourceGroupAPI: stubResourceGroupAPI{},
|
||||
@ -91,12 +91,12 @@ func TestTerminateResourceGroup(t *testing.T) {
|
||||
"failed terminate": {
|
||||
resourceGroupAPI: stubResourceGroupAPI{terminateErr: someErr},
|
||||
client: clientWithResourceGroup,
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"failed to poll terminate response": {
|
||||
resourceGroupAPI: stubResourceGroupAPI{stubResponse: stubResourceGroupsDeletePollerResponse{pollerErr: someErr}},
|
||||
client: clientWithResourceGroup,
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
for name, tc := range testCases {
|
||||
@ -105,7 +105,7 @@ func TestTerminateResourceGroup(t *testing.T) {
|
||||
tc.client.resourceGroupAPI = tc.resourceGroupAPI
|
||||
ctx := context.Background()
|
||||
|
||||
if tc.errExpected {
|
||||
if tc.wantErr {
|
||||
assert.Error(tc.client.TerminateResourceGroup(ctx))
|
||||
return
|
||||
}
|
||||
@ -129,7 +129,7 @@ func TestCreateInstances(t *testing.T) {
|
||||
resourceGroupAPI resourceGroupAPI
|
||||
roleAssignmentsAPI roleAssignmentsAPI
|
||||
createInstancesInput CreateInstancesInput
|
||||
errExpected bool
|
||||
wantErr bool
|
||||
}{
|
||||
"successful create": {
|
||||
publicIPAddressesAPI: stubPublicIPAddressesAPI{},
|
||||
@ -166,7 +166,7 @@ func TestCreateInstances(t *testing.T) {
|
||||
Image: "image",
|
||||
UserAssingedIdentity: "identity",
|
||||
},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"error when polling create scale set response": {
|
||||
publicIPAddressesAPI: stubPublicIPAddressesAPI{},
|
||||
@ -181,7 +181,7 @@ func TestCreateInstances(t *testing.T) {
|
||||
Image: "image",
|
||||
UserAssingedIdentity: "identity",
|
||||
},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"error when retrieving private IPs": {
|
||||
publicIPAddressesAPI: stubPublicIPAddressesAPI{},
|
||||
@ -195,7 +195,7 @@ func TestCreateInstances(t *testing.T) {
|
||||
Image: "image",
|
||||
UserAssingedIdentity: "identity",
|
||||
},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
for name, tc := range testCases {
|
||||
@ -218,7 +218,7 @@ func TestCreateInstances(t *testing.T) {
|
||||
coordinators: make(azure.Instances),
|
||||
}
|
||||
|
||||
if tc.errExpected {
|
||||
if tc.wantErr {
|
||||
assert.Error(client.CreateInstances(ctx, tc.createInstancesInput))
|
||||
} else {
|
||||
assert.NoError(client.CreateInstances(ctx, tc.createInstancesInput))
|
||||
@ -243,7 +243,7 @@ func TestCreateInstancesVMs(t *testing.T) {
|
||||
resourceGroupAPI resourceGroupAPI
|
||||
roleAssignmentsAPI roleAssignmentsAPI
|
||||
createInstancesInput CreateInstancesInput
|
||||
errExpected bool
|
||||
wantErr bool
|
||||
}{
|
||||
"successful create": {
|
||||
publicIPAddressesAPI: stubPublicIPAddressesAPI{},
|
||||
@ -278,7 +278,7 @@ func TestCreateInstancesVMs(t *testing.T) {
|
||||
InstanceType: "type",
|
||||
Image: "image",
|
||||
},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"error when polling create scale set response": {
|
||||
publicIPAddressesAPI: stubPublicIPAddressesAPI{},
|
||||
@ -292,7 +292,7 @@ func TestCreateInstancesVMs(t *testing.T) {
|
||||
InstanceType: "type",
|
||||
Image: "image",
|
||||
},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"error when creating NIC": {
|
||||
publicIPAddressesAPI: stubPublicIPAddressesAPI{},
|
||||
@ -306,7 +306,7 @@ func TestCreateInstancesVMs(t *testing.T) {
|
||||
InstanceType: "type",
|
||||
Image: "image",
|
||||
},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"error when creating public IP": {
|
||||
publicIPAddressesAPI: stubPublicIPAddressesAPI{createErr: someErr},
|
||||
@ -320,7 +320,7 @@ func TestCreateInstancesVMs(t *testing.T) {
|
||||
InstanceType: "type",
|
||||
Image: "image",
|
||||
},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"error when retrieving public IP": {
|
||||
publicIPAddressesAPI: stubPublicIPAddressesAPI{getErr: someErr},
|
||||
@ -334,7 +334,7 @@ func TestCreateInstancesVMs(t *testing.T) {
|
||||
InstanceType: "type",
|
||||
Image: "image",
|
||||
},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
for name, tc := range testCases {
|
||||
@ -358,7 +358,7 @@ func TestCreateInstancesVMs(t *testing.T) {
|
||||
coordinators: make(azure.Instances),
|
||||
}
|
||||
|
||||
if tc.errExpected {
|
||||
if tc.wantErr {
|
||||
assert.Error(client.CreateInstancesVMs(ctx, tc.createInstancesInput))
|
||||
return
|
||||
}
|
||||
|
@ -14,18 +14,18 @@ func TestCreateVirtualNetwork(t *testing.T) {
|
||||
someErr := errors.New("failed")
|
||||
testCases := map[string]struct {
|
||||
networksAPI networksAPI
|
||||
errExpected bool
|
||||
wantErr bool
|
||||
}{
|
||||
"successful create": {
|
||||
networksAPI: stubNetworksAPI{},
|
||||
},
|
||||
"failed to get response from successful create": {
|
||||
networksAPI: stubNetworksAPI{stubResponse: stubVirtualNetworksCreateOrUpdatePollerResponse{pollerErr: someErr}},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"failed create": {
|
||||
networksAPI: stubNetworksAPI{createErr: someErr},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -44,7 +44,7 @@ func TestCreateVirtualNetwork(t *testing.T) {
|
||||
coordinators: make(azure.Instances),
|
||||
}
|
||||
|
||||
if tc.errExpected {
|
||||
if tc.wantErr {
|
||||
assert.Error(client.CreateVirtualNetwork(ctx))
|
||||
} else {
|
||||
assert.NoError(client.CreateVirtualNetwork(ctx))
|
||||
@ -78,18 +78,18 @@ func TestCreateSecurityGroup(t *testing.T) {
|
||||
|
||||
testCases := map[string]struct {
|
||||
networkSecurityGroupsAPI networkSecurityGroupsAPI
|
||||
errExpected bool
|
||||
wantErr bool
|
||||
}{
|
||||
"successful create": {
|
||||
networkSecurityGroupsAPI: stubNetworkSecurityGroupsAPI{},
|
||||
},
|
||||
"failed to get response from successful create": {
|
||||
networkSecurityGroupsAPI: stubNetworkSecurityGroupsAPI{stubPoller: stubNetworkSecurityGroupsCreateOrUpdatePollerResponse{pollerErr: someErr}},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"failed create": {
|
||||
networkSecurityGroupsAPI: stubNetworkSecurityGroupsAPI{createErr: someErr},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -108,7 +108,7 @@ func TestCreateSecurityGroup(t *testing.T) {
|
||||
networkSecurityGroupsAPI: tc.networkSecurityGroupsAPI,
|
||||
}
|
||||
|
||||
if tc.errExpected {
|
||||
if tc.wantErr {
|
||||
assert.Error(client.CreateSecurityGroup(ctx, testNetworkSecurityGroupInput))
|
||||
} else {
|
||||
assert.NoError(client.CreateSecurityGroup(ctx, testNetworkSecurityGroupInput))
|
||||
@ -126,7 +126,7 @@ func TestCreateNIC(t *testing.T) {
|
||||
networkInterfacesAPI networkInterfacesAPI
|
||||
name string
|
||||
publicIPAddressID string
|
||||
errExpected bool
|
||||
wantErr bool
|
||||
}{
|
||||
"successful create": {
|
||||
networkInterfacesAPI: stubNetworkInterfacesAPI{},
|
||||
@ -135,11 +135,11 @@ func TestCreateNIC(t *testing.T) {
|
||||
},
|
||||
"failed to get response from successful create": {
|
||||
networkInterfacesAPI: stubNetworkInterfacesAPI{stubResp: stubInterfacesClientCreateOrUpdatePollerResponse{pollErr: someErr}},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"failed create": {
|
||||
networkInterfacesAPI: stubNetworkInterfacesAPI{createErr: someErr},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -159,7 +159,7 @@ func TestCreateNIC(t *testing.T) {
|
||||
}
|
||||
|
||||
ip, id, err := client.createNIC(ctx, tc.name, tc.publicIPAddressID)
|
||||
if tc.errExpected {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
} else {
|
||||
assert.NoError(err)
|
||||
@ -177,7 +177,7 @@ func TestCreatePublicIPAddress(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
publicIPAddressesAPI publicIPAddressesAPI
|
||||
name string
|
||||
errExpected bool
|
||||
wantErr bool
|
||||
}{
|
||||
"successful create": {
|
||||
publicIPAddressesAPI: stubPublicIPAddressesAPI{},
|
||||
@ -185,11 +185,11 @@ func TestCreatePublicIPAddress(t *testing.T) {
|
||||
},
|
||||
"failed to get response from successful create": {
|
||||
publicIPAddressesAPI: stubPublicIPAddressesAPI{stubCreateResponse: stubPublicIPAddressesClientCreateOrUpdatePollerResponse{pollErr: someErr}},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"failed create": {
|
||||
publicIPAddressesAPI: stubPublicIPAddressesAPI{createErr: someErr},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -209,7 +209,7 @@ func TestCreatePublicIPAddress(t *testing.T) {
|
||||
}
|
||||
|
||||
id, err := client.createPublicIPAddress(ctx, tc.name)
|
||||
if tc.errExpected {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
} else {
|
||||
assert.NoError(err)
|
||||
|
@ -9,22 +9,22 @@ import (
|
||||
func TestIDs(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
testState := testInstances()
|
||||
expectedIDs := []string{"id-9", "id-10", "id-11", "id-12"}
|
||||
assert.ElementsMatch(expectedIDs, testState.IDs())
|
||||
wantIDs := []string{"id-9", "id-10", "id-11", "id-12"}
|
||||
assert.ElementsMatch(wantIDs, testState.IDs())
|
||||
}
|
||||
|
||||
func TestPublicIPs(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
testState := testInstances()
|
||||
expectedIPs := []string{"192.0.2.1", "192.0.2.3", "192.0.2.5", "192.0.2.7"}
|
||||
assert.ElementsMatch(expectedIPs, testState.PublicIPs())
|
||||
wantIPs := []string{"192.0.2.1", "192.0.2.3", "192.0.2.5", "192.0.2.7"}
|
||||
assert.ElementsMatch(wantIPs, testState.PublicIPs())
|
||||
}
|
||||
|
||||
func TestPrivateIPs(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
testState := testInstances()
|
||||
expectedIPs := []string{"192.0.2.2", "192.0.2.4", "192.0.2.6", "192.0.2.8"}
|
||||
assert.ElementsMatch(expectedIPs, testState.PrivateIPs())
|
||||
wantIPs := []string{"192.0.2.2", "192.0.2.4", "192.0.2.6", "192.0.2.8"}
|
||||
assert.ElementsMatch(wantIPs, testState.PrivateIPs())
|
||||
}
|
||||
|
||||
func TestGetOne(t *testing.T) {
|
||||
@ -43,9 +43,9 @@ func TestGetOthers(t *testing.T) {
|
||||
for _, id := range testCases {
|
||||
others := testInstances().GetOthers(id)
|
||||
assert.NotContains(others, id)
|
||||
expectedInstances := testInstances()
|
||||
delete(expectedInstances, id)
|
||||
assert.ElementsMatch(others.IDs(), expectedInstances.IDs())
|
||||
wantInstances := testInstances()
|
||||
delete(wantInstances, id)
|
||||
assert.ElementsMatch(others.IDs(), wantInstances.IDs())
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -75,7 +75,7 @@ func TestFirewallAzure(t *testing.T) {
|
||||
Port: 4433,
|
||||
},
|
||||
}
|
||||
expectedOutput := []*armnetwork.SecurityRule{
|
||||
wantOutput := []*armnetwork.SecurityRule{
|
||||
{
|
||||
Name: proto.String("perm1"),
|
||||
Properties: &armnetwork.SecurityRulePropertiesFormat{
|
||||
@ -121,7 +121,7 @@ func TestFirewallAzure(t *testing.T) {
|
||||
}
|
||||
|
||||
out := input.Azure()
|
||||
assert.Equal(expectedOutput, out)
|
||||
assert.Equal(wantOutput, out)
|
||||
}
|
||||
|
||||
func TestIPPermissonsToAWS(t *testing.T) {
|
||||
@ -147,7 +147,7 @@ func TestIPPermissonsToAWS(t *testing.T) {
|
||||
Port: 4433,
|
||||
},
|
||||
}
|
||||
expectedOutput := []ec2types.IpPermission{
|
||||
wantOutput := []ec2types.IpPermission{
|
||||
{
|
||||
FromPort: proto.Int32(int32(22)),
|
||||
ToPort: proto.Int32(int32(22)),
|
||||
@ -184,5 +184,5 @@ func TestIPPermissonsToAWS(t *testing.T) {
|
||||
}
|
||||
|
||||
out := input.AWS()
|
||||
assert.Equal(expectedOutput, out)
|
||||
assert.Equal(wantOutput, out)
|
||||
}
|
||||
|
@ -77,7 +77,7 @@ func TestInitialize(t *testing.T) {
|
||||
privKey string
|
||||
vpnHandler vpnHandler
|
||||
initVPN bool
|
||||
errExpected bool
|
||||
wantErr bool
|
||||
}{
|
||||
"initialize some gcp instances": {
|
||||
existingState: testGcpState,
|
||||
@ -112,33 +112,33 @@ func TestInitialize(t *testing.T) {
|
||||
client: &fakeProtoClient{
|
||||
respClient: &fakeActivationRespClient{responses: testActivationResps},
|
||||
},
|
||||
waiter: &stubStatusWaiter{},
|
||||
vpnHandler: &stubVPNHandler{applyErr: someErr},
|
||||
initVPN: true,
|
||||
privKey: testKey,
|
||||
errExpected: true,
|
||||
waiter: &stubStatusWaiter{},
|
||||
vpnHandler: &stubVPNHandler{applyErr: someErr},
|
||||
initVPN: true,
|
||||
privKey: testKey,
|
||||
wantErr: true,
|
||||
},
|
||||
"invalid create vpn config": {
|
||||
existingState: testAzureState,
|
||||
client: &fakeProtoClient{
|
||||
respClient: &fakeActivationRespClient{responses: testActivationResps},
|
||||
},
|
||||
waiter: &stubStatusWaiter{},
|
||||
vpnHandler: &stubVPNHandler{createErr: someErr},
|
||||
initVPN: true,
|
||||
privKey: testKey,
|
||||
errExpected: true,
|
||||
waiter: &stubStatusWaiter{},
|
||||
vpnHandler: &stubVPNHandler{createErr: someErr},
|
||||
initVPN: true,
|
||||
privKey: testKey,
|
||||
wantErr: true,
|
||||
},
|
||||
"invalid write vpn config": {
|
||||
existingState: testAzureState,
|
||||
client: &fakeProtoClient{
|
||||
respClient: &fakeActivationRespClient{responses: testActivationResps},
|
||||
},
|
||||
waiter: &stubStatusWaiter{},
|
||||
vpnHandler: &stubVPNHandler{marshalErr: someErr},
|
||||
initVPN: true,
|
||||
privKey: testKey,
|
||||
errExpected: true,
|
||||
waiter: &stubStatusWaiter{},
|
||||
vpnHandler: &stubVPNHandler{marshalErr: someErr},
|
||||
initVPN: true,
|
||||
privKey: testKey,
|
||||
wantErr: true,
|
||||
},
|
||||
"no state exists": {
|
||||
existingState: state.ConstellationState{},
|
||||
@ -146,18 +146,18 @@ func TestInitialize(t *testing.T) {
|
||||
waiter: &stubStatusWaiter{},
|
||||
privKey: testKey,
|
||||
vpnHandler: &stubVPNHandler{},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"no instances to pick one": {
|
||||
existingState: state.ConstellationState{
|
||||
EC2Instances: ec2.Instances{},
|
||||
EC2SecurityGroup: "sg-test",
|
||||
},
|
||||
client: &stubProtoClient{},
|
||||
waiter: &stubStatusWaiter{},
|
||||
privKey: testKey,
|
||||
vpnHandler: &stubVPNHandler{},
|
||||
errExpected: true,
|
||||
client: &stubProtoClient{},
|
||||
waiter: &stubStatusWaiter{},
|
||||
privKey: testKey,
|
||||
vpnHandler: &stubVPNHandler{},
|
||||
wantErr: true,
|
||||
},
|
||||
"public key to short": {
|
||||
existingState: testGcpState,
|
||||
@ -165,7 +165,7 @@ func TestInitialize(t *testing.T) {
|
||||
waiter: &stubStatusWaiter{},
|
||||
privKey: base64.StdEncoding.EncodeToString([]byte("tooShortKey")),
|
||||
vpnHandler: &stubVPNHandler{},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"public key to long": {
|
||||
existingState: testGcpState,
|
||||
@ -173,7 +173,7 @@ func TestInitialize(t *testing.T) {
|
||||
waiter: &stubStatusWaiter{},
|
||||
privKey: base64.StdEncoding.EncodeToString([]byte("thisWireguardKeyIsToLongAndHasTooManyBytes")),
|
||||
vpnHandler: &stubVPNHandler{},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"public key not base64": {
|
||||
existingState: testGcpState,
|
||||
@ -181,7 +181,7 @@ func TestInitialize(t *testing.T) {
|
||||
waiter: &stubStatusWaiter{},
|
||||
privKey: "this is not base64 encoded",
|
||||
vpnHandler: &stubVPNHandler{},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"fail Connect": {
|
||||
existingState: testGcpState,
|
||||
@ -189,7 +189,7 @@ func TestInitialize(t *testing.T) {
|
||||
waiter: &stubStatusWaiter{},
|
||||
privKey: testKey,
|
||||
vpnHandler: &stubVPNHandler{},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"fail Activate": {
|
||||
existingState: testGcpState,
|
||||
@ -197,7 +197,7 @@ func TestInitialize(t *testing.T) {
|
||||
waiter: &stubStatusWaiter{},
|
||||
privKey: testKey,
|
||||
vpnHandler: &stubVPNHandler{},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"fail respClient WriteLogStream": {
|
||||
existingState: testGcpState,
|
||||
@ -205,7 +205,7 @@ func TestInitialize(t *testing.T) {
|
||||
waiter: &stubStatusWaiter{},
|
||||
privKey: testKey,
|
||||
vpnHandler: &stubVPNHandler{},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"fail respClient getKubeconfig": {
|
||||
existingState: testGcpState,
|
||||
@ -213,7 +213,7 @@ func TestInitialize(t *testing.T) {
|
||||
waiter: &stubStatusWaiter{},
|
||||
privKey: testKey,
|
||||
vpnHandler: &stubVPNHandler{},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"fail respClient getCoordinatorVpnKey": {
|
||||
existingState: testGcpState,
|
||||
@ -221,7 +221,7 @@ func TestInitialize(t *testing.T) {
|
||||
waiter: &stubStatusWaiter{},
|
||||
privKey: testKey,
|
||||
vpnHandler: &stubVPNHandler{},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"fail respClient getClientVpnIp": {
|
||||
existingState: testGcpState,
|
||||
@ -229,7 +229,7 @@ func TestInitialize(t *testing.T) {
|
||||
waiter: &stubStatusWaiter{},
|
||||
privKey: testKey,
|
||||
vpnHandler: &stubVPNHandler{},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"fail respClient getOwnerID": {
|
||||
existingState: testGcpState,
|
||||
@ -237,7 +237,7 @@ func TestInitialize(t *testing.T) {
|
||||
waiter: &stubStatusWaiter{},
|
||||
privKey: testKey,
|
||||
vpnHandler: &stubVPNHandler{},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"fail respClient getClusterID": {
|
||||
existingState: testGcpState,
|
||||
@ -245,7 +245,7 @@ func TestInitialize(t *testing.T) {
|
||||
waiter: &stubStatusWaiter{},
|
||||
privKey: testKey,
|
||||
vpnHandler: &stubVPNHandler{},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"fail to wait for required status": {
|
||||
existingState: testGcpState,
|
||||
@ -253,7 +253,7 @@ func TestInitialize(t *testing.T) {
|
||||
waiter: &stubStatusWaiter{waitForAllErr: someErr},
|
||||
privKey: testKey,
|
||||
vpnHandler: &stubVPNHandler{},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"fail to create service account": {
|
||||
existingState: testGcpState,
|
||||
@ -262,7 +262,7 @@ func TestInitialize(t *testing.T) {
|
||||
waiter: &stubStatusWaiter{},
|
||||
privKey: testKey,
|
||||
vpnHandler: &stubVPNHandler{},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -294,7 +294,7 @@ func TestInitialize(t *testing.T) {
|
||||
|
||||
err := initialize(ctx, cmd, tc.client, &tc.serviceAccountCreator, fileHandler, tc.waiter, tc.vpnHandler)
|
||||
|
||||
if tc.errExpected {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
} else {
|
||||
require.NoError(err)
|
||||
@ -343,28 +343,28 @@ func TestIpsToEndpoints(t *testing.T) {
|
||||
|
||||
func TestInitCompletion(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
args []string
|
||||
toComplete string
|
||||
resultExpected []string
|
||||
shellCDExpected cobra.ShellCompDirective
|
||||
args []string
|
||||
toComplete string
|
||||
wantResult []string
|
||||
wantShellCD cobra.ShellCompDirective
|
||||
}{
|
||||
"first arg": {
|
||||
args: []string{},
|
||||
toComplete: "hello",
|
||||
resultExpected: []string{},
|
||||
shellCDExpected: cobra.ShellCompDirectiveDefault,
|
||||
args: []string{},
|
||||
toComplete: "hello",
|
||||
wantResult: []string{},
|
||||
wantShellCD: cobra.ShellCompDirectiveDefault,
|
||||
},
|
||||
"secnod arg": {
|
||||
args: []string{"23"},
|
||||
toComplete: "/test/h",
|
||||
resultExpected: []string{},
|
||||
shellCDExpected: cobra.ShellCompDirectiveError,
|
||||
args: []string{"23"},
|
||||
toComplete: "/test/h",
|
||||
wantResult: []string{},
|
||||
wantShellCD: cobra.ShellCompDirectiveError,
|
||||
},
|
||||
"third arg": {
|
||||
args: []string{"./file", "sth"},
|
||||
toComplete: "./file",
|
||||
resultExpected: []string{},
|
||||
shellCDExpected: cobra.ShellCompDirectiveError,
|
||||
args: []string{"./file", "sth"},
|
||||
toComplete: "./file",
|
||||
wantResult: []string{},
|
||||
wantShellCD: cobra.ShellCompDirectiveError,
|
||||
},
|
||||
}
|
||||
|
||||
@ -374,8 +374,8 @@ func TestInitCompletion(t *testing.T) {
|
||||
|
||||
cmd := &cobra.Command{}
|
||||
result, shellCD := initCompletion(cmd, tc.args, tc.toComplete)
|
||||
assert.Equal(tc.resultExpected, result)
|
||||
assert.Equal(tc.shellCDExpected, shellCD)
|
||||
assert.Equal(tc.wantResult, result)
|
||||
assert.Equal(tc.wantShellCD, shellCD)
|
||||
})
|
||||
}
|
||||
}
|
||||
@ -406,55 +406,55 @@ func TestReadOrGeneratedMasterSecret(t *testing.T) {
|
||||
filecontent string
|
||||
createFile bool
|
||||
fs func() afero.Fs
|
||||
errExpected bool
|
||||
wantErr bool
|
||||
}{
|
||||
"file with secret exists": {
|
||||
filename: "someSecret",
|
||||
filecontent: base64.StdEncoding.EncodeToString([]byte("ConstellationSecret")),
|
||||
createFile: true,
|
||||
fs: afero.NewMemMapFs,
|
||||
errExpected: false,
|
||||
wantErr: false,
|
||||
},
|
||||
"no file given": {
|
||||
filename: "",
|
||||
filecontent: "",
|
||||
fs: afero.NewMemMapFs,
|
||||
errExpected: false,
|
||||
wantErr: false,
|
||||
},
|
||||
"file does not exist": {
|
||||
filename: "nonExistingSecret",
|
||||
filecontent: "",
|
||||
createFile: false,
|
||||
fs: afero.NewMemMapFs,
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"file is empty": {
|
||||
filename: "emptySecret",
|
||||
filecontent: "",
|
||||
createFile: true,
|
||||
fs: afero.NewMemMapFs,
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"secret too short": {
|
||||
filename: "shortSecret",
|
||||
filecontent: base64.StdEncoding.EncodeToString([]byte("short")),
|
||||
createFile: true,
|
||||
fs: afero.NewMemMapFs,
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"secret not encoded": {
|
||||
filename: "unencodedSecret",
|
||||
filecontent: "Constellation",
|
||||
createFile: true,
|
||||
fs: afero.NewMemMapFs,
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"file not writeable": {
|
||||
filename: "",
|
||||
filecontent: "",
|
||||
createFile: false,
|
||||
fs: func() afero.Fs { return afero.NewReadOnlyFs(afero.NewMemMapFs()) },
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -472,7 +472,7 @@ func TestReadOrGeneratedMasterSecret(t *testing.T) {
|
||||
var out bytes.Buffer
|
||||
secret, err := readOrGeneratedMasterSecret(&out, fileHandler, tc.filename)
|
||||
|
||||
if tc.errExpected {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
} else {
|
||||
assert.NoError(err)
|
||||
|
@ -15,8 +15,8 @@ import (
|
||||
|
||||
func TestTerminateCmdArgumentValidation(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
args []string
|
||||
expectErr bool
|
||||
args []string
|
||||
wantErr bool
|
||||
}{
|
||||
"no args": {[]string{}, false},
|
||||
"some args": {[]string{"hello", "test"}, true},
|
||||
@ -30,7 +30,7 @@ func TestTerminateCmdArgumentValidation(t *testing.T) {
|
||||
cmd := newTerminateCmd()
|
||||
err := cmd.ValidateArgs(tc.args)
|
||||
|
||||
if tc.expectErr {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
} else {
|
||||
assert.NoError(err)
|
||||
|
@ -30,8 +30,8 @@ func TestAskToConfirm(t *testing.T) {
|
||||
}
|
||||
|
||||
testCases := map[string]struct {
|
||||
input string
|
||||
expectedErr error
|
||||
input string
|
||||
wantErr error
|
||||
}{
|
||||
"user confirms": {"y\n", nil},
|
||||
"user confirms long": {"yes\n", nil},
|
||||
@ -53,7 +53,7 @@ func TestAskToConfirm(t *testing.T) {
|
||||
cmd.SetIn(in)
|
||||
|
||||
err := cmd.Execute()
|
||||
assert.ErrorIs(err, tc.expectedErr)
|
||||
assert.ErrorIs(err, tc.wantErr)
|
||||
|
||||
output, err := io.ReadAll(out)
|
||||
assert.NoError(err)
|
||||
@ -66,10 +66,10 @@ func TestWarnAboutPCRs(t *testing.T) {
|
||||
zero := []byte("00000000000000000000000000000000")
|
||||
|
||||
testCases := map[string]struct {
|
||||
pcrs map[uint32][]byte
|
||||
dontWarnInit bool
|
||||
expectedWarnings []string
|
||||
errExpected bool
|
||||
pcrs map[uint32][]byte
|
||||
dontWarnInit bool
|
||||
wantWarnings []string
|
||||
wantErr bool
|
||||
}{
|
||||
"no warnings": {
|
||||
pcrs: map[uint32][]byte{
|
||||
@ -114,7 +114,7 @@ func TestWarnAboutPCRs(t *testing.T) {
|
||||
11: zero,
|
||||
12: zero,
|
||||
},
|
||||
expectedWarnings: []string{"BIOS"},
|
||||
wantWarnings: []string{"BIOS"},
|
||||
},
|
||||
"warn for OPROM": {
|
||||
pcrs: map[uint32][]byte{
|
||||
@ -128,7 +128,7 @@ func TestWarnAboutPCRs(t *testing.T) {
|
||||
11: zero,
|
||||
12: zero,
|
||||
},
|
||||
expectedWarnings: []string{"OPROM"},
|
||||
wantWarnings: []string{"OPROM"},
|
||||
},
|
||||
"warn for MBR": {
|
||||
pcrs: map[uint32][]byte{
|
||||
@ -142,7 +142,7 @@ func TestWarnAboutPCRs(t *testing.T) {
|
||||
11: zero,
|
||||
12: zero,
|
||||
},
|
||||
expectedWarnings: []string{"MBR"},
|
||||
wantWarnings: []string{"MBR"},
|
||||
},
|
||||
"warn for kernel": {
|
||||
pcrs: map[uint32][]byte{
|
||||
@ -156,7 +156,7 @@ func TestWarnAboutPCRs(t *testing.T) {
|
||||
11: zero,
|
||||
12: zero,
|
||||
},
|
||||
expectedWarnings: []string{"kernel"},
|
||||
wantWarnings: []string{"kernel"},
|
||||
},
|
||||
"warn for initrd": {
|
||||
pcrs: map[uint32][]byte{
|
||||
@ -170,7 +170,7 @@ func TestWarnAboutPCRs(t *testing.T) {
|
||||
11: zero,
|
||||
12: zero,
|
||||
},
|
||||
expectedWarnings: []string{"initrd"},
|
||||
wantWarnings: []string{"initrd"},
|
||||
},
|
||||
"warn for initialization": {
|
||||
pcrs: map[uint32][]byte{
|
||||
@ -184,8 +184,8 @@ func TestWarnAboutPCRs(t *testing.T) {
|
||||
9: zero,
|
||||
11: zero,
|
||||
},
|
||||
dontWarnInit: false,
|
||||
expectedWarnings: []string{"initialization"},
|
||||
dontWarnInit: false,
|
||||
wantWarnings: []string{"initialization"},
|
||||
},
|
||||
"don't warn for initialization": {
|
||||
pcrs: map[uint32][]byte{
|
||||
@ -203,7 +203,7 @@ func TestWarnAboutPCRs(t *testing.T) {
|
||||
},
|
||||
"multi warning": {
|
||||
pcrs: map[uint32][]byte{},
|
||||
expectedWarnings: []string{
|
||||
wantWarnings: []string{
|
||||
"BIOS",
|
||||
"OPROM",
|
||||
"MBR",
|
||||
@ -216,7 +216,7 @@ func TestWarnAboutPCRs(t *testing.T) {
|
||||
pcrs: map[uint32][]byte{
|
||||
0: []byte("000"),
|
||||
},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -231,14 +231,14 @@ func TestWarnAboutPCRs(t *testing.T) {
|
||||
cmd.SetErr(&errOut)
|
||||
|
||||
err := warnAboutPCRs(cmd, tc.pcrs, !tc.dontWarnInit)
|
||||
if tc.errExpected {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
} else {
|
||||
assert.NoError(err)
|
||||
if len(tc.expectedWarnings) == 0 {
|
||||
if len(tc.wantWarnings) == 0 {
|
||||
assert.Empty(errOut.String())
|
||||
} else {
|
||||
for _, warning := range tc.expectedWarnings {
|
||||
for _, warning := range tc.wantWarnings {
|
||||
assert.Contains(errOut.String(), warning)
|
||||
}
|
||||
}
|
||||
|
@ -9,8 +9,8 @@ import (
|
||||
|
||||
func TestIsIntArg(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
args []string
|
||||
expectErr bool
|
||||
args []string
|
||||
wantErr bool
|
||||
}{
|
||||
"valid int 1": {[]string{"1"}, false},
|
||||
"valid int 2": {[]string{"42"}, false},
|
||||
@ -31,7 +31,7 @@ func TestIsIntArg(t *testing.T) {
|
||||
|
||||
err := testCmd.ValidateArgs(tc.args)
|
||||
|
||||
if tc.expectErr {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
} else {
|
||||
assert.NoError(err)
|
||||
@ -42,8 +42,8 @@ func TestIsIntArg(t *testing.T) {
|
||||
|
||||
func TestIsIntGreaterArg(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
args []string
|
||||
expectErr bool
|
||||
args []string
|
||||
wantErr bool
|
||||
}{
|
||||
"valid int 1": {[]string{"13"}, false},
|
||||
"valid int 2": {[]string{"42"}, false},
|
||||
@ -61,7 +61,7 @@ func TestIsIntGreaterArg(t *testing.T) {
|
||||
|
||||
err := testCmd.ValidateArgs(tc.args)
|
||||
|
||||
if tc.expectErr {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
} else {
|
||||
assert.NoError(err)
|
||||
@ -72,8 +72,8 @@ func TestIsIntGreaterArg(t *testing.T) {
|
||||
|
||||
func TestIsIntGreaterZeroArg(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
args []string
|
||||
expectErr bool
|
||||
args []string
|
||||
wantErr bool
|
||||
}{
|
||||
"valid int 1": {[]string{"13"}, false},
|
||||
"valid int 2": {[]string{"42"}, false},
|
||||
@ -91,7 +91,7 @@ func TestIsIntGreaterZeroArg(t *testing.T) {
|
||||
|
||||
err := testCmd.ValidateArgs(tc.args)
|
||||
|
||||
if tc.expectErr {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
} else {
|
||||
assert.NoError(err)
|
||||
@ -102,8 +102,8 @@ func TestIsIntGreaterZeroArg(t *testing.T) {
|
||||
|
||||
func TestIsEC2InstanceType(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
args []string
|
||||
expectErr bool
|
||||
args []string
|
||||
wantErr bool
|
||||
}{
|
||||
"is instance type 1": {[]string{"4xl"}, false},
|
||||
"is instance type 2": {[]string{"12xlarge", "something else"}, false},
|
||||
@ -119,7 +119,7 @@ func TestIsEC2InstanceType(t *testing.T) {
|
||||
|
||||
err := testCmd.ValidateArgs(tc.args)
|
||||
|
||||
if tc.expectErr {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
} else {
|
||||
assert.NoError(err)
|
||||
@ -130,8 +130,8 @@ func TestIsEC2InstanceType(t *testing.T) {
|
||||
|
||||
func TestIsGCPInstanceType(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
args []string
|
||||
expectErr bool
|
||||
args []string
|
||||
wantErr bool
|
||||
}{
|
||||
"is instance type 1": {[]string{"n2d-standard-4"}, false},
|
||||
"is instance type 2": {[]string{"n2d-standard-16", "something else"}, false},
|
||||
@ -147,7 +147,7 @@ func TestIsGCPInstanceType(t *testing.T) {
|
||||
|
||||
err := testCmd.ValidateArgs(tc.args)
|
||||
|
||||
if tc.expectErr {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
} else {
|
||||
assert.NoError(err)
|
||||
@ -158,8 +158,8 @@ func TestIsGCPInstanceType(t *testing.T) {
|
||||
|
||||
func TestIsAzureInstanceType(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
args []string
|
||||
expectErr bool
|
||||
args []string
|
||||
wantErr bool
|
||||
}{
|
||||
"is instance type 1": {[]string{"Standard_DC2as_v5"}, false},
|
||||
"is instance type 2": {[]string{"Standard_DC8as_v5", "something else"}, false},
|
||||
@ -175,7 +175,7 @@ func TestIsAzureInstanceType(t *testing.T) {
|
||||
|
||||
err := testCmd.ValidateArgs(tc.args)
|
||||
|
||||
if tc.expectErr {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
} else {
|
||||
assert.NoError(err)
|
||||
@ -189,7 +189,7 @@ func TestIsInstanceTypeForProvider(t *testing.T) {
|
||||
typePos int
|
||||
providerPos int
|
||||
args []string
|
||||
expectErr bool
|
||||
wantErr bool
|
||||
}{
|
||||
"valid gcp type 1": {1, 0, []string{"gcp", "n2d-standard-4"}, false},
|
||||
"valid gcp type 2": {1, 0, []string{"gcp", "n2d-standard-16", "foo"}, false},
|
||||
@ -213,7 +213,7 @@ func TestIsInstanceTypeForProvider(t *testing.T) {
|
||||
|
||||
err := testCmd.ValidateArgs(tc.args)
|
||||
|
||||
if tc.expectErr {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
} else {
|
||||
assert.NoError(err)
|
||||
|
@ -11,29 +11,29 @@ import (
|
||||
|
||||
func TestGetAzureValidator(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
ownerID string
|
||||
clusterID string
|
||||
errExpected bool
|
||||
ownerID string
|
||||
clusterID string
|
||||
wantErr bool
|
||||
}{
|
||||
"no input": {
|
||||
ownerID: "",
|
||||
clusterID: "",
|
||||
errExpected: true,
|
||||
ownerID: "",
|
||||
clusterID: "",
|
||||
wantErr: true,
|
||||
},
|
||||
"unencoded secret ID": {
|
||||
ownerID: "owner-id",
|
||||
clusterID: base64.StdEncoding.EncodeToString([]byte("unique-id")),
|
||||
errExpected: true,
|
||||
ownerID: "owner-id",
|
||||
clusterID: base64.StdEncoding.EncodeToString([]byte("unique-id")),
|
||||
wantErr: true,
|
||||
},
|
||||
"unencoded cluster ID": {
|
||||
ownerID: base64.StdEncoding.EncodeToString([]byte("owner-id")),
|
||||
clusterID: "unique-id",
|
||||
errExpected: true,
|
||||
ownerID: base64.StdEncoding.EncodeToString([]byte("owner-id")),
|
||||
clusterID: "unique-id",
|
||||
wantErr: true,
|
||||
},
|
||||
"correct input": {
|
||||
ownerID: base64.StdEncoding.EncodeToString([]byte("owner-id")),
|
||||
clusterID: base64.StdEncoding.EncodeToString([]byte("unique-id")),
|
||||
errExpected: false,
|
||||
ownerID: base64.StdEncoding.EncodeToString([]byte("owner-id")),
|
||||
clusterID: base64.StdEncoding.EncodeToString([]byte("unique-id")),
|
||||
wantErr: false,
|
||||
},
|
||||
}
|
||||
|
||||
@ -56,7 +56,7 @@ func TestGetAzureValidator(t *testing.T) {
|
||||
0: []byte("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"),
|
||||
1: []byte("BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB"),
|
||||
})
|
||||
if tc.errExpected {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
} else {
|
||||
assert.NoError(err)
|
||||
|
@ -11,29 +11,29 @@ import (
|
||||
|
||||
func TestGetGCPNonCVMValidator(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
ownerID string
|
||||
clusterID string
|
||||
errExpected bool
|
||||
ownerID string
|
||||
clusterID string
|
||||
wantErr bool
|
||||
}{
|
||||
"no input": {
|
||||
ownerID: "",
|
||||
clusterID: "",
|
||||
errExpected: true,
|
||||
ownerID: "",
|
||||
clusterID: "",
|
||||
wantErr: true,
|
||||
},
|
||||
"unencoded secret ID": {
|
||||
ownerID: "owner-id",
|
||||
clusterID: base64.StdEncoding.EncodeToString([]byte("unique-id")),
|
||||
errExpected: true,
|
||||
ownerID: "owner-id",
|
||||
clusterID: base64.StdEncoding.EncodeToString([]byte("unique-id")),
|
||||
wantErr: true,
|
||||
},
|
||||
"unencoded cluster ID": {
|
||||
ownerID: base64.StdEncoding.EncodeToString([]byte("owner-id")),
|
||||
clusterID: "unique-id",
|
||||
errExpected: true,
|
||||
ownerID: base64.StdEncoding.EncodeToString([]byte("owner-id")),
|
||||
clusterID: "unique-id",
|
||||
wantErr: true,
|
||||
},
|
||||
"correct input": {
|
||||
ownerID: base64.StdEncoding.EncodeToString([]byte("owner-id")),
|
||||
clusterID: base64.StdEncoding.EncodeToString([]byte("unique-id")),
|
||||
errExpected: false,
|
||||
ownerID: base64.StdEncoding.EncodeToString([]byte("owner-id")),
|
||||
clusterID: base64.StdEncoding.EncodeToString([]byte("unique-id")),
|
||||
wantErr: false,
|
||||
},
|
||||
}
|
||||
|
||||
@ -53,7 +53,7 @@ func TestGetGCPNonCVMValidator(t *testing.T) {
|
||||
cmd.SetErr(&errOut)
|
||||
|
||||
_, err := getGCPNonCVMValidator(cmd, map[uint32][]byte{})
|
||||
if tc.errExpected {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
} else {
|
||||
assert.NoError(err)
|
||||
|
@ -11,29 +11,29 @@ import (
|
||||
|
||||
func TestGetGCPValidator(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
ownerID string
|
||||
clusterID string
|
||||
errExpected bool
|
||||
ownerID string
|
||||
clusterID string
|
||||
wantErr bool
|
||||
}{
|
||||
"no input": {
|
||||
ownerID: "",
|
||||
clusterID: "",
|
||||
errExpected: true,
|
||||
ownerID: "",
|
||||
clusterID: "",
|
||||
wantErr: true,
|
||||
},
|
||||
"unencoded secret ID": {
|
||||
ownerID: "owner-id",
|
||||
clusterID: base64.StdEncoding.EncodeToString([]byte("unique-id")),
|
||||
errExpected: true,
|
||||
ownerID: "owner-id",
|
||||
clusterID: base64.StdEncoding.EncodeToString([]byte("unique-id")),
|
||||
wantErr: true,
|
||||
},
|
||||
"unencoded cluster ID": {
|
||||
ownerID: base64.StdEncoding.EncodeToString([]byte("owner-id")),
|
||||
clusterID: "unique-id",
|
||||
errExpected: true,
|
||||
ownerID: base64.StdEncoding.EncodeToString([]byte("owner-id")),
|
||||
clusterID: "unique-id",
|
||||
wantErr: true,
|
||||
},
|
||||
"correct input": {
|
||||
ownerID: base64.StdEncoding.EncodeToString([]byte("owner-id")),
|
||||
clusterID: base64.StdEncoding.EncodeToString([]byte("unique-id")),
|
||||
errExpected: false,
|
||||
ownerID: base64.StdEncoding.EncodeToString([]byte("owner-id")),
|
||||
clusterID: base64.StdEncoding.EncodeToString([]byte("unique-id")),
|
||||
wantErr: false,
|
||||
},
|
||||
}
|
||||
|
||||
@ -56,7 +56,7 @@ func TestGetGCPValidator(t *testing.T) {
|
||||
0: []byte("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"),
|
||||
1: []byte("BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB"),
|
||||
})
|
||||
if tc.errExpected {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
} else {
|
||||
assert.NoError(err)
|
||||
|
@ -23,40 +23,40 @@ import (
|
||||
|
||||
func TestVerify(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
connErr error
|
||||
checkErr error
|
||||
state state.State
|
||||
errExpected bool
|
||||
connErr error
|
||||
checkErr error
|
||||
state state.State
|
||||
wantErr bool
|
||||
}{
|
||||
"connection error": {
|
||||
connErr: errors.New("connection error"),
|
||||
checkErr: nil,
|
||||
state: 0,
|
||||
errExpected: true,
|
||||
connErr: errors.New("connection error"),
|
||||
checkErr: nil,
|
||||
state: 0,
|
||||
wantErr: true,
|
||||
},
|
||||
"check error": {
|
||||
connErr: nil,
|
||||
checkErr: errors.New("check error"),
|
||||
state: 0,
|
||||
errExpected: true,
|
||||
connErr: nil,
|
||||
checkErr: errors.New("check error"),
|
||||
state: 0,
|
||||
wantErr: true,
|
||||
},
|
||||
"check error, rpc status": {
|
||||
connErr: nil,
|
||||
checkErr: rpcStatus.Error(codes.Unavailable, "check error"),
|
||||
state: 0,
|
||||
errExpected: true,
|
||||
connErr: nil,
|
||||
checkErr: rpcStatus.Error(codes.Unavailable, "check error"),
|
||||
state: 0,
|
||||
wantErr: true,
|
||||
},
|
||||
"verify on worker node": {
|
||||
connErr: nil,
|
||||
checkErr: nil,
|
||||
state: state.IsNode,
|
||||
errExpected: false,
|
||||
connErr: nil,
|
||||
checkErr: nil,
|
||||
state: state.IsNode,
|
||||
wantErr: false,
|
||||
},
|
||||
"verify on master node": {
|
||||
connErr: nil,
|
||||
checkErr: nil,
|
||||
state: state.ActivatingNodes,
|
||||
errExpected: false,
|
||||
connErr: nil,
|
||||
checkErr: nil,
|
||||
state: state.ActivatingNodes,
|
||||
wantErr: false,
|
||||
},
|
||||
}
|
||||
|
||||
@ -81,7 +81,7 @@ func TestVerify(t *testing.T) {
|
||||
}
|
||||
err := verify(ctx, &out, "", []atls.Validator{gcp.NewValidator(pcrs)}, verifier)
|
||||
|
||||
if tc.errExpected {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
} else {
|
||||
assert.NoError(err)
|
||||
@ -130,29 +130,29 @@ func (c *stubPeerStatusClient) GetState(ctx context.Context, in *pubproto.GetSta
|
||||
|
||||
func TestPrepareValidator(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
ownerID string
|
||||
clusterID string
|
||||
errExpected bool
|
||||
ownerID string
|
||||
clusterID string
|
||||
wantErr bool
|
||||
}{
|
||||
"no input": {
|
||||
ownerID: "",
|
||||
clusterID: "",
|
||||
errExpected: true,
|
||||
ownerID: "",
|
||||
clusterID: "",
|
||||
wantErr: true,
|
||||
},
|
||||
"unencoded secret ID": {
|
||||
ownerID: "owner-id",
|
||||
clusterID: base64.StdEncoding.EncodeToString([]byte("unique-id")),
|
||||
errExpected: true,
|
||||
ownerID: "owner-id",
|
||||
clusterID: base64.StdEncoding.EncodeToString([]byte("unique-id")),
|
||||
wantErr: true,
|
||||
},
|
||||
"unencoded cluster ID": {
|
||||
ownerID: base64.StdEncoding.EncodeToString([]byte("owner-id")),
|
||||
clusterID: "unique-id",
|
||||
errExpected: true,
|
||||
ownerID: base64.StdEncoding.EncodeToString([]byte("owner-id")),
|
||||
clusterID: "unique-id",
|
||||
wantErr: true,
|
||||
},
|
||||
"correct input": {
|
||||
ownerID: base64.StdEncoding.EncodeToString([]byte("owner-id")),
|
||||
clusterID: base64.StdEncoding.EncodeToString([]byte("unique-id")),
|
||||
errExpected: false,
|
||||
ownerID: base64.StdEncoding.EncodeToString([]byte("owner-id")),
|
||||
clusterID: base64.StdEncoding.EncodeToString([]byte("unique-id")),
|
||||
wantErr: false,
|
||||
},
|
||||
}
|
||||
|
||||
@ -177,7 +177,7 @@ func TestPrepareValidator(t *testing.T) {
|
||||
}
|
||||
|
||||
err := prepareValidator(cmd, pcrs)
|
||||
if tc.errExpected {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
} else {
|
||||
assert.NoError(err)
|
||||
@ -204,60 +204,60 @@ func TestAddOrSkipPcr(t *testing.T) {
|
||||
}
|
||||
|
||||
testCases := map[string]struct {
|
||||
pcrMap map[uint32][]byte
|
||||
pcrIndex uint32
|
||||
encoded string
|
||||
expectedEntries int
|
||||
errExpected bool
|
||||
pcrMap map[uint32][]byte
|
||||
pcrIndex uint32
|
||||
encoded string
|
||||
wantEntries int
|
||||
wantErr bool
|
||||
}{
|
||||
"empty input, empty map": {
|
||||
pcrMap: emptyMap,
|
||||
pcrIndex: 10,
|
||||
encoded: "",
|
||||
expectedEntries: 0,
|
||||
errExpected: false,
|
||||
pcrMap: emptyMap,
|
||||
pcrIndex: 10,
|
||||
encoded: "",
|
||||
wantEntries: 0,
|
||||
wantErr: false,
|
||||
},
|
||||
"empty input, default map": {
|
||||
pcrMap: defaultMap,
|
||||
pcrIndex: 10,
|
||||
encoded: "",
|
||||
expectedEntries: len(defaultMap),
|
||||
errExpected: false,
|
||||
pcrMap: defaultMap,
|
||||
pcrIndex: 10,
|
||||
encoded: "",
|
||||
wantEntries: len(defaultMap),
|
||||
wantErr: false,
|
||||
},
|
||||
"correct input, empty map": {
|
||||
pcrMap: emptyMap,
|
||||
pcrIndex: 10,
|
||||
encoded: base64.StdEncoding.EncodeToString([]byte("Constellation")),
|
||||
expectedEntries: 1,
|
||||
errExpected: false,
|
||||
pcrMap: emptyMap,
|
||||
pcrIndex: 10,
|
||||
encoded: base64.StdEncoding.EncodeToString([]byte("Constellation")),
|
||||
wantEntries: 1,
|
||||
wantErr: false,
|
||||
},
|
||||
"correct input, default map": {
|
||||
pcrMap: defaultMap,
|
||||
pcrIndex: 10,
|
||||
encoded: base64.StdEncoding.EncodeToString([]byte("Constellation")),
|
||||
expectedEntries: len(defaultMap) + 1,
|
||||
errExpected: false,
|
||||
pcrMap: defaultMap,
|
||||
pcrIndex: 10,
|
||||
encoded: base64.StdEncoding.EncodeToString([]byte("Constellation")),
|
||||
wantEntries: len(defaultMap) + 1,
|
||||
wantErr: false,
|
||||
},
|
||||
"unencoded input, empty map": {
|
||||
pcrMap: emptyMap,
|
||||
pcrIndex: 10,
|
||||
encoded: "Constellation",
|
||||
expectedEntries: 0,
|
||||
errExpected: true,
|
||||
pcrMap: emptyMap,
|
||||
pcrIndex: 10,
|
||||
encoded: "Constellation",
|
||||
wantEntries: 0,
|
||||
wantErr: true,
|
||||
},
|
||||
"unencoded input, default map": {
|
||||
pcrMap: defaultMap,
|
||||
pcrIndex: 10,
|
||||
encoded: "Constellation",
|
||||
expectedEntries: len(defaultMap),
|
||||
errExpected: true,
|
||||
pcrMap: defaultMap,
|
||||
pcrIndex: 10,
|
||||
encoded: "Constellation",
|
||||
wantEntries: len(defaultMap),
|
||||
wantErr: true,
|
||||
},
|
||||
"empty input at occupied index": {
|
||||
pcrMap: defaultMap,
|
||||
pcrIndex: 0,
|
||||
encoded: "",
|
||||
expectedEntries: len(defaultMap) - 1,
|
||||
errExpected: false,
|
||||
pcrMap: defaultMap,
|
||||
pcrIndex: 0,
|
||||
encoded: "",
|
||||
wantEntries: len(defaultMap) - 1,
|
||||
wantErr: false,
|
||||
},
|
||||
}
|
||||
|
||||
@ -272,12 +272,12 @@ func TestAddOrSkipPcr(t *testing.T) {
|
||||
|
||||
err := addOrSkipPCR(res, tc.pcrIndex, tc.encoded)
|
||||
|
||||
if tc.errExpected {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
} else {
|
||||
assert.NoError(err)
|
||||
}
|
||||
assert.Len(res, tc.expectedEntries)
|
||||
assert.Len(res, tc.wantEntries)
|
||||
for _, v := range res {
|
||||
assert.Len(v, 32)
|
||||
}
|
||||
@ -287,28 +287,28 @@ func TestAddOrSkipPcr(t *testing.T) {
|
||||
|
||||
func TestVerifyCompletion(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
args []string
|
||||
toComplete string
|
||||
resultExpected []string
|
||||
shellCDExpected cobra.ShellCompDirective
|
||||
args []string
|
||||
toComplete string
|
||||
wantResult []string
|
||||
wantShellCD cobra.ShellCompDirective
|
||||
}{
|
||||
"first arg": {
|
||||
args: []string{},
|
||||
toComplete: "192.0.2.1",
|
||||
resultExpected: []string{},
|
||||
shellCDExpected: cobra.ShellCompDirectiveNoFileComp,
|
||||
args: []string{},
|
||||
toComplete: "192.0.2.1",
|
||||
wantResult: []string{},
|
||||
wantShellCD: cobra.ShellCompDirectiveNoFileComp,
|
||||
},
|
||||
"second arg": {
|
||||
args: []string{"192.0.2.1"},
|
||||
toComplete: "443",
|
||||
resultExpected: []string{},
|
||||
shellCDExpected: cobra.ShellCompDirectiveNoFileComp,
|
||||
args: []string{"192.0.2.1"},
|
||||
toComplete: "443",
|
||||
wantResult: []string{},
|
||||
wantShellCD: cobra.ShellCompDirectiveNoFileComp,
|
||||
},
|
||||
"third arg": {
|
||||
args: []string{"192.0.2.1", "443"},
|
||||
toComplete: "./file",
|
||||
resultExpected: []string{},
|
||||
shellCDExpected: cobra.ShellCompDirectiveError,
|
||||
args: []string{"192.0.2.1", "443"},
|
||||
toComplete: "./file",
|
||||
wantResult: []string{},
|
||||
wantShellCD: cobra.ShellCompDirectiveError,
|
||||
},
|
||||
}
|
||||
|
||||
@ -318,8 +318,8 @@ func TestVerifyCompletion(t *testing.T) {
|
||||
|
||||
cmd := &cobra.Command{}
|
||||
result, shellCD := verifyCompletion(cmd, tc.args, tc.toComplete)
|
||||
assert.Equal(tc.resultExpected, result)
|
||||
assert.Equal(tc.shellCDExpected, shellCD)
|
||||
assert.Equal(tc.wantResult, result)
|
||||
assert.Equal(tc.wantShellCD, shellCD)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -41,7 +41,7 @@ func TestCreateInstances(t *testing.T) {
|
||||
api stubAPI
|
||||
instances ec2.Instances
|
||||
securityGroup string
|
||||
errExpected bool
|
||||
wantErr bool
|
||||
wantInstances ec2.Instances
|
||||
}{
|
||||
"create": {
|
||||
@ -69,7 +69,7 @@ func TestCreateInstances(t *testing.T) {
|
||||
api: stubAPI{instances: testInstances},
|
||||
instances: ec2.Instances{"id-1": {}, "id-4": {}, "id-5": {}},
|
||||
securityGroup: "sg-test",
|
||||
errExpected: false,
|
||||
wantErr: false,
|
||||
wantInstances: ec2.Instances{
|
||||
"id-1": {PublicIP: "192.0.2.1", PrivateIP: "192.0.2.2"},
|
||||
"id-2": {PublicIP: "192.0.2.3", PrivateIP: "192.0.2.4"},
|
||||
@ -79,23 +79,23 @@ func TestCreateInstances(t *testing.T) {
|
||||
},
|
||||
},
|
||||
"client has no security group": {
|
||||
api: stubAPI{},
|
||||
errExpected: true,
|
||||
api: stubAPI{},
|
||||
wantErr: true,
|
||||
},
|
||||
"run API error": {
|
||||
api: stubAPI{runInstancesErr: someErr},
|
||||
securityGroup: "sg-test",
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"runDryRun API error": {
|
||||
api: stubAPI{runInstancesDryRunErr: &someErr},
|
||||
securityGroup: "sg-test",
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"runDryRun missing expected API error": {
|
||||
api: stubAPI{runInstancesDryRunErr: &noErr},
|
||||
securityGroup: "sg-test",
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -120,7 +120,7 @@ func TestCreateInstances(t *testing.T) {
|
||||
|
||||
err := client.CreateInstances(context.Background(), input)
|
||||
|
||||
if tc.errExpected {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
} else {
|
||||
assert.NoError(err)
|
||||
@ -142,32 +142,32 @@ func TestTerminateInstances(t *testing.T) {
|
||||
var noErr error
|
||||
|
||||
testCases := map[string]struct {
|
||||
api stubAPI
|
||||
instances ec2.Instances
|
||||
errExpected bool
|
||||
api stubAPI
|
||||
instances ec2.Instances
|
||||
wantErr bool
|
||||
}{
|
||||
"client with instances": {
|
||||
api: stubAPI{instances: testAWSInstances},
|
||||
instances: ec2.Instances{"id-1": {}, "id-2": {}, "id-3": {}},
|
||||
errExpected: false,
|
||||
api: stubAPI{instances: testAWSInstances},
|
||||
instances: ec2.Instances{"id-1": {}, "id-2": {}, "id-3": {}},
|
||||
wantErr: false,
|
||||
},
|
||||
"client no instances set": {
|
||||
api: stubAPI{},
|
||||
},
|
||||
"terminate API error": {
|
||||
api: stubAPI{terminateInstancesErr: someErr},
|
||||
instances: ec2.Instances{"id-1": {}, "id-2": {}, "id-3": {}},
|
||||
errExpected: true,
|
||||
api: stubAPI{terminateInstancesErr: someErr},
|
||||
instances: ec2.Instances{"id-1": {}, "id-2": {}, "id-3": {}},
|
||||
wantErr: true,
|
||||
},
|
||||
"terminateDryRun API error": {
|
||||
api: stubAPI{terminateInstancesDryRunErr: &someErr},
|
||||
instances: ec2.Instances{"id-1": {}, "id-2": {}, "id-3": {}},
|
||||
errExpected: true,
|
||||
api: stubAPI{terminateInstancesDryRunErr: &someErr},
|
||||
instances: ec2.Instances{"id-1": {}, "id-2": {}, "id-3": {}},
|
||||
wantErr: true,
|
||||
},
|
||||
"terminateDryRun miss expected API error": {
|
||||
api: stubAPI{terminateInstancesDryRunErr: &noErr},
|
||||
instances: ec2.Instances{"id-1": {}, "id-2": {}, "id-3": {}},
|
||||
errExpected: true,
|
||||
api: stubAPI{terminateInstancesDryRunErr: &noErr},
|
||||
instances: ec2.Instances{"id-1": {}, "id-2": {}, "id-3": {}},
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -185,7 +185,7 @@ func TestTerminateInstances(t *testing.T) {
|
||||
}
|
||||
|
||||
err := client.TerminateInstances(context.Background())
|
||||
if tc.errExpected {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
} else {
|
||||
assert.NoError(err)
|
||||
@ -197,9 +197,9 @@ func TestTerminateInstances(t *testing.T) {
|
||||
|
||||
func TestWaitStateRunning(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
api api
|
||||
instances ec2.Instances
|
||||
errExpected bool
|
||||
api api
|
||||
instances ec2.Instances
|
||||
wantErr bool
|
||||
}{
|
||||
"instances are running": {
|
||||
api: stubAPI{instances: []types.Instance{
|
||||
@ -216,8 +216,8 @@ func TestWaitStateRunning(t *testing.T) {
|
||||
State: &stateRunning,
|
||||
},
|
||||
}},
|
||||
instances: ec2.Instances{"id-1": {}, "id-2": {}, "id-3": {}},
|
||||
errExpected: false,
|
||||
instances: ec2.Instances{"id-1": {}, "id-2": {}, "id-3": {}},
|
||||
wantErr: false,
|
||||
},
|
||||
"one instance running, rest nil": {
|
||||
api: stubAPI{instances: []types.Instance{
|
||||
@ -228,8 +228,8 @@ func TestWaitStateRunning(t *testing.T) {
|
||||
{InstanceId: aws.String("id-2")},
|
||||
{InstanceId: aws.String("id-3")},
|
||||
}},
|
||||
instances: ec2.Instances{"id-1": {}, "id-2": {}, "id-3": {}},
|
||||
errExpected: false,
|
||||
instances: ec2.Instances{"id-1": {}, "id-2": {}, "id-3": {}},
|
||||
wantErr: false,
|
||||
},
|
||||
"one instance terminated, rest nil": {
|
||||
api: stubAPI{instances: []types.Instance{
|
||||
@ -240,8 +240,8 @@ func TestWaitStateRunning(t *testing.T) {
|
||||
{InstanceId: aws.String("id-2")},
|
||||
{InstanceId: aws.String("id-3")},
|
||||
}},
|
||||
instances: ec2.Instances{"id-1": {}, "id-2": {}, "id-3": {}},
|
||||
errExpected: true,
|
||||
instances: ec2.Instances{"id-1": {}, "id-2": {}, "id-3": {}},
|
||||
wantErr: true,
|
||||
},
|
||||
"instances with different state": {
|
||||
api: stubAPI{instances: []types.Instance{
|
||||
@ -255,8 +255,8 @@ func TestWaitStateRunning(t *testing.T) {
|
||||
},
|
||||
{InstanceId: aws.String("id-3")},
|
||||
}},
|
||||
instances: ec2.Instances{"id-1": {}, "id-2": {}, "id-3": {}},
|
||||
errExpected: true,
|
||||
instances: ec2.Instances{"id-1": {}, "id-2": {}, "id-3": {}},
|
||||
wantErr: true,
|
||||
},
|
||||
"all instances have nil state": {
|
||||
api: stubAPI{instances: []types.Instance{
|
||||
@ -264,12 +264,12 @@ func TestWaitStateRunning(t *testing.T) {
|
||||
{InstanceId: aws.String("id-2")},
|
||||
{InstanceId: aws.String("id-3")},
|
||||
}},
|
||||
instances: ec2.Instances{"id-1": {}, "id-2": {}, "id-3": {}},
|
||||
errExpected: true,
|
||||
instances: ec2.Instances{"id-1": {}, "id-2": {}, "id-3": {}},
|
||||
wantErr: true,
|
||||
},
|
||||
"client has no instances": {
|
||||
api: &stubAPI{},
|
||||
errExpected: true,
|
||||
api: &stubAPI{},
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -286,7 +286,7 @@ func TestWaitStateRunning(t *testing.T) {
|
||||
}
|
||||
|
||||
err := client.waitStateRunning(context.Background())
|
||||
if tc.errExpected {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
} else {
|
||||
assert.NoError(err)
|
||||
@ -297,9 +297,9 @@ func TestWaitStateRunning(t *testing.T) {
|
||||
|
||||
func TestWaitStateTerminated(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
api api
|
||||
instances ec2.Instances
|
||||
errExpected bool
|
||||
api api
|
||||
instances ec2.Instances
|
||||
wantErr bool
|
||||
}{
|
||||
"instances are terminated": {
|
||||
api: stubAPI{instances: []types.Instance{
|
||||
@ -316,8 +316,8 @@ func TestWaitStateTerminated(t *testing.T) {
|
||||
State: &stateTerminated,
|
||||
},
|
||||
}},
|
||||
instances: ec2.Instances{"id-1": {}, "id-2": {}, "id-3": {}},
|
||||
errExpected: false,
|
||||
instances: ec2.Instances{"id-1": {}, "id-2": {}, "id-3": {}},
|
||||
wantErr: false,
|
||||
},
|
||||
"one instance terminated, rest nil": {
|
||||
api: stubAPI{instances: []types.Instance{
|
||||
@ -328,8 +328,8 @@ func TestWaitStateTerminated(t *testing.T) {
|
||||
{InstanceId: aws.String("id-2")},
|
||||
{InstanceId: aws.String("id-3")},
|
||||
}},
|
||||
instances: ec2.Instances{"id-1": {}, "id-2": {}, "id-3": {}},
|
||||
errExpected: false,
|
||||
instances: ec2.Instances{"id-1": {}, "id-2": {}, "id-3": {}},
|
||||
wantErr: false,
|
||||
},
|
||||
"one instance running, rest nil": {
|
||||
api: stubAPI{instances: []types.Instance{
|
||||
@ -340,8 +340,8 @@ func TestWaitStateTerminated(t *testing.T) {
|
||||
{InstanceId: aws.String("id-2")},
|
||||
{InstanceId: aws.String("id-3")},
|
||||
}},
|
||||
instances: ec2.Instances{"id-1": {}, "id-2": {}, "id-3": {}},
|
||||
errExpected: true,
|
||||
instances: ec2.Instances{"id-1": {}, "id-2": {}, "id-3": {}},
|
||||
wantErr: true,
|
||||
},
|
||||
"instances with different state": {
|
||||
api: stubAPI{instances: []types.Instance{
|
||||
@ -355,8 +355,8 @@ func TestWaitStateTerminated(t *testing.T) {
|
||||
},
|
||||
{InstanceId: aws.String("id-3")},
|
||||
}},
|
||||
instances: ec2.Instances{"id-1": {}, "id-2": {}, "id-3": {}},
|
||||
errExpected: true,
|
||||
instances: ec2.Instances{"id-1": {}, "id-2": {}, "id-3": {}},
|
||||
wantErr: true,
|
||||
},
|
||||
"all instances have nil state": {
|
||||
api: stubAPI{instances: []types.Instance{
|
||||
@ -364,12 +364,12 @@ func TestWaitStateTerminated(t *testing.T) {
|
||||
{InstanceId: aws.String("id-2")},
|
||||
{InstanceId: aws.String("id-3")},
|
||||
}},
|
||||
instances: ec2.Instances{"id-1": {}, "id-2": {}, "id-3": {}},
|
||||
errExpected: true,
|
||||
instances: ec2.Instances{"id-1": {}, "id-2": {}, "id-3": {}},
|
||||
wantErr: true,
|
||||
},
|
||||
"client has no instances": {
|
||||
api: &stubAPI{},
|
||||
errExpected: true,
|
||||
api: &stubAPI{},
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -387,7 +387,7 @@ func TestWaitStateTerminated(t *testing.T) {
|
||||
}
|
||||
|
||||
err := client.waitStateTerminated(context.Background())
|
||||
if tc.errExpected {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
} else {
|
||||
assert.NoError(err)
|
||||
@ -403,23 +403,23 @@ func TestTagInstances(t *testing.T) {
|
||||
}
|
||||
|
||||
testCases := map[string]struct {
|
||||
api stubAPI
|
||||
instances ec2.Instances
|
||||
errExpected bool
|
||||
api stubAPI
|
||||
instances ec2.Instances
|
||||
wantErr bool
|
||||
}{
|
||||
"tag": {
|
||||
api: stubAPI{},
|
||||
instances: ec2.Instances{"id-1": {}, "id-2": {}, "id-3": {}},
|
||||
errExpected: false,
|
||||
api: stubAPI{},
|
||||
instances: ec2.Instances{"id-1": {}, "id-2": {}, "id-3": {}},
|
||||
wantErr: false,
|
||||
},
|
||||
"client without instances": {
|
||||
api: stubAPI{createTagsErr: errors.New("failed")},
|
||||
errExpected: true,
|
||||
api: stubAPI{createTagsErr: errors.New("failed")},
|
||||
wantErr: true,
|
||||
},
|
||||
"tag API error": {
|
||||
api: stubAPI{createTagsErr: errors.New("failed")},
|
||||
instances: ec2.Instances{"id-1": {}, "id-2": {}, "id-3": {}},
|
||||
errExpected: true,
|
||||
api: stubAPI{createTagsErr: errors.New("failed")},
|
||||
instances: ec2.Instances{"id-1": {}, "id-2": {}, "id-3": {}},
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
for name, tc := range testCases {
|
||||
@ -436,7 +436,7 @@ func TestTagInstances(t *testing.T) {
|
||||
}
|
||||
|
||||
err := client.tagInstances(context.Background(), testTags)
|
||||
if tc.errExpected {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
} else {
|
||||
assert.NoError(err)
|
||||
@ -449,8 +449,8 @@ func TestEc2RunInstanceInput(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
testCases := []struct {
|
||||
in CreateInput
|
||||
outExpected awsec2.RunInstancesInput
|
||||
in CreateInput
|
||||
wantOutput awsec2.RunInstancesInput
|
||||
}{
|
||||
{
|
||||
in: CreateInput{
|
||||
@ -459,7 +459,7 @@ func TestEc2RunInstanceInput(t *testing.T) {
|
||||
Count: 13,
|
||||
securityGroupIds: []string{"test-sec-group"},
|
||||
},
|
||||
outExpected: awsec2.RunInstancesInput{
|
||||
wantOutput: awsec2.RunInstancesInput{
|
||||
ImageId: aws.String("test-image"),
|
||||
InstanceType: types.InstanceTypeC5a4xlarge,
|
||||
MinCount: aws.Int32(int32(13)),
|
||||
@ -475,7 +475,7 @@ func TestEc2RunInstanceInput(t *testing.T) {
|
||||
Count: 2,
|
||||
securityGroupIds: []string{"test-sec-group-2"},
|
||||
},
|
||||
outExpected: awsec2.RunInstancesInput{
|
||||
wantOutput: awsec2.RunInstancesInput{
|
||||
ImageId: aws.String("test-image-2"),
|
||||
InstanceType: types.InstanceTypeC5a12xlarge,
|
||||
MinCount: aws.Int32(int32(2)),
|
||||
@ -488,6 +488,6 @@ func TestEc2RunInstanceInput(t *testing.T) {
|
||||
|
||||
for _, tc := range testCases {
|
||||
out := tc.in.AWS()
|
||||
assert.Equal(tc.outExpected, *out)
|
||||
assert.Equal(tc.wantOutput, *out)
|
||||
}
|
||||
}
|
||||
|
@ -41,55 +41,55 @@ func TestCreateSecurityGroup(t *testing.T) {
|
||||
var noErr error
|
||||
|
||||
testCases := map[string]struct {
|
||||
api stubAPI
|
||||
securityGroup string
|
||||
input SecurityGroupInput
|
||||
errExpected bool
|
||||
securityGroupExpected string
|
||||
api stubAPI
|
||||
securityGroup string
|
||||
input SecurityGroupInput
|
||||
wantErr bool
|
||||
wantSecurityGroup string
|
||||
}{
|
||||
"create security group": {
|
||||
api: stubAPI{securityGroup: types.SecurityGroup{GroupId: aws.String("sg-test")}},
|
||||
input: testInput,
|
||||
securityGroupExpected: "sg-test",
|
||||
api: stubAPI{securityGroup: types.SecurityGroup{GroupId: aws.String("sg-test")}},
|
||||
input: testInput,
|
||||
wantSecurityGroup: "sg-test",
|
||||
},
|
||||
"create security group without permissions": {
|
||||
api: stubAPI{securityGroup: types.SecurityGroup{GroupId: aws.String("sg-test")}},
|
||||
input: SecurityGroupInput{},
|
||||
securityGroupExpected: "sg-test",
|
||||
api: stubAPI{securityGroup: types.SecurityGroup{GroupId: aws.String("sg-test")}},
|
||||
input: SecurityGroupInput{},
|
||||
wantSecurityGroup: "sg-test",
|
||||
},
|
||||
"client already has security group": {
|
||||
api: stubAPI{},
|
||||
securityGroup: "sg-test",
|
||||
input: testInput,
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"create returns nil security group ID": {
|
||||
api: stubAPI{securityGroup: types.SecurityGroup{GroupId: nil}},
|
||||
input: testInput,
|
||||
errExpected: true,
|
||||
api: stubAPI{securityGroup: types.SecurityGroup{GroupId: nil}},
|
||||
input: testInput,
|
||||
wantErr: true,
|
||||
},
|
||||
"create API error": {
|
||||
api: stubAPI{createSecurityGroupErr: someErr},
|
||||
input: testInput,
|
||||
errExpected: true,
|
||||
api: stubAPI{createSecurityGroupErr: someErr},
|
||||
input: testInput,
|
||||
wantErr: true,
|
||||
},
|
||||
"create DryRun API error": {
|
||||
api: stubAPI{createSecurityGroupDryRunErr: &someErr},
|
||||
input: testInput,
|
||||
errExpected: true,
|
||||
api: stubAPI{createSecurityGroupDryRunErr: &someErr},
|
||||
input: testInput,
|
||||
wantErr: true,
|
||||
},
|
||||
"create DryRun missing expected error": {
|
||||
api: stubAPI{createSecurityGroupDryRunErr: &noErr},
|
||||
input: testInput,
|
||||
errExpected: true,
|
||||
api: stubAPI{createSecurityGroupDryRunErr: &noErr},
|
||||
input: testInput,
|
||||
wantErr: true,
|
||||
},
|
||||
"authorize error": {
|
||||
api: stubAPI{
|
||||
securityGroup: types.SecurityGroup{GroupId: aws.String("sg-test")},
|
||||
authorizeSecurityGroupIngressErr: someErr,
|
||||
},
|
||||
input: testInput,
|
||||
errExpected: true,
|
||||
input: testInput,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -103,11 +103,11 @@ func TestCreateSecurityGroup(t *testing.T) {
|
||||
client.securityGroup = tc.securityGroup
|
||||
|
||||
err = client.CreateSecurityGroup(context.Background(), tc.input)
|
||||
if tc.errExpected {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
} else {
|
||||
assert.NoError(err)
|
||||
assert.Equal(tc.securityGroupExpected, client.securityGroup)
|
||||
assert.Equal(tc.wantSecurityGroup, client.securityGroup)
|
||||
}
|
||||
})
|
||||
}
|
||||
@ -120,7 +120,7 @@ func TestDeleteSecurityGroup(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
api stubAPI
|
||||
securityGroup string
|
||||
errExpected bool
|
||||
wantErr bool
|
||||
}{
|
||||
"delete security group": {
|
||||
api: stubAPI{},
|
||||
@ -132,17 +132,17 @@ func TestDeleteSecurityGroup(t *testing.T) {
|
||||
"delete API error": {
|
||||
api: stubAPI{deleteSecurityGroupErr: someErr},
|
||||
securityGroup: "sg-test",
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"delete DryRun API error": {
|
||||
api: stubAPI{deleteSecurityGroupDryRunErr: &someErr},
|
||||
securityGroup: "sg-test",
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"delete DryRun missing expected error": {
|
||||
api: stubAPI{deleteSecurityGroupDryRunErr: &noErr},
|
||||
securityGroup: "sg-test",
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -156,7 +156,7 @@ func TestDeleteSecurityGroup(t *testing.T) {
|
||||
client.securityGroup = tc.securityGroup
|
||||
|
||||
err = client.DeleteSecurityGroup(context.Background())
|
||||
if tc.errExpected {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
} else {
|
||||
assert.NoError(err)
|
||||
@ -198,54 +198,54 @@ func TestAuthorizeSecurityGroup(t *testing.T) {
|
||||
api stubAPI
|
||||
securityGroup string
|
||||
input SecurityGroupInput
|
||||
errExpected bool
|
||||
wantErr bool
|
||||
}{
|
||||
"authorize": {
|
||||
api: stubAPI{},
|
||||
securityGroup: "sg-test",
|
||||
input: testInput,
|
||||
errExpected: false,
|
||||
wantErr: false,
|
||||
},
|
||||
"client without security group": {
|
||||
api: stubAPI{},
|
||||
input: testInput,
|
||||
errExpected: true,
|
||||
api: stubAPI{},
|
||||
input: testInput,
|
||||
wantErr: true,
|
||||
},
|
||||
"authorizeIngress API error": {
|
||||
api: stubAPI{authorizeSecurityGroupIngressErr: someErr},
|
||||
securityGroup: "sg-test",
|
||||
input: testInput,
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"authorizeIngress DryRun API error": {
|
||||
api: stubAPI{authorizeSecurityGroupIngressDryRunErr: &someErr},
|
||||
securityGroup: "sg-test",
|
||||
input: testInput,
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"authorizeIngress DryRun missing expected error": {
|
||||
api: stubAPI{authorizeSecurityGroupIngressDryRunErr: &noErr},
|
||||
securityGroup: "sg-test",
|
||||
input: testInput,
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"authorizeEgress API error": {
|
||||
api: stubAPI{authorizeSecurityGroupEgressErr: someErr},
|
||||
securityGroup: "sg-test",
|
||||
input: testInput,
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"authorizeEgress DryRun API error": {
|
||||
api: stubAPI{authorizeSecurityGroupEgressDryRunErr: &someErr},
|
||||
securityGroup: "sg-test",
|
||||
input: testInput,
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"authorizeEgress DryRun missing expected error": {
|
||||
api: stubAPI{authorizeSecurityGroupEgressDryRunErr: &noErr},
|
||||
securityGroup: "sg-test",
|
||||
input: testInput,
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -259,7 +259,7 @@ func TestAuthorizeSecurityGroup(t *testing.T) {
|
||||
client.securityGroup = tc.securityGroup
|
||||
|
||||
err = client.authorizeSecurityGroup(context.Background(), tc.input)
|
||||
if tc.errExpected {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
} else {
|
||||
assert.NoError(err)
|
||||
|
@ -9,22 +9,22 @@ import (
|
||||
func TestIDs(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
testState := testInstances()
|
||||
expectedIDs := []string{"id-9", "id-10", "id-11", "id-12"}
|
||||
assert.ElementsMatch(expectedIDs, testState.IDs())
|
||||
wantIDs := []string{"id-9", "id-10", "id-11", "id-12"}
|
||||
assert.ElementsMatch(wantIDs, testState.IDs())
|
||||
}
|
||||
|
||||
func TestPublicIPs(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
testState := testInstances()
|
||||
expectedIPs := []string{"192.0.2.1", "192.0.2.3", "192.0.2.5", "192.0.2.7"}
|
||||
assert.ElementsMatch(expectedIPs, testState.PublicIPs())
|
||||
wantIPs := []string{"192.0.2.1", "192.0.2.3", "192.0.2.5", "192.0.2.7"}
|
||||
assert.ElementsMatch(wantIPs, testState.PublicIPs())
|
||||
}
|
||||
|
||||
func TestPrivateIPs(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
testState := testInstances()
|
||||
expectedIPs := []string{"192.0.2.2", "192.0.2.4", "192.0.2.6", "192.0.2.8"}
|
||||
assert.ElementsMatch(expectedIPs, testState.PrivateIPs())
|
||||
wantIPs := []string{"192.0.2.2", "192.0.2.4", "192.0.2.6", "192.0.2.8"}
|
||||
assert.ElementsMatch(wantIPs, testState.PrivateIPs())
|
||||
}
|
||||
|
||||
func TestGetOne(t *testing.T) {
|
||||
@ -43,9 +43,9 @@ func TestGetOthers(t *testing.T) {
|
||||
for _, id := range testCases {
|
||||
others := testInstances().GetOthers(id)
|
||||
assert.NotContains(others, id)
|
||||
expectedInstances := testInstances()
|
||||
delete(expectedInstances, id)
|
||||
assert.ElementsMatch(others.IDs(), expectedInstances.IDs())
|
||||
wantInstances := testInstances()
|
||||
delete(wantInstances, id)
|
||||
assert.ElementsMatch(others.IDs(), wantInstances.IDs())
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -21,7 +21,7 @@ func TestTagsAws(t *testing.T) {
|
||||
Value: "Bar",
|
||||
},
|
||||
}
|
||||
expected := []types.Tag{
|
||||
wantTags := []types.Tag{
|
||||
{
|
||||
Key: aws.String("Name"),
|
||||
Value: aws.String("Test"),
|
||||
@ -33,5 +33,5 @@ func TestTagsAws(t *testing.T) {
|
||||
}
|
||||
|
||||
awsTags := testTags.AWS()
|
||||
assert.Equal(expected, awsTags)
|
||||
assert.Equal(wantTags, awsTags)
|
||||
}
|
||||
|
@ -9,6 +9,6 @@ import (
|
||||
func TestAutoscalingNodeGroup(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
nodeGroups := AutoscalingNodeGroup("some-project", "some-zone", "some-group", 0, 100)
|
||||
expectedNodeGroups := "0:100:https://www.googleapis.com/compute/v1/projects/some-project/zones/some-zone/instanceGroups/some-group"
|
||||
assert.Equal(expectedNodeGroups, nodeGroups)
|
||||
wantNodeGroups := "0:100:https://www.googleapis.com/compute/v1/projects/some-project/zones/some-zone/instanceGroups/some-group"
|
||||
assert.Equal(wantNodeGroups, nodeGroups)
|
||||
}
|
||||
|
@ -13,8 +13,8 @@ import (
|
||||
|
||||
func TestSetGetState(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
state state.ConstellationState
|
||||
errExpected bool
|
||||
state state.ConstellationState
|
||||
wantErr bool
|
||||
}{
|
||||
"valid state": {
|
||||
state: state.ConstellationState{
|
||||
@ -68,7 +68,7 @@ func TestSetGetState(t *testing.T) {
|
||||
GCPNodeInstanceTemplate: "temp-id",
|
||||
GCPCoordinatorInstanceTemplate: "temp-id",
|
||||
},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"missing coordinator": {
|
||||
state: state.ConstellationState{
|
||||
@ -92,7 +92,7 @@ func TestSetGetState(t *testing.T) {
|
||||
GCPNodeInstanceTemplate: "temp-id",
|
||||
GCPCoordinatorInstanceTemplate: "temp-id",
|
||||
},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"missing node group": {
|
||||
state: state.ConstellationState{
|
||||
@ -121,7 +121,7 @@ func TestSetGetState(t *testing.T) {
|
||||
GCPNodeInstanceTemplate: "temp-id",
|
||||
GCPCoordinatorInstanceTemplate: "temp-id",
|
||||
},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"missing coordinator group": {
|
||||
state: state.ConstellationState{
|
||||
@ -150,7 +150,7 @@ func TestSetGetState(t *testing.T) {
|
||||
GCPNodeInstanceTemplate: "temp-id",
|
||||
GCPCoordinatorInstanceTemplate: "temp-id",
|
||||
},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"missing project id": {
|
||||
state: state.ConstellationState{
|
||||
@ -179,7 +179,7 @@ func TestSetGetState(t *testing.T) {
|
||||
GCPNodeInstanceTemplate: "temp-id",
|
||||
GCPCoordinatorInstanceTemplate: "temp-id",
|
||||
},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"missing zone": {
|
||||
state: state.ConstellationState{
|
||||
@ -208,7 +208,7 @@ func TestSetGetState(t *testing.T) {
|
||||
GCPNodeInstanceTemplate: "temp-id",
|
||||
GCPCoordinatorInstanceTemplate: "temp-id",
|
||||
},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"missing region": {
|
||||
state: state.ConstellationState{
|
||||
@ -237,7 +237,7 @@ func TestSetGetState(t *testing.T) {
|
||||
GCPNodeInstanceTemplate: "temp-id",
|
||||
GCPCoordinatorInstanceTemplate: "temp-id",
|
||||
},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"missing name": {
|
||||
state: state.ConstellationState{
|
||||
@ -265,7 +265,7 @@ func TestSetGetState(t *testing.T) {
|
||||
GCPNodeInstanceTemplate: "temp-id",
|
||||
GCPCoordinatorInstanceTemplate: "temp-id",
|
||||
},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"missing uid": {
|
||||
state: state.ConstellationState{
|
||||
@ -294,7 +294,7 @@ func TestSetGetState(t *testing.T) {
|
||||
GCPNodeInstanceTemplate: "temp-id",
|
||||
GCPCoordinatorInstanceTemplate: "temp-id",
|
||||
},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"missing firewalls": {
|
||||
state: state.ConstellationState{
|
||||
@ -323,7 +323,7 @@ func TestSetGetState(t *testing.T) {
|
||||
GCPNodeInstanceTemplate: "temp-id",
|
||||
GCPCoordinatorInstanceTemplate: "temp-id",
|
||||
},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"missing network": {
|
||||
state: state.ConstellationState{
|
||||
@ -351,7 +351,7 @@ func TestSetGetState(t *testing.T) {
|
||||
GCPNodeInstanceTemplate: "temp-id",
|
||||
GCPCoordinatorInstanceTemplate: "temp-id",
|
||||
},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"missing external network": {
|
||||
state: state.ConstellationState{
|
||||
@ -380,7 +380,7 @@ func TestSetGetState(t *testing.T) {
|
||||
GCPNodeInstanceTemplate: "temp-id",
|
||||
GCPCoordinatorInstanceTemplate: "temp-id",
|
||||
},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"missing subnetwork": {
|
||||
state: state.ConstellationState{
|
||||
@ -409,7 +409,7 @@ func TestSetGetState(t *testing.T) {
|
||||
GCPNodeInstanceTemplate: "temp-id",
|
||||
GCPCoordinatorInstanceTemplate: "temp-id",
|
||||
},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"missing external subnetwork": {
|
||||
state: state.ConstellationState{
|
||||
@ -438,7 +438,7 @@ func TestSetGetState(t *testing.T) {
|
||||
GCPNodeInstanceTemplate: "temp-id",
|
||||
GCPCoordinatorInstanceTemplate: "temp-id",
|
||||
},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"missing node template": {
|
||||
state: state.ConstellationState{
|
||||
@ -467,7 +467,7 @@ func TestSetGetState(t *testing.T) {
|
||||
GCPFirewalls: []string{"fw-1", "fw-2"},
|
||||
GCPCoordinatorInstanceTemplate: "temp-id",
|
||||
},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"missing coordinator template": {
|
||||
state: state.ConstellationState{
|
||||
@ -496,7 +496,7 @@ func TestSetGetState(t *testing.T) {
|
||||
GCPFirewalls: []string{"fw-1", "fw-2"},
|
||||
GCPNodeInstanceTemplate: "temp-id",
|
||||
},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -506,7 +506,7 @@ func TestSetGetState(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
client := Client{}
|
||||
if tc.errExpected {
|
||||
if tc.wantErr {
|
||||
assert.Error(client.SetState(tc.state))
|
||||
} else {
|
||||
assert.NoError(client.SetState(tc.state))
|
||||
@ -550,7 +550,7 @@ func TestSetGetState(t *testing.T) {
|
||||
coordinatorTemplate: tc.state.GCPCoordinatorInstanceTemplate,
|
||||
serviceAccount: tc.state.GCPServiceAccount,
|
||||
}
|
||||
if tc.errExpected {
|
||||
if tc.wantErr {
|
||||
_, err := client.GetState()
|
||||
assert.Error(err)
|
||||
} else {
|
||||
|
@ -57,7 +57,7 @@ func TestCreateInstances(t *testing.T) {
|
||||
instanceGroupManagersAPI instanceGroupManagersAPI
|
||||
input CreateInstancesInput
|
||||
network string
|
||||
errExpected bool
|
||||
wantErr bool
|
||||
}{
|
||||
"successful create": {
|
||||
instanceAPI: stubInstanceAPI{listIterator: &stubInstanceIterator{instances: testInstances}},
|
||||
@ -75,7 +75,7 @@ func TestCreateInstances(t *testing.T) {
|
||||
instanceTemplateAPI: stubInstanceTemplateAPI{},
|
||||
instanceGroupManagersAPI: stubInstanceGroupManagersAPI{listIterator: &stubManagedInstanceIterator{instances: testManagedInstances}},
|
||||
input: testInput,
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"failed wait zonal op": {
|
||||
instanceAPI: stubInstanceAPI{listIterator: &stubInstanceIterator{instances: testInstances}},
|
||||
@ -85,7 +85,7 @@ func TestCreateInstances(t *testing.T) {
|
||||
instanceGroupManagersAPI: stubInstanceGroupManagersAPI{listIterator: &stubManagedInstanceIterator{instances: testManagedInstances}},
|
||||
network: "network",
|
||||
input: testInput,
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"failed wait global op": {
|
||||
instanceAPI: stubInstanceAPI{listIterator: &stubInstanceIterator{instances: testInstances}},
|
||||
@ -95,7 +95,7 @@ func TestCreateInstances(t *testing.T) {
|
||||
instanceGroupManagersAPI: stubInstanceGroupManagersAPI{listIterator: &stubManagedInstanceIterator{instances: testManagedInstances}},
|
||||
network: "network",
|
||||
input: testInput,
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"failed insert template": {
|
||||
instanceAPI: stubInstanceAPI{listIterator: &stubInstanceIterator{instances: testInstances}},
|
||||
@ -105,7 +105,7 @@ func TestCreateInstances(t *testing.T) {
|
||||
instanceGroupManagersAPI: stubInstanceGroupManagersAPI{listIterator: &stubManagedInstanceIterator{instances: testManagedInstances}},
|
||||
input: testInput,
|
||||
network: "network",
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"failed insert instanceGroupManager": {
|
||||
instanceAPI: stubInstanceAPI{listIterator: &stubInstanceIterator{instances: testInstances}},
|
||||
@ -115,7 +115,7 @@ func TestCreateInstances(t *testing.T) {
|
||||
instanceGroupManagersAPI: stubInstanceGroupManagersAPI{insertErr: someErr},
|
||||
network: "network",
|
||||
input: testInput,
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"failed instanceGroupManager iterator": {
|
||||
instanceAPI: stubInstanceAPI{listIterator: &stubInstanceIterator{instances: testInstances}},
|
||||
@ -125,7 +125,7 @@ func TestCreateInstances(t *testing.T) {
|
||||
instanceGroupManagersAPI: stubInstanceGroupManagersAPI{listIterator: &stubManagedInstanceIterator{nextErr: someErr}},
|
||||
network: "network",
|
||||
input: testInput,
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"failed instance iterator": {
|
||||
instanceAPI: stubInstanceAPI{listIterator: &stubInstanceIterator{nextErr: someErr}},
|
||||
@ -135,7 +135,7 @@ func TestCreateInstances(t *testing.T) {
|
||||
instanceGroupManagersAPI: stubInstanceGroupManagersAPI{listIterator: &stubManagedInstanceIterator{instances: testManagedInstances}},
|
||||
network: "network",
|
||||
input: testInput,
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -161,7 +161,7 @@ func TestCreateInstances(t *testing.T) {
|
||||
coordinators: make(gcp.Instances),
|
||||
}
|
||||
|
||||
if tc.errExpected {
|
||||
if tc.wantErr {
|
||||
assert.Error(client.CreateInstances(ctx, tc.input))
|
||||
} else {
|
||||
assert.NoError(client.CreateInstances(ctx, tc.input))
|
||||
@ -187,7 +187,7 @@ func TestTerminateInstances(t *testing.T) {
|
||||
instanceGroupManagersAPI instanceGroupManagersAPI
|
||||
|
||||
missingNodeInstanceGroup bool
|
||||
errExpected bool
|
||||
wantErr bool
|
||||
}{
|
||||
"successful terminate": {
|
||||
operationZoneAPI: stubOperationZoneAPI{},
|
||||
@ -207,14 +207,14 @@ func TestTerminateInstances(t *testing.T) {
|
||||
operationGlobalAPI: stubOperationGlobalAPI{},
|
||||
instanceTemplateAPI: stubInstanceTemplateAPI{},
|
||||
instanceGroupManagersAPI: stubInstanceGroupManagersAPI{deleteErr: someErr},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"fail delete instanceTemplate": {
|
||||
operationZoneAPI: stubOperationZoneAPI{},
|
||||
operationGlobalAPI: stubOperationGlobalAPI{},
|
||||
instanceTemplateAPI: stubInstanceTemplateAPI{deleteErr: someErr},
|
||||
instanceGroupManagersAPI: stubInstanceGroupManagersAPI{},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
for name, tc := range testCases {
|
||||
@ -245,7 +245,7 @@ func TestTerminateInstances(t *testing.T) {
|
||||
client.nodes = gcp.Instances{}
|
||||
}
|
||||
|
||||
if tc.errExpected {
|
||||
if tc.wantErr {
|
||||
assert.Error(client.TerminateInstances(ctx))
|
||||
} else {
|
||||
assert.NoError(client.TerminateInstances(ctx))
|
||||
|
@ -23,7 +23,7 @@ func TestCreateVPCs(t *testing.T) {
|
||||
operationRegionAPI operationRegionAPI
|
||||
networksAPI networksAPI
|
||||
subnetworksAPI subnetworksAPI
|
||||
errExpected bool
|
||||
wantErr bool
|
||||
}{
|
||||
"successful create": {
|
||||
operationGlobalAPI: stubOperationGlobalAPI{},
|
||||
@ -36,28 +36,28 @@ func TestCreateVPCs(t *testing.T) {
|
||||
operationRegionAPI: stubOperationRegionAPI{},
|
||||
networksAPI: stubNetworksAPI{},
|
||||
subnetworksAPI: stubSubnetworksAPI{},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"failed wait region op": {
|
||||
operationGlobalAPI: stubOperationGlobalAPI{},
|
||||
operationRegionAPI: stubOperationRegionAPI{waitErr: someErr},
|
||||
networksAPI: stubNetworksAPI{},
|
||||
subnetworksAPI: stubSubnetworksAPI{},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"failed insert networks": {
|
||||
operationGlobalAPI: stubOperationGlobalAPI{},
|
||||
operationRegionAPI: stubOperationRegionAPI{},
|
||||
networksAPI: stubNetworksAPI{insertErr: someErr},
|
||||
subnetworksAPI: stubSubnetworksAPI{},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"failed insert subnetworks": {
|
||||
operationGlobalAPI: stubOperationGlobalAPI{},
|
||||
operationRegionAPI: stubOperationRegionAPI{},
|
||||
networksAPI: stubNetworksAPI{},
|
||||
subnetworksAPI: stubSubnetworksAPI{insertErr: someErr},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -79,7 +79,7 @@ func TestCreateVPCs(t *testing.T) {
|
||||
coordinators: make(gcp.Instances),
|
||||
}
|
||||
|
||||
if tc.errExpected {
|
||||
if tc.wantErr {
|
||||
assert.Error(client.CreateVPCs(ctx, testInput))
|
||||
} else {
|
||||
assert.NoError(client.CreateVPCs(ctx, testInput))
|
||||
@ -97,7 +97,7 @@ func TestTerminateVPCs(t *testing.T) {
|
||||
networksAPI networksAPI
|
||||
subnetworksAPI subnetworksAPI
|
||||
firewalls []string
|
||||
errExpected bool
|
||||
wantErr bool
|
||||
}{
|
||||
"successful terminate": {
|
||||
operationGlobalAPI: stubOperationGlobalAPI{},
|
||||
@ -110,21 +110,21 @@ func TestTerminateVPCs(t *testing.T) {
|
||||
operationRegionAPI: stubOperationRegionAPI{},
|
||||
networksAPI: stubNetworksAPI{},
|
||||
subnetworksAPI: stubSubnetworksAPI{},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"failed delete networks": {
|
||||
operationGlobalAPI: stubOperationGlobalAPI{},
|
||||
operationRegionAPI: stubOperationRegionAPI{},
|
||||
networksAPI: stubNetworksAPI{deleteErr: someErr},
|
||||
subnetworksAPI: stubSubnetworksAPI{},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"failed delete subnetworks": {
|
||||
operationGlobalAPI: stubOperationGlobalAPI{},
|
||||
operationRegionAPI: stubOperationRegionAPI{},
|
||||
networksAPI: stubNetworksAPI{},
|
||||
subnetworksAPI: stubSubnetworksAPI{deleteErr: someErr},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"must delete firewalls first": {
|
||||
firewalls: []string{"firewall-1", "firewall-2"},
|
||||
@ -132,7 +132,7 @@ func TestTerminateVPCs(t *testing.T) {
|
||||
operationGlobalAPI: stubOperationGlobalAPI{},
|
||||
networksAPI: stubNetworksAPI{},
|
||||
subnetworksAPI: stubSubnetworksAPI{},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -155,7 +155,7 @@ func TestTerminateVPCs(t *testing.T) {
|
||||
subnetwork: "subnetwork-id-1",
|
||||
}
|
||||
|
||||
if tc.errExpected {
|
||||
if tc.wantErr {
|
||||
assert.Error(client.TerminateVPCs(ctx))
|
||||
} else {
|
||||
assert.NoError(client.TerminateVPCs(ctx))
|
||||
@ -192,7 +192,7 @@ func TestCreateFirewall(t *testing.T) {
|
||||
operationGlobalAPI operationGlobalAPI
|
||||
firewallsAPI firewallsAPI
|
||||
firewallInput FirewallInput
|
||||
errExpected bool
|
||||
wantErr bool
|
||||
}{
|
||||
"successful create": {
|
||||
network: "network",
|
||||
@ -203,18 +203,18 @@ func TestCreateFirewall(t *testing.T) {
|
||||
network: "network",
|
||||
operationGlobalAPI: stubOperationGlobalAPI{waitErr: someErr},
|
||||
firewallsAPI: stubFirewallsAPI{},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"failed insert networks": {
|
||||
network: "network",
|
||||
operationGlobalAPI: stubOperationGlobalAPI{},
|
||||
firewallsAPI: stubFirewallsAPI{insertErr: someErr},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"no network set": {
|
||||
operationGlobalAPI: stubOperationGlobalAPI{},
|
||||
firewallsAPI: stubFirewallsAPI{},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -233,7 +233,7 @@ func TestCreateFirewall(t *testing.T) {
|
||||
firewallsAPI: tc.firewallsAPI,
|
||||
}
|
||||
|
||||
if tc.errExpected {
|
||||
if tc.wantErr {
|
||||
assert.Error(client.CreateFirewall(ctx, testFirewallInput))
|
||||
} else {
|
||||
assert.NoError(client.CreateFirewall(ctx, testFirewallInput))
|
||||
@ -250,7 +250,7 @@ func TestTerminateFirewall(t *testing.T) {
|
||||
operationGlobalAPI operationGlobalAPI
|
||||
firewallsAPI firewallsAPI
|
||||
firewalls []string
|
||||
errExpected bool
|
||||
wantErr bool
|
||||
}{
|
||||
"successful terminate": {
|
||||
operationGlobalAPI: stubOperationGlobalAPI{},
|
||||
@ -266,13 +266,13 @@ func TestTerminateFirewall(t *testing.T) {
|
||||
operationGlobalAPI: stubOperationGlobalAPI{waitErr: someErr},
|
||||
firewallsAPI: stubFirewallsAPI{},
|
||||
firewalls: []string{"firewall-1", "firewall-2"},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"failed to delete firewalls": {
|
||||
operationGlobalAPI: stubOperationGlobalAPI{},
|
||||
firewallsAPI: stubFirewallsAPI{deleteErr: someErr},
|
||||
firewalls: []string{"firewall-1", "firewall-2"},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -291,7 +291,7 @@ func TestTerminateFirewall(t *testing.T) {
|
||||
firewallsAPI: tc.firewallsAPI,
|
||||
}
|
||||
|
||||
if tc.errExpected {
|
||||
if tc.wantErr {
|
||||
assert.Error(client.TerminateFirewall(ctx))
|
||||
} else {
|
||||
assert.NoError(client.TerminateFirewall(ctx))
|
||||
|
@ -16,7 +16,7 @@ func TestAddIAMPolicyBindings(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
projectsAPI stubProjectsAPI
|
||||
input AddIAMPolicyBindingInput
|
||||
errExpected bool
|
||||
wantErr bool
|
||||
}{
|
||||
"successful set without new bindings": {
|
||||
input: AddIAMPolicyBindingInput{
|
||||
@ -37,13 +37,13 @@ func TestAddIAMPolicyBindings(t *testing.T) {
|
||||
projectsAPI: stubProjectsAPI{
|
||||
getPolicyErr: someErr,
|
||||
},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"setting iam policy fails": {
|
||||
projectsAPI: stubProjectsAPI{
|
||||
setPolicyErr: someErr,
|
||||
},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -61,7 +61,7 @@ func TestAddIAMPolicyBindings(t *testing.T) {
|
||||
}
|
||||
|
||||
err := client.addIAMPolicyBindings(ctx, tc.input)
|
||||
if tc.errExpected {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
} else {
|
||||
assert.NoError(err)
|
||||
@ -72,10 +72,10 @@ func TestAddIAMPolicyBindings(t *testing.T) {
|
||||
|
||||
func TestAddIAMPolicy(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
binding PolicyBinding
|
||||
policy *iampb.Policy
|
||||
errExpected bool
|
||||
policyExpected *iampb.Policy
|
||||
binding PolicyBinding
|
||||
policy *iampb.Policy
|
||||
wantErr bool
|
||||
wantPolicy *iampb.Policy
|
||||
}{
|
||||
"successful on empty policy": {
|
||||
binding: PolicyBinding{
|
||||
@ -85,7 +85,7 @@ func TestAddIAMPolicy(t *testing.T) {
|
||||
policy: &iampb.Policy{
|
||||
Bindings: []*iampb.Binding{},
|
||||
},
|
||||
policyExpected: &iampb.Policy{
|
||||
wantPolicy: &iampb.Policy{
|
||||
Bindings: []*iampb.Binding{
|
||||
{
|
||||
Role: "role",
|
||||
@ -107,7 +107,7 @@ func TestAddIAMPolicy(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
policyExpected: &iampb.Policy{
|
||||
wantPolicy: &iampb.Policy{
|
||||
Bindings: []*iampb.Binding{
|
||||
{
|
||||
Role: "other-role",
|
||||
@ -133,7 +133,7 @@ func TestAddIAMPolicy(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
policyExpected: &iampb.Policy{
|
||||
wantPolicy: &iampb.Policy{
|
||||
Bindings: []*iampb.Binding{
|
||||
{
|
||||
Role: "role",
|
||||
@ -155,7 +155,7 @@ func TestAddIAMPolicy(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
policyExpected: &iampb.Policy{
|
||||
wantPolicy: &iampb.Policy{
|
||||
Bindings: []*iampb.Binding{
|
||||
{
|
||||
Role: "role",
|
||||
@ -171,7 +171,7 @@ func TestAddIAMPolicy(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
addIAMPolicy(tc.policy, tc.binding)
|
||||
assert.True(proto.Equal(tc.policyExpected, tc.policy))
|
||||
assert.True(proto.Equal(tc.wantPolicy, tc.policy))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -33,7 +33,7 @@ func TestCreateServiceAccount(t *testing.T) {
|
||||
iamAPI iamAPI
|
||||
projectsAPI stubProjectsAPI
|
||||
input ServiceAccountInput
|
||||
errExpected bool
|
||||
wantErr bool
|
||||
}{
|
||||
"successful create": {
|
||||
iamAPI: stubIAMAPI{serviceAccountKeyData: keyData},
|
||||
@ -45,30 +45,30 @@ func TestCreateServiceAccount(t *testing.T) {
|
||||
iamAPI: stubIAMAPI{serviceAccountKeyData: keyData},
|
||||
},
|
||||
"creating account fails": {
|
||||
iamAPI: stubIAMAPI{createErr: someErr},
|
||||
errExpected: true,
|
||||
iamAPI: stubIAMAPI{createErr: someErr},
|
||||
wantErr: true,
|
||||
},
|
||||
"creating account key fails": {
|
||||
iamAPI: stubIAMAPI{createKeyErr: someErr},
|
||||
errExpected: true,
|
||||
iamAPI: stubIAMAPI{createKeyErr: someErr},
|
||||
wantErr: true,
|
||||
},
|
||||
"key data missing": {
|
||||
iamAPI: stubIAMAPI{},
|
||||
errExpected: true,
|
||||
iamAPI: stubIAMAPI{},
|
||||
wantErr: true,
|
||||
},
|
||||
"key data corrupt": {
|
||||
iamAPI: stubIAMAPI{serviceAccountKeyData: []byte("invalid key data")},
|
||||
errExpected: true,
|
||||
iamAPI: stubIAMAPI{serviceAccountKeyData: []byte("invalid key data")},
|
||||
wantErr: true,
|
||||
},
|
||||
"retrieving iam policy bindings fails": {
|
||||
iamAPI: stubIAMAPI{},
|
||||
projectsAPI: stubProjectsAPI{getPolicyErr: someErr},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"setting iam policy bindings fails": {
|
||||
iamAPI: stubIAMAPI{},
|
||||
projectsAPI: stubProjectsAPI{setPolicyErr: someErr},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -87,7 +87,7 @@ func TestCreateServiceAccount(t *testing.T) {
|
||||
}
|
||||
|
||||
serviceAccountKey, err := client.CreateServiceAccount(ctx, tc.input)
|
||||
if tc.errExpected {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
} else {
|
||||
assert.NoError(err)
|
||||
@ -100,8 +100,8 @@ func TestCreateServiceAccount(t *testing.T) {
|
||||
|
||||
func TestTerminateServiceAccount(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
iamAPI iamAPI
|
||||
errExpected bool
|
||||
iamAPI iamAPI
|
||||
wantErr bool
|
||||
}{
|
||||
"delete works": {
|
||||
iamAPI: stubIAMAPI{},
|
||||
@ -110,7 +110,7 @@ func TestTerminateServiceAccount(t *testing.T) {
|
||||
iamAPI: stubIAMAPI{
|
||||
deleteServiceAccountErr: errors.New("someErr"),
|
||||
},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -129,7 +129,7 @@ func TestTerminateServiceAccount(t *testing.T) {
|
||||
}
|
||||
|
||||
err := client.TerminateServiceAccount(ctx)
|
||||
if tc.errExpected {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
} else {
|
||||
assert.NoError(err)
|
||||
|
@ -9,22 +9,22 @@ import (
|
||||
func TestIDs(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
testState := testInstances()
|
||||
expectedIDs := []string{"id-9", "id-10", "id-11", "id-12"}
|
||||
assert.ElementsMatch(expectedIDs, testState.IDs())
|
||||
wantIDs := []string{"id-9", "id-10", "id-11", "id-12"}
|
||||
assert.ElementsMatch(wantIDs, testState.IDs())
|
||||
}
|
||||
|
||||
func TestPublicIPs(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
testState := testInstances()
|
||||
expectedIPs := []string{"192.0.2.1", "192.0.2.3", "192.0.2.5", "192.0.2.7"}
|
||||
assert.ElementsMatch(expectedIPs, testState.PublicIPs())
|
||||
wantIPs := []string{"192.0.2.1", "192.0.2.3", "192.0.2.5", "192.0.2.7"}
|
||||
assert.ElementsMatch(wantIPs, testState.PublicIPs())
|
||||
}
|
||||
|
||||
func TestPrivateIPs(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
testState := testInstances()
|
||||
expectedIPs := []string{"192.0.2.2", "192.0.2.4", "192.0.2.6", "192.0.2.8"}
|
||||
assert.ElementsMatch(expectedIPs, testState.PrivateIPs())
|
||||
wantIPs := []string{"192.0.2.2", "192.0.2.4", "192.0.2.6", "192.0.2.8"}
|
||||
assert.ElementsMatch(wantIPs, testState.PrivateIPs())
|
||||
}
|
||||
|
||||
func TestGetOne(t *testing.T) {
|
||||
@ -43,9 +43,9 @@ func TestGetOthers(t *testing.T) {
|
||||
for _, id := range testCases {
|
||||
others := testInstances().GetOthers(id)
|
||||
assert.NotContains(others, id)
|
||||
expectedInstances := testInstances()
|
||||
delete(expectedInstances, id)
|
||||
assert.ElementsMatch(others.IDs(), expectedInstances.IDs())
|
||||
wantInstances := testInstances()
|
||||
delete(wantInstances, id)
|
||||
assert.ElementsMatch(others.IDs(), wantInstances.IDs())
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -73,42 +73,42 @@ func TestActivate(t *testing.T) {
|
||||
avpn *stubAVPNClient
|
||||
userPublicKey string
|
||||
ips []string
|
||||
errExpected bool
|
||||
wantErr bool
|
||||
}{
|
||||
"normal activation": {
|
||||
avpn: &stubAVPNClient{},
|
||||
userPublicKey: testKey,
|
||||
ips: []string{"192.0.2.1", "192.0.2.1", "192.0.2.1"},
|
||||
errExpected: false,
|
||||
wantErr: false,
|
||||
},
|
||||
"client without avpn": {
|
||||
userPublicKey: testKey,
|
||||
ips: []string{"192.0.2.1", "192.0.2.1", "192.0.2.1"},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"empty public key parameter": {
|
||||
avpn: &stubAVPNClient{},
|
||||
userPublicKey: "",
|
||||
ips: []string{"192.0.2.1", "192.0.2.1", "192.0.2.1"},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"invalid public key parameter": {
|
||||
avpn: &stubAVPNClient{},
|
||||
userPublicKey: "invalid Key",
|
||||
ips: []string{"192.0.2.1", "192.0.2.1", "192.0.2.1"},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"empty ips parameter": {
|
||||
avpn: &stubAVPNClient{},
|
||||
userPublicKey: testKey,
|
||||
ips: []string{},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"fail ActivateAsCoordinator": {
|
||||
avpn: &stubAVPNClient{activateAsCoordinatorErr: someErr},
|
||||
userPublicKey: testKey,
|
||||
ips: []string{"192.0.2.1", "192.0.2.1", "192.0.2.1"},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -121,7 +121,7 @@ func TestActivate(t *testing.T) {
|
||||
client.avpn = tc.avpn
|
||||
}
|
||||
_, err := client.Activate(context.Background(), []byte(tc.userPublicKey), []byte("Constellation"), tc.ips, nil, nil, "serviceaccount://test")
|
||||
if tc.errExpected {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
} else {
|
||||
assert.NoError(err)
|
||||
|
@ -89,32 +89,32 @@ func TestNextLog(t *testing.T) {
|
||||
someErr := errors.New("failed")
|
||||
|
||||
testCases := map[string]struct {
|
||||
msgs []*pubproto.ActivateAsCoordinatorResponse
|
||||
logLenExpected int
|
||||
stateExpected bool
|
||||
recvErr error
|
||||
errExpected bool
|
||||
msgs []*pubproto.ActivateAsCoordinatorResponse
|
||||
wantLogLen int
|
||||
wantState bool
|
||||
recvErr error
|
||||
wantErr bool
|
||||
}{
|
||||
"some logs": {
|
||||
msgs: []*pubproto.ActivateAsCoordinatorResponse{testLogResp, testLogResp, testLogResp},
|
||||
logLenExpected: 3,
|
||||
msgs: []*pubproto.ActivateAsCoordinatorResponse{testLogResp, testLogResp, testLogResp},
|
||||
wantLogLen: 3,
|
||||
},
|
||||
"only admin config": {
|
||||
msgs: []*pubproto.ActivateAsCoordinatorResponse{testConfigResp},
|
||||
stateExpected: true,
|
||||
msgs: []*pubproto.ActivateAsCoordinatorResponse{testConfigResp},
|
||||
wantState: true,
|
||||
},
|
||||
"logs and configs": {
|
||||
msgs: []*pubproto.ActivateAsCoordinatorResponse{testLogResp, testConfigResp, testLogResp, testConfigResp},
|
||||
logLenExpected: 2,
|
||||
stateExpected: true,
|
||||
msgs: []*pubproto.ActivateAsCoordinatorResponse{testLogResp, testConfigResp, testLogResp, testConfigResp},
|
||||
wantLogLen: 2,
|
||||
wantState: true,
|
||||
},
|
||||
"no response": {
|
||||
msgs: []*pubproto.ActivateAsCoordinatorResponse{},
|
||||
logLenExpected: 0,
|
||||
msgs: []*pubproto.ActivateAsCoordinatorResponse{},
|
||||
wantLogLen: 0,
|
||||
},
|
||||
"recv fail": {
|
||||
recvErr: someErr,
|
||||
errExpected: true,
|
||||
recvErr: someErr,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -141,15 +141,15 @@ func TestNextLog(t *testing.T) {
|
||||
}
|
||||
|
||||
assert.Error(err)
|
||||
if tc.errExpected {
|
||||
if tc.wantErr {
|
||||
assert.NotErrorIs(err, io.EOF)
|
||||
return
|
||||
}
|
||||
|
||||
assert.ErrorIs(err, io.EOF)
|
||||
assert.Len(logs, tc.logLenExpected)
|
||||
assert.Len(logs, tc.wantLogLen)
|
||||
|
||||
if tc.stateExpected {
|
||||
if tc.wantState {
|
||||
ip, err := client.GetClientVpnIp()
|
||||
assert.NoError(err)
|
||||
assert.Equal(testClientVpnIp, ip)
|
||||
|
@ -22,7 +22,7 @@ func TestTLSConfig(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
issuer Issuer
|
||||
validators []Validator
|
||||
expectErr bool
|
||||
wantErr bool
|
||||
}{
|
||||
"basic": {
|
||||
issuer: fakeIssuer{fakeOID: oid1},
|
||||
@ -35,12 +35,12 @@ func TestTLSConfig(t *testing.T) {
|
||||
"validate error": {
|
||||
issuer: fakeIssuer{fakeOID: oid1},
|
||||
validators: []Validator{fakeValidator{fakeOID: oid1, err: errors.New("failed")}},
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"unknown oid": {
|
||||
issuer: fakeIssuer{fakeOID: oid1},
|
||||
validators: []Validator{fakeValidator{fakeOID: oid2}},
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -79,7 +79,7 @@ func TestTLSConfig(t *testing.T) {
|
||||
req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, server.URL, http.NoBody)
|
||||
require.NoError(err)
|
||||
resp, err := client.Do(req)
|
||||
if tc.expectErr {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
return
|
||||
}
|
||||
|
@ -11,12 +11,12 @@ import (
|
||||
|
||||
func TestGetSNPAttestation(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
tpmFunc vtpm.TPMOpenFunc
|
||||
errExpected bool
|
||||
tpmFunc vtpm.TPMOpenFunc
|
||||
wantErr bool
|
||||
}{
|
||||
"success": {
|
||||
tpmFunc: simulator.OpenSimulatedTPM,
|
||||
errExpected: false,
|
||||
tpmFunc: simulator.OpenSimulatedTPM,
|
||||
wantErr: false,
|
||||
},
|
||||
}
|
||||
|
||||
@ -30,7 +30,7 @@ func TestGetSNPAttestation(t *testing.T) {
|
||||
defer tpm.Close()
|
||||
|
||||
_, err = getSNPAttestation(tpm)
|
||||
if tc.errExpected {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
} else {
|
||||
assert.NoError(err)
|
||||
|
@ -25,12 +25,12 @@ func TestTrustedKeyFromSNP(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
key []byte
|
||||
instanceInfo []byte
|
||||
errExpected bool
|
||||
wantErr bool
|
||||
}{
|
||||
"success": {
|
||||
key: akPub,
|
||||
instanceInfo: []byte{},
|
||||
errExpected: false,
|
||||
wantErr: false,
|
||||
},
|
||||
}
|
||||
|
||||
@ -39,7 +39,7 @@ func TestTrustedKeyFromSNP(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
key, err := trustedKeyFromSNP(tc.key, tc.instanceInfo)
|
||||
if tc.errExpected {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
} else {
|
||||
assert.NoError(err)
|
||||
@ -51,12 +51,12 @@ func TestTrustedKeyFromSNP(t *testing.T) {
|
||||
|
||||
func TestValidateAzureCVM(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
attDoc vtpm.AttestationDocument
|
||||
errExpected bool
|
||||
attDoc vtpm.AttestationDocument
|
||||
wantErr bool
|
||||
}{
|
||||
"success": {
|
||||
attDoc: vtpm.AttestationDocument{},
|
||||
errExpected: false,
|
||||
attDoc: vtpm.AttestationDocument{},
|
||||
wantErr: false,
|
||||
},
|
||||
}
|
||||
|
||||
@ -65,7 +65,7 @@ func TestValidateAzureCVM(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
err := validateAzureCVM(tc.attDoc)
|
||||
if tc.errExpected {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
} else {
|
||||
assert.NoError(err)
|
||||
|
@ -13,8 +13,8 @@ import (
|
||||
|
||||
func TestGetGCEInstanceInfo(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
client fakeMetadataClient
|
||||
errExpected bool
|
||||
client fakeMetadataClient
|
||||
wantErr bool
|
||||
}{
|
||||
"success": {
|
||||
client: fakeMetadataClient{
|
||||
@ -22,7 +22,7 @@ func TestGetGCEInstanceInfo(t *testing.T) {
|
||||
instanceNameString: "instanceName",
|
||||
zoneString: "zone",
|
||||
},
|
||||
errExpected: false,
|
||||
wantErr: false,
|
||||
},
|
||||
"projectID error": {
|
||||
client: fakeMetadataClient{
|
||||
@ -31,7 +31,7 @@ func TestGetGCEInstanceInfo(t *testing.T) {
|
||||
zoneString: "zone",
|
||||
projecIdErr: errors.New("error"),
|
||||
},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"instanceName error": {
|
||||
client: fakeMetadataClient{
|
||||
@ -40,7 +40,7 @@ func TestGetGCEInstanceInfo(t *testing.T) {
|
||||
zoneString: "zone",
|
||||
instanceNameErr: errors.New("error"),
|
||||
},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"zone error": {
|
||||
client: fakeMetadataClient{
|
||||
@ -49,7 +49,7 @@ func TestGetGCEInstanceInfo(t *testing.T) {
|
||||
zoneString: "zone",
|
||||
zoneErr: errors.New("error"),
|
||||
},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -60,7 +60,7 @@ func TestGetGCEInstanceInfo(t *testing.T) {
|
||||
var tpm io.ReadWriteCloser
|
||||
|
||||
out, err := getGCEInstanceInfo(tc.client)(tpm)
|
||||
if tc.errExpected {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
} else {
|
||||
assert.NoError(err)
|
||||
|
@ -19,8 +19,8 @@ import (
|
||||
|
||||
func TestGceNonHostInfoEvent(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
attDoc vtpm.AttestationDocument
|
||||
errExpected bool
|
||||
attDoc vtpm.AttestationDocument
|
||||
wantErr bool
|
||||
}{
|
||||
"is cvm": {
|
||||
attDoc: vtpm.AttestationDocument{
|
||||
@ -33,7 +33,7 @@ func TestGceNonHostInfoEvent(t *testing.T) {
|
||||
attDoc: vtpm.AttestationDocument{
|
||||
Attestation: nil,
|
||||
},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"missing GCE Non-Host info event": {
|
||||
attDoc: vtpm.AttestationDocument{
|
||||
@ -41,7 +41,7 @@ func TestGceNonHostInfoEvent(t *testing.T) {
|
||||
EventLog: []byte("No GCE Event"),
|
||||
},
|
||||
},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"not a cvm": {
|
||||
attDoc: vtpm.AttestationDocument{
|
||||
@ -49,7 +49,7 @@ func TestGceNonHostInfoEvent(t *testing.T) {
|
||||
EventLog: []byte("\x00\x00\x00GCE NonHostInfo\x00\x00\x00\x00"),
|
||||
},
|
||||
},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -57,7 +57,7 @@ func TestGceNonHostInfoEvent(t *testing.T) {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
err := gceNonHostInfoEvent(tc.attDoc)
|
||||
if tc.errExpected {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
} else {
|
||||
assert.NoError(err)
|
||||
@ -85,7 +85,7 @@ Y+t5OxL3kL15VzY1Ob0d5cMCAwEAAQ==
|
||||
testCases := map[string]struct {
|
||||
instanceInfo []byte
|
||||
getClient func(ctx context.Context, opts ...option.ClientOption) (gcpRestClient, error)
|
||||
errExpected bool
|
||||
wantErr bool
|
||||
}{
|
||||
"success": {
|
||||
instanceInfo: mustMarshal(attest.GCEInstanceInfo{}, require.New(t)),
|
||||
@ -94,7 +94,7 @@ Y+t5OxL3kL15VzY1Ob0d5cMCAwEAAQ==
|
||||
EkPub: proto.String(testPubK),
|
||||
},
|
||||
}, nil, nil),
|
||||
errExpected: false,
|
||||
wantErr: false,
|
||||
},
|
||||
"Unmarshal error": {
|
||||
instanceInfo: []byte("error"),
|
||||
@ -103,12 +103,12 @@ Y+t5OxL3kL15VzY1Ob0d5cMCAwEAAQ==
|
||||
EkPub: proto.String(testPubK),
|
||||
},
|
||||
}, nil, nil),
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"empty signing key": {
|
||||
instanceInfo: mustMarshal(attest.GCEInstanceInfo{}, require.New(t)),
|
||||
getClient: prepareFakeClient(&computepb.ShieldedInstanceIdentity{}, nil, nil),
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"new client error": {
|
||||
instanceInfo: mustMarshal(attest.GCEInstanceInfo{}, require.New(t)),
|
||||
@ -117,7 +117,7 @@ Y+t5OxL3kL15VzY1Ob0d5cMCAwEAAQ==
|
||||
EkPub: proto.String(testPubK),
|
||||
},
|
||||
}, errors.New("error"), nil),
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"GetShieldedInstanceIdentity error": {
|
||||
instanceInfo: mustMarshal(attest.GCEInstanceInfo{}, require.New(t)),
|
||||
@ -126,7 +126,7 @@ Y+t5OxL3kL15VzY1Ob0d5cMCAwEAAQ==
|
||||
EkPub: proto.String(testPubK),
|
||||
},
|
||||
}, nil, errors.New("error")),
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"Decode error": {
|
||||
instanceInfo: mustMarshal(attest.GCEInstanceInfo{}, require.New(t)),
|
||||
@ -135,7 +135,7 @@ Y+t5OxL3kL15VzY1Ob0d5cMCAwEAAQ==
|
||||
EkPub: proto.String("Not a public key"),
|
||||
},
|
||||
}, nil, nil),
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -145,7 +145,7 @@ Y+t5OxL3kL15VzY1Ob0d5cMCAwEAAQ==
|
||||
|
||||
out, err := trustedKeyFromGCEAPI(tc.getClient)(nil, tc.instanceInfo)
|
||||
|
||||
if tc.errExpected {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
} else {
|
||||
assert.NoError(err)
|
||||
|
@ -83,16 +83,16 @@ func TestValidate(t *testing.T) {
|
||||
require.Equal(challenge, out)
|
||||
|
||||
testCases := map[string]struct {
|
||||
validator *Validator
|
||||
attDoc []byte
|
||||
nonce []byte
|
||||
errExpected bool
|
||||
validator *Validator
|
||||
attDoc []byte
|
||||
nonce []byte
|
||||
wantErr bool
|
||||
}{
|
||||
"invalid nonce": {
|
||||
validator: NewValidator(fakeTrustedPcrs, fakeGetTrustedKey, fakeValidateCVM, VerifyPKCS1v15),
|
||||
attDoc: mustMarshalAttestation(attDoc, require),
|
||||
nonce: []byte{4, 3, 2, 1},
|
||||
errExpected: true,
|
||||
validator: NewValidator(fakeTrustedPcrs, fakeGetTrustedKey, fakeValidateCVM, VerifyPKCS1v15),
|
||||
attDoc: mustMarshalAttestation(attDoc, require),
|
||||
nonce: []byte{4, 3, 2, 1},
|
||||
wantErr: true,
|
||||
},
|
||||
"invalid signature": {
|
||||
validator: NewValidator(fakeTrustedPcrs, fakeGetTrustedKey, fakeValidateCVM, VerifyPKCS1v15),
|
||||
@ -102,8 +102,8 @@ func TestValidate(t *testing.T) {
|
||||
UserData: []byte("wrong data"),
|
||||
UserDataSignature: attDoc.UserDataSignature,
|
||||
}, require),
|
||||
nonce: nonce,
|
||||
errExpected: true,
|
||||
nonce: nonce,
|
||||
wantErr: true,
|
||||
},
|
||||
"untrusted attestation public key": {
|
||||
validator: NewValidator(
|
||||
@ -112,9 +112,9 @@ func TestValidate(t *testing.T) {
|
||||
return nil, errors.New("untrusted")
|
||||
},
|
||||
fakeValidateCVM, VerifyPKCS1v15),
|
||||
attDoc: mustMarshalAttestation(attDoc, require),
|
||||
nonce: nonce,
|
||||
errExpected: true,
|
||||
attDoc: mustMarshalAttestation(attDoc, require),
|
||||
nonce: nonce,
|
||||
wantErr: true,
|
||||
},
|
||||
"not a CVM": {
|
||||
validator: NewValidator(
|
||||
@ -124,9 +124,9 @@ func TestValidate(t *testing.T) {
|
||||
return errors.New("untrusted")
|
||||
},
|
||||
VerifyPKCS1v15),
|
||||
attDoc: mustMarshalAttestation(attDoc, require),
|
||||
nonce: nonce,
|
||||
errExpected: true,
|
||||
attDoc: mustMarshalAttestation(attDoc, require),
|
||||
nonce: nonce,
|
||||
wantErr: true,
|
||||
},
|
||||
"untrusted PCRs": {
|
||||
validator: NewValidator(
|
||||
@ -136,9 +136,9 @@ func TestValidate(t *testing.T) {
|
||||
fakeGetTrustedKey,
|
||||
fakeValidateCVM,
|
||||
VerifyPKCS1v15),
|
||||
attDoc: mustMarshalAttestation(attDoc, require),
|
||||
nonce: nonce,
|
||||
errExpected: true,
|
||||
attDoc: mustMarshalAttestation(attDoc, require),
|
||||
nonce: nonce,
|
||||
wantErr: true,
|
||||
},
|
||||
"no sha256 quote": {
|
||||
validator: NewValidator(fakeTrustedPcrs, fakeGetTrustedKey, fakeValidateCVM, VerifyPKCS1v15),
|
||||
@ -155,14 +155,14 @@ func TestValidate(t *testing.T) {
|
||||
UserData: attDoc.UserData,
|
||||
UserDataSignature: attDoc.UserDataSignature,
|
||||
}, require),
|
||||
nonce: nonce,
|
||||
errExpected: true,
|
||||
nonce: nonce,
|
||||
wantErr: true,
|
||||
},
|
||||
"invalid attestation document": {
|
||||
validator: NewValidator(fakeTrustedPcrs, fakeGetTrustedKey, fakeValidateCVM, VerifyPKCS1v15),
|
||||
attDoc: []byte("invalid attestation"),
|
||||
nonce: nonce,
|
||||
errExpected: true,
|
||||
validator: NewValidator(fakeTrustedPcrs, fakeGetTrustedKey, fakeValidateCVM, VerifyPKCS1v15),
|
||||
attDoc: []byte("invalid attestation"),
|
||||
nonce: nonce,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -171,7 +171,7 @@ func TestValidate(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
_, err = tc.validator.Validate(tc.attDoc, tc.nonce)
|
||||
if tc.errExpected {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
} else {
|
||||
assert.NoError(err)
|
||||
@ -248,9 +248,9 @@ func TestFailIssuer(t *testing.T) {
|
||||
|
||||
func TestGetSHA256QuoteIndex(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
quotes []*tpm.Quote
|
||||
idxExpected int
|
||||
errExpected bool
|
||||
quotes []*tpm.Quote
|
||||
wantIdx int
|
||||
wantErr bool
|
||||
}{
|
||||
"idx 0 is valid": {
|
||||
quotes: []*tpm.Quote{
|
||||
@ -260,8 +260,8 @@ func TestGetSHA256QuoteIndex(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
idxExpected: 0,
|
||||
errExpected: false,
|
||||
wantIdx: 0,
|
||||
wantErr: false,
|
||||
},
|
||||
"idx 1 is valid": {
|
||||
quotes: []*tpm.Quote{
|
||||
@ -276,16 +276,16 @@ func TestGetSHA256QuoteIndex(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
idxExpected: 1,
|
||||
errExpected: false,
|
||||
wantIdx: 1,
|
||||
wantErr: false,
|
||||
},
|
||||
"no quotes": {
|
||||
quotes: nil,
|
||||
errExpected: true,
|
||||
quotes: nil,
|
||||
wantErr: true,
|
||||
},
|
||||
"quotes is nil": {
|
||||
quotes: make([]*tpm.Quote, 2),
|
||||
errExpected: true,
|
||||
quotes: make([]*tpm.Quote, 2),
|
||||
wantErr: true,
|
||||
},
|
||||
"pcrs is nil": {
|
||||
quotes: []*tpm.Quote{
|
||||
@ -298,7 +298,7 @@ func TestGetSHA256QuoteIndex(t *testing.T) {
|
||||
Pcrs: nil,
|
||||
},
|
||||
},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -307,12 +307,12 @@ func TestGetSHA256QuoteIndex(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
idx, err := GetSHA256QuoteIndex(tc.quotes)
|
||||
if tc.errExpected {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
assert.Equal(0, idx)
|
||||
} else {
|
||||
assert.NoError(err)
|
||||
assert.Equal(tc.idxExpected, idx)
|
||||
assert.Equal(tc.wantIdx, idx)
|
||||
}
|
||||
})
|
||||
}
|
||||
@ -322,7 +322,7 @@ func TestGetSelectedPCRs(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
openFunc TPMOpenFunc
|
||||
pcrSelection tpm2.PCRSelection
|
||||
errExpected bool
|
||||
wantErr bool
|
||||
}{
|
||||
"error": {
|
||||
openFunc: func() (io.ReadWriteCloser, error) { return nil, errors.New("error") },
|
||||
@ -330,7 +330,7 @@ func TestGetSelectedPCRs(t *testing.T) {
|
||||
Hash: tpm2.AlgSHA256,
|
||||
PCRs: []int{0, 1, 2},
|
||||
},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"3 PCRs": {
|
||||
openFunc: tpmsim.OpenSimulatedTPM,
|
||||
@ -355,7 +355,7 @@ func TestGetSelectedPCRs(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
pcrs, err := GetSelectedPCRs(tc.openFunc, tc.pcrSelection)
|
||||
if tc.errExpected {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
} else {
|
||||
require.NoError(err)
|
||||
|
@ -54,22 +54,22 @@ func TestIsNodeInitialized(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
pcrValueOwnerID []byte
|
||||
pcrValueClusterID []byte
|
||||
expectInitialized bool
|
||||
expectErr bool
|
||||
wantInitialized bool
|
||||
wantErr bool
|
||||
}{
|
||||
"uninitialized PCRs results in uninitialized node": {},
|
||||
"initializing PCRs result in initialized node": {
|
||||
pcrValueOwnerID: []byte{0x0, 0x1, 0x2, 0x3},
|
||||
pcrValueClusterID: []byte{0x4, 0x5, 0x6, 0x7},
|
||||
expectInitialized: true,
|
||||
wantInitialized: true,
|
||||
},
|
||||
"initializing ownerID alone fails": {
|
||||
pcrValueOwnerID: []byte{0x0, 0x1, 0x2, 0x3},
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"initializing clusterID alone fails": {
|
||||
pcrValueClusterID: []byte{0x4, 0x5, 0x6, 0x7},
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -89,12 +89,12 @@ func TestIsNodeInitialized(t *testing.T) {
|
||||
initialized, err := IsNodeInitialized(func() (io.ReadWriteCloser, error) {
|
||||
return &simTPMNOPCloser{tpm}, nil
|
||||
})
|
||||
if tc.expectErr {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
return
|
||||
}
|
||||
require.NoError(err)
|
||||
require.Equal(tc.expectInitialized, initialized)
|
||||
require.Equal(tc.wantInitialized, initialized)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -15,13 +15,13 @@ func TestAutoscalerSecrets(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
instance core.Instance
|
||||
cloudServiceAccountURI string
|
||||
expectedSecrets resources.Secrets
|
||||
expectErr bool
|
||||
wantSecrets resources.Secrets
|
||||
wantErr bool
|
||||
}{
|
||||
"Secrets works": {
|
||||
instance: core.Instance{ProviderID: "azure:///subscriptions/subscription-id/resourceGroups/resource-group/providers/Microsoft.Compute/virtualMachines/instance-name"},
|
||||
cloudServiceAccountURI: "serviceaccount://azure?tenant_id=tenant-id&client_id=client-id&client_secret=client-secret",
|
||||
expectedSecrets: resources.Secrets{
|
||||
wantSecrets: resources.Secrets{
|
||||
&k8s.Secret{
|
||||
TypeMeta: meta.TypeMeta{
|
||||
Kind: "Secret",
|
||||
@ -43,13 +43,13 @@ func TestAutoscalerSecrets(t *testing.T) {
|
||||
},
|
||||
},
|
||||
"invalid providerID fails": {
|
||||
instance: core.Instance{ProviderID: "invalid"},
|
||||
expectErr: true,
|
||||
instance: core.Instance{ProviderID: "invalid"},
|
||||
wantErr: true,
|
||||
},
|
||||
"invalid cloudServiceAccountURI fails": {
|
||||
instance: core.Instance{ProviderID: "azure:///subscriptions/subscription-id/resourceGroups/resource-group/providers/Microsoft.Compute/virtualMachines/instance-name"},
|
||||
cloudServiceAccountURI: "invalid",
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -60,12 +60,12 @@ func TestAutoscalerSecrets(t *testing.T) {
|
||||
|
||||
autoscaler := Autoscaler{}
|
||||
secrets, err := autoscaler.Secrets(tc.instance, tc.cloudServiceAccountURI)
|
||||
if tc.expectErr {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
return
|
||||
}
|
||||
require.NoError(err)
|
||||
assert.Equal(tc.expectedSecrets, secrets)
|
||||
assert.Equal(tc.wantSecrets, secrets)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -15,13 +15,13 @@ func TestSecrets(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
instance core.Instance
|
||||
cloudServiceAccountURI string
|
||||
expectedSecrets resources.Secrets
|
||||
expectErr bool
|
||||
wantSecrets resources.Secrets
|
||||
wantErr bool
|
||||
}{
|
||||
"Secrets works": {
|
||||
instance: core.Instance{ProviderID: "azure:///subscriptions/subscription-id/resourceGroups/resource-group/providers/Microsoft.Compute/virtualMachines/instance-name"},
|
||||
cloudServiceAccountURI: "serviceaccount://azure?tenant_id=tenant-id&client_id=client-id&client_secret=client-secret&location=location",
|
||||
expectedSecrets: resources.Secrets{
|
||||
wantSecrets: resources.Secrets{
|
||||
&k8s.Secret{
|
||||
TypeMeta: meta.TypeMeta{
|
||||
Kind: "Secret",
|
||||
@ -40,7 +40,7 @@ func TestSecrets(t *testing.T) {
|
||||
"Secrets works for scale sets": {
|
||||
instance: core.Instance{ProviderID: "azure:///subscriptions/subscription-id/resourceGroups/resource-group/providers/Microsoft.Compute/virtualMachineScaleSets/scale-set-name/virtualMachines/instance-id"},
|
||||
cloudServiceAccountURI: "serviceaccount://azure?tenant_id=tenant-id&client_id=client-id&client_secret=client-secret&location=location",
|
||||
expectedSecrets: resources.Secrets{
|
||||
wantSecrets: resources.Secrets{
|
||||
&k8s.Secret{
|
||||
TypeMeta: meta.TypeMeta{
|
||||
Kind: "Secret",
|
||||
@ -57,13 +57,13 @@ func TestSecrets(t *testing.T) {
|
||||
},
|
||||
},
|
||||
"invalid providerID fails": {
|
||||
instance: core.Instance{ProviderID: "invalid"},
|
||||
expectErr: true,
|
||||
instance: core.Instance{ProviderID: "invalid"},
|
||||
wantErr: true,
|
||||
},
|
||||
"invalid cloudServiceAccountURI fails": {
|
||||
instance: core.Instance{ProviderID: "azure:///subscriptions/subscription-id/resourceGroups/resource-group/providers/Microsoft.Compute/virtualMachines/instance-name"},
|
||||
cloudServiceAccountURI: "invalid",
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -74,12 +74,12 @@ func TestSecrets(t *testing.T) {
|
||||
|
||||
cloud := CloudControllerManager{}
|
||||
secrets, err := cloud.Secrets(tc.instance, tc.cloudServiceAccountURI)
|
||||
if tc.expectErr {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
return
|
||||
}
|
||||
require.NoError(err)
|
||||
assert.Equal(tc.expectedSecrets, secrets)
|
||||
assert.Equal(tc.wantSecrets, secrets)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -23,19 +23,19 @@ func TestRetrieve(t *testing.T) {
|
||||
},
|
||||
}
|
||||
testCases := map[string]struct {
|
||||
server httpBufconnServer
|
||||
expectErr bool
|
||||
expectedResponse metadataResponse
|
||||
server httpBufconnServer
|
||||
wantErr bool
|
||||
wantResponse metadataResponse
|
||||
}{
|
||||
"metadata response parsed": {
|
||||
server: newHTTPBufconnServerWithMetadataResponse(response),
|
||||
expectedResponse: response,
|
||||
server: newHTTPBufconnServerWithMetadataResponse(response),
|
||||
wantResponse: response,
|
||||
},
|
||||
"invalid imds response detected": {
|
||||
server: newHTTPBufconnServer(func(writer http.ResponseWriter, request *http.Request) {
|
||||
fmt.Fprintln(writer, "invalid-result")
|
||||
}),
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -59,12 +59,12 @@ func TestRetrieve(t *testing.T) {
|
||||
}
|
||||
resp, err := iClient.Retrieve(context.Background())
|
||||
|
||||
if tc.expectErr {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
return
|
||||
}
|
||||
require.NoError(err)
|
||||
assert.Equal(tc.expectedResponse, resp)
|
||||
assert.Equal(tc.wantResponse, resp)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -15,7 +15,7 @@ import (
|
||||
)
|
||||
|
||||
func TestList(t *testing.T) {
|
||||
expectedInstances := []core.Instance{
|
||||
wantInstances := []core.Instance{
|
||||
{
|
||||
Name: "instance-name",
|
||||
ProviderID: "azure:///subscriptions/subscription-id/resourceGroups/resource-group/providers/Microsoft.Compute/virtualMachines/instance-name",
|
||||
@ -36,8 +36,8 @@ func TestList(t *testing.T) {
|
||||
virtualMachinesAPI virtualMachinesAPI
|
||||
virtualMachineScaleSetVMsAPI virtualMachineScaleSetVMsAPI
|
||||
tagsAPI tagsAPI
|
||||
expectErr bool
|
||||
expectedInstances []core.Instance
|
||||
wantErr bool
|
||||
wantInstances []core.Instance
|
||||
}{
|
||||
"List works": {
|
||||
imdsAPI: newIMDSStub(),
|
||||
@ -46,20 +46,20 @@ func TestList(t *testing.T) {
|
||||
virtualMachinesAPI: newVirtualMachinesStub(),
|
||||
virtualMachineScaleSetVMsAPI: newVirtualMachineScaleSetsVMsStub(),
|
||||
tagsAPI: newTagsStub(),
|
||||
expectedInstances: expectedInstances,
|
||||
wantInstances: wantInstances,
|
||||
},
|
||||
"providerID cannot be retrieved": {
|
||||
imdsAPI: &stubIMDSAPI{retrieveErr: errors.New("imds err")},
|
||||
expectErr: true,
|
||||
imdsAPI: &stubIMDSAPI{retrieveErr: errors.New("imds err")},
|
||||
wantErr: true,
|
||||
},
|
||||
"providerID cannot be parsed": {
|
||||
imdsAPI: newInvalidIMDSStub(),
|
||||
expectErr: true,
|
||||
imdsAPI: newInvalidIMDSStub(),
|
||||
wantErr: true,
|
||||
},
|
||||
"listVMs fails": {
|
||||
imdsAPI: newIMDSStub(),
|
||||
virtualMachinesAPI: newFailingListsVirtualMachinesStub(),
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"listScaleSetVMs fails": {
|
||||
imdsAPI: newIMDSStub(),
|
||||
@ -68,7 +68,7 @@ func TestList(t *testing.T) {
|
||||
virtualMachinesAPI: newVirtualMachinesStub(),
|
||||
virtualMachineScaleSetVMsAPI: newFailingListsVirtualMachineScaleSetsVMsStub(),
|
||||
tagsAPI: newTagsStub(),
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -87,24 +87,24 @@ func TestList(t *testing.T) {
|
||||
}
|
||||
instances, err := metadata.List(context.Background())
|
||||
|
||||
if tc.expectErr {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
return
|
||||
}
|
||||
require.NoError(err)
|
||||
assert.ElementsMatch(tc.expectedInstances, instances)
|
||||
assert.ElementsMatch(tc.wantInstances, instances)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestSelf(t *testing.T) {
|
||||
expectedVMInstance := core.Instance{
|
||||
wantVMInstance := core.Instance{
|
||||
Name: "instance-name",
|
||||
ProviderID: "azure:///subscriptions/subscription-id/resourceGroups/resource-group/providers/Microsoft.Compute/virtualMachines/instance-name",
|
||||
IPs: []string{"192.0.2.0"},
|
||||
SSHKeys: map[string][]string{"user": {"key-data"}},
|
||||
}
|
||||
expectedScaleSetInstance := core.Instance{
|
||||
wantScaleSetInstance := core.Instance{
|
||||
Name: "scale-set-name-instance-id",
|
||||
ProviderID: "azure:///subscriptions/subscription-id/resourceGroups/resource-group/providers/Microsoft.Compute/virtualMachineScaleSets/scale-set-name/virtualMachines/instance-id",
|
||||
IPs: []string{"192.0.2.0"},
|
||||
@ -115,31 +115,31 @@ func TestSelf(t *testing.T) {
|
||||
networkInterfacesAPI networkInterfacesAPI
|
||||
virtualMachinesAPI virtualMachinesAPI
|
||||
virtualMachineScaleSetVMsAPI virtualMachineScaleSetVMsAPI
|
||||
expectErr bool
|
||||
expectedInstance core.Instance
|
||||
wantErr bool
|
||||
wantInstance core.Instance
|
||||
}{
|
||||
"self for individual instance works": {
|
||||
imdsAPI: newIMDSStub(),
|
||||
networkInterfacesAPI: newNetworkInterfacesStub(),
|
||||
virtualMachinesAPI: newVirtualMachinesStub(),
|
||||
virtualMachineScaleSetVMsAPI: newVirtualMachineScaleSetsVMsStub(),
|
||||
expectedInstance: expectedVMInstance,
|
||||
wantInstance: wantVMInstance,
|
||||
},
|
||||
"self for scale set instance works": {
|
||||
imdsAPI: newScaleSetIMDSStub(),
|
||||
networkInterfacesAPI: newNetworkInterfacesStub(),
|
||||
virtualMachinesAPI: newVirtualMachinesStub(),
|
||||
virtualMachineScaleSetVMsAPI: newVirtualMachineScaleSetsVMsStub(),
|
||||
expectedInstance: expectedScaleSetInstance,
|
||||
wantInstance: wantScaleSetInstance,
|
||||
},
|
||||
"providerID cannot be retrieved": {
|
||||
imdsAPI: &stubIMDSAPI{retrieveErr: errors.New("imds err")},
|
||||
expectErr: true,
|
||||
imdsAPI: &stubIMDSAPI{retrieveErr: errors.New("imds err")},
|
||||
wantErr: true,
|
||||
},
|
||||
"GetInstance fails": {
|
||||
imdsAPI: newIMDSStub(),
|
||||
virtualMachinesAPI: newFailingGetVirtualMachinesStub(),
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -156,21 +156,21 @@ func TestSelf(t *testing.T) {
|
||||
}
|
||||
instance, err := metadata.Self(context.Background())
|
||||
|
||||
if tc.expectErr {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
return
|
||||
}
|
||||
require.NoError(err)
|
||||
assert.Equal(tc.expectedInstance, instance)
|
||||
assert.Equal(tc.wantInstance, instance)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestSignalRole(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
imdsAPI imdsAPI
|
||||
tagsAPI tagsAPI
|
||||
expectErr bool
|
||||
imdsAPI imdsAPI
|
||||
tagsAPI tagsAPI
|
||||
wantErr bool
|
||||
}{
|
||||
"SignalRole works": {
|
||||
imdsAPI: newIMDSStub(),
|
||||
@ -180,13 +180,13 @@ func TestSignalRole(t *testing.T) {
|
||||
imdsAPI: newScaleSetIMDSStub(),
|
||||
},
|
||||
"providerID cannot be retrieved": {
|
||||
imdsAPI: &stubIMDSAPI{retrieveErr: errors.New("imds err")},
|
||||
expectErr: true,
|
||||
imdsAPI: &stubIMDSAPI{retrieveErr: errors.New("imds err")},
|
||||
wantErr: true,
|
||||
},
|
||||
"setting tag fails": {
|
||||
imdsAPI: newIMDSStub(),
|
||||
tagsAPI: newFailingTagsStub(),
|
||||
expectErr: true,
|
||||
imdsAPI: newIMDSStub(),
|
||||
tagsAPI: newFailingTagsStub(),
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -201,7 +201,7 @@ func TestSignalRole(t *testing.T) {
|
||||
}
|
||||
err := metadata.SignalRole(context.Background(), role.Coordinator)
|
||||
|
||||
if tc.expectErr {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
return
|
||||
}
|
||||
@ -224,21 +224,21 @@ func TestMetadataSupported(t *testing.T) {
|
||||
|
||||
func TestProviderID(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
imdsAPI imdsAPI
|
||||
expectErr bool
|
||||
expectedProviderID string
|
||||
imdsAPI imdsAPI
|
||||
wantErr bool
|
||||
wantProviderID string
|
||||
}{
|
||||
"providerID for individual instance works": {
|
||||
imdsAPI: newIMDSStub(),
|
||||
expectedProviderID: "azure:///subscriptions/subscription-id/resourceGroups/resource-group/providers/Microsoft.Compute/virtualMachines/instance-name",
|
||||
imdsAPI: newIMDSStub(),
|
||||
wantProviderID: "azure:///subscriptions/subscription-id/resourceGroups/resource-group/providers/Microsoft.Compute/virtualMachines/instance-name",
|
||||
},
|
||||
"providerID for scale set instance works": {
|
||||
imdsAPI: newScaleSetIMDSStub(),
|
||||
expectedProviderID: "azure:///subscriptions/subscription-id/resourceGroups/resource-group/providers/Microsoft.Compute/virtualMachineScaleSets/scale-set-name/virtualMachines/instance-id",
|
||||
imdsAPI: newScaleSetIMDSStub(),
|
||||
wantProviderID: "azure:///subscriptions/subscription-id/resourceGroups/resource-group/providers/Microsoft.Compute/virtualMachineScaleSets/scale-set-name/virtualMachines/instance-id",
|
||||
},
|
||||
"imds retrieval fails": {
|
||||
imdsAPI: &stubIMDSAPI{retrieveErr: errors.New("imds err")},
|
||||
expectErr: true,
|
||||
imdsAPI: &stubIMDSAPI{retrieveErr: errors.New("imds err")},
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -252,36 +252,36 @@ func TestProviderID(t *testing.T) {
|
||||
}
|
||||
providerID, err := metadata.providerID(context.Background())
|
||||
|
||||
if tc.expectErr {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
return
|
||||
}
|
||||
require.NoError(err)
|
||||
assert.Equal(tc.expectedProviderID, providerID)
|
||||
assert.Equal(tc.wantProviderID, providerID)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestExtractBasicsFromProviderID(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
providerID string
|
||||
expectErr bool
|
||||
expectedSubscriptionID string
|
||||
expectedResourceGroup string
|
||||
providerID string
|
||||
wantErr bool
|
||||
wantSubscriptionID string
|
||||
wantResourceGroup string
|
||||
}{
|
||||
"providerID for individual instance works": {
|
||||
providerID: "azure:///subscriptions/subscription-id/resourceGroups/resource-group/providers/Microsoft.Compute/virtualMachines/instance-name",
|
||||
expectedSubscriptionID: "subscription-id",
|
||||
expectedResourceGroup: "resource-group",
|
||||
providerID: "azure:///subscriptions/subscription-id/resourceGroups/resource-group/providers/Microsoft.Compute/virtualMachines/instance-name",
|
||||
wantSubscriptionID: "subscription-id",
|
||||
wantResourceGroup: "resource-group",
|
||||
},
|
||||
"providerID for scale set instance works": {
|
||||
providerID: "azure:///subscriptions/subscription-id/resourceGroups/resource-group/providers/Microsoft.Compute/virtualMachineScaleSets/scale-set-name/virtualMachines/instance-id",
|
||||
expectedSubscriptionID: "subscription-id",
|
||||
expectedResourceGroup: "resource-group",
|
||||
providerID: "azure:///subscriptions/subscription-id/resourceGroups/resource-group/providers/Microsoft.Compute/virtualMachineScaleSets/scale-set-name/virtualMachines/instance-id",
|
||||
wantSubscriptionID: "subscription-id",
|
||||
wantResourceGroup: "resource-group",
|
||||
},
|
||||
"providerID is malformed": {
|
||||
providerID: "malformed-provider-id",
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -292,29 +292,29 @@ func TestExtractBasicsFromProviderID(t *testing.T) {
|
||||
|
||||
subscriptionID, resourceGroup, err := extractBasicsFromProviderID(tc.providerID)
|
||||
|
||||
if tc.expectErr {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
return
|
||||
}
|
||||
require.NoError(err)
|
||||
assert.Equal(tc.expectedSubscriptionID, subscriptionID)
|
||||
assert.Equal(tc.expectedResourceGroup, resourceGroup)
|
||||
assert.Equal(tc.wantSubscriptionID, subscriptionID)
|
||||
assert.Equal(tc.wantResourceGroup, resourceGroup)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestExtractInstanceTags(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
in map[string]*string
|
||||
expectedTags map[string]string
|
||||
in map[string]*string
|
||||
wantTags map[string]string
|
||||
}{
|
||||
"tags are extracted": {
|
||||
in: map[string]*string{"key": to.StringPtr("value")},
|
||||
expectedTags: map[string]string{"key": "value"},
|
||||
in: map[string]*string{"key": to.StringPtr("value")},
|
||||
wantTags: map[string]string{"key": "value"},
|
||||
},
|
||||
"nil values are skipped": {
|
||||
in: map[string]*string{"key": nil},
|
||||
expectedTags: map[string]string{},
|
||||
in: map[string]*string{"key": nil},
|
||||
wantTags: map[string]string{},
|
||||
},
|
||||
}
|
||||
|
||||
@ -324,15 +324,15 @@ func TestExtractInstanceTags(t *testing.T) {
|
||||
|
||||
tags := extractInstanceTags(tc.in)
|
||||
|
||||
assert.Equal(tc.expectedTags, tags)
|
||||
assert.Equal(tc.wantTags, tags)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestExtractSSHKeys(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
in armcompute.SSHConfiguration
|
||||
expectedKeys map[string][]string
|
||||
in armcompute.SSHConfiguration
|
||||
wantKeys map[string][]string
|
||||
}{
|
||||
"ssh key is extracted": {
|
||||
in: armcompute.SSHConfiguration{
|
||||
@ -343,7 +343,7 @@ func TestExtractSSHKeys(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
expectedKeys: map[string][]string{"user": {"key-data"}},
|
||||
wantKeys: map[string][]string{"user": {"key-data"}},
|
||||
},
|
||||
"invalid path is skipped": {
|
||||
in: armcompute.SSHConfiguration{
|
||||
@ -354,7 +354,7 @@ func TestExtractSSHKeys(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
expectedKeys: map[string][]string{},
|
||||
wantKeys: map[string][]string{},
|
||||
},
|
||||
"key data is nil": {
|
||||
in: armcompute.SSHConfiguration{
|
||||
@ -364,7 +364,7 @@ func TestExtractSSHKeys(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
expectedKeys: map[string][]string{},
|
||||
wantKeys: map[string][]string{},
|
||||
},
|
||||
"path is nil": {
|
||||
in: armcompute.SSHConfiguration{
|
||||
@ -374,11 +374,11 @@ func TestExtractSSHKeys(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
expectedKeys: map[string][]string{},
|
||||
wantKeys: map[string][]string{},
|
||||
},
|
||||
"public keys are nil": {
|
||||
in: armcompute.SSHConfiguration{},
|
||||
expectedKeys: map[string][]string{},
|
||||
in: armcompute.SSHConfiguration{},
|
||||
wantKeys: map[string][]string{},
|
||||
},
|
||||
}
|
||||
|
||||
@ -388,7 +388,7 @@ func TestExtractSSHKeys(t *testing.T) {
|
||||
|
||||
keys := extractSSHKeys(tc.in)
|
||||
|
||||
assert.Equal(tc.expectedKeys, keys)
|
||||
assert.Equal(tc.wantKeys, keys)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -12,7 +12,7 @@ import (
|
||||
)
|
||||
|
||||
func TestGetVMInterfaces(t *testing.T) {
|
||||
expectedConfigs := []*armnetwork.InterfaceIPConfiguration{
|
||||
wantConfigs := []*armnetwork.InterfaceIPConfiguration{
|
||||
{
|
||||
Properties: &armnetwork.InterfaceIPConfigurationPropertiesFormat{
|
||||
PrivateIPAddress: to.StringPtr("192.0.2.0"),
|
||||
@ -33,23 +33,23 @@ func TestGetVMInterfaces(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
vm armcompute.VirtualMachine
|
||||
networkInterfacesAPI networkInterfacesAPI
|
||||
expectErr bool
|
||||
expectedConfigs []*armnetwork.InterfaceIPConfiguration
|
||||
wantErr bool
|
||||
wantConfigs []*armnetwork.InterfaceIPConfiguration
|
||||
}{
|
||||
"retrieval works": {
|
||||
vm: vm,
|
||||
networkInterfacesAPI: newNetworkInterfacesStub(),
|
||||
expectedConfigs: expectedConfigs,
|
||||
wantConfigs: wantConfigs,
|
||||
},
|
||||
"vm can have 0 interfaces": {
|
||||
vm: armcompute.VirtualMachine{},
|
||||
networkInterfacesAPI: newNetworkInterfacesStub(),
|
||||
expectedConfigs: []*armnetwork.InterfaceIPConfiguration{},
|
||||
wantConfigs: []*armnetwork.InterfaceIPConfiguration{},
|
||||
},
|
||||
"interface retrieval fails": {
|
||||
vm: vm,
|
||||
networkInterfacesAPI: newFailingNetworkInterfacesStub(),
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -63,18 +63,18 @@ func TestGetVMInterfaces(t *testing.T) {
|
||||
}
|
||||
configs, err := metadata.getVMInterfaces(context.Background(), tc.vm, "resource-group")
|
||||
|
||||
if tc.expectErr {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
return
|
||||
}
|
||||
require.NoError(err)
|
||||
assert.Equal(tc.expectedConfigs, configs)
|
||||
assert.Equal(tc.wantConfigs, configs)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetScaleSetVMInterfaces(t *testing.T) {
|
||||
expectedConfigs := []*armnetwork.InterfaceIPConfiguration{
|
||||
wantConfigs := []*armnetwork.InterfaceIPConfiguration{
|
||||
{
|
||||
Properties: &armnetwork.InterfaceIPConfigurationPropertiesFormat{
|
||||
PrivateIPAddress: to.StringPtr("192.0.2.0"),
|
||||
@ -95,23 +95,23 @@ func TestGetScaleSetVMInterfaces(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
vm armcompute.VirtualMachineScaleSetVM
|
||||
networkInterfacesAPI networkInterfacesAPI
|
||||
expectErr bool
|
||||
expectedConfigs []*armnetwork.InterfaceIPConfiguration
|
||||
wantErr bool
|
||||
wantConfigs []*armnetwork.InterfaceIPConfiguration
|
||||
}{
|
||||
"retrieval works": {
|
||||
vm: vm,
|
||||
networkInterfacesAPI: newNetworkInterfacesStub(),
|
||||
expectedConfigs: expectedConfigs,
|
||||
wantConfigs: wantConfigs,
|
||||
},
|
||||
"vm can have 0 interfaces": {
|
||||
vm: armcompute.VirtualMachineScaleSetVM{},
|
||||
networkInterfacesAPI: newNetworkInterfacesStub(),
|
||||
expectedConfigs: []*armnetwork.InterfaceIPConfiguration{},
|
||||
wantConfigs: []*armnetwork.InterfaceIPConfiguration{},
|
||||
},
|
||||
"interface retrieval fails": {
|
||||
vm: vm,
|
||||
networkInterfacesAPI: newFailingNetworkInterfacesStub(),
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -125,12 +125,12 @@ func TestGetScaleSetVMInterfaces(t *testing.T) {
|
||||
}
|
||||
configs, err := metadata.getScaleSetVMInterfaces(context.Background(), tc.vm, "resource-group", "scale-set-name", "instance-id")
|
||||
|
||||
if tc.expectErr {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
return
|
||||
}
|
||||
require.NoError(err)
|
||||
assert.Equal(tc.expectedConfigs, configs)
|
||||
assert.Equal(tc.wantConfigs, configs)
|
||||
})
|
||||
}
|
||||
}
|
||||
@ -138,7 +138,7 @@ func TestGetScaleSetVMInterfaces(t *testing.T) {
|
||||
func TestExtractPrivateIPs(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
interfaceIPConfigs []*armnetwork.InterfaceIPConfiguration
|
||||
expectedIPs []string
|
||||
wantIPs []string
|
||||
}{
|
||||
"extraction works": {
|
||||
interfaceIPConfigs: []*armnetwork.InterfaceIPConfiguration{
|
||||
@ -148,7 +148,7 @@ func TestExtractPrivateIPs(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
expectedIPs: []string{"192.0.2.0"},
|
||||
wantIPs: []string{"192.0.2.0"},
|
||||
},
|
||||
"can be empty": {
|
||||
interfaceIPConfigs: []*armnetwork.InterfaceIPConfiguration{},
|
||||
@ -166,15 +166,15 @@ func TestExtractPrivateIPs(t *testing.T) {
|
||||
|
||||
ips := extractPrivateIPs(tc.interfaceIPConfigs)
|
||||
|
||||
assert.ElementsMatch(tc.expectedIPs, ips)
|
||||
assert.ElementsMatch(tc.wantIPs, ips)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestExtractInterfaceNamesFromInterfaceReferences(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
references []*armcompute.NetworkInterfaceReference
|
||||
expectedNames []string
|
||||
references []*armcompute.NetworkInterfaceReference
|
||||
wantNames []string
|
||||
}{
|
||||
"extraction with individual interface reference works": {
|
||||
references: []*armcompute.NetworkInterfaceReference{
|
||||
@ -182,7 +182,7 @@ func TestExtractInterfaceNamesFromInterfaceReferences(t *testing.T) {
|
||||
ID: to.StringPtr("/subscriptions/subscription-id/resourceGroups/resource-group/providers/Microsoft.Network/networkInterfaces/interface-name"),
|
||||
},
|
||||
},
|
||||
expectedNames: []string{"interface-name"},
|
||||
wantNames: []string{"interface-name"},
|
||||
},
|
||||
"extraction with scale set interface reference works": {
|
||||
references: []*armcompute.NetworkInterfaceReference{
|
||||
@ -190,7 +190,7 @@ func TestExtractInterfaceNamesFromInterfaceReferences(t *testing.T) {
|
||||
ID: to.StringPtr("/subscriptions/subscription-id/resourceGroups/resource-group/providers/Microsoft.Compute/virtualMachineScaleSets/scale-set-name/virtualMachines/instance-id/networkInterfaces/interface-name"),
|
||||
},
|
||||
},
|
||||
expectedNames: []string{"interface-name"},
|
||||
wantNames: []string{"interface-name"},
|
||||
},
|
||||
"can be empty": {
|
||||
references: []*armcompute.NetworkInterfaceReference{},
|
||||
@ -208,7 +208,7 @@ func TestExtractInterfaceNamesFromInterfaceReferences(t *testing.T) {
|
||||
|
||||
names := extractInterfaceNamesFromInterfaceReferences(tc.references)
|
||||
|
||||
assert.ElementsMatch(tc.expectedNames, names)
|
||||
assert.ElementsMatch(tc.wantNames, names)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -15,7 +15,7 @@ import (
|
||||
)
|
||||
|
||||
func TestGetScaleSetVM(t *testing.T) {
|
||||
expectedInstance := core.Instance{
|
||||
wantInstance := core.Instance{
|
||||
Name: "scale-set-name-instance-id",
|
||||
ProviderID: "azure:///subscriptions/subscription-id/resourceGroups/resource-group/providers/Microsoft.Compute/virtualMachineScaleSets/scale-set-name/virtualMachines/instance-id",
|
||||
IPs: []string{"192.0.2.0"},
|
||||
@ -25,35 +25,35 @@ func TestGetScaleSetVM(t *testing.T) {
|
||||
providerID string
|
||||
networkInterfacesAPI networkInterfacesAPI
|
||||
virtualMachineScaleSetVMsAPI virtualMachineScaleSetVMsAPI
|
||||
expectErr bool
|
||||
expectedInstance core.Instance
|
||||
wantErr bool
|
||||
wantInstance core.Instance
|
||||
}{
|
||||
"getVM for scale set instance works": {
|
||||
providerID: "azure:///subscriptions/subscription-id/resourceGroups/resource-group/providers/Microsoft.Compute/virtualMachineScaleSets/scale-set-name/virtualMachines/instance-id",
|
||||
networkInterfacesAPI: newNetworkInterfacesStub(),
|
||||
virtualMachineScaleSetVMsAPI: newVirtualMachineScaleSetsVMsStub(),
|
||||
expectedInstance: expectedInstance,
|
||||
wantInstance: wantInstance,
|
||||
},
|
||||
"getVM for individual instance must fail": {
|
||||
providerID: "azure:///subscriptions/subscription-id/resourceGroups/resource-group/providers/Microsoft.Compute/virtualMachines/instance-name",
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"Get fails": {
|
||||
providerID: "azure:///subscriptions/subscription-id/resourceGroups/resource-group/providers/Microsoft.Compute/virtualMachineScaleSets/scale-set-name/virtualMachines/instance-id",
|
||||
virtualMachineScaleSetVMsAPI: newFailingGetScaleSetVirtualMachinesStub(),
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"retrieving interfaces fails": {
|
||||
providerID: "azure:///subscriptions/subscription-id/resourceGroups/resource-group/providers/Microsoft.Compute/virtualMachineScaleSets/scale-set-name/virtualMachines/instance-id",
|
||||
virtualMachineScaleSetVMsAPI: newVirtualMachineScaleSetsVMsStub(),
|
||||
networkInterfacesAPI: newFailingNetworkInterfacesStub(),
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"conversion fails": {
|
||||
providerID: "azure:///subscriptions/subscription-id/resourceGroups/resource-group/providers/Microsoft.Compute/virtualMachineScaleSets/scale-set-name/virtualMachines/instance-id",
|
||||
virtualMachineScaleSetVMsAPI: newGetInvalidScaleSetVirtualMachinesStub(),
|
||||
networkInterfacesAPI: newNetworkInterfacesStub(),
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -68,18 +68,18 @@ func TestGetScaleSetVM(t *testing.T) {
|
||||
}
|
||||
instance, err := metadata.getScaleSetVM(context.Background(), tc.providerID)
|
||||
|
||||
if tc.expectErr {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
return
|
||||
}
|
||||
require.NoError(err)
|
||||
assert.Equal(tc.expectedInstance, instance)
|
||||
assert.Equal(tc.wantInstance, instance)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestListScaleSetVMs(t *testing.T) {
|
||||
expectedInstances := []core.Instance{
|
||||
wantInstances := []core.Instance{
|
||||
{
|
||||
Name: "scale-set-name-instance-id",
|
||||
ProviderID: "azure:///subscriptions/subscription-id/resourceGroups/resource-group/providers/Microsoft.Compute/virtualMachineScaleSets/scale-set-name/virtualMachines/instance-id",
|
||||
@ -92,50 +92,50 @@ func TestListScaleSetVMs(t *testing.T) {
|
||||
networkInterfacesAPI networkInterfacesAPI
|
||||
virtualMachineScaleSetVMsAPI virtualMachineScaleSetVMsAPI
|
||||
scaleSetsAPI scaleSetsAPI
|
||||
expectErr bool
|
||||
expectedInstances []core.Instance
|
||||
wantErr bool
|
||||
wantInstances []core.Instance
|
||||
}{
|
||||
"listVMs works": {
|
||||
imdsAPI: newIMDSStub(),
|
||||
networkInterfacesAPI: newNetworkInterfacesStub(),
|
||||
virtualMachineScaleSetVMsAPI: newVirtualMachineScaleSetsVMsStub(),
|
||||
scaleSetsAPI: newScaleSetsStub(),
|
||||
expectedInstances: expectedInstances,
|
||||
wantInstances: wantInstances,
|
||||
},
|
||||
"invalid scale sets are skipped": {
|
||||
imdsAPI: newIMDSStub(),
|
||||
networkInterfacesAPI: newNetworkInterfacesStub(),
|
||||
virtualMachineScaleSetVMsAPI: newVirtualMachineScaleSetsVMsStub(),
|
||||
scaleSetsAPI: newListContainingNilScaleSetStub(),
|
||||
expectedInstances: expectedInstances,
|
||||
wantInstances: wantInstances,
|
||||
},
|
||||
"listVMs can return 0 VMs": {
|
||||
imdsAPI: newIMDSStub(),
|
||||
networkInterfacesAPI: newNetworkInterfacesStub(),
|
||||
virtualMachineScaleSetVMsAPI: &stubVirtualMachineScaleSetVMsAPI{},
|
||||
scaleSetsAPI: newScaleSetsStub(),
|
||||
expectedInstances: []core.Instance{},
|
||||
wantInstances: []core.Instance{},
|
||||
},
|
||||
"can skip nil in VM list": {
|
||||
imdsAPI: newIMDSStub(),
|
||||
networkInterfacesAPI: newNetworkInterfacesStub(),
|
||||
virtualMachineScaleSetVMsAPI: newListContainingNilScaleSetVirtualMachinesStub(),
|
||||
scaleSetsAPI: newScaleSetsStub(),
|
||||
expectedInstances: expectedInstances,
|
||||
wantInstances: wantInstances,
|
||||
},
|
||||
"retrieving network interfaces fails": {
|
||||
imdsAPI: newIMDSStub(),
|
||||
networkInterfacesAPI: newFailingNetworkInterfacesStub(),
|
||||
virtualMachineScaleSetVMsAPI: newVirtualMachineScaleSetsVMsStub(),
|
||||
scaleSetsAPI: newScaleSetsStub(),
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"converting instance fails": {
|
||||
imdsAPI: newIMDSStub(),
|
||||
networkInterfacesAPI: newNetworkInterfacesStub(),
|
||||
virtualMachineScaleSetVMsAPI: newListContainingInvalidScaleSetVirtualMachinesStub(),
|
||||
scaleSetsAPI: newScaleSetsStub(),
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -152,39 +152,39 @@ func TestListScaleSetVMs(t *testing.T) {
|
||||
}
|
||||
instances, err := metadata.listScaleSetVMs(context.Background(), "resource-group")
|
||||
|
||||
if tc.expectErr {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
return
|
||||
}
|
||||
require.NoError(err)
|
||||
assert.ElementsMatch(tc.expectedInstances, instances)
|
||||
assert.ElementsMatch(tc.wantInstances, instances)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestSplitScaleSetProviderID(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
providerID string
|
||||
expectErr bool
|
||||
expectedSubscriptionID string
|
||||
expectedResourceGroup string
|
||||
expectedScaleSet string
|
||||
expectedInstanceID string
|
||||
providerID string
|
||||
wantErr bool
|
||||
wantSubscriptionID string
|
||||
wantResourceGroup string
|
||||
wantScaleSet string
|
||||
wantInstanceID string
|
||||
}{
|
||||
"providerID for scale set instance works": {
|
||||
providerID: "azure:///subscriptions/subscription-id/resourceGroups/resource-group/providers/Microsoft.Compute/virtualMachineScaleSets/scale-set-name/virtualMachines/instance-id",
|
||||
expectedSubscriptionID: "subscription-id",
|
||||
expectedResourceGroup: "resource-group",
|
||||
expectedScaleSet: "scale-set-name",
|
||||
expectedInstanceID: "instance-id",
|
||||
providerID: "azure:///subscriptions/subscription-id/resourceGroups/resource-group/providers/Microsoft.Compute/virtualMachineScaleSets/scale-set-name/virtualMachines/instance-id",
|
||||
wantSubscriptionID: "subscription-id",
|
||||
wantResourceGroup: "resource-group",
|
||||
wantScaleSet: "scale-set-name",
|
||||
wantInstanceID: "instance-id",
|
||||
},
|
||||
"providerID for individual instance must fail": {
|
||||
providerID: "azure:///subscriptions/subscription-id/resourceGroups/resource-group/providers/Microsoft.Compute/virtualMachines/instance-name",
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"providerID is malformed": {
|
||||
providerID: "malformed-provider-id",
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -195,15 +195,15 @@ func TestSplitScaleSetProviderID(t *testing.T) {
|
||||
|
||||
subscriptionID, resourceGroup, scaleSet, instanceID, err := splitScaleSetProviderID(tc.providerID)
|
||||
|
||||
if tc.expectErr {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
return
|
||||
}
|
||||
require.NoError(err)
|
||||
assert.Equal(tc.expectedSubscriptionID, subscriptionID)
|
||||
assert.Equal(tc.expectedResourceGroup, resourceGroup)
|
||||
assert.Equal(tc.expectedScaleSet, scaleSet)
|
||||
assert.Equal(tc.expectedInstanceID, instanceID)
|
||||
assert.Equal(tc.wantSubscriptionID, subscriptionID)
|
||||
assert.Equal(tc.wantResourceGroup, resourceGroup)
|
||||
assert.Equal(tc.wantScaleSet, scaleSet)
|
||||
assert.Equal(tc.wantInstanceID, instanceID)
|
||||
})
|
||||
}
|
||||
}
|
||||
@ -212,8 +212,8 @@ func TestConvertScaleSetVMToCoreInstance(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
inVM armcompute.VirtualMachineScaleSetVM
|
||||
inInterfaceIPConfigs []*armnetwork.InterfaceIPConfiguration
|
||||
expectErr bool
|
||||
expectedInstance core.Instance
|
||||
wantErr bool
|
||||
wantInstance core.Instance
|
||||
}{
|
||||
"conversion works": {
|
||||
inVM: armcompute.VirtualMachineScaleSetVM{
|
||||
@ -233,7 +233,7 @@ func TestConvertScaleSetVMToCoreInstance(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
expectedInstance: core.Instance{
|
||||
wantInstance: core.Instance{
|
||||
Name: "scale-set-name-instance-id",
|
||||
ProviderID: "azure:///subscriptions/subscription-id/resourceGroups/resource-group/providers/Microsoft.Compute/virtualMachineScaleSets/scale-set-name/virtualMachines/instance-id",
|
||||
IPs: []string{"192.0.2.0"},
|
||||
@ -241,8 +241,8 @@ func TestConvertScaleSetVMToCoreInstance(t *testing.T) {
|
||||
},
|
||||
},
|
||||
"invalid instance": {
|
||||
inVM: armcompute.VirtualMachineScaleSetVM{},
|
||||
expectErr: true,
|
||||
inVM: armcompute.VirtualMachineScaleSetVM{},
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -253,32 +253,32 @@ func TestConvertScaleSetVMToCoreInstance(t *testing.T) {
|
||||
|
||||
instance, err := convertScaleSetVMToCoreInstance("scale-set", tc.inVM, tc.inInterfaceIPConfigs)
|
||||
|
||||
if tc.expectErr {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
return
|
||||
}
|
||||
require.NoError(err)
|
||||
assert.Equal(tc.expectedInstance, instance)
|
||||
assert.Equal(tc.wantInstance, instance)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestExtractScaleSetVMRole(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
scaleSet string
|
||||
expectedRole role.Role
|
||||
scaleSet string
|
||||
wantRole role.Role
|
||||
}{
|
||||
"coordinator role": {
|
||||
scaleSet: "constellation-scale-set-coordinators-abcd123",
|
||||
expectedRole: role.Coordinator,
|
||||
scaleSet: "constellation-scale-set-coordinators-abcd123",
|
||||
wantRole: role.Coordinator,
|
||||
},
|
||||
"node role": {
|
||||
scaleSet: "constellation-scale-set-nodes-abcd123",
|
||||
expectedRole: role.Node,
|
||||
scaleSet: "constellation-scale-set-nodes-abcd123",
|
||||
wantRole: role.Node,
|
||||
},
|
||||
"unknown role": {
|
||||
scaleSet: "unknown",
|
||||
expectedRole: role.Unknown,
|
||||
scaleSet: "unknown",
|
||||
wantRole: role.Unknown,
|
||||
},
|
||||
}
|
||||
|
||||
@ -288,7 +288,7 @@ func TestExtractScaleSetVMRole(t *testing.T) {
|
||||
|
||||
role := extractScaleSetVMRole(tc.scaleSet)
|
||||
|
||||
assert.Equal(tc.expectedRole, role)
|
||||
assert.Equal(tc.wantRole, role)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -17,24 +17,24 @@ func TestGetApplicationCredentials(t *testing.T) {
|
||||
}
|
||||
testCases := map[string]struct {
|
||||
cloudServiceAccountURI string
|
||||
expectedCreds client.ApplicationCredentials
|
||||
expectErr bool
|
||||
wantCreds client.ApplicationCredentials
|
||||
wantErr bool
|
||||
}{
|
||||
"getApplicationCredentials works": {
|
||||
cloudServiceAccountURI: "serviceaccount://azure?tenant_id=tenant-id&client_id=client-id&client_secret=client-secret&location=location",
|
||||
expectedCreds: creds,
|
||||
wantCreds: creds,
|
||||
},
|
||||
"invalid URI fails": {
|
||||
cloudServiceAccountURI: "\x00",
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"incorrect URI scheme fails": {
|
||||
cloudServiceAccountURI: "invalid",
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"incorrect URI host fails": {
|
||||
cloudServiceAccountURI: "serviceaccount://incorrect",
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -44,12 +44,12 @@ func TestGetApplicationCredentials(t *testing.T) {
|
||||
require := require.New(t)
|
||||
|
||||
creds, err := getApplicationCredentials(tc.cloudServiceAccountURI)
|
||||
if tc.expectErr {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
return
|
||||
}
|
||||
require.NoError(err)
|
||||
assert.Equal(tc.expectedCreds, creds)
|
||||
assert.Equal(tc.wantCreds, creds)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -14,7 +14,7 @@ import (
|
||||
)
|
||||
|
||||
func TestGetVM(t *testing.T) {
|
||||
expectedInstance := core.Instance{
|
||||
wantInstance := core.Instance{
|
||||
Name: "instance-name",
|
||||
ProviderID: "azure:///subscriptions/subscription-id/resourceGroups/resource-group/providers/Microsoft.Compute/virtualMachines/instance-name",
|
||||
IPs: []string{"192.0.2.0"},
|
||||
@ -24,35 +24,35 @@ func TestGetVM(t *testing.T) {
|
||||
providerID string
|
||||
networkInterfacesAPI networkInterfacesAPI
|
||||
virtualMachinesAPI virtualMachinesAPI
|
||||
expectErr bool
|
||||
expectedInstance core.Instance
|
||||
wantErr bool
|
||||
wantInstance core.Instance
|
||||
}{
|
||||
"getVM for individual instance works": {
|
||||
providerID: "azure:///subscriptions/subscription-id/resourceGroups/resource-group/providers/Microsoft.Compute/virtualMachines/instance-name",
|
||||
networkInterfacesAPI: newNetworkInterfacesStub(),
|
||||
virtualMachinesAPI: newVirtualMachinesStub(),
|
||||
expectedInstance: expectedInstance,
|
||||
wantInstance: wantInstance,
|
||||
},
|
||||
"getVM for scale set instance must fail": {
|
||||
providerID: "azure:///subscriptions/subscription-id/resourceGroups/resource-group/providers/Microsoft.Compute/virtualMachineScaleSets/scale-set-name/virtualMachines/instance-id",
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"Get fails": {
|
||||
providerID: "azure:///subscriptions/subscription-id/resourceGroups/resource-group/providers/Microsoft.Compute/virtualMachines/instance-name",
|
||||
virtualMachinesAPI: newFailingGetVirtualMachinesStub(),
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"retrieving interfaces fails": {
|
||||
providerID: "azure:///subscriptions/subscription-id/resourceGroups/resource-group/providers/Microsoft.Compute/virtualMachines/instance-name",
|
||||
virtualMachinesAPI: newVirtualMachinesStub(),
|
||||
networkInterfacesAPI: newFailingNetworkInterfacesStub(),
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"conversion fails": {
|
||||
providerID: "azure:///subscriptions/subscription-id/resourceGroups/resource-group/providers/Microsoft.Compute/virtualMachines/instance-name",
|
||||
virtualMachinesAPI: newGetInvalidVirtualMachinesStub(),
|
||||
networkInterfacesAPI: newNetworkInterfacesStub(),
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -67,18 +67,18 @@ func TestGetVM(t *testing.T) {
|
||||
}
|
||||
instance, err := metadata.getVM(context.Background(), tc.providerID)
|
||||
|
||||
if tc.expectErr {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
return
|
||||
}
|
||||
require.NoError(err)
|
||||
assert.Equal(tc.expectedInstance, instance)
|
||||
assert.Equal(tc.wantInstance, instance)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestListVMs(t *testing.T) {
|
||||
expectedInstances := []core.Instance{
|
||||
wantInstances := []core.Instance{
|
||||
{
|
||||
Name: "instance-name",
|
||||
ProviderID: "azure:///subscriptions/subscription-id/resourceGroups/resource-group/providers/Microsoft.Compute/virtualMachines/instance-name",
|
||||
@ -90,38 +90,38 @@ func TestListVMs(t *testing.T) {
|
||||
imdsAPI imdsAPI
|
||||
networkInterfacesAPI networkInterfacesAPI
|
||||
virtualMachinesAPI virtualMachinesAPI
|
||||
expectErr bool
|
||||
expectedInstances []core.Instance
|
||||
wantErr bool
|
||||
wantInstances []core.Instance
|
||||
}{
|
||||
"listVMs works": {
|
||||
imdsAPI: newIMDSStub(),
|
||||
networkInterfacesAPI: newNetworkInterfacesStub(),
|
||||
virtualMachinesAPI: newVirtualMachinesStub(),
|
||||
expectedInstances: expectedInstances,
|
||||
wantInstances: wantInstances,
|
||||
},
|
||||
"listVMs can return 0 VMs": {
|
||||
imdsAPI: newIMDSStub(),
|
||||
networkInterfacesAPI: newNetworkInterfacesStub(),
|
||||
virtualMachinesAPI: &stubVirtualMachinesAPI{},
|
||||
expectedInstances: []core.Instance{},
|
||||
wantInstances: []core.Instance{},
|
||||
},
|
||||
"can skip nil in VM list": {
|
||||
imdsAPI: newIMDSStub(),
|
||||
networkInterfacesAPI: newNetworkInterfacesStub(),
|
||||
virtualMachinesAPI: newListContainingNilVirtualMachinesStub(),
|
||||
expectedInstances: expectedInstances,
|
||||
wantInstances: wantInstances,
|
||||
},
|
||||
"retrieving network interfaces fails": {
|
||||
imdsAPI: newIMDSStub(),
|
||||
networkInterfacesAPI: newFailingNetworkInterfacesStub(),
|
||||
virtualMachinesAPI: newVirtualMachinesStub(),
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"converting instance fails": {
|
||||
imdsAPI: newIMDSStub(),
|
||||
networkInterfacesAPI: newNetworkInterfacesStub(),
|
||||
virtualMachinesAPI: newListContainingInvalidVirtualMachinesStub(),
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -137,35 +137,35 @@ func TestListVMs(t *testing.T) {
|
||||
}
|
||||
instances, err := metadata.listVMs(context.Background(), "resource-group")
|
||||
|
||||
if tc.expectErr {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
return
|
||||
}
|
||||
require.NoError(err)
|
||||
assert.ElementsMatch(tc.expectedInstances, instances)
|
||||
assert.ElementsMatch(tc.wantInstances, instances)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetTag(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
imdsAPI imdsAPI
|
||||
tagsAPI tagsAPI
|
||||
expectErr bool
|
||||
imdsAPI imdsAPI
|
||||
tagsAPI tagsAPI
|
||||
wantErr bool
|
||||
}{
|
||||
"setTag works": {
|
||||
imdsAPI: newIMDSStub(),
|
||||
tagsAPI: newTagsStub(),
|
||||
},
|
||||
"retrieving resource ID fails": {
|
||||
imdsAPI: newFailingIMDSStub(),
|
||||
tagsAPI: newTagsStub(),
|
||||
expectErr: true,
|
||||
imdsAPI: newFailingIMDSStub(),
|
||||
tagsAPI: newTagsStub(),
|
||||
wantErr: true,
|
||||
},
|
||||
"updating tags fails": {
|
||||
imdsAPI: newIMDSStub(),
|
||||
tagsAPI: newFailingTagsStub(),
|
||||
expectErr: true,
|
||||
imdsAPI: newIMDSStub(),
|
||||
tagsAPI: newFailingTagsStub(),
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -180,7 +180,7 @@ func TestSetTag(t *testing.T) {
|
||||
}
|
||||
err := metadata.setTag(context.Background(), "key", "value")
|
||||
|
||||
if tc.expectErr {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
return
|
||||
}
|
||||
@ -191,25 +191,25 @@ func TestSetTag(t *testing.T) {
|
||||
|
||||
func TestSplitVMProviderID(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
providerID string
|
||||
expectErr bool
|
||||
expectedSubscriptionID string
|
||||
expectedResourceGroup string
|
||||
expectedInstanceName string
|
||||
providerID string
|
||||
wantErr bool
|
||||
wantSubscriptionID string
|
||||
wantResourceGroup string
|
||||
wantInstanceName string
|
||||
}{
|
||||
"providerID for individual instance works": {
|
||||
providerID: "azure:///subscriptions/subscription-id/resourceGroups/resource-group/providers/Microsoft.Compute/virtualMachines/instance-name",
|
||||
expectedSubscriptionID: "subscription-id",
|
||||
expectedResourceGroup: "resource-group",
|
||||
expectedInstanceName: "instance-name",
|
||||
providerID: "azure:///subscriptions/subscription-id/resourceGroups/resource-group/providers/Microsoft.Compute/virtualMachines/instance-name",
|
||||
wantSubscriptionID: "subscription-id",
|
||||
wantResourceGroup: "resource-group",
|
||||
wantInstanceName: "instance-name",
|
||||
},
|
||||
"providerID for scale set instance must fail": {
|
||||
providerID: "azure:///subscriptions/subscription-id/resourceGroups/resource-group/providers/Microsoft.Compute/virtualMachineScaleSets/scale-set-name/virtualMachines/instance-id",
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"providerID is malformed": {
|
||||
providerID: "malformed-provider-id",
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -220,14 +220,14 @@ func TestSplitVMProviderID(t *testing.T) {
|
||||
|
||||
subscriptionID, resourceGroup, instanceName, err := splitVMProviderID(tc.providerID)
|
||||
|
||||
if tc.expectErr {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
return
|
||||
}
|
||||
require.NoError(err)
|
||||
assert.Equal(tc.expectedSubscriptionID, subscriptionID)
|
||||
assert.Equal(tc.expectedResourceGroup, resourceGroup)
|
||||
assert.Equal(tc.expectedInstanceName, instanceName)
|
||||
assert.Equal(tc.wantSubscriptionID, subscriptionID)
|
||||
assert.Equal(tc.wantResourceGroup, resourceGroup)
|
||||
assert.Equal(tc.wantInstanceName, instanceName)
|
||||
})
|
||||
}
|
||||
}
|
||||
@ -236,8 +236,8 @@ func TestConvertVMToCoreInstance(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
inVM armcompute.VirtualMachine
|
||||
inInterfaceIPConfigs []*armnetwork.InterfaceIPConfiguration
|
||||
expectErr bool
|
||||
expectedInstance core.Instance
|
||||
wantErr bool
|
||||
wantInstance core.Instance
|
||||
}{
|
||||
"conversion works": {
|
||||
inVM: armcompute.VirtualMachine{
|
||||
@ -266,7 +266,7 @@ func TestConvertVMToCoreInstance(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
expectedInstance: core.Instance{
|
||||
wantInstance: core.Instance{
|
||||
Name: "instance-name",
|
||||
ProviderID: "azure:///subscriptions/subscription-id/resourceGroups/resource-group/providers/Microsoft.Compute/virtualMachines/instance-name",
|
||||
IPs: []string{"192.0.2.0"},
|
||||
@ -286,7 +286,7 @@ func TestConvertVMToCoreInstance(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
expectedInstance: core.Instance{
|
||||
wantInstance: core.Instance{
|
||||
Name: "instance-name",
|
||||
ProviderID: "azure:///subscriptions/subscription-id/resourceGroups/resource-group/providers/Microsoft.Compute/virtualMachines/instance-name",
|
||||
IPs: []string{"192.0.2.0"},
|
||||
@ -294,8 +294,8 @@ func TestConvertVMToCoreInstance(t *testing.T) {
|
||||
},
|
||||
},
|
||||
"invalid instance": {
|
||||
inVM: armcompute.VirtualMachine{},
|
||||
expectErr: true,
|
||||
inVM: armcompute.VirtualMachine{},
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -306,12 +306,12 @@ func TestConvertVMToCoreInstance(t *testing.T) {
|
||||
|
||||
instance, err := convertVMToCoreInstance(tc.inVM, tc.inInterfaceIPConfigs)
|
||||
|
||||
if tc.expectErr {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
return
|
||||
}
|
||||
require.NoError(err)
|
||||
assert.Equal(tc.expectedInstance, instance)
|
||||
assert.Equal(tc.wantInstance, instance)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -15,13 +15,13 @@ import (
|
||||
|
||||
func TestConfigMaps(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
instance core.Instance
|
||||
expectedConfigMaps resources.ConfigMaps
|
||||
expectErr bool
|
||||
instance core.Instance
|
||||
wantConfigMaps resources.ConfigMaps
|
||||
wantErr bool
|
||||
}{
|
||||
"ConfigMaps works": {
|
||||
instance: core.Instance{ProviderID: "gce://project-id/zone/instance-name"},
|
||||
expectedConfigMaps: resources.ConfigMaps{
|
||||
wantConfigMaps: resources.ConfigMaps{
|
||||
&k8s.ConfigMap{
|
||||
TypeMeta: v1.TypeMeta{
|
||||
Kind: "ConfigMap",
|
||||
@ -41,8 +41,8 @@ use-metadata-server = false
|
||||
},
|
||||
},
|
||||
"invalid providerID fails": {
|
||||
instance: core.Instance{ProviderID: "invalid"},
|
||||
expectErr: true,
|
||||
instance: core.Instance{ProviderID: "invalid"},
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -54,12 +54,12 @@ use-metadata-server = false
|
||||
cloud := CloudControllerManager{}
|
||||
configMaps, err := cloud.ConfigMaps(tc.instance)
|
||||
|
||||
if tc.expectErr {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
return
|
||||
}
|
||||
require.NoError(err)
|
||||
assert.Equal(tc.expectedConfigMaps, configMaps)
|
||||
assert.Equal(tc.wantConfigMaps, configMaps)
|
||||
})
|
||||
}
|
||||
}
|
||||
@ -82,12 +82,12 @@ func TestSecrets(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
instance core.Instance
|
||||
cloudServiceAccountURI string
|
||||
expectedSecrets resources.Secrets
|
||||
expectErr bool
|
||||
wantSecrets resources.Secrets
|
||||
wantErr bool
|
||||
}{
|
||||
"Secrets works": {
|
||||
cloudServiceAccountURI: "serviceaccount://gcp?type=type&project_id=project-id&private_key_id=private-key-id&private_key=private-key&client_email=client-email&client_id=client-id&auth_uri=auth-uri&token_uri=token-uri&auth_provider_x509_cert_url=auth-provider-x509-cert-url&client_x509_cert_url=client-x509-cert-url",
|
||||
expectedSecrets: resources.Secrets{
|
||||
wantSecrets: resources.Secrets{
|
||||
&k8s.Secret{
|
||||
TypeMeta: v1.TypeMeta{
|
||||
Kind: "Secret",
|
||||
@ -105,7 +105,7 @@ func TestSecrets(t *testing.T) {
|
||||
},
|
||||
"invalid serviceAccountKey fails": {
|
||||
cloudServiceAccountURI: "invalid",
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -116,12 +116,12 @@ func TestSecrets(t *testing.T) {
|
||||
|
||||
cloud := CloudControllerManager{}
|
||||
secrets, err := cloud.Secrets(tc.instance, tc.cloudServiceAccountURI)
|
||||
if tc.expectErr {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
return
|
||||
}
|
||||
require.NoError(err)
|
||||
assert.Equal(tc.expectedSecrets, secrets)
|
||||
assert.Equal(tc.wantSecrets, secrets)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -67,14 +67,14 @@ func TestRetrieveInstances(t *testing.T) {
|
||||
metadata stubMetadataClient
|
||||
instanceIter *stubInstanceIterator
|
||||
instanceIterMutator func(*stubInstanceIterator)
|
||||
expectedInstances []core.Instance
|
||||
expectErr bool
|
||||
wantInstances []core.Instance
|
||||
wantErr bool
|
||||
}{
|
||||
"retrieve works": {
|
||||
client: stubInstancesClient{},
|
||||
metadata: stubMetadataClient{InstanceValue: uid},
|
||||
instanceIter: newTestIter(),
|
||||
expectedInstances: []core.Instance{
|
||||
wantInstances: []core.Instance{
|
||||
{
|
||||
Name: "someInstance",
|
||||
ProviderID: "gce://someProject/someZone/someInstance",
|
||||
@ -89,14 +89,14 @@ func TestRetrieveInstances(t *testing.T) {
|
||||
metadata: stubMetadataClient{InstanceValue: uid},
|
||||
instanceIter: newTestIter(),
|
||||
instanceIterMutator: func(sii *stubInstanceIterator) { sii.instances[0].Name = nil },
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"no instance with network ip": {
|
||||
client: stubInstancesClient{},
|
||||
metadata: stubMetadataClient{InstanceValue: uid},
|
||||
instanceIter: newTestIter(),
|
||||
instanceIterMutator: func(sii *stubInstanceIterator) { sii.instances[0].NetworkInterfaces = nil },
|
||||
expectedInstances: []core.Instance{
|
||||
wantInstances: []core.Instance{
|
||||
{
|
||||
Name: "someInstance",
|
||||
ProviderID: "gce://someProject/someZone/someInstance",
|
||||
@ -111,7 +111,7 @@ func TestRetrieveInstances(t *testing.T) {
|
||||
metadata: stubMetadataClient{InstanceValue: uid},
|
||||
instanceIter: newTestIter(),
|
||||
instanceIterMutator: func(sii *stubInstanceIterator) { sii.instances[0].NetworkInterfaces[0].NetworkIP = nil },
|
||||
expectedInstances: []core.Instance{
|
||||
wantInstances: []core.Instance{
|
||||
{
|
||||
Name: "someInstance",
|
||||
ProviderID: "gce://someProject/someZone/someInstance",
|
||||
@ -126,20 +126,20 @@ func TestRetrieveInstances(t *testing.T) {
|
||||
metadata: stubMetadataClient{InstanceValue: uid},
|
||||
instanceIter: newTestIter(),
|
||||
instanceIterMutator: func(sii *stubInstanceIterator) { sii.instances[0].Metadata.Items[2].Key = proto.String("") },
|
||||
expectedInstances: []core.Instance{},
|
||||
wantInstances: []core.Instance{},
|
||||
},
|
||||
"constellation retrieval fails": {
|
||||
client: stubInstancesClient{},
|
||||
metadata: stubMetadataClient{InstanceErr: someErr},
|
||||
instanceIter: newTestIter(),
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"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("") },
|
||||
expectedInstances: []core.Instance{
|
||||
wantInstances: []core.Instance{
|
||||
{
|
||||
Name: "someInstance",
|
||||
ProviderID: "gce://someProject/someZone/someInstance",
|
||||
@ -153,7 +153,7 @@ func TestRetrieveInstances(t *testing.T) {
|
||||
client: stubInstancesClient{},
|
||||
metadata: stubMetadataClient{InstanceValue: uid},
|
||||
instanceIter: &stubInstanceIterator{nextErr: someErr},
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -173,12 +173,12 @@ func TestRetrieveInstances(t *testing.T) {
|
||||
|
||||
instances, err := client.RetrieveInstances(context.Background(), "someProject", "someZone")
|
||||
|
||||
if tc.expectErr {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
return
|
||||
}
|
||||
require.NoError(err)
|
||||
assert.Equal(tc.expectedInstances, instances)
|
||||
assert.Equal(tc.wantInstances, instances)
|
||||
})
|
||||
}
|
||||
}
|
||||
@ -209,13 +209,13 @@ func TestRetrieveInstance(t *testing.T) {
|
||||
client stubInstancesClient
|
||||
clientInstance *computepb.Instance
|
||||
clientInstanceMutator func(*computepb.Instance)
|
||||
expectedInstance core.Instance
|
||||
expectErr bool
|
||||
wantInstance core.Instance
|
||||
wantErr bool
|
||||
}{
|
||||
"retrieve works": {
|
||||
client: stubInstancesClient{},
|
||||
clientInstance: newTestInstance(),
|
||||
expectedInstance: core.Instance{
|
||||
wantInstance: core.Instance{
|
||||
Name: "someInstance",
|
||||
ProviderID: "gce://someProject/someZone/someInstance",
|
||||
IPs: []string{"192.0.2.0"},
|
||||
@ -229,7 +229,7 @@ func TestRetrieveInstance(t *testing.T) {
|
||||
i.Metadata.Items[0].Key = proto.String("ssh-keys")
|
||||
i.Metadata.Items[0].Value = proto.String("bob:ssh-rsa bobskey")
|
||||
},
|
||||
expectedInstance: core.Instance{
|
||||
wantInstance: core.Instance{
|
||||
Name: "someInstance",
|
||||
ProviderID: "gce://someProject/someZone/someInstance",
|
||||
IPs: []string{"192.0.2.0"},
|
||||
@ -243,7 +243,7 @@ func TestRetrieveInstance(t *testing.T) {
|
||||
i.Metadata.Items[0].Key = proto.String(core.RoleMetadataKey)
|
||||
i.Metadata.Items[0].Value = proto.String(role.Coordinator.String())
|
||||
},
|
||||
expectedInstance: core.Instance{
|
||||
wantInstance: core.Instance{
|
||||
Name: "someInstance",
|
||||
ProviderID: "gce://someProject/someZone/someInstance",
|
||||
Role: role.Coordinator,
|
||||
@ -256,13 +256,13 @@ func TestRetrieveInstance(t *testing.T) {
|
||||
GetErr: errors.New("retrieve error"),
|
||||
},
|
||||
clientInstance: nil,
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"metadata item is null": {
|
||||
client: stubInstancesClient{},
|
||||
clientInstance: newTestInstance(),
|
||||
clientInstanceMutator: func(i *computepb.Instance) { i.Metadata.Items[0] = nil },
|
||||
expectedInstance: core.Instance{
|
||||
wantInstance: core.Instance{
|
||||
Name: "someInstance",
|
||||
ProviderID: "gce://someProject/someZone/someInstance",
|
||||
IPs: []string{"192.0.2.0"},
|
||||
@ -273,7 +273,7 @@ func TestRetrieveInstance(t *testing.T) {
|
||||
client: stubInstancesClient{},
|
||||
clientInstance: newTestInstance(),
|
||||
clientInstanceMutator: func(i *computepb.Instance) { i.Metadata.Items[0].Key = nil },
|
||||
expectedInstance: core.Instance{
|
||||
wantInstance: core.Instance{
|
||||
Name: "someInstance",
|
||||
ProviderID: "gce://someProject/someZone/someInstance",
|
||||
IPs: []string{"192.0.2.0"},
|
||||
@ -284,7 +284,7 @@ func TestRetrieveInstance(t *testing.T) {
|
||||
client: stubInstancesClient{},
|
||||
clientInstance: newTestInstance(),
|
||||
clientInstanceMutator: func(i *computepb.Instance) { i.Metadata.Items[0].Value = nil },
|
||||
expectedInstance: core.Instance{
|
||||
wantInstance: core.Instance{
|
||||
Name: "someInstance",
|
||||
ProviderID: "gce://someProject/someZone/someInstance",
|
||||
IPs: []string{"192.0.2.0"},
|
||||
@ -295,7 +295,7 @@ func TestRetrieveInstance(t *testing.T) {
|
||||
client: stubInstancesClient{},
|
||||
clientInstance: newTestInstance(),
|
||||
clientInstanceMutator: func(i *computepb.Instance) { i.NetworkInterfaces[0] = nil },
|
||||
expectedInstance: core.Instance{
|
||||
wantInstance: core.Instance{
|
||||
Name: "someInstance",
|
||||
ProviderID: "gce://someProject/someZone/someInstance",
|
||||
IPs: []string{},
|
||||
@ -306,7 +306,7 @@ func TestRetrieveInstance(t *testing.T) {
|
||||
client: stubInstancesClient{},
|
||||
clientInstance: newTestInstance(),
|
||||
clientInstanceMutator: func(i *computepb.Instance) { i.NetworkInterfaces[0].NetworkIP = nil },
|
||||
expectedInstance: core.Instance{
|
||||
wantInstance: core.Instance{
|
||||
Name: "someInstance",
|
||||
ProviderID: "gce://someProject/someZone/someInstance",
|
||||
IPs: []string{},
|
||||
@ -328,12 +328,12 @@ func TestRetrieveInstance(t *testing.T) {
|
||||
|
||||
instance, err := client.RetrieveInstance(context.Background(), "someProject", "someZone", "someInstance")
|
||||
|
||||
if tc.expectErr {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
return
|
||||
}
|
||||
require.NoError(err)
|
||||
assert.Equal(tc.expectedInstance, instance)
|
||||
assert.Equal(tc.wantInstance, instance)
|
||||
})
|
||||
}
|
||||
}
|
||||
@ -342,17 +342,17 @@ func TestRetrieveProjectID(t *testing.T) {
|
||||
someErr := errors.New("failed")
|
||||
|
||||
testCases := map[string]struct {
|
||||
client stubMetadataClient
|
||||
expectedValue string
|
||||
expectErr bool
|
||||
client stubMetadataClient
|
||||
wantValue string
|
||||
wantErr bool
|
||||
}{
|
||||
"retrieve works": {
|
||||
client: stubMetadataClient{ProjectIDValue: "someProjectID"},
|
||||
expectedValue: "someProjectID",
|
||||
client: stubMetadataClient{ProjectIDValue: "someProjectID"},
|
||||
wantValue: "someProjectID",
|
||||
},
|
||||
"retrieve fails": {
|
||||
client: stubMetadataClient{ProjectIDErr: someErr},
|
||||
expectErr: true,
|
||||
client: stubMetadataClient{ProjectIDErr: someErr},
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -364,12 +364,12 @@ func TestRetrieveProjectID(t *testing.T) {
|
||||
client := Client{metadataAPI: tc.client}
|
||||
value, err := client.RetrieveProjectID()
|
||||
|
||||
if tc.expectErr {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
return
|
||||
}
|
||||
require.NoError(err)
|
||||
assert.Equal(tc.expectedValue, value)
|
||||
assert.Equal(tc.wantValue, value)
|
||||
})
|
||||
}
|
||||
}
|
||||
@ -378,17 +378,17 @@ func TestRetrieveZone(t *testing.T) {
|
||||
someErr := errors.New("failed")
|
||||
|
||||
testCases := map[string]struct {
|
||||
client stubMetadataClient
|
||||
expectedValue string
|
||||
expectErr bool
|
||||
client stubMetadataClient
|
||||
wantValue string
|
||||
wantErr bool
|
||||
}{
|
||||
"retrieve works": {
|
||||
client: stubMetadataClient{ZoneValue: "someZone"},
|
||||
expectedValue: "someZone",
|
||||
client: stubMetadataClient{ZoneValue: "someZone"},
|
||||
wantValue: "someZone",
|
||||
},
|
||||
"retrieve fails": {
|
||||
client: stubMetadataClient{ZoneErr: someErr},
|
||||
expectErr: true,
|
||||
client: stubMetadataClient{ZoneErr: someErr},
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -400,12 +400,12 @@ func TestRetrieveZone(t *testing.T) {
|
||||
client := Client{metadataAPI: tc.client}
|
||||
value, err := client.RetrieveZone()
|
||||
|
||||
if tc.expectErr {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
return
|
||||
}
|
||||
require.NoError(err)
|
||||
assert.Equal(tc.expectedValue, value)
|
||||
assert.Equal(tc.wantValue, value)
|
||||
})
|
||||
}
|
||||
}
|
||||
@ -414,17 +414,17 @@ func TestRetrieveInstanceName(t *testing.T) {
|
||||
someErr := errors.New("failed")
|
||||
|
||||
testCases := map[string]struct {
|
||||
client stubMetadataClient
|
||||
expectedValue string
|
||||
expectErr bool
|
||||
client stubMetadataClient
|
||||
wantValue string
|
||||
wantErr bool
|
||||
}{
|
||||
"retrieve works": {
|
||||
client: stubMetadataClient{InstanceNameValue: "someInstanceName"},
|
||||
expectedValue: "someInstanceName",
|
||||
client: stubMetadataClient{InstanceNameValue: "someInstanceName"},
|
||||
wantValue: "someInstanceName",
|
||||
},
|
||||
"retrieve fails": {
|
||||
client: stubMetadataClient{InstanceNameErr: someErr},
|
||||
expectErr: true,
|
||||
client: stubMetadataClient{InstanceNameErr: someErr},
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -436,12 +436,12 @@ func TestRetrieveInstanceName(t *testing.T) {
|
||||
client := Client{metadataAPI: tc.client}
|
||||
value, err := client.RetrieveInstanceName()
|
||||
|
||||
if tc.expectErr {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
return
|
||||
}
|
||||
require.NoError(err)
|
||||
assert.Equal(tc.expectedValue, value)
|
||||
assert.Equal(tc.wantValue, value)
|
||||
})
|
||||
}
|
||||
}
|
||||
@ -451,24 +451,24 @@ func TestRetrieveInstanceMetadata(t *testing.T) {
|
||||
attr := "someAttribute"
|
||||
|
||||
testCases := map[string]struct {
|
||||
client stubMetadataClient
|
||||
attr string
|
||||
expectedValue string
|
||||
expectErr bool
|
||||
client stubMetadataClient
|
||||
attr string
|
||||
wantValue string
|
||||
wantErr bool
|
||||
}{
|
||||
"retrieve works": {
|
||||
client: stubMetadataClient{
|
||||
InstanceValue: "someValue",
|
||||
InstanceErr: nil,
|
||||
},
|
||||
expectedValue: "someValue",
|
||||
wantValue: "someValue",
|
||||
},
|
||||
"retrieve fails": {
|
||||
client: stubMetadataClient{
|
||||
InstanceValue: "",
|
||||
InstanceErr: someErr,
|
||||
},
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -480,12 +480,12 @@ func TestRetrieveInstanceMetadata(t *testing.T) {
|
||||
client := Client{metadataAPI: tc.client}
|
||||
value, err := client.RetrieveInstanceMetadata(attr)
|
||||
|
||||
if tc.expectErr {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
return
|
||||
}
|
||||
require.NoError(err)
|
||||
assert.Equal(tc.expectedValue, value)
|
||||
assert.Equal(tc.wantValue, value)
|
||||
})
|
||||
}
|
||||
}
|
||||
@ -494,8 +494,8 @@ func TestSetInstanceMetadata(t *testing.T) {
|
||||
someErr := errors.New("failed")
|
||||
|
||||
testCases := map[string]struct {
|
||||
client stubInstancesClient
|
||||
expectErr bool
|
||||
client stubInstancesClient
|
||||
wantErr bool
|
||||
}{
|
||||
"set works": {
|
||||
client: stubInstancesClient{
|
||||
@ -512,10 +512,10 @@ func TestSetInstanceMetadata(t *testing.T) {
|
||||
client: stubInstancesClient{
|
||||
GetErr: someErr,
|
||||
},
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"retrieve returns nil": {
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"setting fails": {
|
||||
client: stubInstancesClient{
|
||||
@ -528,7 +528,7 @@ func TestSetInstanceMetadata(t *testing.T) {
|
||||
},
|
||||
SetMetadataErr: someErr,
|
||||
},
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -540,7 +540,7 @@ func TestSetInstanceMetadata(t *testing.T) {
|
||||
client := Client{instanceAPI: tc.client}
|
||||
err := client.SetInstanceMetadata(context.Background(), "project", "zone", "instanceName", "key", "value")
|
||||
|
||||
if tc.expectErr {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
return
|
||||
}
|
||||
@ -553,8 +553,8 @@ func TestUnsetInstanceMetadata(t *testing.T) {
|
||||
someErr := errors.New("failed")
|
||||
|
||||
testCases := map[string]struct {
|
||||
client stubInstancesClient
|
||||
expectErr bool
|
||||
client stubInstancesClient
|
||||
wantErr bool
|
||||
}{
|
||||
"unset works": {
|
||||
client: stubInstancesClient{
|
||||
@ -584,11 +584,11 @@ func TestUnsetInstanceMetadata(t *testing.T) {
|
||||
},
|
||||
},
|
||||
"retrieve fails": {
|
||||
client: stubInstancesClient{GetErr: someErr},
|
||||
expectErr: true,
|
||||
client: stubInstancesClient{GetErr: someErr},
|
||||
wantErr: true,
|
||||
},
|
||||
"retrieve returns nil": {
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"setting fails": {
|
||||
client: stubInstancesClient{
|
||||
@ -601,7 +601,7 @@ func TestUnsetInstanceMetadata(t *testing.T) {
|
||||
},
|
||||
SetMetadataErr: someErr,
|
||||
},
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -613,7 +613,7 @@ func TestUnsetInstanceMetadata(t *testing.T) {
|
||||
client := Client{instanceAPI: tc.client}
|
||||
err := client.UnsetInstanceMetadata(context.Background(), "project", "zone", "instanceName", "key")
|
||||
|
||||
if tc.expectErr {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
return
|
||||
}
|
||||
@ -636,36 +636,36 @@ func TestClose(t *testing.T) {
|
||||
|
||||
func TestFetchSSHKeys(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
metadata map[string]string
|
||||
expectedKeys map[string][]string
|
||||
metadata map[string]string
|
||||
wantKeys map[string][]string
|
||||
}{
|
||||
"fetch works": {
|
||||
metadata: map[string]string{"ssh-keys": "bob:ssh-rsa bobskey"},
|
||||
expectedKeys: map[string][]string{"bob": {"ssh-rsa bobskey"}},
|
||||
metadata: map[string]string{"ssh-keys": "bob:ssh-rsa bobskey"},
|
||||
wantKeys: map[string][]string{"bob": {"ssh-rsa bobskey"}},
|
||||
},
|
||||
"google ssh key metadata is ignored": {
|
||||
metadata: map[string]string{"ssh-keys": "bob:ssh-rsa bobskey google-ssh {\"userName\":\"bob\",\"expireOn\":\"2021-06-14T16:59:03+0000\"}"},
|
||||
expectedKeys: map[string][]string{"bob": {"ssh-rsa bobskey"}},
|
||||
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"}},
|
||||
},
|
||||
"ssh key format error is ignored": {
|
||||
metadata: map[string]string{"ssh-keys": "incorrect-format"},
|
||||
expectedKeys: map[string][]string{},
|
||||
metadata: map[string]string{"ssh-keys": "incorrect-format"},
|
||||
wantKeys: map[string][]string{},
|
||||
},
|
||||
"ssh key format space error is ignored": {
|
||||
metadata: map[string]string{"ssh-keys": "user:incorrect-key-format"},
|
||||
expectedKeys: map[string][]string{},
|
||||
metadata: map[string]string{"ssh-keys": "user:incorrect-key-format"},
|
||||
wantKeys: map[string][]string{},
|
||||
},
|
||||
"metadata field empty": {
|
||||
metadata: map[string]string{"ssh-keys": ""},
|
||||
expectedKeys: map[string][]string{},
|
||||
metadata: map[string]string{"ssh-keys": ""},
|
||||
wantKeys: map[string][]string{},
|
||||
},
|
||||
"metadata field missing": {
|
||||
metadata: map[string]string{},
|
||||
expectedKeys: map[string][]string{},
|
||||
metadata: map[string]string{},
|
||||
wantKeys: map[string][]string{},
|
||||
},
|
||||
"multiple keys": {
|
||||
metadata: map[string]string{"ssh-keys": "bob:ssh-rsa bobskey\nalice:ssh-rsa alicekey"},
|
||||
expectedKeys: map[string][]string{
|
||||
wantKeys: map[string][]string{
|
||||
"bob": {"ssh-rsa bobskey"},
|
||||
"alice": {"ssh-rsa alicekey"},
|
||||
},
|
||||
@ -677,7 +677,7 @@ func TestFetchSSHKeys(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
keys := extractSSHKeys(tc.metadata)
|
||||
assert.Equal(tc.expectedKeys, keys)
|
||||
assert.Equal(tc.wantKeys, keys)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -28,8 +28,8 @@ func TestList(t *testing.T) {
|
||||
client stubGCPClient
|
||||
instancesGenerator func() *[]core.Instance
|
||||
instancesMutator func(*[]core.Instance)
|
||||
expectErr bool
|
||||
expectedInstances []core.Instance
|
||||
wantErr bool
|
||||
wantInstances []core.Instance
|
||||
}{
|
||||
"retrieve works": {
|
||||
client: stubGCPClient{
|
||||
@ -40,7 +40,7 @@ func TestList(t *testing.T) {
|
||||
},
|
||||
},
|
||||
instancesGenerator: instancesGenerator,
|
||||
expectedInstances: []core.Instance{
|
||||
wantInstances: []core.Instance{
|
||||
{
|
||||
Name: "someInstance",
|
||||
ProviderID: "gce://someProject/someZone/someInstance",
|
||||
@ -58,21 +58,21 @@ func TestList(t *testing.T) {
|
||||
retrieveInstancesErr: err,
|
||||
},
|
||||
instancesGenerator: instancesGenerator,
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"project metadata retrieval error is detected": {
|
||||
client: stubGCPClient{
|
||||
retrieveProjectIDErr: err,
|
||||
},
|
||||
instancesGenerator: instancesGenerator,
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"zone retrieval error is detected": {
|
||||
client: stubGCPClient{
|
||||
retrieveZoneErr: err,
|
||||
},
|
||||
instancesGenerator: instancesGenerator,
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -88,12 +88,12 @@ func TestList(t *testing.T) {
|
||||
metadata := New(&tc.client)
|
||||
instances, err := metadata.List(context.Background())
|
||||
|
||||
if tc.expectErr {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
return
|
||||
}
|
||||
require.NoError(err)
|
||||
assert.ElementsMatch(tc.expectedInstances, instances)
|
||||
assert.ElementsMatch(tc.wantInstances, instances)
|
||||
})
|
||||
}
|
||||
}
|
||||
@ -103,9 +103,9 @@ func TestSelf(t *testing.T) {
|
||||
uid := "1234"
|
||||
|
||||
testCases := map[string]struct {
|
||||
client stubGCPClient
|
||||
expectErr bool
|
||||
expectedInstance core.Instance
|
||||
client stubGCPClient
|
||||
wantErr bool
|
||||
wantInstance core.Instance
|
||||
}{
|
||||
"retrieve works": {
|
||||
client: stubGCPClient{
|
||||
@ -117,7 +117,7 @@ func TestSelf(t *testing.T) {
|
||||
IPs: []string{"192.0.2.0"},
|
||||
},
|
||||
},
|
||||
expectedInstance: core.Instance{
|
||||
wantInstance: core.Instance{
|
||||
Name: "someInstance",
|
||||
ProviderID: "gce://someProject/someZone/someInstance",
|
||||
IPs: []string{"192.0.2.0"},
|
||||
@ -132,25 +132,25 @@ func TestSelf(t *testing.T) {
|
||||
},
|
||||
retrieveInstanceErr: err,
|
||||
},
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"project id retrieval error is detected": {
|
||||
client: stubGCPClient{
|
||||
retrieveProjectIDErr: err,
|
||||
},
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"zone retrieval error is detected": {
|
||||
client: stubGCPClient{
|
||||
retrieveZoneErr: err,
|
||||
},
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"instance name retrieval error is detected": {
|
||||
client: stubGCPClient{
|
||||
retrieveInstanceNameErr: err,
|
||||
},
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -162,12 +162,12 @@ func TestSelf(t *testing.T) {
|
||||
cloud := New(&tc.client)
|
||||
instance, err := cloud.Self(context.Background())
|
||||
|
||||
if tc.expectErr {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
return
|
||||
}
|
||||
require.NoError(err)
|
||||
assert.Equal(tc.expectedInstance, instance)
|
||||
assert.Equal(tc.wantInstance, instance)
|
||||
})
|
||||
}
|
||||
}
|
||||
@ -176,10 +176,10 @@ func TestGetInstance(t *testing.T) {
|
||||
err := errors.New("some err")
|
||||
|
||||
testCases := map[string]struct {
|
||||
providerID string
|
||||
client stubGCPClient
|
||||
expectErr bool
|
||||
expectedInstance core.Instance
|
||||
providerID string
|
||||
client stubGCPClient
|
||||
wantErr bool
|
||||
wantInstance core.Instance
|
||||
}{
|
||||
"retrieve works": {
|
||||
providerID: "gce://someProject/someZone/someInstance",
|
||||
@ -190,7 +190,7 @@ func TestGetInstance(t *testing.T) {
|
||||
IPs: []string{"192.0.2.0"},
|
||||
},
|
||||
},
|
||||
expectedInstance: core.Instance{
|
||||
wantInstance: core.Instance{
|
||||
Name: "someInstance",
|
||||
ProviderID: "gce://someProject/someZone/someInstance",
|
||||
IPs: []string{"192.0.2.0"},
|
||||
@ -201,15 +201,15 @@ func TestGetInstance(t *testing.T) {
|
||||
client: stubGCPClient{
|
||||
retrieveInstanceErr: err,
|
||||
},
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"malformed providerID with too many fields is detected": {
|
||||
providerID: "gce://someProject/someZone/someInstance/tooMany/fields",
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"malformed providerID with too few fields is detected": {
|
||||
providerID: "gce://someProject",
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -221,12 +221,12 @@ func TestGetInstance(t *testing.T) {
|
||||
cloud := New(&tc.client)
|
||||
instance, err := cloud.GetInstance(context.Background(), tc.providerID)
|
||||
|
||||
if tc.expectErr {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
return
|
||||
}
|
||||
require.NoError(err)
|
||||
assert.Equal(tc.expectedInstance, instance)
|
||||
assert.Equal(tc.wantInstance, instance)
|
||||
})
|
||||
}
|
||||
}
|
||||
@ -235,9 +235,9 @@ func TestSignalRole(t *testing.T) {
|
||||
err := errors.New("some err")
|
||||
|
||||
testCases := map[string]struct {
|
||||
client stubGCPClient
|
||||
expectErr bool
|
||||
expectedRole role.Role
|
||||
client stubGCPClient
|
||||
wantErr bool
|
||||
wantRole role.Role
|
||||
}{
|
||||
"signaling role works": {
|
||||
client: stubGCPClient{
|
||||
@ -245,25 +245,25 @@ func TestSignalRole(t *testing.T) {
|
||||
zone: "someZone",
|
||||
instanceName: "someName",
|
||||
},
|
||||
expectedRole: role.Coordinator,
|
||||
wantRole: role.Coordinator,
|
||||
},
|
||||
"project metadata retrieval error is detected": {
|
||||
client: stubGCPClient{
|
||||
retrieveProjectIDErr: err,
|
||||
},
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"instance zone retrieval error is detected": {
|
||||
client: stubGCPClient{
|
||||
retrieveZoneErr: err,
|
||||
},
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"instance name retrieval error is detected": {
|
||||
client: stubGCPClient{
|
||||
retrieveInstanceNameErr: err,
|
||||
},
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -273,9 +273,9 @@ func TestSignalRole(t *testing.T) {
|
||||
require := require.New(t)
|
||||
|
||||
cloud := New(&tc.client)
|
||||
err := cloud.SignalRole(context.Background(), tc.expectedRole)
|
||||
err := cloud.SignalRole(context.Background(), tc.wantRole)
|
||||
|
||||
if tc.expectErr {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
return
|
||||
}
|
||||
@ -285,7 +285,7 @@ func TestSignalRole(t *testing.T) {
|
||||
assert.ElementsMatch([]string{"someZone"}, tc.client.instanceMetadataZones)
|
||||
assert.ElementsMatch([]string{"someName"}, tc.client.instanceMetadataInstanceNames)
|
||||
assert.ElementsMatch([]string{core.RoleMetadataKey}, tc.client.instanceMetadataKeys)
|
||||
assert.ElementsMatch([]string{tc.expectedRole.String()}, tc.client.instanceMetadataValues)
|
||||
assert.ElementsMatch([]string{tc.wantRole.String()}, tc.client.instanceMetadataValues)
|
||||
})
|
||||
}
|
||||
}
|
||||
@ -294,9 +294,9 @@ func TestSetVPNIP(t *testing.T) {
|
||||
err := errors.New("some err")
|
||||
|
||||
testCases := map[string]struct {
|
||||
client stubGCPClient
|
||||
expectErr bool
|
||||
expectedVPNIP string
|
||||
client stubGCPClient
|
||||
wantErr bool
|
||||
wantVPNIP string
|
||||
}{
|
||||
"signaling role works": {
|
||||
client: stubGCPClient{
|
||||
@ -304,25 +304,25 @@ func TestSetVPNIP(t *testing.T) {
|
||||
zone: "someZone",
|
||||
instanceName: "someName",
|
||||
},
|
||||
expectedVPNIP: "192.0.2.0",
|
||||
wantVPNIP: "192.0.2.0",
|
||||
},
|
||||
"project metadata retrieval error is detected": {
|
||||
client: stubGCPClient{
|
||||
retrieveProjectIDErr: err,
|
||||
},
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"instance zone retrieval error is detected": {
|
||||
client: stubGCPClient{
|
||||
retrieveZoneErr: err,
|
||||
},
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"instance name retrieval error is detected": {
|
||||
client: stubGCPClient{
|
||||
retrieveInstanceNameErr: err,
|
||||
},
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -332,9 +332,9 @@ func TestSetVPNIP(t *testing.T) {
|
||||
require := require.New(t)
|
||||
|
||||
cloud := New(&tc.client)
|
||||
err := cloud.SetVPNIP(context.Background(), tc.expectedVPNIP)
|
||||
err := cloud.SetVPNIP(context.Background(), tc.wantVPNIP)
|
||||
|
||||
if tc.expectErr {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
return
|
||||
}
|
||||
@ -344,7 +344,7 @@ func TestSetVPNIP(t *testing.T) {
|
||||
assert.ElementsMatch([]string{"someZone"}, tc.client.instanceMetadataZones)
|
||||
assert.ElementsMatch([]string{"someName"}, tc.client.instanceMetadataInstanceNames)
|
||||
assert.ElementsMatch([]string{core.VPNIPMetadataKey}, tc.client.instanceMetadataKeys)
|
||||
assert.ElementsMatch([]string{tc.expectedVPNIP}, tc.client.instanceMetadataValues)
|
||||
assert.ElementsMatch([]string{tc.wantVPNIP}, tc.client.instanceMetadataValues)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -23,60 +23,60 @@ func TestGetServiceAccountKey(t *testing.T) {
|
||||
}
|
||||
testCases := map[string]struct {
|
||||
cloudServiceAccountURI string
|
||||
expectedKey client.ServiceAccountKey
|
||||
expectErr bool
|
||||
wantKey client.ServiceAccountKey
|
||||
wantErr bool
|
||||
}{
|
||||
"getServiceAccountKey works": {
|
||||
cloudServiceAccountURI: "serviceaccount://gcp?type=type&project_id=project-id&private_key_id=private-key-id&private_key=private-key&client_email=client-email&client_id=client-id&auth_uri=auth-uri&token_uri=token-uri&auth_provider_x509_cert_url=auth-provider-x509-cert-url&client_x509_cert_url=client-x509-cert-url",
|
||||
expectedKey: serviceAccountKey,
|
||||
wantKey: serviceAccountKey,
|
||||
},
|
||||
"missing type": {
|
||||
cloudServiceAccountURI: "serviceaccount://gcp?project_id=project-id&private_key_id=private-key-id&private_key=private-key&client_email=client-email&client_id=client-id&auth_uri=auth-uri&token_uri=token-uri&auth_provider_x509_cert_url=auth-provider-x509-cert-url&client_x509_cert_url=client-x509-cert-url",
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"missing project_id": {
|
||||
cloudServiceAccountURI: "serviceaccount://gcp?type=type&private_key_id=private-key-id&private_key=private-key&client_email=client-email&client_id=client-id&auth_uri=auth-uri&token_uri=token-uri&auth_provider_x509_cert_url=auth-provider-x509-cert-url&client_x509_cert_url=client-x509-cert-url",
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"missing private_key_id": {
|
||||
cloudServiceAccountURI: "serviceaccount://gcp?type=type&project_id=project-id&private_key=private-key&client_email=client-email&client_id=client-id&auth_uri=auth-uri&token_uri=token-uri&auth_provider_x509_cert_url=auth-provider-x509-cert-url&client_x509_cert_url=client-x509-cert-url",
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"missing private_key": {
|
||||
cloudServiceAccountURI: "serviceaccount://gcp?type=type&project_id=project-id&private_key_id=private-key-id&client_email=client-email&client_id=client-id&auth_uri=auth-uri&token_uri=token-uri&auth_provider_x509_cert_url=auth-provider-x509-cert-url&client_x509_cert_url=client-x509-cert-url",
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"missing client_email": {
|
||||
cloudServiceAccountURI: "serviceaccount://gcp?type=type&project_id=project-id&private_key_id=private-key-id&private_key=private-key&client_id=client-id&auth_uri=auth-uri&token_uri=token-uri&auth_provider_x509_cert_url=auth-provider-x509-cert-url&client_x509_cert_url=client-x509-cert-url",
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"missing client_id": {
|
||||
cloudServiceAccountURI: "serviceaccount://gcp?type=type&project_id=project-id&private_key_id=private-key-id&private_key=private-key&client_email=client-email&auth_uri=auth-uri&token_uri=token-uri&auth_provider_x509_cert_url=auth-provider-x509-cert-url&client_x509_cert_url=client-x509-cert-url",
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"missing token_uri": {
|
||||
cloudServiceAccountURI: "serviceaccount://gcp?type=type&project_id=project-id&private_key_id=private-key-id&private_key=private-key&client_email=client-email&client_id=client-id&auth_uri=auth-uri&auth_provider_x509_cert_url=auth-provider-x509-cert-url&client_x509_cert_url=client-x509-cert-url",
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"missing auth_provider_x509_cert_url": {
|
||||
cloudServiceAccountURI: "serviceaccount://gcp?type=type&project_id=project-id&private_key_id=private-key-id&private_key=private-key&client_email=client-email&client_id=client-id&auth_uri=auth-uri&token_uri=token-uri&client_x509_cert_url=client-x509-cert-url",
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"missing client_x509_cert_url": {
|
||||
cloudServiceAccountURI: "serviceaccount://gcp?type=type&project_id=project-id&private_key_id=private-key-id&private_key=private-key&client_email=client-email&client_id=client-id&auth_uri=auth-uri&token_uri=token-uri&auth_provider_x509_cert_url=auth-provider-x509-cert-url",
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"invalid URI fails": {
|
||||
cloudServiceAccountURI: "\x00",
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"incorrect URI scheme fails": {
|
||||
cloudServiceAccountURI: "invalid",
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"incorrect URI host fails": {
|
||||
cloudServiceAccountURI: "serviceaccount://incorrect",
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -86,12 +86,12 @@ func TestGetServiceAccountKey(t *testing.T) {
|
||||
require := require.New(t)
|
||||
|
||||
key, err := getServiceAccountKey(tc.cloudServiceAccountURI)
|
||||
if tc.expectErr {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
return
|
||||
}
|
||||
require.NoError(err)
|
||||
assert.Equal(tc.expectedKey, key)
|
||||
assert.Equal(tc.wantKey, key)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -12,22 +12,22 @@ func TestWriteGCEConf(t *testing.T) {
|
||||
config := "someConfig"
|
||||
|
||||
testCases := map[string]struct {
|
||||
fs afero.Afero
|
||||
expectedValue string
|
||||
expectErr bool
|
||||
fs afero.Afero
|
||||
wantValue string
|
||||
wantErr bool
|
||||
}{
|
||||
"write works": {
|
||||
fs: afero.Afero{
|
||||
Fs: afero.NewMemMapFs(),
|
||||
},
|
||||
expectedValue: config,
|
||||
expectErr: false,
|
||||
wantValue: config,
|
||||
wantErr: false,
|
||||
},
|
||||
"write fails": {
|
||||
fs: afero.Afero{
|
||||
Fs: afero.NewReadOnlyFs(afero.NewMemMapFs()),
|
||||
},
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -41,14 +41,14 @@ func TestWriteGCEConf(t *testing.T) {
|
||||
}
|
||||
err := writer.WriteGCEConf(config)
|
||||
|
||||
if tc.expectErr {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
return
|
||||
}
|
||||
require.NoError(err)
|
||||
value, err := tc.fs.ReadFile("/etc/gce.conf")
|
||||
assert.NoError(err)
|
||||
assert.Equal(tc.expectedValue, string(value))
|
||||
assert.Equal(tc.wantValue, string(value))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -10,29 +10,29 @@ import (
|
||||
|
||||
func TestExtractRole(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
metadata map[string]string
|
||||
expectedRole role.Role
|
||||
metadata map[string]string
|
||||
wantRole role.Role
|
||||
}{
|
||||
"coordinator role": {
|
||||
metadata: map[string]string{
|
||||
core.RoleMetadataKey: role.Coordinator.String(),
|
||||
},
|
||||
expectedRole: role.Coordinator,
|
||||
wantRole: role.Coordinator,
|
||||
},
|
||||
"node role": {
|
||||
metadata: map[string]string{
|
||||
core.RoleMetadataKey: role.Node.String(),
|
||||
},
|
||||
expectedRole: role.Node,
|
||||
wantRole: role.Node,
|
||||
},
|
||||
"unknown role": {
|
||||
metadata: map[string]string{
|
||||
core.RoleMetadataKey: "some-unknown-role",
|
||||
},
|
||||
expectedRole: role.Unknown,
|
||||
wantRole: role.Unknown,
|
||||
},
|
||||
"no role": {
|
||||
expectedRole: role.Unknown,
|
||||
wantRole: role.Unknown,
|
||||
},
|
||||
}
|
||||
|
||||
@ -42,7 +42,7 @@ func TestExtractRole(t *testing.T) {
|
||||
|
||||
role := ExtractRole(tc.metadata)
|
||||
|
||||
assert.Equal(tc.expectedRole, role)
|
||||
assert.Equal(tc.wantRole, role)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -14,9 +14,9 @@ func TestCoordinatorEndpoints(t *testing.T) {
|
||||
err := errors.New("some err")
|
||||
|
||||
testCases := map[string]struct {
|
||||
metadata stubMetadata
|
||||
expectErr bool
|
||||
expectedEndpoints []string
|
||||
metadata stubMetadata
|
||||
wantErr bool
|
||||
wantEndpoints []string
|
||||
}{
|
||||
"getting coordinator endpoints works and role is checked": {
|
||||
metadata: stubMetadata{
|
||||
@ -36,19 +36,19 @@ func TestCoordinatorEndpoints(t *testing.T) {
|
||||
},
|
||||
supportedRes: true,
|
||||
},
|
||||
expectErr: false,
|
||||
expectedEndpoints: []string{"192.0.2.1:9000"},
|
||||
wantErr: false,
|
||||
wantEndpoints: []string{"192.0.2.1:9000"},
|
||||
},
|
||||
"List fails": {
|
||||
metadata: stubMetadata{
|
||||
listErr: err,
|
||||
supportedRes: true,
|
||||
},
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"metadata API unsupported": {
|
||||
metadata: stubMetadata{},
|
||||
expectErr: true,
|
||||
metadata: stubMetadata{},
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -59,13 +59,13 @@ func TestCoordinatorEndpoints(t *testing.T) {
|
||||
|
||||
endpoints, err := CoordinatorEndpoints(context.Background(), &tc.metadata)
|
||||
|
||||
if tc.expectErr {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
return
|
||||
}
|
||||
require.NoError(err)
|
||||
|
||||
assert.ElementsMatch(tc.expectedEndpoints, endpoints)
|
||||
assert.ElementsMatch(tc.wantEndpoints, endpoints)
|
||||
})
|
||||
}
|
||||
}
|
||||
@ -74,20 +74,20 @@ func TestPrepareInstanceForCCM(t *testing.T) {
|
||||
err := errors.New("some err")
|
||||
|
||||
testCases := map[string]struct {
|
||||
metadata stubMetadata
|
||||
vpnIP string
|
||||
expectErr bool
|
||||
metadata stubMetadata
|
||||
vpnIP string
|
||||
wantErr bool
|
||||
}{
|
||||
"updating role works": {
|
||||
metadata: stubMetadata{},
|
||||
vpnIP: "192.0.2.1",
|
||||
expectErr: false,
|
||||
metadata: stubMetadata{},
|
||||
vpnIP: "192.0.2.1",
|
||||
wantErr: false,
|
||||
},
|
||||
"setting VPN IP fails": {
|
||||
metadata: stubMetadata{
|
||||
setVPNIPErr: err,
|
||||
},
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -98,7 +98,7 @@ func TestPrepareInstanceForCCM(t *testing.T) {
|
||||
|
||||
err := PrepareInstanceForCCM(context.Background(), &tc.metadata, &CloudControllerManagerFake{}, tc.vpnIP)
|
||||
|
||||
if tc.expectErr {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
return
|
||||
}
|
||||
|
@ -22,21 +22,21 @@ func TestInitCluster(t *testing.T) {
|
||||
someErr := errors.New("someErr")
|
||||
|
||||
testCases := map[string]struct {
|
||||
cluster clusterStub
|
||||
metadata stubMetadata
|
||||
cloudControllerManager stubCloudControllerManager
|
||||
cloudNodeManager stubCloudNodeManager
|
||||
clusterAutoscaler stubClusterAutoscaler
|
||||
autoscalingNodeGroups []string
|
||||
expectErr bool
|
||||
expectedInitClusterInput kubernetes.InitClusterInput
|
||||
cluster clusterStub
|
||||
metadata stubMetadata
|
||||
cloudControllerManager stubCloudControllerManager
|
||||
cloudNodeManager stubCloudNodeManager
|
||||
clusterAutoscaler stubClusterAutoscaler
|
||||
autoscalingNodeGroups []string
|
||||
wantErr bool
|
||||
wantInitClusterInput kubernetes.InitClusterInput
|
||||
}{
|
||||
"InitCluster works": {
|
||||
cluster: clusterStub{
|
||||
kubeconfig: []byte("kubeconfig"),
|
||||
},
|
||||
autoscalingNodeGroups: []string{"someNodeGroup"},
|
||||
expectedInitClusterInput: kubernetes.InitClusterInput{
|
||||
wantInitClusterInput: kubernetes.InitClusterInput{
|
||||
APIServerAdvertiseIP: "10.118.0.1",
|
||||
NodeIP: "10.118.0.1",
|
||||
NodeName: "10.118.0.1",
|
||||
@ -44,7 +44,7 @@ func TestInitCluster(t *testing.T) {
|
||||
SupportClusterAutoscaler: false,
|
||||
AutoscalingNodeGroups: []string{"someNodeGroup"},
|
||||
},
|
||||
expectErr: false,
|
||||
wantErr: false,
|
||||
},
|
||||
"Instance metadata is retrieved": {
|
||||
cluster: clusterStub{
|
||||
@ -57,7 +57,7 @@ func TestInitCluster(t *testing.T) {
|
||||
},
|
||||
supportedRes: true,
|
||||
},
|
||||
expectedInitClusterInput: kubernetes.InitClusterInput{
|
||||
wantInitClusterInput: kubernetes.InitClusterInput{
|
||||
APIServerAdvertiseIP: "10.118.0.1",
|
||||
NodeIP: "10.118.0.1",
|
||||
NodeName: "some-name",
|
||||
@ -65,7 +65,7 @@ func TestInitCluster(t *testing.T) {
|
||||
SupportsCloudControllerManager: false,
|
||||
SupportClusterAutoscaler: false,
|
||||
},
|
||||
expectErr: false,
|
||||
wantErr: false,
|
||||
},
|
||||
"metadata of self retrieval error is checked": {
|
||||
cluster: clusterStub{
|
||||
@ -75,7 +75,7 @@ func TestInitCluster(t *testing.T) {
|
||||
supportedRes: true,
|
||||
selfErr: errors.New("metadata retrieval error"),
|
||||
},
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"Autoscaler is prepared when supported": {
|
||||
cluster: clusterStub{
|
||||
@ -86,7 +86,7 @@ func TestInitCluster(t *testing.T) {
|
||||
supportedRes: true,
|
||||
},
|
||||
autoscalingNodeGroups: []string{"someNodeGroup"},
|
||||
expectedInitClusterInput: kubernetes.InitClusterInput{
|
||||
wantInitClusterInput: kubernetes.InitClusterInput{
|
||||
APIServerAdvertiseIP: "10.118.0.1",
|
||||
NodeIP: "10.118.0.1",
|
||||
NodeName: "10.118.0.1",
|
||||
@ -95,7 +95,7 @@ func TestInitCluster(t *testing.T) {
|
||||
AutoscalingCloudprovider: "some-name",
|
||||
AutoscalingNodeGroups: []string{"someNodeGroup"},
|
||||
},
|
||||
expectErr: false,
|
||||
wantErr: false,
|
||||
},
|
||||
"Node is prepared for CCM if supported": {
|
||||
cluster: clusterStub{
|
||||
@ -107,7 +107,7 @@ func TestInitCluster(t *testing.T) {
|
||||
imageRes: "someImage",
|
||||
pathRes: "/some/path",
|
||||
},
|
||||
expectedInitClusterInput: kubernetes.InitClusterInput{
|
||||
wantInitClusterInput: kubernetes.InitClusterInput{
|
||||
APIServerAdvertiseIP: "10.118.0.1",
|
||||
NodeIP: "10.118.0.1",
|
||||
NodeName: "10.118.0.1",
|
||||
@ -117,7 +117,7 @@ func TestInitCluster(t *testing.T) {
|
||||
CloudControllerManagerImage: "someImage",
|
||||
CloudControllerManagerPath: "/some/path",
|
||||
},
|
||||
expectErr: false,
|
||||
wantErr: false,
|
||||
},
|
||||
"Node preparation for CCM can fail": {
|
||||
cluster: clusterStub{
|
||||
@ -133,7 +133,7 @@ func TestInitCluster(t *testing.T) {
|
||||
pathRes: "/some/path",
|
||||
prepareInstanceRes: errors.New("preparing node for CCM failed"),
|
||||
},
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"updating role fails without error": {
|
||||
cluster: clusterStub{
|
||||
@ -143,8 +143,8 @@ func TestInitCluster(t *testing.T) {
|
||||
signalRoleErr: errors.New("updating role fails"),
|
||||
supportedRes: true,
|
||||
},
|
||||
expectErr: false,
|
||||
expectedInitClusterInput: kubernetes.InitClusterInput{
|
||||
wantErr: false,
|
||||
wantInitClusterInput: kubernetes.InitClusterInput{
|
||||
APIServerAdvertiseIP: "10.118.0.1",
|
||||
NodeIP: "10.118.0.1",
|
||||
},
|
||||
@ -153,13 +153,13 @@ func TestInitCluster(t *testing.T) {
|
||||
cluster: clusterStub{
|
||||
getKubeconfigErr: errors.New("getting kubeconfig fails"),
|
||||
},
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"InitCluster fail detected": {
|
||||
cluster: clusterStub{
|
||||
initErr: someErr,
|
||||
},
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -175,13 +175,13 @@ func TestInitCluster(t *testing.T) {
|
||||
|
||||
kubeconfig, err := core.InitCluster(tc.autoscalingNodeGroups, "cloud-service-account-uri")
|
||||
|
||||
if tc.expectErr {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
return
|
||||
}
|
||||
require.NoError(err)
|
||||
require.Len(tc.cluster.initInputs, 1)
|
||||
assert.Equal(tc.expectedInitClusterInput, tc.cluster.initInputs[0])
|
||||
assert.Equal(tc.wantInitClusterInput, tc.cluster.initInputs[0])
|
||||
assert.Equal(tc.cluster.kubeconfig, kubeconfig)
|
||||
})
|
||||
}
|
||||
@ -191,20 +191,20 @@ func TestJoinCluster(t *testing.T) {
|
||||
someErr := errors.New("someErr")
|
||||
|
||||
testCases := map[string]struct {
|
||||
cluster clusterStub
|
||||
metadata stubMetadata
|
||||
cloudControllerManager stubCloudControllerManager
|
||||
cloudNodeManager stubCloudNodeManager
|
||||
clusterAutoscaler stubClusterAutoscaler
|
||||
vpn stubVPN
|
||||
expectErr bool
|
||||
expectedJoinClusterArgs joinClusterArgs
|
||||
cluster clusterStub
|
||||
metadata stubMetadata
|
||||
cloudControllerManager stubCloudControllerManager
|
||||
cloudNodeManager stubCloudNodeManager
|
||||
clusterAutoscaler stubClusterAutoscaler
|
||||
vpn stubVPN
|
||||
wantErr bool
|
||||
wantJoinClusterArgs joinClusterArgs
|
||||
}{
|
||||
"JoinCluster works": {
|
||||
vpn: stubVPN{
|
||||
interfaceIP: "192.0.2.0",
|
||||
},
|
||||
expectedJoinClusterArgs: joinClusterArgs{
|
||||
wantJoinClusterArgs: joinClusterArgs{
|
||||
args: &kubeadm.BootstrapTokenDiscovery{
|
||||
APIServerEndpoint: "192.0.2.0:6443",
|
||||
Token: "someToken",
|
||||
@ -218,13 +218,13 @@ func TestJoinCluster(t *testing.T) {
|
||||
cluster: clusterStub{
|
||||
joinErr: someErr,
|
||||
},
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"retrieving vpn ip failure detected": {
|
||||
vpn: stubVPN{
|
||||
getInterfaceIPErr: errors.New("retrieving interface ip error"),
|
||||
},
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"Instance metadata is retrieved": {
|
||||
metadata: stubMetadata{
|
||||
@ -234,7 +234,7 @@ func TestJoinCluster(t *testing.T) {
|
||||
},
|
||||
supportedRes: true,
|
||||
},
|
||||
expectedJoinClusterArgs: joinClusterArgs{
|
||||
wantJoinClusterArgs: joinClusterArgs{
|
||||
args: &kubeadm.BootstrapTokenDiscovery{
|
||||
APIServerEndpoint: "192.0.2.0:6443",
|
||||
Token: "someToken",
|
||||
@ -243,14 +243,14 @@ func TestJoinCluster(t *testing.T) {
|
||||
nodeName: "some-name",
|
||||
providerID: "fake://providerid",
|
||||
},
|
||||
expectErr: false,
|
||||
wantErr: false,
|
||||
},
|
||||
"Instance metadata retrieval can fail": {
|
||||
metadata: stubMetadata{
|
||||
supportedRes: true,
|
||||
selfErr: errors.New("metadata retrieval error"),
|
||||
},
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"CCM preparation failure is detected": {
|
||||
metadata: stubMetadata{
|
||||
@ -260,15 +260,15 @@ func TestJoinCluster(t *testing.T) {
|
||||
supportedRes: true,
|
||||
prepareInstanceRes: errors.New("ccm prepare fails"),
|
||||
},
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"updating role fails without error": {
|
||||
metadata: stubMetadata{
|
||||
signalRoleErr: errors.New("updating role fails"),
|
||||
supportedRes: true,
|
||||
},
|
||||
expectErr: false,
|
||||
expectedJoinClusterArgs: joinClusterArgs{
|
||||
wantErr: false,
|
||||
wantJoinClusterArgs: joinClusterArgs{
|
||||
args: &kubeadm.BootstrapTokenDiscovery{
|
||||
APIServerEndpoint: "192.0.2.0:6443",
|
||||
Token: "someToken",
|
||||
@ -295,13 +295,13 @@ func TestJoinCluster(t *testing.T) {
|
||||
}
|
||||
err = core.JoinCluster(joinReq, "", role.Node)
|
||||
|
||||
if tc.expectErr {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
return
|
||||
}
|
||||
require.NoError(err)
|
||||
require.Len(tc.cluster.joinClusterArgs, 1)
|
||||
assert.Equal(tc.expectedJoinClusterArgs, tc.cluster.joinClusterArgs[0])
|
||||
assert.Equal(tc.wantJoinClusterArgs, tc.cluster.joinClusterArgs[0])
|
||||
})
|
||||
}
|
||||
}
|
||||
@ -309,20 +309,20 @@ func TestJoinCluster(t *testing.T) {
|
||||
func TestK8sCompliantHostname(t *testing.T) {
|
||||
compliantHostname := regexp.MustCompile(`^[a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*$`)
|
||||
testCases := map[string]struct {
|
||||
hostname string
|
||||
expectedHostname string
|
||||
hostname string
|
||||
wantHostname string
|
||||
}{
|
||||
"azure scale set names work": {
|
||||
hostname: "constellation-scale-set-coordinators-name_0",
|
||||
expectedHostname: "constellation-scale-set-coordinators-name-0",
|
||||
hostname: "constellation-scale-set-coordinators-name_0",
|
||||
wantHostname: "constellation-scale-set-coordinators-name-0",
|
||||
},
|
||||
"compliant hostname is not modified": {
|
||||
hostname: "abcd-123",
|
||||
expectedHostname: "abcd-123",
|
||||
hostname: "abcd-123",
|
||||
wantHostname: "abcd-123",
|
||||
},
|
||||
"uppercase hostnames are lowercased": {
|
||||
hostname: "ABCD",
|
||||
expectedHostname: "abcd",
|
||||
hostname: "ABCD",
|
||||
wantHostname: "abcd",
|
||||
},
|
||||
}
|
||||
|
||||
@ -332,7 +332,7 @@ func TestK8sCompliantHostname(t *testing.T) {
|
||||
|
||||
hostname := k8sCompliantHostname(tc.hostname)
|
||||
|
||||
assert.Equal(tc.expectedHostname, hostname)
|
||||
assert.Equal(tc.wantHostname, hostname)
|
||||
assert.Regexp(compliantHostname, hostname)
|
||||
})
|
||||
}
|
||||
|
@ -156,37 +156,37 @@ func TestDeriveKey(t *testing.T) {
|
||||
|
||||
func TestInitialize(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
initializePCRs bool
|
||||
writeNodeState bool
|
||||
role role.Role
|
||||
expectActivated bool
|
||||
expectedState state.State
|
||||
expectPanic bool
|
||||
expectErr bool
|
||||
initializePCRs bool
|
||||
writeNodeState bool
|
||||
role role.Role
|
||||
wantActivated bool
|
||||
wantState state.State
|
||||
wantPanic bool
|
||||
wantErr bool
|
||||
}{
|
||||
"fresh node": {
|
||||
expectedState: state.AcceptingInit,
|
||||
wantState: state.AcceptingInit,
|
||||
},
|
||||
"activated coordinator": {
|
||||
initializePCRs: true,
|
||||
writeNodeState: true,
|
||||
role: role.Coordinator,
|
||||
expectPanic: true, // TODO: adapt test case once restart is implemented
|
||||
expectActivated: true,
|
||||
expectedState: state.ActivatingNodes,
|
||||
initializePCRs: true,
|
||||
writeNodeState: true,
|
||||
role: role.Coordinator,
|
||||
wantPanic: true, // TODO: adapt test case once restart is implemented
|
||||
wantActivated: true,
|
||||
wantState: state.ActivatingNodes,
|
||||
},
|
||||
"activated node": {
|
||||
initializePCRs: true,
|
||||
writeNodeState: true,
|
||||
role: role.Node,
|
||||
expectPanic: true, // TODO: adapt test case once restart is implemented
|
||||
expectActivated: true,
|
||||
expectedState: state.IsNode,
|
||||
initializePCRs: true,
|
||||
writeNodeState: true,
|
||||
role: role.Node,
|
||||
wantPanic: true, // TODO: adapt test case once restart is implemented
|
||||
wantActivated: true,
|
||||
wantState: state.IsNode,
|
||||
},
|
||||
"activated node with no node state": {
|
||||
initializePCRs: true,
|
||||
writeNodeState: false,
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -211,19 +211,19 @@ func TestInitialize(t *testing.T) {
|
||||
core, err := NewCore(&stubVPN{}, nil, nil, nil, nil, nil, nil, zaptest.NewLogger(t), openTPM, nil, fileHandler)
|
||||
require.NoError(err)
|
||||
|
||||
if tc.expectPanic {
|
||||
if tc.wantPanic {
|
||||
assert.Panics(func() { _, _ = core.Initialize() })
|
||||
return
|
||||
}
|
||||
|
||||
nodeActivated, err := core.Initialize()
|
||||
if tc.expectErr {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
return
|
||||
}
|
||||
require.NoError(err)
|
||||
assert.Equal(tc.expectActivated, nodeActivated)
|
||||
assert.Equal(tc.expectedState, core.state)
|
||||
assert.Equal(tc.wantActivated, nodeActivated)
|
||||
assert.Equal(tc.wantState, core.state)
|
||||
})
|
||||
}
|
||||
}
|
||||
@ -232,7 +232,7 @@ func TestPersistNodeState(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
vpn VPN
|
||||
touchStateFile bool
|
||||
errExpected bool
|
||||
wantErr bool
|
||||
}{
|
||||
"persisting works": {
|
||||
vpn: &stubVPN{
|
||||
@ -243,14 +243,14 @@ func TestPersistNodeState(t *testing.T) {
|
||||
vpn: &stubVPN{
|
||||
getPrivateKeyErr: errors.New("error"),
|
||||
},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"writing node state over existing file fails": {
|
||||
vpn: &stubVPN{
|
||||
privateKey: []byte("private-key"),
|
||||
},
|
||||
touchStateFile: true,
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -269,7 +269,7 @@ func TestPersistNodeState(t *testing.T) {
|
||||
core, err := NewCore(tc.vpn, nil, nil, nil, nil, nil, nil, zaptest.NewLogger(t), nil, nil, fileHandler)
|
||||
require.NoError(err)
|
||||
err = core.PersistNodeState(role.Coordinator, []byte("owner-id"), []byte("cluster-id"))
|
||||
if tc.errExpected {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
return
|
||||
}
|
||||
|
@ -13,21 +13,21 @@ import (
|
||||
|
||||
func TestGetDiskUUID(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
expectedUUID string
|
||||
openErr error
|
||||
uuidErr error
|
||||
errExpected bool
|
||||
wantUUID string
|
||||
openErr error
|
||||
uuidErr error
|
||||
wantErr bool
|
||||
}{
|
||||
"getting uuid works": {
|
||||
expectedUUID: "uuid",
|
||||
wantUUID: "uuid",
|
||||
},
|
||||
"open can fail": {
|
||||
openErr: errors.New("open-error"),
|
||||
errExpected: true,
|
||||
openErr: errors.New("open-error"),
|
||||
wantErr: true,
|
||||
},
|
||||
"getting disk uuid can fail": {
|
||||
uuidErr: errors.New("uuid-err"),
|
||||
errExpected: true,
|
||||
uuidErr: errors.New("uuid-err"),
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -41,18 +41,18 @@ func TestGetDiskUUID(t *testing.T) {
|
||||
diskStub := encryptedDiskStub{
|
||||
openErr: tc.openErr,
|
||||
uuidErr: tc.uuidErr,
|
||||
uuid: tc.expectedUUID,
|
||||
uuid: tc.wantUUID,
|
||||
}
|
||||
core, err := NewCore(&stubVPN{}, nil, nil, nil, nil, nil, &diskStub, zapLogger, nil, nil, file.NewHandler(afero.NewMemMapFs()))
|
||||
require.NoError(err)
|
||||
uuid, err := core.GetDiskUUID()
|
||||
if tc.errExpected {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
return
|
||||
}
|
||||
require.NoError(err)
|
||||
|
||||
assert.Equal(tc.expectedUUID, uuid)
|
||||
assert.Equal(tc.wantUUID, uuid)
|
||||
})
|
||||
}
|
||||
}
|
||||
@ -61,16 +61,16 @@ func TestUpdateDiskPassphrase(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
openErr error
|
||||
updatePassphraseErr error
|
||||
errExpected bool
|
||||
wantErr bool
|
||||
}{
|
||||
"updating passphrase works": {},
|
||||
"open can fail": {
|
||||
openErr: errors.New("open-error"),
|
||||
errExpected: true,
|
||||
openErr: errors.New("open-error"),
|
||||
wantErr: true,
|
||||
},
|
||||
"updating disk passphrase can fail": {
|
||||
updatePassphraseErr: errors.New("update-err"),
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -88,7 +88,7 @@ func TestUpdateDiskPassphrase(t *testing.T) {
|
||||
core, err := NewCore(&stubVPN{}, nil, nil, nil, nil, nil, &diskStub, zapLogger, nil, nil, file.NewHandler(afero.NewMemMapFs()))
|
||||
require.NoError(err)
|
||||
err = core.UpdateDiskPassphrase("passphrase")
|
||||
if tc.errExpected {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
return
|
||||
}
|
||||
|
@ -19,32 +19,32 @@ func TestGetPeers(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
storePeers []peer.Peer
|
||||
resourceVersion int
|
||||
expectedPeers []peer.Peer
|
||||
wantPeers []peer.Peer
|
||||
}{
|
||||
"request version 0": { // store has version 2
|
||||
storePeers: []peer.Peer{peer1, peer2},
|
||||
resourceVersion: 0,
|
||||
expectedPeers: []peer.Peer{peer1, peer2},
|
||||
wantPeers: []peer.Peer{peer1, peer2},
|
||||
},
|
||||
"request version 1": {
|
||||
storePeers: []peer.Peer{peer1, peer2},
|
||||
resourceVersion: 1,
|
||||
expectedPeers: []peer.Peer{peer1, peer2},
|
||||
wantPeers: []peer.Peer{peer1, peer2},
|
||||
},
|
||||
"request version 2": {
|
||||
storePeers: []peer.Peer{peer1, peer2},
|
||||
resourceVersion: 2,
|
||||
expectedPeers: nil,
|
||||
wantPeers: nil,
|
||||
},
|
||||
"request version 3": {
|
||||
storePeers: []peer.Peer{peer1, peer2},
|
||||
resourceVersion: 3,
|
||||
expectedPeers: []peer.Peer{peer1, peer2},
|
||||
wantPeers: []peer.Peer{peer1, peer2},
|
||||
},
|
||||
"request version 4": {
|
||||
storePeers: []peer.Peer{peer1, peer2},
|
||||
resourceVersion: 4,
|
||||
expectedPeers: []peer.Peer{peer1, peer2},
|
||||
wantPeers: []peer.Peer{peer1, peer2},
|
||||
},
|
||||
}
|
||||
|
||||
@ -66,7 +66,7 @@ func TestGetPeers(t *testing.T) {
|
||||
require.NoError(err)
|
||||
|
||||
assert.Equal(2, resourceVersion)
|
||||
assert.ElementsMatch(tc.expectedPeers, peers)
|
||||
assert.ElementsMatch(tc.wantPeers, peers)
|
||||
})
|
||||
}
|
||||
}
|
||||
@ -78,33 +78,33 @@ func TestAddPeer(t *testing.T) {
|
||||
VPNIP: "192.0.2.21",
|
||||
VPNPubKey: []byte{2, 3, 4},
|
||||
}
|
||||
expectedVPNPeers := []stubVPNPeer{{
|
||||
wantVPNPeers := []stubVPNPeer{{
|
||||
pubKey: testPeer.VPNPubKey,
|
||||
publicIP: "192.0.2.11",
|
||||
vpnIP: testPeer.VPNIP,
|
||||
}}
|
||||
|
||||
testCases := map[string]struct {
|
||||
peer peer.Peer
|
||||
vpn stubVPN
|
||||
expectErr bool
|
||||
expectedVPNPeers []stubVPNPeer
|
||||
expectedStorePeers []peer.Peer
|
||||
peer peer.Peer
|
||||
vpn stubVPN
|
||||
wantErr bool
|
||||
wantVPNPeers []stubVPNPeer
|
||||
wantStorePeers []peer.Peer
|
||||
}{
|
||||
"add peer": {
|
||||
peer: testPeer,
|
||||
expectedVPNPeers: expectedVPNPeers,
|
||||
expectedStorePeers: []peer.Peer{testPeer},
|
||||
peer: testPeer,
|
||||
wantVPNPeers: wantVPNPeers,
|
||||
wantStorePeers: []peer.Peer{testPeer},
|
||||
},
|
||||
"don't add self to vpn": {
|
||||
peer: testPeer,
|
||||
vpn: stubVPN{interfaceIP: testPeer.VPNIP},
|
||||
expectedStorePeers: []peer.Peer{testPeer},
|
||||
peer: testPeer,
|
||||
vpn: stubVPN{interfaceIP: testPeer.VPNIP},
|
||||
wantStorePeers: []peer.Peer{testPeer},
|
||||
},
|
||||
"vpn add peer error": {
|
||||
peer: testPeer,
|
||||
vpn: stubVPN{addPeerErr: someErr},
|
||||
expectErr: true,
|
||||
peer: testPeer,
|
||||
vpn: stubVPN{addPeerErr: someErr},
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -118,17 +118,17 @@ func TestAddPeer(t *testing.T) {
|
||||
|
||||
err = core.AddPeer(tc.peer)
|
||||
|
||||
if tc.expectErr {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
return
|
||||
}
|
||||
require.NoError(err)
|
||||
|
||||
assert.Equal(tc.expectedVPNPeers, tc.vpn.peers)
|
||||
assert.Equal(tc.wantVPNPeers, tc.vpn.peers)
|
||||
|
||||
actualStorePeers, err := core.data().GetPeers()
|
||||
require.NoError(err)
|
||||
assert.Equal(tc.expectedStorePeers, actualStorePeers)
|
||||
assert.Equal(tc.wantStorePeers, actualStorePeers)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -18,21 +18,21 @@ func TestAdvanceState(t *testing.T) {
|
||||
someErr := errors.New("failed")
|
||||
|
||||
testCases := map[string]struct {
|
||||
initialState state.State
|
||||
newState state.State
|
||||
openTPMErr error
|
||||
expectErr bool
|
||||
expectOpenTPMCalled bool
|
||||
initialState state.State
|
||||
newState state.State
|
||||
openTPMErr error
|
||||
wantErr bool
|
||||
wantOpenTPMCalled bool
|
||||
}{
|
||||
"init -> coordinator": {
|
||||
initialState: state.AcceptingInit,
|
||||
newState: state.ActivatingNodes,
|
||||
expectOpenTPMCalled: true,
|
||||
initialState: state.AcceptingInit,
|
||||
newState: state.ActivatingNodes,
|
||||
wantOpenTPMCalled: true,
|
||||
},
|
||||
"init -> node": {
|
||||
initialState: state.AcceptingInit,
|
||||
newState: state.IsNode,
|
||||
expectOpenTPMCalled: true,
|
||||
initialState: state.AcceptingInit,
|
||||
newState: state.IsNode,
|
||||
wantOpenTPMCalled: true,
|
||||
},
|
||||
"init -> failed": {
|
||||
initialState: state.AcceptingInit,
|
||||
@ -43,11 +43,11 @@ func TestAdvanceState(t *testing.T) {
|
||||
newState: state.AcceptingInit,
|
||||
},
|
||||
"openTPM error": {
|
||||
initialState: state.AcceptingInit,
|
||||
newState: state.ActivatingNodes,
|
||||
openTPMErr: someErr,
|
||||
expectErr: true,
|
||||
expectOpenTPMCalled: true,
|
||||
initialState: state.AcceptingInit,
|
||||
newState: state.ActivatingNodes,
|
||||
openTPMErr: someErr,
|
||||
wantErr: true,
|
||||
wantOpenTPMCalled: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -71,9 +71,9 @@ func TestAdvanceState(t *testing.T) {
|
||||
core.state = tc.initialState
|
||||
|
||||
err = core.AdvanceState(tc.newState, []byte("secret"), []byte("cluster"))
|
||||
assert.Equal(tc.expectOpenTPMCalled, openTPMCalled)
|
||||
assert.Equal(tc.wantOpenTPMCalled, openTPMCalled)
|
||||
|
||||
if tc.expectErr {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
assert.Equal(tc.initialState, core.GetState())
|
||||
return
|
||||
|
@ -14,23 +14,23 @@ func TestOpenClose(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
initByNameErr error
|
||||
operations []string
|
||||
errExpected bool
|
||||
wantErr bool
|
||||
}{
|
||||
"open and close work": {
|
||||
operations: []string{"open", "close"},
|
||||
},
|
||||
"opening twice fails": {
|
||||
operations: []string{"open", "open"},
|
||||
errExpected: true,
|
||||
operations: []string{"open", "open"},
|
||||
wantErr: true,
|
||||
},
|
||||
"closing first fails": {
|
||||
operations: []string{"close"},
|
||||
errExpected: true,
|
||||
operations: []string{"close"},
|
||||
wantErr: true,
|
||||
},
|
||||
"initByName failure detected": {
|
||||
initByNameErr: errors.New("initByNameErr"),
|
||||
operations: []string{"open"},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -47,7 +47,7 @@ func TestOpenClose(t *testing.T) {
|
||||
}
|
||||
|
||||
err := executeOperations(&crypt, tc.operations)
|
||||
if tc.errExpected {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
return
|
||||
}
|
||||
@ -58,20 +58,20 @@ func TestOpenClose(t *testing.T) {
|
||||
|
||||
func TestUUID(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
open bool
|
||||
expectedUUID string
|
||||
errExpected bool
|
||||
open bool
|
||||
wantUUID string
|
||||
wantErr bool
|
||||
}{
|
||||
"getting uuid works": {
|
||||
open: true,
|
||||
expectedUUID: "uuid",
|
||||
open: true,
|
||||
wantUUID: "uuid",
|
||||
},
|
||||
"getting uuid on closed device fails": {
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"empty uuid is detected": {
|
||||
open: true,
|
||||
errExpected: true,
|
||||
open: true,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -83,7 +83,7 @@ func TestUUID(t *testing.T) {
|
||||
crypt := Cryptsetup{
|
||||
fs: afero.NewMemMapFs(),
|
||||
initByName: func(name string) (cryptdevice, error) {
|
||||
return &stubCryptdevice{uuid: tc.expectedUUID}, nil
|
||||
return &stubCryptdevice{uuid: tc.wantUUID}, nil
|
||||
},
|
||||
}
|
||||
|
||||
@ -92,12 +92,12 @@ func TestUUID(t *testing.T) {
|
||||
defer crypt.Close()
|
||||
}
|
||||
uuid, err := crypt.UUID()
|
||||
if tc.errExpected {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
return
|
||||
}
|
||||
require.NoError(err)
|
||||
assert.Equal(tc.expectedUUID, uuid)
|
||||
assert.Equal(tc.wantUUID, uuid)
|
||||
})
|
||||
}
|
||||
}
|
||||
@ -107,24 +107,24 @@ func TestUpdatePassphrase(t *testing.T) {
|
||||
writePassphrase bool
|
||||
open bool
|
||||
keyslotChangeByPassphraseErr error
|
||||
errExpected bool
|
||||
wantErr bool
|
||||
}{
|
||||
"updating passphrase works": {
|
||||
writePassphrase: true,
|
||||
open: true,
|
||||
},
|
||||
"updating passphrase on closed device fails": {
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"reading initial passphrase can fail": {
|
||||
open: true,
|
||||
errExpected: true,
|
||||
open: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"changing keyslot passphrase can fail": {
|
||||
open: true,
|
||||
writePassphrase: true,
|
||||
keyslotChangeByPassphraseErr: errors.New("keyslotChangeByPassphraseErr"),
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -151,7 +151,7 @@ func TestUpdatePassphrase(t *testing.T) {
|
||||
defer crypt.Close()
|
||||
}
|
||||
err := crypt.UpdatePassphrase("new-key")
|
||||
if tc.errExpected {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
return
|
||||
}
|
||||
|
@ -12,36 +12,36 @@ import (
|
||||
|
||||
func TestGetStore(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
uri string
|
||||
errExpected bool
|
||||
uri string
|
||||
wantErr bool
|
||||
}{
|
||||
"no store": {
|
||||
uri: NoStoreURI,
|
||||
errExpected: false,
|
||||
uri: NoStoreURI,
|
||||
wantErr: false,
|
||||
},
|
||||
"aws s3": {
|
||||
uri: fmt.Sprintf(AWSS3URI, ""),
|
||||
errExpected: true,
|
||||
uri: fmt.Sprintf(AWSS3URI, ""),
|
||||
wantErr: true,
|
||||
},
|
||||
"azure blob": {
|
||||
uri: fmt.Sprintf(AzureBlobURI, "", ""),
|
||||
errExpected: true,
|
||||
uri: fmt.Sprintf(AzureBlobURI, "", ""),
|
||||
wantErr: true,
|
||||
},
|
||||
"gcp storage": {
|
||||
uri: fmt.Sprintf(GCPStorageURI, "", ""),
|
||||
errExpected: true,
|
||||
uri: fmt.Sprintf(GCPStorageURI, "", ""),
|
||||
wantErr: true,
|
||||
},
|
||||
"unknown store": {
|
||||
uri: "storage://unknown",
|
||||
errExpected: true,
|
||||
uri: "storage://unknown",
|
||||
wantErr: true,
|
||||
},
|
||||
"invalid scheme": {
|
||||
uri: ClusterKMSURI,
|
||||
errExpected: true,
|
||||
uri: ClusterKMSURI,
|
||||
wantErr: true,
|
||||
},
|
||||
"not a url": {
|
||||
uri: ":/123",
|
||||
errExpected: true,
|
||||
uri: ":/123",
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -50,7 +50,7 @@ func TestGetStore(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
_, err := getStore(context.Background(), tc.uri)
|
||||
if tc.errExpected {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
} else {
|
||||
assert.NoError(err)
|
||||
@ -61,40 +61,40 @@ func TestGetStore(t *testing.T) {
|
||||
|
||||
func TestGetKMS(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
uri string
|
||||
errExpected bool
|
||||
uri string
|
||||
wantErr bool
|
||||
}{
|
||||
"cluster kms": {
|
||||
uri: ClusterKMSURI,
|
||||
errExpected: false,
|
||||
uri: ClusterKMSURI,
|
||||
wantErr: false,
|
||||
},
|
||||
"aws kms": {
|
||||
uri: fmt.Sprintf(AWSKMSURI, ""),
|
||||
errExpected: true,
|
||||
uri: fmt.Sprintf(AWSKMSURI, ""),
|
||||
wantErr: true,
|
||||
},
|
||||
"azure kms": {
|
||||
uri: fmt.Sprintf(AzureKMSURI, "", ""),
|
||||
errExpected: true,
|
||||
uri: fmt.Sprintf(AzureKMSURI, "", ""),
|
||||
wantErr: true,
|
||||
},
|
||||
"azure hsm": {
|
||||
uri: fmt.Sprintf(AzureHSMURI, ""),
|
||||
errExpected: true,
|
||||
uri: fmt.Sprintf(AzureHSMURI, ""),
|
||||
wantErr: true,
|
||||
},
|
||||
"gcp kms": {
|
||||
uri: fmt.Sprintf(GCPKMSURI, "", "", "", ""),
|
||||
errExpected: true,
|
||||
uri: fmt.Sprintf(GCPKMSURI, "", "", "", ""),
|
||||
wantErr: true,
|
||||
},
|
||||
"unknown kms": {
|
||||
uri: "kms://unknown",
|
||||
errExpected: true,
|
||||
uri: "kms://unknown",
|
||||
wantErr: true,
|
||||
},
|
||||
"invalid scheme": {
|
||||
uri: NoStoreURI,
|
||||
errExpected: true,
|
||||
uri: NoStoreURI,
|
||||
wantErr: true,
|
||||
},
|
||||
"not a url": {
|
||||
uri: ":/123",
|
||||
errExpected: true,
|
||||
uri: ":/123",
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -103,7 +103,7 @@ func TestGetKMS(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
kms, err := getKMS(context.Background(), tc.uri, nil)
|
||||
if tc.errExpected {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
} else {
|
||||
assert.NoError(err)
|
||||
@ -182,34 +182,34 @@ func TestGetConfig(t *testing.T) {
|
||||
const testUri = "test://config?name=test-name&data=test-data&value=test-value"
|
||||
|
||||
testCases := map[string]struct {
|
||||
uri string
|
||||
keys []string
|
||||
errExpected bool
|
||||
uri string
|
||||
keys []string
|
||||
wantErr bool
|
||||
}{
|
||||
"success": {
|
||||
uri: testUri,
|
||||
keys: []string{"name", "data", "value"},
|
||||
errExpected: false,
|
||||
uri: testUri,
|
||||
keys: []string{"name", "data", "value"},
|
||||
wantErr: false,
|
||||
},
|
||||
"less keys than capture groups": {
|
||||
uri: testUri,
|
||||
keys: []string{"name", "data"},
|
||||
errExpected: false,
|
||||
uri: testUri,
|
||||
keys: []string{"name", "data"},
|
||||
wantErr: false,
|
||||
},
|
||||
"invalid regex": {
|
||||
uri: testUri,
|
||||
keys: []string{"name", "data", "test-value"},
|
||||
errExpected: true,
|
||||
uri: testUri,
|
||||
keys: []string{"name", "data", "test-value"},
|
||||
wantErr: true,
|
||||
},
|
||||
"missing value": {
|
||||
uri: "test://config?name=test-name&data=test-data&value",
|
||||
keys: []string{"name", "data", "value"},
|
||||
errExpected: true,
|
||||
uri: "test://config?name=test-name&data=test-data&value",
|
||||
keys: []string{"name", "data", "value"},
|
||||
wantErr: true,
|
||||
},
|
||||
"more keys than expected": {
|
||||
uri: testUri,
|
||||
keys: []string{"name", "data", "value", "anotherValue"},
|
||||
errExpected: true,
|
||||
uri: testUri,
|
||||
keys: []string{"name", "data", "value", "anotherValue"},
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -222,7 +222,7 @@ func TestGetConfig(t *testing.T) {
|
||||
require.NoError(err)
|
||||
|
||||
res, err := getConfig(uri.Query(), tc.keys)
|
||||
if tc.errExpected {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
assert.Len(res, len(tc.keys))
|
||||
} else {
|
||||
|
@ -10,43 +10,43 @@ import (
|
||||
|
||||
func TestParseJoinCommand(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
joinCommand string
|
||||
expectedJoinArgs kubeadm.BootstrapTokenDiscovery
|
||||
expectErr bool
|
||||
joinCommand string
|
||||
wantJoinArgs kubeadm.BootstrapTokenDiscovery
|
||||
wantErr bool
|
||||
}{
|
||||
"join command can be parsed": {
|
||||
joinCommand: "kubeadm join 192.0.2.0:8443 --token dummy-token --discovery-token-ca-cert-hash sha512:dummy-hash --control-plane",
|
||||
expectedJoinArgs: kubeadm.BootstrapTokenDiscovery{
|
||||
wantJoinArgs: kubeadm.BootstrapTokenDiscovery{
|
||||
APIServerEndpoint: "192.0.2.0:8443",
|
||||
Token: "dummy-token",
|
||||
CACertHashes: []string{"sha512:dummy-hash"},
|
||||
},
|
||||
expectErr: false,
|
||||
wantErr: false,
|
||||
},
|
||||
"incorrect join command returns error": {
|
||||
joinCommand: "some string",
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"missing api server endpoint is checked": {
|
||||
joinCommand: "kubeadm join --token dummy-token --discovery-token-ca-cert-hash sha512:dummy-hash --control-plane",
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"missing token is checked": {
|
||||
joinCommand: "kubeadm join 192.0.2.0:8443 --discovery-token-ca-cert-hash sha512:dummy-hash --control-plane",
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"missing discovery-token-ca-cert-hash is checked": {
|
||||
joinCommand: "kubeadm join 192.0.2.0:8443 --token dummy-token --control-plane",
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"missing control-plane": {
|
||||
joinCommand: "kubeadm join 192.0.2.0:8443 --token dummy-token --discovery-token-ca-cert-hash sha512:dummy-hash",
|
||||
expectedJoinArgs: kubeadm.BootstrapTokenDiscovery{
|
||||
wantJoinArgs: kubeadm.BootstrapTokenDiscovery{
|
||||
APIServerEndpoint: "192.0.2.0:8443",
|
||||
Token: "dummy-token",
|
||||
CACertHashes: []string{"sha512:dummy-hash"},
|
||||
},
|
||||
expectErr: false,
|
||||
wantErr: false,
|
||||
},
|
||||
}
|
||||
|
||||
@ -57,13 +57,13 @@ func TestParseJoinCommand(t *testing.T) {
|
||||
|
||||
joinArgs, err := ParseJoinCommand(tc.joinCommand)
|
||||
|
||||
if tc.expectErr {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
return
|
||||
}
|
||||
require.NoError(err)
|
||||
|
||||
assert.Equal(&tc.expectedJoinArgs, joinArgs)
|
||||
assert.Equal(&tc.wantJoinArgs, joinArgs)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -176,25 +176,25 @@ func marshalYAML(obj runtime.Object) ([]byte, error) {
|
||||
func TestApplyOneObject(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
httpResponseData map[string]string
|
||||
expectedObj runtime.Object
|
||||
wantObj runtime.Object
|
||||
resourcesYAML string
|
||||
failingClient bool
|
||||
expectErr bool
|
||||
wantErr bool
|
||||
}{
|
||||
"apply works": {
|
||||
httpResponseData: map[string]string{
|
||||
"/deployments/my-nginx?fieldManager=constellation-coordinator&force=true": string(nginxDeplJSON),
|
||||
},
|
||||
expectedObj: nginxDeployment,
|
||||
wantObj: nginxDeployment,
|
||||
resourcesYAML: string(nginxDeplYAML),
|
||||
expectErr: false,
|
||||
wantErr: false,
|
||||
},
|
||||
"apply fails": {
|
||||
httpResponseData: map[string]string{},
|
||||
expectedObj: nginxDeployment,
|
||||
wantObj: nginxDeployment,
|
||||
resourcesYAML: string(nginxDeplYAML),
|
||||
failingClient: true,
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -205,9 +205,9 @@ func TestApplyOneObject(t *testing.T) {
|
||||
|
||||
var client Client
|
||||
if tc.failingClient {
|
||||
client = newFailingClient(tc.expectedObj)
|
||||
client = newFailingClient(tc.wantObj)
|
||||
} else {
|
||||
client = newClientWithFakes(t, tc.httpResponseData, tc.expectedObj)
|
||||
client = newClientWithFakes(t, tc.httpResponseData, tc.wantObj)
|
||||
}
|
||||
|
||||
reader := bytes.NewReader([]byte(tc.resourcesYAML))
|
||||
@ -223,7 +223,7 @@ func TestApplyOneObject(t *testing.T) {
|
||||
|
||||
err = client.ApplyOneObject(infos[0], true)
|
||||
|
||||
if tc.expectErr {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
return
|
||||
}
|
||||
@ -234,30 +234,30 @@ func TestApplyOneObject(t *testing.T) {
|
||||
|
||||
func TestGetObjects(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
expectedResources resources.Marshaler
|
||||
httpResponseData map[string]string
|
||||
resourcesYAML string
|
||||
expectErr bool
|
||||
wantResources resources.Marshaler
|
||||
httpResponseData map[string]string
|
||||
resourcesYAML string
|
||||
wantErr bool
|
||||
}{
|
||||
"GetObjects works on flannel deployment": {
|
||||
expectedResources: resources.NewDefaultFlannelDeployment(),
|
||||
resourcesYAML: string(nginxDeplYAML),
|
||||
expectErr: false,
|
||||
wantResources: resources.NewDefaultFlannelDeployment(),
|
||||
resourcesYAML: string(nginxDeplYAML),
|
||||
wantErr: false,
|
||||
},
|
||||
"GetObjects works on cluster-autoscaler deployment": {
|
||||
expectedResources: resources.NewDefaultFlannelDeployment(),
|
||||
resourcesYAML: string(nginxDeplYAML),
|
||||
expectErr: false,
|
||||
wantResources: resources.NewDefaultFlannelDeployment(),
|
||||
resourcesYAML: string(nginxDeplYAML),
|
||||
wantErr: false,
|
||||
},
|
||||
"GetObjects works on cloud-controller-manager deployment": {
|
||||
expectedResources: resources.NewDefaultCloudControllerManagerDeployment("someProvider", "someImage", "somePath", nil, nil, nil, nil),
|
||||
resourcesYAML: string(nginxDeplYAML),
|
||||
expectErr: false,
|
||||
wantResources: resources.NewDefaultCloudControllerManagerDeployment("someProvider", "someImage", "somePath", nil, nil, nil, nil),
|
||||
resourcesYAML: string(nginxDeplYAML),
|
||||
wantErr: false,
|
||||
},
|
||||
"GetObjects Marshal failure detected": {
|
||||
expectedResources: &unmarshableResource{},
|
||||
resourcesYAML: string(nginxDeplYAML),
|
||||
expectErr: true,
|
||||
wantResources: &unmarshableResource{},
|
||||
resourcesYAML: string(nginxDeplYAML),
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -267,9 +267,9 @@ func TestGetObjects(t *testing.T) {
|
||||
require := require.New(t)
|
||||
|
||||
client := newClientWithFakes(t, tc.httpResponseData)
|
||||
infos, err := client.GetObjects(tc.expectedResources)
|
||||
infos, err := client.GetObjects(tc.wantResources)
|
||||
|
||||
if tc.expectErr {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
return
|
||||
}
|
||||
|
@ -13,9 +13,9 @@ import (
|
||||
|
||||
func TestMarshalK8SResources(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
resources interface{}
|
||||
expectErr bool
|
||||
expectedYAML string
|
||||
resources interface{}
|
||||
wantErr bool
|
||||
wantYAML string
|
||||
}{
|
||||
"ConfigMap as only field can be marshaled": {
|
||||
resources: &struct {
|
||||
@ -31,7 +31,7 @@ func TestMarshalK8SResources(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
expectedYAML: `apiVersion: v1
|
||||
wantYAML: `apiVersion: v1
|
||||
data:
|
||||
key: value
|
||||
kind: ConfigMap
|
||||
@ -63,7 +63,7 @@ metadata:
|
||||
},
|
||||
},
|
||||
},
|
||||
expectedYAML: `apiVersion: v1
|
||||
wantYAML: `apiVersion: v1
|
||||
data:
|
||||
key: value
|
||||
kind: ConfigMap
|
||||
@ -80,11 +80,11 @@ metadata:
|
||||
},
|
||||
"Non-pointer is detected": {
|
||||
resources: "non-pointer",
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"Nil resource pointer is detected": {
|
||||
resources: nil,
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"Non-pointer field is ignored": {
|
||||
resources: &struct{ String string }{String: "somestring"},
|
||||
@ -105,23 +105,23 @@ metadata:
|
||||
|
||||
yaml, err := MarshalK8SResources(tc.resources)
|
||||
|
||||
if tc.expectErr {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
return
|
||||
}
|
||||
require.NoError(err)
|
||||
|
||||
assert.Equal(tc.expectedYAML, string(yaml))
|
||||
assert.Equal(tc.wantYAML, string(yaml))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestUnmarshalK8SResources(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
data string
|
||||
into interface{}
|
||||
expectedObj interface{}
|
||||
expectErr bool
|
||||
data string
|
||||
into interface{}
|
||||
wantObj interface{}
|
||||
wantErr bool
|
||||
}{
|
||||
"ConfigMap as only field can be unmarshaled": {
|
||||
data: `apiVersion: v1
|
||||
@ -134,7 +134,7 @@ metadata:
|
||||
into: &struct {
|
||||
ConfigMap k8s.ConfigMap
|
||||
}{},
|
||||
expectedObj: &struct {
|
||||
wantObj: &struct {
|
||||
ConfigMap k8s.ConfigMap
|
||||
}{
|
||||
ConfigMap: k8s.ConfigMap{
|
||||
@ -167,7 +167,7 @@ metadata:
|
||||
ConfigMap k8s.ConfigMap
|
||||
Secret k8s.Secret
|
||||
}{},
|
||||
expectedObj: &struct {
|
||||
wantObj: &struct {
|
||||
ConfigMap k8s.ConfigMap
|
||||
Secret k8s.Secret
|
||||
}{
|
||||
@ -209,15 +209,15 @@ metadata:
|
||||
into: &struct {
|
||||
ConfigMap k8s.ConfigMap
|
||||
}{},
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"Non-struct pointer is detected": {
|
||||
into: proto.String("test"),
|
||||
expectErr: true,
|
||||
into: proto.String("test"),
|
||||
wantErr: true,
|
||||
},
|
||||
"Nil into is detected": {
|
||||
into: nil,
|
||||
expectErr: true,
|
||||
into: nil,
|
||||
wantErr: true,
|
||||
},
|
||||
"Invalid yaml is detected": {
|
||||
data: `duplicateKey: value
|
||||
@ -225,7 +225,7 @@ metadata:
|
||||
into: &struct {
|
||||
ConfigMap k8s.ConfigMap
|
||||
}{},
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"Struct field cannot interface with runtime.Object": {
|
||||
data: `apiVersion: v1
|
||||
@ -238,7 +238,7 @@ metadata:
|
||||
into: &struct {
|
||||
String string
|
||||
}{},
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"Struct field mismatch": {
|
||||
data: `apiVersion: v1
|
||||
@ -251,7 +251,7 @@ metadata:
|
||||
into: &struct {
|
||||
Secret k8s.Secret
|
||||
}{},
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -262,22 +262,22 @@ metadata:
|
||||
|
||||
err := UnmarshalK8SResources([]byte(tc.data), tc.into)
|
||||
|
||||
if tc.expectErr {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
return
|
||||
}
|
||||
require.NoError(err)
|
||||
|
||||
assert.Equal(tc.expectedObj, tc.into)
|
||||
assert.Equal(tc.wantObj, tc.into)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestMarshalK8SResourcesList(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
resources []runtime.Object
|
||||
expectErr bool
|
||||
expectedYAML string
|
||||
resources []runtime.Object
|
||||
wantErr bool
|
||||
wantYAML string
|
||||
}{
|
||||
"ConfigMap as only element be marshaled": {
|
||||
resources: []runtime.Object{
|
||||
@ -291,7 +291,7 @@ func TestMarshalK8SResourcesList(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
expectedYAML: `apiVersion: v1
|
||||
wantYAML: `apiVersion: v1
|
||||
data:
|
||||
key: value
|
||||
kind: ConfigMap
|
||||
@ -320,7 +320,7 @@ metadata:
|
||||
},
|
||||
},
|
||||
},
|
||||
expectedYAML: `apiVersion: v1
|
||||
wantYAML: `apiVersion: v1
|
||||
data:
|
||||
key: value
|
||||
kind: ConfigMap
|
||||
@ -336,8 +336,8 @@ metadata:
|
||||
`,
|
||||
},
|
||||
"Nil resource pointer is encodes": {
|
||||
resources: []runtime.Object{nil},
|
||||
expectedYAML: "null\n",
|
||||
resources: []runtime.Object{nil},
|
||||
wantYAML: "null\n",
|
||||
},
|
||||
}
|
||||
|
||||
@ -348,13 +348,13 @@ metadata:
|
||||
|
||||
yaml, err := MarshalK8SResourcesList(tc.resources)
|
||||
|
||||
if tc.expectErr {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
return
|
||||
}
|
||||
require.NoError(err)
|
||||
|
||||
assert.Equal(tc.expectedYAML, string(yaml))
|
||||
assert.Equal(tc.wantYAML, string(yaml))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -131,7 +131,7 @@ func TestInitCluster(t *testing.T) {
|
||||
kubeconfigReader stubKubeconfigReader
|
||||
initConfig k8sapi.KubeadmInitYAML
|
||||
joinConfig k8sapi.KubeadmJoinYAML
|
||||
expectErr bool
|
||||
wantErr bool
|
||||
}{
|
||||
"kubeadm init works": {
|
||||
clusterUtil: stubClusterUtil{
|
||||
@ -144,7 +144,7 @@ func TestInitCluster(t *testing.T) {
|
||||
kubeconfigReader: stubKubeconfigReader{
|
||||
Kubeconfig: []byte("someKubeconfig"),
|
||||
},
|
||||
expectErr: false,
|
||||
wantErr: false,
|
||||
},
|
||||
"kubeadm init errors": {
|
||||
clusterUtil: stubClusterUtil{
|
||||
@ -154,7 +154,7 @@ func TestInitCluster(t *testing.T) {
|
||||
kubeconfigReader: stubKubeconfigReader{
|
||||
Kubeconfig: []byte("someKubeconfig"),
|
||||
},
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"pod network setup errors": {
|
||||
clusterUtil: stubClusterUtil{
|
||||
@ -164,7 +164,7 @@ func TestInitCluster(t *testing.T) {
|
||||
kubeconfigReader: stubKubeconfigReader{
|
||||
Kubeconfig: []byte("someKubeconfig"),
|
||||
},
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -193,7 +193,7 @@ func TestInitCluster(t *testing.T) {
|
||||
},
|
||||
)
|
||||
|
||||
if tc.expectErr {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
return
|
||||
}
|
||||
@ -223,15 +223,15 @@ func TestJoinCluster(t *testing.T) {
|
||||
|
||||
testCases := map[string]struct {
|
||||
clusterUtil stubClusterUtil
|
||||
expectErr bool
|
||||
wantErr bool
|
||||
}{
|
||||
"kubeadm join works": {
|
||||
clusterUtil: stubClusterUtil{},
|
||||
expectErr: false,
|
||||
wantErr: false,
|
||||
},
|
||||
"kubeadm join errors": {
|
||||
clusterUtil: stubClusterUtil{joinClusterErr: someErr},
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -242,7 +242,7 @@ func TestJoinCluster(t *testing.T) {
|
||||
|
||||
kube := New(&tc.clusterUtil, &stubConfigProvider{}, &client)
|
||||
err := kube.JoinCluster(joinCommand, instanceName, nodeVPNIP, nodeVPNIP, coordinatorProviderID, "", role.Node)
|
||||
if tc.expectErr {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
return
|
||||
}
|
||||
@ -262,7 +262,7 @@ func TestJoinCluster(t *testing.T) {
|
||||
func TestGetKubeconfig(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
Kubewrapper KubeWrapper
|
||||
expectErr bool
|
||||
wantErr bool
|
||||
}{
|
||||
"check single replacement": {
|
||||
Kubewrapper: KubeWrapper{kubeconfigReader: &stubKubeconfigReader{
|
||||
|
@ -13,13 +13,13 @@ import (
|
||||
|
||||
func TestFromFile(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
fileContents string
|
||||
expectedState *NodeState
|
||||
errExpected bool
|
||||
fileContents string
|
||||
wantState *NodeState
|
||||
wantErr bool
|
||||
}{
|
||||
"nodestate exists": {
|
||||
fileContents: `{ "Role": "Coordinator", "VPNPrivKey": "dGVzdA==", "OwnerID": "T3duZXJJRA==", "ClusterID": "Q2x1c3RlcklE" }`,
|
||||
expectedState: &NodeState{
|
||||
wantState: &NodeState{
|
||||
Role: role.Coordinator,
|
||||
VPNPrivKey: []byte("test"),
|
||||
OwnerID: []byte("OwnerID"),
|
||||
@ -27,7 +27,7 @@ func TestFromFile(t *testing.T) {
|
||||
},
|
||||
},
|
||||
"nodestate file does not exist": {
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -43,12 +43,12 @@ func TestFromFile(t *testing.T) {
|
||||
}
|
||||
fileHandler := file.NewHandler(fs)
|
||||
state, err := FromFile(fileHandler)
|
||||
if tc.errExpected {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
return
|
||||
}
|
||||
require.NoError(err)
|
||||
assert.Equal(tc.expectedState, state)
|
||||
assert.Equal(tc.wantState, state)
|
||||
})
|
||||
}
|
||||
}
|
||||
@ -57,8 +57,8 @@ func TestToFile(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
precreateFile bool
|
||||
state *NodeState
|
||||
expectedFile string
|
||||
errExpected bool
|
||||
wantFile string
|
||||
wantErr bool
|
||||
}{
|
||||
"writing works": {
|
||||
state: &NodeState{
|
||||
@ -67,7 +67,7 @@ func TestToFile(t *testing.T) {
|
||||
OwnerID: []byte("OwnerID"),
|
||||
ClusterID: []byte("ClusterID"),
|
||||
},
|
||||
expectedFile: `{
|
||||
wantFile: `{
|
||||
"Role": "Coordinator",
|
||||
"VPNPrivKey": "dGVzdA==",
|
||||
"OwnerID": "T3duZXJJRA==",
|
||||
@ -76,7 +76,7 @@ func TestToFile(t *testing.T) {
|
||||
},
|
||||
"file exists already": {
|
||||
precreateFile: true,
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -92,7 +92,7 @@ func TestToFile(t *testing.T) {
|
||||
}
|
||||
fileHandler := file.NewHandler(fs)
|
||||
err := tc.state.ToFile(fileHandler)
|
||||
if tc.errExpected {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
return
|
||||
}
|
||||
@ -100,7 +100,7 @@ func TestToFile(t *testing.T) {
|
||||
|
||||
fileContents, err := afero.ReadFile(fs, nodeStatePath)
|
||||
require.NoError(err)
|
||||
assert.Equal(tc.expectedFile, string(fileContents))
|
||||
assert.Equal(tc.wantFile, string(fileContents))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -34,78 +34,78 @@ func TestActivateAsCoordinator(t *testing.T) {
|
||||
testNode1 := newStubPeer("192.0.2.11", []byte{1, 2, 3})
|
||||
testNode2 := newStubPeer("192.0.2.12", []byte{2, 3, 4})
|
||||
testNode3 := newStubPeer("192.0.2.13", []byte{3, 4, 5})
|
||||
expectedNode1 := peer.Peer{PublicIP: "192.0.2.11", VPNIP: "10.118.0.11", VPNPubKey: []byte{1, 2, 3}, Role: role.Node}
|
||||
expectedNode2 := peer.Peer{PublicIP: "192.0.2.12", VPNIP: "10.118.0.12", VPNPubKey: []byte{2, 3, 4}, Role: role.Node}
|
||||
expectedNode3 := peer.Peer{PublicIP: "192.0.2.13", VPNIP: "10.118.0.13", VPNPubKey: []byte{3, 4, 5}, Role: role.Node}
|
||||
expectedCoord := peer.Peer{PublicIP: "192.0.2.1", VPNIP: "10.118.0.1", VPNPubKey: coordinatorPubKey, Role: role.Coordinator}
|
||||
wantNode1 := peer.Peer{PublicIP: "192.0.2.11", VPNIP: "10.118.0.11", VPNPubKey: []byte{1, 2, 3}, Role: role.Node}
|
||||
wantNode2 := peer.Peer{PublicIP: "192.0.2.12", VPNIP: "10.118.0.12", VPNPubKey: []byte{2, 3, 4}, Role: role.Node}
|
||||
wantNode3 := peer.Peer{PublicIP: "192.0.2.13", VPNIP: "10.118.0.13", VPNPubKey: []byte{3, 4, 5}, Role: role.Node}
|
||||
wantCoord := peer.Peer{PublicIP: "192.0.2.1", VPNIP: "10.118.0.1", VPNPubKey: coordinatorPubKey, Role: role.Coordinator}
|
||||
adminPeer := peer.Peer{VPNPubKey: []byte{7, 8, 9}, Role: role.Admin}
|
||||
|
||||
testCases := map[string]struct {
|
||||
nodes []*stubPeer
|
||||
state state.State
|
||||
switchToPersistentStoreErr error
|
||||
expectErr bool
|
||||
expectedPeers []peer.Peer
|
||||
expectedState state.State
|
||||
wantErr bool
|
||||
wantPeers []peer.Peer
|
||||
wantState state.State
|
||||
adminVPNIP string
|
||||
}{
|
||||
"0 nodes": {
|
||||
state: state.AcceptingInit,
|
||||
expectedPeers: []peer.Peer{expectedCoord},
|
||||
expectedState: state.ActivatingNodes,
|
||||
adminVPNIP: "10.118.0.11",
|
||||
state: state.AcceptingInit,
|
||||
wantPeers: []peer.Peer{wantCoord},
|
||||
wantState: state.ActivatingNodes,
|
||||
adminVPNIP: "10.118.0.11",
|
||||
},
|
||||
"1 node": {
|
||||
nodes: []*stubPeer{testNode1},
|
||||
state: state.AcceptingInit,
|
||||
expectedPeers: []peer.Peer{expectedCoord, expectedNode1},
|
||||
expectedState: state.ActivatingNodes,
|
||||
adminVPNIP: "10.118.0.12",
|
||||
nodes: []*stubPeer{testNode1},
|
||||
state: state.AcceptingInit,
|
||||
wantPeers: []peer.Peer{wantCoord, wantNode1},
|
||||
wantState: state.ActivatingNodes,
|
||||
adminVPNIP: "10.118.0.12",
|
||||
},
|
||||
"2 nodes": {
|
||||
nodes: []*stubPeer{testNode1, testNode2},
|
||||
state: state.AcceptingInit,
|
||||
expectedPeers: []peer.Peer{expectedCoord, expectedNode1, expectedNode2},
|
||||
expectedState: state.ActivatingNodes,
|
||||
adminVPNIP: "10.118.0.13",
|
||||
nodes: []*stubPeer{testNode1, testNode2},
|
||||
state: state.AcceptingInit,
|
||||
wantPeers: []peer.Peer{wantCoord, wantNode1, wantNode2},
|
||||
wantState: state.ActivatingNodes,
|
||||
adminVPNIP: "10.118.0.13",
|
||||
},
|
||||
"3 nodes": {
|
||||
nodes: []*stubPeer{testNode1, testNode2, testNode3},
|
||||
state: state.AcceptingInit,
|
||||
expectedPeers: []peer.Peer{expectedCoord, expectedNode1, expectedNode2, expectedNode3},
|
||||
expectedState: state.ActivatingNodes,
|
||||
adminVPNIP: "10.118.0.14",
|
||||
nodes: []*stubPeer{testNode1, testNode2, testNode3},
|
||||
state: state.AcceptingInit,
|
||||
wantPeers: []peer.Peer{wantCoord, wantNode1, wantNode2, wantNode3},
|
||||
wantState: state.ActivatingNodes,
|
||||
adminVPNIP: "10.118.0.14",
|
||||
},
|
||||
"already activated": {
|
||||
nodes: []*stubPeer{testNode1},
|
||||
state: state.ActivatingNodes,
|
||||
expectErr: true,
|
||||
expectedState: state.ActivatingNodes,
|
||||
nodes: []*stubPeer{testNode1},
|
||||
state: state.ActivatingNodes,
|
||||
wantErr: true,
|
||||
wantState: state.ActivatingNodes,
|
||||
},
|
||||
"wrong peer kind": {
|
||||
nodes: []*stubPeer{testNode1},
|
||||
state: state.IsNode,
|
||||
expectErr: true,
|
||||
expectedState: state.IsNode,
|
||||
nodes: []*stubPeer{testNode1},
|
||||
state: state.IsNode,
|
||||
wantErr: true,
|
||||
wantState: state.IsNode,
|
||||
},
|
||||
"node activation error": {
|
||||
nodes: []*stubPeer{testNode1, {activateErr: someErr}, testNode3},
|
||||
state: state.AcceptingInit,
|
||||
expectErr: true,
|
||||
expectedState: state.Failed,
|
||||
nodes: []*stubPeer{testNode1, {activateErr: someErr}, testNode3},
|
||||
state: state.AcceptingInit,
|
||||
wantErr: true,
|
||||
wantState: state.Failed,
|
||||
},
|
||||
"node join error": {
|
||||
nodes: []*stubPeer{testNode1, {joinErr: someErr}, testNode3},
|
||||
state: state.AcceptingInit,
|
||||
expectErr: true,
|
||||
expectedState: state.Failed,
|
||||
nodes: []*stubPeer{testNode1, {joinErr: someErr}, testNode3},
|
||||
state: state.AcceptingInit,
|
||||
wantErr: true,
|
||||
wantState: state.Failed,
|
||||
},
|
||||
"SwitchToPersistentStore error": {
|
||||
nodes: []*stubPeer{testNode1},
|
||||
state: state.AcceptingInit,
|
||||
switchToPersistentStoreErr: someErr,
|
||||
expectErr: true,
|
||||
expectedState: state.Failed,
|
||||
wantErr: true,
|
||||
wantState: state.Failed,
|
||||
},
|
||||
}
|
||||
|
||||
@ -162,9 +162,9 @@ func TestActivateAsCoordinator(t *testing.T) {
|
||||
StorageUri: kms.NoStoreURI,
|
||||
}, stream)
|
||||
|
||||
assert.Equal(tc.expectedState, core.state)
|
||||
assert.Equal(tc.wantState, core.state)
|
||||
|
||||
if tc.expectErr {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
return
|
||||
}
|
||||
@ -188,7 +188,7 @@ func TestActivateAsCoordinator(t *testing.T) {
|
||||
assert.Equal(vpnIP, core.vpnIP)
|
||||
// construct full list of expected peers
|
||||
adminPeer.VPNIP = tc.adminVPNIP
|
||||
assert.Equal(append(tc.expectedPeers, adminPeer), core.peers)
|
||||
assert.Equal(append(tc.wantPeers, adminPeer), core.peers)
|
||||
assert.Equal(autoscalingNodeGroups, core.autoscalingNodeGroups)
|
||||
assert.Equal(keyEncryptionKeyID, core.kekID)
|
||||
assert.Equal([]role.Role{role.Coordinator}, core.persistNodeStateRoles)
|
||||
@ -201,52 +201,52 @@ func TestActivateAdditionalNodes(t *testing.T) {
|
||||
testNode1 := newStubPeer("192.0.2.11", []byte{1, 2, 3})
|
||||
testNode2 := newStubPeer("192.0.2.12", []byte{2, 3, 4})
|
||||
testNode3 := newStubPeer("192.0.2.13", []byte{3, 4, 5})
|
||||
expectedNode1 := peer.Peer{PublicIP: "192.0.2.11", VPNIP: "10.118.0.11", VPNPubKey: []byte{1, 2, 3}, Role: role.Node}
|
||||
expectedNode2 := peer.Peer{PublicIP: "192.0.2.12", VPNIP: "10.118.0.12", VPNPubKey: []byte{2, 3, 4}, Role: role.Node}
|
||||
expectedNode3 := peer.Peer{PublicIP: "192.0.2.13", VPNIP: "10.118.0.13", VPNPubKey: []byte{3, 4, 5}, Role: role.Node}
|
||||
wantNode1 := peer.Peer{PublicIP: "192.0.2.11", VPNIP: "10.118.0.11", VPNPubKey: []byte{1, 2, 3}, Role: role.Node}
|
||||
wantNode2 := peer.Peer{PublicIP: "192.0.2.12", VPNIP: "10.118.0.12", VPNPubKey: []byte{2, 3, 4}, Role: role.Node}
|
||||
wantNode3 := peer.Peer{PublicIP: "192.0.2.13", VPNIP: "10.118.0.13", VPNPubKey: []byte{3, 4, 5}, Role: role.Node}
|
||||
|
||||
testCases := map[string]struct {
|
||||
nodes []*stubPeer
|
||||
state state.State
|
||||
expectErr bool
|
||||
expectedPeers []peer.Peer
|
||||
nodes []*stubPeer
|
||||
state state.State
|
||||
wantErr bool
|
||||
wantPeers []peer.Peer
|
||||
}{
|
||||
"0 nodes": {
|
||||
state: state.ActivatingNodes,
|
||||
},
|
||||
"1 node": {
|
||||
nodes: []*stubPeer{testNode1},
|
||||
state: state.ActivatingNodes,
|
||||
expectedPeers: []peer.Peer{expectedNode1},
|
||||
nodes: []*stubPeer{testNode1},
|
||||
state: state.ActivatingNodes,
|
||||
wantPeers: []peer.Peer{wantNode1},
|
||||
},
|
||||
"2 nodes": {
|
||||
nodes: []*stubPeer{testNode1, testNode2},
|
||||
state: state.ActivatingNodes,
|
||||
expectedPeers: []peer.Peer{expectedNode1, expectedNode2},
|
||||
nodes: []*stubPeer{testNode1, testNode2},
|
||||
state: state.ActivatingNodes,
|
||||
wantPeers: []peer.Peer{wantNode1, wantNode2},
|
||||
},
|
||||
"3 nodes": {
|
||||
nodes: []*stubPeer{testNode1, testNode2, testNode3},
|
||||
state: state.ActivatingNodes,
|
||||
expectedPeers: []peer.Peer{expectedNode1, expectedNode2, expectedNode3},
|
||||
nodes: []*stubPeer{testNode1, testNode2, testNode3},
|
||||
state: state.ActivatingNodes,
|
||||
wantPeers: []peer.Peer{wantNode1, wantNode2, wantNode3},
|
||||
},
|
||||
"uninitialized": {
|
||||
nodes: []*stubPeer{testNode1},
|
||||
expectErr: true,
|
||||
nodes: []*stubPeer{testNode1},
|
||||
wantErr: true,
|
||||
},
|
||||
"wrong peer kind": {
|
||||
nodes: []*stubPeer{testNode1},
|
||||
state: state.IsNode,
|
||||
expectErr: true,
|
||||
nodes: []*stubPeer{testNode1},
|
||||
state: state.IsNode,
|
||||
wantErr: true,
|
||||
},
|
||||
"node activation error": {
|
||||
nodes: []*stubPeer{testNode1, {activateErr: someErr}, testNode3},
|
||||
state: state.ActivatingNodes,
|
||||
expectErr: true,
|
||||
nodes: []*stubPeer{testNode1, {activateErr: someErr}, testNode3},
|
||||
state: state.ActivatingNodes,
|
||||
wantErr: true,
|
||||
},
|
||||
"node join error": {
|
||||
nodes: []*stubPeer{testNode1, {joinErr: someErr}, testNode3},
|
||||
state: state.ActivatingNodes,
|
||||
expectErr: true,
|
||||
nodes: []*stubPeer{testNode1, {joinErr: someErr}, testNode3},
|
||||
state: state.ActivatingNodes,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -283,7 +283,7 @@ func TestActivateAdditionalNodes(t *testing.T) {
|
||||
require.NoError(core.InitializeStoreIPs())
|
||||
stream := &stubActivateAdditionalNodesServer{}
|
||||
err := api.ActivateAdditionalNodes(&pubproto.ActivateAdditionalNodesRequest{NodePublicIps: nodePublicIPs}, stream)
|
||||
if tc.expectErr {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
return
|
||||
}
|
||||
@ -296,7 +296,7 @@ func TestActivateAdditionalNodes(t *testing.T) {
|
||||
}
|
||||
|
||||
// Core is updated
|
||||
assert.Equal(tc.expectedPeers, core.peers)
|
||||
assert.Equal(tc.wantPeers, core.peers)
|
||||
})
|
||||
}
|
||||
}
|
||||
@ -316,7 +316,7 @@ func TestAssemblePeerStruct(t *testing.T) {
|
||||
|
||||
vpnIP, err := core.GetVPNIP()
|
||||
require.NoError(err)
|
||||
expected := peer.Peer{
|
||||
want := peer.Peer{
|
||||
PublicIP: "192.0.2.1",
|
||||
VPNIP: vpnIP,
|
||||
VPNPubKey: vpnPubKey,
|
||||
@ -325,7 +325,7 @@ func TestAssemblePeerStruct(t *testing.T) {
|
||||
|
||||
actual, err := api.assemblePeerStruct(vpnIP, role.Coordinator)
|
||||
require.NoError(err)
|
||||
assert.Equal(expected, actual)
|
||||
assert.Equal(want, actual)
|
||||
}
|
||||
|
||||
type stubPeer struct {
|
||||
@ -452,28 +452,28 @@ func TestRequestStateDiskKey(t *testing.T) {
|
||||
dataKey []byte
|
||||
getDataKeyErr error
|
||||
pushKeyErr error
|
||||
errExpected bool
|
||||
wantErr bool
|
||||
}{
|
||||
"success": {
|
||||
state: state.ActivatingNodes,
|
||||
dataKey: defaultKey,
|
||||
},
|
||||
"Coordinator in wrong state": {
|
||||
state: state.IsNode,
|
||||
dataKey: defaultKey,
|
||||
errExpected: true,
|
||||
state: state.IsNode,
|
||||
dataKey: defaultKey,
|
||||
wantErr: true,
|
||||
},
|
||||
"GetDataKey fails": {
|
||||
state: state.ActivatingNodes,
|
||||
dataKey: defaultKey,
|
||||
getDataKeyErr: someErr,
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"key pushing fails": {
|
||||
state: state.ActivatingNodes,
|
||||
dataKey: defaultKey,
|
||||
pushKeyErr: someErr,
|
||||
errExpected: true,
|
||||
state: state.ActivatingNodes,
|
||||
dataKey: defaultKey,
|
||||
pushKeyErr: someErr,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -515,7 +515,7 @@ func TestRequestStateDiskKey(t *testing.T) {
|
||||
api := New(zaptest.NewLogger(t), core, &net.Dialer{}, nil, dummyValidator{}, nil, getPeerFromContext)
|
||||
|
||||
_, err = api.RequestStateDiskKey(ctx, &pubproto.RequestStateDiskKeyRequest{})
|
||||
if tc.errExpected {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
} else {
|
||||
assert.NoError(err)
|
||||
|
@ -30,39 +30,39 @@ func TestActivateAsAdditionalCoordinator(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
coordinators stubPeer
|
||||
state state.State
|
||||
expectedState state.State
|
||||
wantState state.State
|
||||
vpnapi stubVPNAPI
|
||||
expectErr bool
|
||||
wantErr bool
|
||||
switchToPersistentStoreErr error
|
||||
k8sJoinargsErr error
|
||||
k8sCertKeyErr error
|
||||
}{
|
||||
"basic": {
|
||||
coordinators: testCoord1,
|
||||
state: state.AcceptingInit,
|
||||
expectedState: state.ActivatingNodes,
|
||||
vpnapi: stubVPN,
|
||||
coordinators: testCoord1,
|
||||
state: state.AcceptingInit,
|
||||
wantState: state.ActivatingNodes,
|
||||
vpnapi: stubVPN,
|
||||
},
|
||||
"already activated": {
|
||||
state: state.ActivatingNodes,
|
||||
expectErr: true,
|
||||
expectedState: state.ActivatingNodes,
|
||||
vpnapi: stubVPN,
|
||||
state: state.ActivatingNodes,
|
||||
wantErr: true,
|
||||
wantState: state.ActivatingNodes,
|
||||
vpnapi: stubVPN,
|
||||
},
|
||||
"SwitchToPersistentStore error": {
|
||||
coordinators: testCoord1,
|
||||
state: state.AcceptingInit,
|
||||
switchToPersistentStoreErr: someErr,
|
||||
expectErr: true,
|
||||
expectedState: state.Failed,
|
||||
wantErr: true,
|
||||
wantState: state.Failed,
|
||||
vpnapi: stubVPN,
|
||||
},
|
||||
"GetK8SJoinArgs error": {
|
||||
coordinators: testCoord1,
|
||||
state: state.AcceptingInit,
|
||||
switchToPersistentStoreErr: someErr,
|
||||
expectErr: true,
|
||||
expectedState: state.Failed,
|
||||
wantErr: true,
|
||||
wantState: state.Failed,
|
||||
vpnapi: stubVPN,
|
||||
k8sJoinargsErr: someErr,
|
||||
},
|
||||
@ -70,8 +70,8 @@ func TestActivateAsAdditionalCoordinator(t *testing.T) {
|
||||
coordinators: testCoord1,
|
||||
state: state.AcceptingInit,
|
||||
switchToPersistentStoreErr: someErr,
|
||||
expectErr: true,
|
||||
expectedState: state.Failed,
|
||||
wantErr: true,
|
||||
wantState: state.Failed,
|
||||
vpnapi: stubVPN,
|
||||
k8sCertKeyErr: someErr,
|
||||
},
|
||||
@ -113,9 +113,9 @@ func TestActivateAsAdditionalCoordinator(t *testing.T) {
|
||||
ClusterId: core.clusterID,
|
||||
})
|
||||
|
||||
assert.Equal(tc.expectedState, core.state)
|
||||
assert.Equal(tc.wantState, core.state)
|
||||
|
||||
if tc.expectErr {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
return
|
||||
}
|
||||
@ -135,21 +135,21 @@ func TestTriggerCoordinatorUpdate(t *testing.T) {
|
||||
peers []peer.Peer
|
||||
state state.State
|
||||
getUpdateErr error
|
||||
expectErr bool
|
||||
wantErr bool
|
||||
}{
|
||||
"basic": {
|
||||
peers: peers,
|
||||
state: state.ActivatingNodes,
|
||||
},
|
||||
"not activated": {
|
||||
peers: peers,
|
||||
state: state.AcceptingInit,
|
||||
expectErr: true,
|
||||
peers: peers,
|
||||
state: state.AcceptingInit,
|
||||
wantErr: true,
|
||||
},
|
||||
"wrong peer kind": {
|
||||
peers: peers,
|
||||
state: state.IsNode,
|
||||
expectErr: true,
|
||||
peers: peers,
|
||||
state: state.IsNode,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -168,7 +168,7 @@ func TestTriggerCoordinatorUpdate(t *testing.T) {
|
||||
api := New(logger, core, dialer, nil, nil, nil, nil)
|
||||
|
||||
_, err := api.TriggerCoordinatorUpdate(context.Background(), &pubproto.TriggerCoordinatorUpdateRequest{})
|
||||
if tc.expectErr {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
return
|
||||
}
|
||||
@ -194,33 +194,33 @@ func TestActivateAdditionalCoordinators(t *testing.T) {
|
||||
state state.State
|
||||
activateErr error
|
||||
getPublicKeyErr error
|
||||
expectErr bool
|
||||
expectedState state.State
|
||||
wantErr bool
|
||||
wantState state.State
|
||||
}{
|
||||
"basic": {
|
||||
coordinators: testCoord1,
|
||||
state: state.ActivatingNodes,
|
||||
expectedState: state.ActivatingNodes,
|
||||
coordinators: testCoord1,
|
||||
state: state.ActivatingNodes,
|
||||
wantState: state.ActivatingNodes,
|
||||
},
|
||||
"Activation Err": {
|
||||
coordinators: testCoord1,
|
||||
state: state.ActivatingNodes,
|
||||
expectedState: state.ActivatingNodes,
|
||||
activateErr: someErr,
|
||||
expectErr: true,
|
||||
coordinators: testCoord1,
|
||||
state: state.ActivatingNodes,
|
||||
wantState: state.ActivatingNodes,
|
||||
activateErr: someErr,
|
||||
wantErr: true,
|
||||
},
|
||||
"Not in exprected state": {
|
||||
coordinators: testCoord1,
|
||||
state: state.AcceptingInit,
|
||||
expectedState: state.AcceptingInit,
|
||||
expectErr: true,
|
||||
coordinators: testCoord1,
|
||||
state: state.AcceptingInit,
|
||||
wantState: state.AcceptingInit,
|
||||
wantErr: true,
|
||||
},
|
||||
"getPeerPublicKey error": {
|
||||
coordinators: testCoord1,
|
||||
state: state.ActivatingNodes,
|
||||
expectedState: state.ActivatingNodes,
|
||||
wantState: state.ActivatingNodes,
|
||||
getPublicKeyErr: someErr,
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -254,9 +254,9 @@ func TestActivateAdditionalCoordinators(t *testing.T) {
|
||||
|
||||
_, err := api.ActivateAdditionalCoordinator(context.Background(), &pubproto.ActivateAdditionalCoordinatorRequest{CoordinatorPublicIp: tc.coordinators.peer.PublicIP})
|
||||
|
||||
assert.Equal(tc.expectedState, core.state)
|
||||
assert.Equal(tc.wantState, core.state)
|
||||
|
||||
if tc.expectErr {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
return
|
||||
}
|
||||
@ -272,7 +272,7 @@ func TestGetPeerVPNPublicKey(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
coordinator stubPeer
|
||||
getVPNPubKeyErr error
|
||||
expectErr bool
|
||||
wantErr bool
|
||||
}{
|
||||
"basic": {
|
||||
coordinator: testCoord,
|
||||
@ -280,7 +280,7 @@ func TestGetPeerVPNPublicKey(t *testing.T) {
|
||||
"Activation Err": {
|
||||
coordinator: testCoord,
|
||||
getVPNPubKeyErr: someErr,
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -304,7 +304,7 @@ func TestGetPeerVPNPublicKey(t *testing.T) {
|
||||
|
||||
resp, err := api.GetPeerVPNPublicKey(context.Background(), &pubproto.GetPeerVPNPublicKeyRequest{})
|
||||
|
||||
if tc.expectErr {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
return
|
||||
}
|
||||
|
@ -35,75 +35,75 @@ func TestActivateAsNode(t *testing.T) {
|
||||
getUpdateErr error
|
||||
setVPNIPErr error
|
||||
messageSequenceOverride []string
|
||||
expectErr bool
|
||||
expectedState state.State
|
||||
wantErr bool
|
||||
wantState state.State
|
||||
}{
|
||||
"basic": {
|
||||
initialPeers: []peer.Peer{peer1},
|
||||
updatedPeers: []peer.Peer{peer2},
|
||||
state: state.AcceptingInit,
|
||||
expectedState: state.NodeWaitingForClusterJoin,
|
||||
initialPeers: []peer.Peer{peer1},
|
||||
updatedPeers: []peer.Peer{peer2},
|
||||
state: state.AcceptingInit,
|
||||
wantState: state.NodeWaitingForClusterJoin,
|
||||
},
|
||||
"already activated": {
|
||||
initialPeers: []peer.Peer{peer1},
|
||||
updatedPeers: []peer.Peer{peer2},
|
||||
state: state.IsNode,
|
||||
expectErr: true,
|
||||
expectedState: state.IsNode,
|
||||
initialPeers: []peer.Peer{peer1},
|
||||
updatedPeers: []peer.Peer{peer2},
|
||||
state: state.IsNode,
|
||||
wantErr: true,
|
||||
wantState: state.IsNode,
|
||||
},
|
||||
"wrong peer kind": {
|
||||
initialPeers: []peer.Peer{peer1},
|
||||
updatedPeers: []peer.Peer{peer2},
|
||||
state: state.ActivatingNodes,
|
||||
expectErr: true,
|
||||
expectedState: state.ActivatingNodes,
|
||||
initialPeers: []peer.Peer{peer1},
|
||||
updatedPeers: []peer.Peer{peer2},
|
||||
state: state.ActivatingNodes,
|
||||
wantErr: true,
|
||||
wantState: state.ActivatingNodes,
|
||||
},
|
||||
"GetUpdate error": {
|
||||
initialPeers: []peer.Peer{peer1},
|
||||
updatedPeers: []peer.Peer{peer2},
|
||||
state: state.AcceptingInit,
|
||||
getUpdateErr: someErr,
|
||||
expectedState: state.NodeWaitingForClusterJoin,
|
||||
initialPeers: []peer.Peer{peer1},
|
||||
updatedPeers: []peer.Peer{peer2},
|
||||
state: state.AcceptingInit,
|
||||
getUpdateErr: someErr,
|
||||
wantState: state.NodeWaitingForClusterJoin,
|
||||
},
|
||||
"SetVPNIP error": {
|
||||
initialPeers: []peer.Peer{peer1},
|
||||
updatedPeers: []peer.Peer{peer2},
|
||||
state: state.AcceptingInit,
|
||||
setVPNIPErr: someErr,
|
||||
expectErr: true,
|
||||
expectedState: state.Failed,
|
||||
initialPeers: []peer.Peer{peer1},
|
||||
updatedPeers: []peer.Peer{peer2},
|
||||
state: state.AcceptingInit,
|
||||
setVPNIPErr: someErr,
|
||||
wantErr: true,
|
||||
wantState: state.Failed,
|
||||
},
|
||||
"no messages sent to node": {
|
||||
initialPeers: []peer.Peer{peer1},
|
||||
updatedPeers: []peer.Peer{peer2},
|
||||
state: state.AcceptingInit,
|
||||
messageSequenceOverride: []string{},
|
||||
expectErr: true,
|
||||
expectedState: state.AcceptingInit,
|
||||
wantErr: true,
|
||||
wantState: state.AcceptingInit,
|
||||
},
|
||||
"only initial message sent to node": {
|
||||
initialPeers: []peer.Peer{peer1},
|
||||
updatedPeers: []peer.Peer{peer2},
|
||||
state: state.AcceptingInit,
|
||||
messageSequenceOverride: []string{"initialRequest"},
|
||||
expectErr: true,
|
||||
expectedState: state.Failed,
|
||||
wantErr: true,
|
||||
wantState: state.Failed,
|
||||
},
|
||||
"wrong initial message sent to node": {
|
||||
initialPeers: []peer.Peer{peer1},
|
||||
updatedPeers: []peer.Peer{peer2},
|
||||
state: state.AcceptingInit,
|
||||
messageSequenceOverride: []string{"stateDiskKey"},
|
||||
expectErr: true,
|
||||
expectedState: state.AcceptingInit,
|
||||
wantErr: true,
|
||||
wantState: state.AcceptingInit,
|
||||
},
|
||||
"initial message sent twice to node": {
|
||||
initialPeers: []peer.Peer{peer1},
|
||||
updatedPeers: []peer.Peer{peer2},
|
||||
state: state.AcceptingInit,
|
||||
messageSequenceOverride: []string{"initialRequest", "initialRequest"},
|
||||
expectErr: true,
|
||||
expectedState: state.Failed,
|
||||
wantErr: true,
|
||||
wantState: state.Failed,
|
||||
},
|
||||
}
|
||||
|
||||
@ -146,9 +146,9 @@ func TestActivateAsNode(t *testing.T) {
|
||||
defer pubserver.GracefulStop()
|
||||
|
||||
_, nodeVPNPubKey, err := activateNode(require, dialer, messageSequence, nodeIP, "9000", nodeVPNIP, peer.ToPubProto(tc.initialPeers), ownerID, clusterID, stateDiskKey)
|
||||
assert.Equal(tc.expectedState, cor.state)
|
||||
assert.Equal(tc.wantState, cor.state)
|
||||
|
||||
if tc.expectErr {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
return
|
||||
}
|
||||
@ -184,27 +184,27 @@ func TestTriggerNodeUpdate(t *testing.T) {
|
||||
peers []peer.Peer
|
||||
state state.State
|
||||
getUpdateErr error
|
||||
expectErr bool
|
||||
wantErr bool
|
||||
}{
|
||||
"basic": {
|
||||
peers: peers,
|
||||
state: state.IsNode,
|
||||
},
|
||||
"not activated": {
|
||||
peers: peers,
|
||||
state: state.AcceptingInit,
|
||||
expectErr: true,
|
||||
peers: peers,
|
||||
state: state.AcceptingInit,
|
||||
wantErr: true,
|
||||
},
|
||||
"wrong peer kind": {
|
||||
peers: peers,
|
||||
state: state.ActivatingNodes,
|
||||
expectErr: true,
|
||||
peers: peers,
|
||||
state: state.ActivatingNodes,
|
||||
wantErr: true,
|
||||
},
|
||||
"GetUpdate error": {
|
||||
peers: peers,
|
||||
state: state.IsNode,
|
||||
getUpdateErr: someErr,
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -229,7 +229,7 @@ func TestTriggerNodeUpdate(t *testing.T) {
|
||||
defer vserver.GracefulStop()
|
||||
|
||||
_, err := api.TriggerNodeUpdate(context.Background(), &pubproto.TriggerNodeUpdateRequest{})
|
||||
if tc.expectErr {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
return
|
||||
}
|
||||
@ -252,34 +252,34 @@ func TestJoinCluster(t *testing.T) {
|
||||
state state.State
|
||||
getJoinArgsErr error
|
||||
joinClusterErr error
|
||||
expectErr bool
|
||||
expectedState state.State
|
||||
wantErr bool
|
||||
wantState state.State
|
||||
}{
|
||||
"basic": {
|
||||
state: state.NodeWaitingForClusterJoin,
|
||||
expectedState: state.IsNode,
|
||||
state: state.NodeWaitingForClusterJoin,
|
||||
wantState: state.IsNode,
|
||||
},
|
||||
"not activated": {
|
||||
state: state.AcceptingInit,
|
||||
expectErr: true,
|
||||
expectedState: state.AcceptingInit,
|
||||
state: state.AcceptingInit,
|
||||
wantErr: true,
|
||||
wantState: state.AcceptingInit,
|
||||
},
|
||||
"wrong peer kind": {
|
||||
state: state.ActivatingNodes,
|
||||
expectErr: true,
|
||||
expectedState: state.ActivatingNodes,
|
||||
state: state.ActivatingNodes,
|
||||
wantErr: true,
|
||||
wantState: state.ActivatingNodes,
|
||||
},
|
||||
"GetK8sJoinArgs error": {
|
||||
state: state.NodeWaitingForClusterJoin,
|
||||
getJoinArgsErr: someErr,
|
||||
expectErr: true,
|
||||
expectedState: state.NodeWaitingForClusterJoin,
|
||||
wantErr: true,
|
||||
wantState: state.NodeWaitingForClusterJoin,
|
||||
},
|
||||
"JoinCluster error": {
|
||||
state: state.NodeWaitingForClusterJoin,
|
||||
joinClusterErr: someErr,
|
||||
expectErr: true,
|
||||
expectedState: state.Failed,
|
||||
wantErr: true,
|
||||
wantState: state.Failed,
|
||||
},
|
||||
}
|
||||
|
||||
@ -309,9 +309,9 @@ func TestJoinCluster(t *testing.T) {
|
||||
|
||||
_, err := api.JoinCluster(context.Background(), &pubproto.JoinClusterRequest{CoordinatorVpnIp: "192.0.2.1"})
|
||||
|
||||
assert.Equal(tc.expectedState, core.state)
|
||||
assert.Equal(tc.wantState, core.state)
|
||||
|
||||
if tc.expectErr {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
return
|
||||
}
|
||||
|
@ -23,14 +23,14 @@ func TestGetRecoveryPeerFromContext(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
testIP := "192.0.2.1"
|
||||
testPort := 1234
|
||||
expectedPeer := net.JoinHostPort(testIP, "9000")
|
||||
wantPeer := net.JoinHostPort(testIP, "9000")
|
||||
|
||||
addr := &net.TCPAddr{IP: net.ParseIP(testIP), Port: testPort}
|
||||
ctx := grpcpeer.NewContext(context.Background(), &grpcpeer.Peer{Addr: addr})
|
||||
|
||||
peer, err := GetRecoveryPeerFromContext(ctx)
|
||||
assert.NoError(err)
|
||||
assert.Equal(expectedPeer, peer)
|
||||
assert.Equal(wantPeer, peer)
|
||||
|
||||
_, err = GetRecoveryPeerFromContext(context.Background())
|
||||
assert.Error(err)
|
||||
|
@ -9,25 +9,25 @@ import (
|
||||
|
||||
func TestMarshal(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
role Role
|
||||
jsonExpected string
|
||||
errExpected bool
|
||||
role Role
|
||||
wantJson string
|
||||
wantErr bool
|
||||
}{
|
||||
"coordinator role": {
|
||||
role: Coordinator,
|
||||
jsonExpected: `"Coordinator"`,
|
||||
role: Coordinator,
|
||||
wantJson: `"Coordinator"`,
|
||||
},
|
||||
"node role": {
|
||||
role: Node,
|
||||
jsonExpected: `"Node"`,
|
||||
role: Node,
|
||||
wantJson: `"Node"`,
|
||||
},
|
||||
"admin role": {
|
||||
role: Admin,
|
||||
jsonExpected: `"Admin"`,
|
||||
role: Admin,
|
||||
wantJson: `"Admin"`,
|
||||
},
|
||||
"unknown role": {
|
||||
role: Unknown,
|
||||
jsonExpected: `"Unknown"`,
|
||||
role: Unknown,
|
||||
wantJson: `"Unknown"`,
|
||||
},
|
||||
}
|
||||
|
||||
@ -37,54 +37,54 @@ func TestMarshal(t *testing.T) {
|
||||
require := require.New(t)
|
||||
|
||||
jsonRole, err := tc.role.MarshalJSON()
|
||||
if tc.errExpected {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
require.NoError(err)
|
||||
assert.Equal(tc.jsonExpected, string(jsonRole))
|
||||
assert.Equal(tc.wantJson, string(jsonRole))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestUnmarshal(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
json string
|
||||
expectedRole Role
|
||||
errExpected bool
|
||||
json string
|
||||
wantRole Role
|
||||
wantErr bool
|
||||
}{
|
||||
"Coordinator can be unmarshaled": {
|
||||
json: `"Coordinator"`,
|
||||
expectedRole: Coordinator,
|
||||
json: `"Coordinator"`,
|
||||
wantRole: Coordinator,
|
||||
},
|
||||
"lowercase coordinator can be unmarshaled": {
|
||||
json: `"coordinator"`,
|
||||
expectedRole: Coordinator,
|
||||
json: `"coordinator"`,
|
||||
wantRole: Coordinator,
|
||||
},
|
||||
"Node can be unmarshaled": {
|
||||
json: `"Node"`,
|
||||
expectedRole: Node,
|
||||
json: `"Node"`,
|
||||
wantRole: Node,
|
||||
},
|
||||
"lowercase node can be unmarshaled": {
|
||||
json: `"node"`,
|
||||
expectedRole: Node,
|
||||
json: `"node"`,
|
||||
wantRole: Node,
|
||||
},
|
||||
"Admin can be unmarshaled": {
|
||||
json: `"Admin"`,
|
||||
expectedRole: Admin,
|
||||
json: `"Admin"`,
|
||||
wantRole: Admin,
|
||||
},
|
||||
"lowercase admin can be unmarshaled": {
|
||||
json: `"admin"`,
|
||||
expectedRole: Admin,
|
||||
json: `"admin"`,
|
||||
wantRole: Admin,
|
||||
},
|
||||
"other strings unmarshal to the unknown role": {
|
||||
json: `"anything"`,
|
||||
expectedRole: Unknown,
|
||||
json: `"anything"`,
|
||||
wantRole: Unknown,
|
||||
},
|
||||
"invalid json fails": {
|
||||
json: `"unterminated string literal`,
|
||||
errExpected: true,
|
||||
json: `"unterminated string literal`,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -96,13 +96,13 @@ func TestUnmarshal(t *testing.T) {
|
||||
var role Role
|
||||
err := role.UnmarshalJSON([]byte(tc.json))
|
||||
|
||||
if tc.errExpected {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
require.NoError(err)
|
||||
assert.Equal(tc.expectedRole, role)
|
||||
assert.Equal(tc.wantRole, role)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -34,7 +34,7 @@ func TestGetUpdate(t *testing.T) {
|
||||
clientAddr net.Addr
|
||||
peers []peer.Peer
|
||||
getPeersErr error
|
||||
expectErr bool
|
||||
wantErr bool
|
||||
}{
|
||||
"0 peers": {
|
||||
clientAddr: clientIP,
|
||||
@ -59,7 +59,7 @@ func TestGetUpdate(t *testing.T) {
|
||||
"getPeers error": {
|
||||
clientAddr: clientIP,
|
||||
getPeersErr: someErr,
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"missing client addr": {
|
||||
peers: []peer.Peer{peer1},
|
||||
@ -83,7 +83,7 @@ func TestGetUpdate(t *testing.T) {
|
||||
}
|
||||
|
||||
resp, err := api.GetUpdate(ctx, &vpnproto.GetUpdateRequest{ResourceVersion: clientResourceVersion})
|
||||
if tc.expectErr {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
return
|
||||
}
|
||||
@ -94,10 +94,10 @@ func TestGetUpdate(t *testing.T) {
|
||||
|
||||
require.Len(resp.Peers, len(tc.peers))
|
||||
for i, actual := range resp.Peers {
|
||||
expected := tc.peers[i]
|
||||
assert.EqualValues(expected.PublicIP, actual.PublicIp)
|
||||
assert.EqualValues(expected.VPNIP, actual.VpnIp)
|
||||
assert.Equal(expected.VPNPubKey, actual.VpnPubKey)
|
||||
want := tc.peers[i]
|
||||
assert.EqualValues(want.PublicIP, actual.PublicIp)
|
||||
assert.EqualValues(want.VPNIP, actual.VpnIp)
|
||||
assert.Equal(want.VPNPubKey, actual.VpnPubKey)
|
||||
}
|
||||
|
||||
if tc.clientAddr == nil {
|
||||
@ -150,14 +150,14 @@ func TestGetK8SCertificateKey(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
certKey string
|
||||
getCertKeyErr error
|
||||
expectErr bool
|
||||
wantErr bool
|
||||
}{
|
||||
"basic": {
|
||||
certKey: certKey,
|
||||
},
|
||||
"error": {
|
||||
getCertKeyErr: someErr,
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
for name, tc := range testCases {
|
||||
@ -173,7 +173,7 @@ func TestGetK8SCertificateKey(t *testing.T) {
|
||||
api := New(zaptest.NewLogger(t), core)
|
||||
resp, err := api.GetK8SCertificateKey(context.Background(), &vpnproto.GetK8SCertificateKeyRequest{})
|
||||
|
||||
if tc.expectErr {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
return
|
||||
}
|
||||
|
@ -36,35 +36,35 @@ func TestUpdatePeer(t *testing.T) {
|
||||
}
|
||||
|
||||
testCases := map[string]struct {
|
||||
storePeers []peer.Peer
|
||||
vpnPeers []wgtypes.Peer
|
||||
excludedIP map[string]struct{}
|
||||
expectErr bool
|
||||
expectedVPNPeers []wgtypes.Peer
|
||||
storePeers []peer.Peer
|
||||
vpnPeers []wgtypes.Peer
|
||||
excludedIP map[string]struct{}
|
||||
wantErr bool
|
||||
wantVPNPeers []wgtypes.Peer
|
||||
}{
|
||||
"basic": {
|
||||
storePeers: []peer.Peer{peer1, peer3},
|
||||
vpnPeers: checkError(transformToWgpeer([]peer.Peer{peer1, peer2})),
|
||||
expectedVPNPeers: checkError(transformToWgpeer([]peer.Peer{peer1, peer3})),
|
||||
storePeers: []peer.Peer{peer1, peer3},
|
||||
vpnPeers: checkError(transformToWgpeer([]peer.Peer{peer1, peer2})),
|
||||
wantVPNPeers: checkError(transformToWgpeer([]peer.Peer{peer1, peer3})),
|
||||
},
|
||||
"previously empty": {
|
||||
storePeers: []peer.Peer{peer1, peer2},
|
||||
expectedVPNPeers: checkError(transformToWgpeer([]peer.Peer{peer1, peer2})),
|
||||
storePeers: []peer.Peer{peer1, peer2},
|
||||
wantVPNPeers: checkError(transformToWgpeer([]peer.Peer{peer1, peer2})),
|
||||
},
|
||||
"no changes": {
|
||||
storePeers: []peer.Peer{peer1, peer2},
|
||||
vpnPeers: checkError(transformToWgpeer([]peer.Peer{peer1, peer2})),
|
||||
expectedVPNPeers: checkError(transformToWgpeer([]peer.Peer{peer1, peer2})),
|
||||
storePeers: []peer.Peer{peer1, peer2},
|
||||
vpnPeers: checkError(transformToWgpeer([]peer.Peer{peer1, peer2})),
|
||||
wantVPNPeers: checkError(transformToWgpeer([]peer.Peer{peer1, peer2})),
|
||||
},
|
||||
"key update": {
|
||||
storePeers: []peer.Peer{peer1KeyUpd, peer3},
|
||||
vpnPeers: checkError(transformToWgpeer([]peer.Peer{peer1, peer2})),
|
||||
expectedVPNPeers: checkError(transformToWgpeer([]peer.Peer{peer1KeyUpd, peer3})),
|
||||
storePeers: []peer.Peer{peer1KeyUpd, peer3},
|
||||
vpnPeers: checkError(transformToWgpeer([]peer.Peer{peer1, peer2})),
|
||||
wantVPNPeers: checkError(transformToWgpeer([]peer.Peer{peer1KeyUpd, peer3})),
|
||||
},
|
||||
"not update Endpoint changes": {
|
||||
storePeers: []peer.Peer{peerAdminNoEndp, peer3},
|
||||
vpnPeers: checkError(transformToWgpeer([]peer.Peer{peerAdmin, peer3})),
|
||||
expectedVPNPeers: checkError(transformToWgpeer([]peer.Peer{peerAdmin, peer3})),
|
||||
storePeers: []peer.Peer{peerAdminNoEndp, peer3},
|
||||
vpnPeers: checkError(transformToWgpeer([]peer.Peer{peerAdmin, peer3})),
|
||||
wantVPNPeers: checkError(transformToWgpeer([]peer.Peer{peerAdmin, peer3})),
|
||||
},
|
||||
}
|
||||
|
||||
@ -81,13 +81,13 @@ func TestUpdatePeer(t *testing.T) {
|
||||
|
||||
updateErr := wg.UpdatePeers(tc.storePeers)
|
||||
|
||||
if tc.expectErr {
|
||||
if tc.wantErr {
|
||||
assert.Error(updateErr)
|
||||
return
|
||||
}
|
||||
require.NoError(updateErr)
|
||||
|
||||
assert.ElementsMatch(tc.expectedVPNPeers, fakewg.devices[netInterface].Peers)
|
||||
assert.ElementsMatch(tc.wantVPNPeers, fakewg.devices[netInterface].Peers)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -18,8 +18,8 @@ func TestWriteStream(t *testing.T) {
|
||||
readChunkStream fakeReadChunkStream
|
||||
fs afero.Fs
|
||||
showProgress bool
|
||||
expectedFile []byte
|
||||
expectErr bool
|
||||
wantFile []byte
|
||||
wantErr bool
|
||||
}{
|
||||
"stream works": {
|
||||
readChunkStream: fakeReadChunkStream{
|
||||
@ -27,9 +27,9 @@ func TestWriteStream(t *testing.T) {
|
||||
[]byte("test"),
|
||||
},
|
||||
},
|
||||
fs: afero.NewMemMapFs(),
|
||||
expectedFile: []byte("test"),
|
||||
expectErr: false,
|
||||
fs: afero.NewMemMapFs(),
|
||||
wantFile: []byte("test"),
|
||||
wantErr: false,
|
||||
},
|
||||
"chunking works": {
|
||||
readChunkStream: fakeReadChunkStream{
|
||||
@ -38,9 +38,9 @@ func TestWriteStream(t *testing.T) {
|
||||
[]byte("st"),
|
||||
},
|
||||
},
|
||||
fs: afero.NewMemMapFs(),
|
||||
expectedFile: []byte("test"),
|
||||
expectErr: false,
|
||||
fs: afero.NewMemMapFs(),
|
||||
wantFile: []byte("test"),
|
||||
wantErr: false,
|
||||
},
|
||||
"showProgress works": {
|
||||
readChunkStream: fakeReadChunkStream{
|
||||
@ -50,19 +50,19 @@ func TestWriteStream(t *testing.T) {
|
||||
},
|
||||
fs: afero.NewMemMapFs(),
|
||||
showProgress: true,
|
||||
expectedFile: []byte("test"),
|
||||
expectErr: false,
|
||||
wantFile: []byte("test"),
|
||||
wantErr: false,
|
||||
},
|
||||
"Open fails": {
|
||||
fs: afero.NewReadOnlyFs(afero.NewMemMapFs()),
|
||||
expectErr: true,
|
||||
fs: afero.NewReadOnlyFs(afero.NewMemMapFs()),
|
||||
wantErr: true,
|
||||
},
|
||||
"recv fails": {
|
||||
readChunkStream: fakeReadChunkStream{
|
||||
recvErr: errors.New("someErr"),
|
||||
},
|
||||
fs: afero.NewMemMapFs(),
|
||||
expectErr: true,
|
||||
fs: afero.NewMemMapFs(),
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -74,14 +74,14 @@ func TestWriteStream(t *testing.T) {
|
||||
writer := NewFileStreamer(tc.fs)
|
||||
err := writer.WriteStream(filename, &tc.readChunkStream, tc.showProgress)
|
||||
|
||||
if tc.expectErr {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
return
|
||||
}
|
||||
require.NoError(err)
|
||||
fileContents, err := afero.ReadFile(tc.fs, filename)
|
||||
require.NoError(err)
|
||||
assert.Equal(tc.expectedFile, fileContents)
|
||||
assert.Equal(tc.wantFile, fileContents)
|
||||
})
|
||||
}
|
||||
}
|
||||
@ -94,48 +94,48 @@ func TestReadStream(t *testing.T) {
|
||||
filename string
|
||||
chunksize uint
|
||||
showProgress bool
|
||||
expectedChunks [][]byte
|
||||
expectErr bool
|
||||
wantChunks [][]byte
|
||||
wantErr bool
|
||||
}{
|
||||
"stream works": {
|
||||
writeChunkStream: stubWriteChunkStream{},
|
||||
filename: correctFilename,
|
||||
chunksize: 4,
|
||||
expectedChunks: [][]byte{
|
||||
wantChunks: [][]byte{
|
||||
[]byte("test"),
|
||||
},
|
||||
expectErr: false,
|
||||
wantErr: false,
|
||||
},
|
||||
"chunking works": {
|
||||
writeChunkStream: stubWriteChunkStream{},
|
||||
filename: correctFilename,
|
||||
chunksize: 2,
|
||||
expectedChunks: [][]byte{
|
||||
wantChunks: [][]byte{
|
||||
[]byte("te"),
|
||||
[]byte("st"),
|
||||
},
|
||||
expectErr: false,
|
||||
wantErr: false,
|
||||
},
|
||||
"chunksize of 0 detected": {
|
||||
writeChunkStream: stubWriteChunkStream{},
|
||||
filename: correctFilename,
|
||||
chunksize: 0,
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"showProgress works": {
|
||||
writeChunkStream: stubWriteChunkStream{},
|
||||
filename: correctFilename,
|
||||
chunksize: 4,
|
||||
showProgress: true,
|
||||
expectedChunks: [][]byte{
|
||||
wantChunks: [][]byte{
|
||||
[]byte("test"),
|
||||
},
|
||||
expectErr: false,
|
||||
wantErr: false,
|
||||
},
|
||||
"Open fails": {
|
||||
filename: "incorrect-filename",
|
||||
chunksize: 4,
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"send fails": {
|
||||
writeChunkStream: stubWriteChunkStream{
|
||||
@ -143,7 +143,7 @@ func TestReadStream(t *testing.T) {
|
||||
},
|
||||
filename: correctFilename,
|
||||
chunksize: 4,
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -157,12 +157,12 @@ func TestReadStream(t *testing.T) {
|
||||
reader := NewFileStreamer(fs)
|
||||
err := reader.ReadStream(tc.filename, &tc.writeChunkStream, tc.chunksize, tc.showProgress)
|
||||
|
||||
if tc.expectErr {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
return
|
||||
}
|
||||
require.NoError(err)
|
||||
assert.Equal(tc.expectedChunks, tc.writeChunkStream.chunks)
|
||||
assert.Equal(tc.wantChunks, tc.writeChunkStream.chunks)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -22,28 +22,28 @@ func TestDownloadCoordinator(t *testing.T) {
|
||||
filename := "/opt/coordinator"
|
||||
|
||||
testCases := map[string]struct {
|
||||
server fakeOnlyDownloadServer
|
||||
downloadClient stubDownloadClient
|
||||
serviceManager stubServiceManager
|
||||
attemptedDownloads map[string]time.Time
|
||||
expectedChunks [][]byte
|
||||
expectDownloadErr bool
|
||||
expectFile bool
|
||||
expectSystemdAction bool
|
||||
expectDeployed bool
|
||||
server fakeOnlyDownloadServer
|
||||
downloadClient stubDownloadClient
|
||||
serviceManager stubServiceManager
|
||||
attemptedDownloads map[string]time.Time
|
||||
wantChunks [][]byte
|
||||
wantDownloadErr bool
|
||||
wantFile bool
|
||||
wantSystemdAction bool
|
||||
wantDeployed bool
|
||||
}{
|
||||
"download works": {
|
||||
server: fakeOnlyDownloadServer{
|
||||
chunks: [][]byte{[]byte("test")},
|
||||
},
|
||||
attemptedDownloads: map[string]time.Time{},
|
||||
expectedChunks: [][]byte{
|
||||
wantChunks: [][]byte{
|
||||
[]byte("test"),
|
||||
},
|
||||
expectDownloadErr: false,
|
||||
expectFile: true,
|
||||
expectSystemdAction: true,
|
||||
expectDeployed: true,
|
||||
wantDownloadErr: false,
|
||||
wantFile: true,
|
||||
wantSystemdAction: true,
|
||||
wantDeployed: true,
|
||||
},
|
||||
"second download is not attempted twice": {
|
||||
server: fakeOnlyDownloadServer{
|
||||
@ -52,14 +52,14 @@ func TestDownloadCoordinator(t *testing.T) {
|
||||
attemptedDownloads: map[string]time.Time{
|
||||
"192.0.2.0:4000": time.Now(),
|
||||
},
|
||||
expectDownloadErr: true,
|
||||
wantDownloadErr: true,
|
||||
},
|
||||
"download rpc call error is detected": {
|
||||
server: fakeOnlyDownloadServer{
|
||||
downladErr: errors.New("download rpc error"),
|
||||
},
|
||||
attemptedDownloads: map[string]time.Time{},
|
||||
expectDownloadErr: true,
|
||||
wantDownloadErr: true,
|
||||
},
|
||||
"service restart error is detected": {
|
||||
server: fakeOnlyDownloadServer{
|
||||
@ -69,13 +69,13 @@ func TestDownloadCoordinator(t *testing.T) {
|
||||
systemdActionErr: errors.New("systemd error"),
|
||||
},
|
||||
attemptedDownloads: map[string]time.Time{},
|
||||
expectedChunks: [][]byte{
|
||||
wantChunks: [][]byte{
|
||||
[]byte("test"),
|
||||
},
|
||||
expectDownloadErr: true,
|
||||
expectFile: true,
|
||||
expectDeployed: true,
|
||||
expectSystemdAction: false,
|
||||
wantDownloadErr: true,
|
||||
wantFile: true,
|
||||
wantDeployed: true,
|
||||
wantSystemdAction: false,
|
||||
},
|
||||
}
|
||||
|
||||
@ -101,17 +101,17 @@ func TestDownloadCoordinator(t *testing.T) {
|
||||
err := download.DownloadCoordinator(context.Background(), ip)
|
||||
grpcServ.GracefulStop()
|
||||
|
||||
if tc.expectDownloadErr {
|
||||
if tc.wantDownloadErr {
|
||||
assert.Error(err)
|
||||
} else {
|
||||
assert.NoError(err)
|
||||
}
|
||||
|
||||
if tc.expectFile {
|
||||
assert.Equal(tc.expectedChunks, writer.chunks)
|
||||
if tc.wantFile {
|
||||
assert.Equal(tc.wantChunks, writer.chunks)
|
||||
assert.Equal(filename, writer.filename)
|
||||
}
|
||||
if tc.expectSystemdAction {
|
||||
if tc.wantSystemdAction {
|
||||
assert.ElementsMatch(
|
||||
[]ServiceManagerRequest{
|
||||
{Unit: debugd.CoordinatorSystemdUnitName, Action: Restart},
|
||||
|
@ -17,14 +17,14 @@ func TestGetLinuxUser(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
userCreator *stubUserCreator
|
||||
passwdContents string
|
||||
expectErr bool
|
||||
expectedUser LinuxUser
|
||||
wantErr bool
|
||||
wantUser LinuxUser
|
||||
}{
|
||||
"get works": {
|
||||
userCreator: &stubUserCreator{},
|
||||
passwdContents: "user:x:1000:1000:user:/home/user:/bin/bash\n",
|
||||
expectErr: false,
|
||||
expectedUser: LinuxUser{
|
||||
wantErr: false,
|
||||
wantUser: LinuxUser{
|
||||
Username: "user",
|
||||
Home: "/home/user",
|
||||
Uid: 1000,
|
||||
@ -34,22 +34,22 @@ func TestGetLinuxUser(t *testing.T) {
|
||||
"user does not exist": {
|
||||
userCreator: &stubUserCreator{},
|
||||
passwdContents: "",
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"parse fails": {
|
||||
userCreator: &stubUserCreator{},
|
||||
passwdContents: "invalid contents\n",
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"invalid uid": {
|
||||
userCreator: &stubUserCreator{},
|
||||
passwdContents: "user:x:invalid:1000:user:/home/user:/bin/bash\n",
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"invalid gid": {
|
||||
userCreator: &stubUserCreator{},
|
||||
passwdContents: "user:x:1000:invalid:user:/home/user:/bin/bash\n",
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -67,12 +67,12 @@ func TestGetLinuxUser(t *testing.T) {
|
||||
}
|
||||
user, err := manager.getLinuxUser(username)
|
||||
|
||||
if tc.expectErr {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
return
|
||||
}
|
||||
require.NoError(err)
|
||||
assert.Equal(tc.expectedUser, user)
|
||||
assert.Equal(tc.wantUser, user)
|
||||
})
|
||||
}
|
||||
}
|
||||
@ -83,14 +83,14 @@ func TestEnsureLinuxUserExists(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
userCreator *stubUserCreator
|
||||
passwdContents string
|
||||
expectErr bool
|
||||
expectedUser LinuxUser
|
||||
wantErr bool
|
||||
wantUser LinuxUser
|
||||
}{
|
||||
"create works": {
|
||||
userCreator: &stubUserCreator{},
|
||||
passwdContents: "user:x:1000:1000:user:/home/user:/bin/bash\n",
|
||||
expectErr: false,
|
||||
expectedUser: LinuxUser{
|
||||
wantErr: false,
|
||||
wantUser: LinuxUser{
|
||||
Username: "user",
|
||||
Home: "/home/user",
|
||||
Uid: 1000,
|
||||
@ -102,7 +102,7 @@ func TestEnsureLinuxUserExists(t *testing.T) {
|
||||
createUserErr: errors.New("create fails"),
|
||||
},
|
||||
passwdContents: "user:x:1000:1000:user:/home/user:/bin/bash\n",
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -120,12 +120,12 @@ func TestEnsureLinuxUserExists(t *testing.T) {
|
||||
}
|
||||
user, err := manager.EnsureLinuxUserExists(context.Background(), username)
|
||||
|
||||
if tc.expectErr {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
return
|
||||
}
|
||||
require.NoError(err)
|
||||
assert.Equal(tc.expectedUser, user)
|
||||
assert.Equal(tc.wantUser, user)
|
||||
assert.ElementsMatch([]string{username}, tc.userCreator.usernames)
|
||||
})
|
||||
}
|
||||
|
@ -12,15 +12,15 @@ func TestParse(t *testing.T) {
|
||||
filename := "/etc/passwd"
|
||||
|
||||
testCases := map[string]struct {
|
||||
passwdContents string
|
||||
createFile bool
|
||||
expectedEntries Entries
|
||||
expectErr bool
|
||||
passwdContents string
|
||||
createFile bool
|
||||
wantEntries Entries
|
||||
wantErr bool
|
||||
}{
|
||||
"parse works": {
|
||||
passwdContents: "root:x:0:0:root:/root:/bin/bash\n",
|
||||
createFile: true,
|
||||
expectedEntries: Entries{
|
||||
wantEntries: Entries{
|
||||
"root": {
|
||||
Pass: "x",
|
||||
Uid: "0",
|
||||
@ -30,16 +30,16 @@ func TestParse(t *testing.T) {
|
||||
Shell: "/bin/bash",
|
||||
},
|
||||
},
|
||||
expectErr: false,
|
||||
wantErr: false,
|
||||
},
|
||||
"passwd is corrupt": {
|
||||
passwdContents: "too:few:fields\n",
|
||||
createFile: true,
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"file does not exist": {
|
||||
createFile: false,
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -55,12 +55,12 @@ func TestParse(t *testing.T) {
|
||||
passwd := Passwd{}
|
||||
entries, err := passwd.Parse(fs)
|
||||
|
||||
if tc.expectErr {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
return
|
||||
}
|
||||
require.NoError(err)
|
||||
assert.Equal(tc.expectedEntries, entries)
|
||||
assert.Equal(tc.wantEntries, entries)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -16,9 +16,9 @@ func TestSystemdAction(t *testing.T) {
|
||||
unitName := "example.service"
|
||||
|
||||
testCases := map[string]struct {
|
||||
dbus stubDbus
|
||||
action SystemdAction
|
||||
expectErr bool
|
||||
dbus stubDbus
|
||||
action SystemdAction
|
||||
wantErr bool
|
||||
}{
|
||||
"start works": {
|
||||
dbus: stubDbus{
|
||||
@ -26,8 +26,8 @@ func TestSystemdAction(t *testing.T) {
|
||||
result: "done",
|
||||
},
|
||||
},
|
||||
action: Start,
|
||||
expectErr: false,
|
||||
action: Start,
|
||||
wantErr: false,
|
||||
},
|
||||
"stop works": {
|
||||
dbus: stubDbus{
|
||||
@ -35,8 +35,8 @@ func TestSystemdAction(t *testing.T) {
|
||||
result: "done",
|
||||
},
|
||||
},
|
||||
action: Stop,
|
||||
expectErr: false,
|
||||
action: Stop,
|
||||
wantErr: false,
|
||||
},
|
||||
"restart works": {
|
||||
dbus: stubDbus{
|
||||
@ -44,22 +44,22 @@ func TestSystemdAction(t *testing.T) {
|
||||
result: "done",
|
||||
},
|
||||
},
|
||||
action: Restart,
|
||||
expectErr: false,
|
||||
action: Restart,
|
||||
wantErr: false,
|
||||
},
|
||||
"reload works": {
|
||||
dbus: stubDbus{
|
||||
conn: &fakeDbusConn{},
|
||||
},
|
||||
action: Reload,
|
||||
expectErr: false,
|
||||
action: Reload,
|
||||
wantErr: false,
|
||||
},
|
||||
"unknown action": {
|
||||
dbus: stubDbus{
|
||||
conn: &fakeDbusConn{},
|
||||
},
|
||||
action: Unknown,
|
||||
expectErr: true,
|
||||
action: Unknown,
|
||||
wantErr: true,
|
||||
},
|
||||
"action fails": {
|
||||
dbus: stubDbus{
|
||||
@ -67,8 +67,8 @@ func TestSystemdAction(t *testing.T) {
|
||||
actionErr: errors.New("action fails"),
|
||||
},
|
||||
},
|
||||
action: Start,
|
||||
expectErr: true,
|
||||
action: Start,
|
||||
wantErr: true,
|
||||
},
|
||||
"action result is failure": {
|
||||
dbus: stubDbus{
|
||||
@ -76,15 +76,15 @@ func TestSystemdAction(t *testing.T) {
|
||||
result: "failure",
|
||||
},
|
||||
},
|
||||
action: Start,
|
||||
expectErr: true,
|
||||
action: Start,
|
||||
wantErr: true,
|
||||
},
|
||||
"newConn fails": {
|
||||
dbus: stubDbus{
|
||||
connErr: errors.New("newConn fails"),
|
||||
},
|
||||
action: Start,
|
||||
expectErr: true,
|
||||
action: Start,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -104,7 +104,7 @@ func TestSystemdAction(t *testing.T) {
|
||||
Action: tc.action,
|
||||
})
|
||||
|
||||
if tc.expectErr {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
return
|
||||
}
|
||||
@ -115,11 +115,11 @@ func TestSystemdAction(t *testing.T) {
|
||||
|
||||
func TestWriteSystemdUnitFile(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
dbus stubDbus
|
||||
unit SystemdUnit
|
||||
readonly bool
|
||||
expectErr bool
|
||||
expectedFileContents string
|
||||
dbus stubDbus
|
||||
unit SystemdUnit
|
||||
readonly bool
|
||||
wantErr bool
|
||||
wantFileContents string
|
||||
}{
|
||||
"start works": {
|
||||
dbus: stubDbus{
|
||||
@ -131,8 +131,8 @@ func TestWriteSystemdUnitFile(t *testing.T) {
|
||||
Name: "test.service",
|
||||
Contents: "testservicefilecontents",
|
||||
},
|
||||
expectErr: false,
|
||||
expectedFileContents: "testservicefilecontents",
|
||||
wantErr: false,
|
||||
wantFileContents: "testservicefilecontents",
|
||||
},
|
||||
"write fails": {
|
||||
dbus: stubDbus{
|
||||
@ -144,8 +144,8 @@ func TestWriteSystemdUnitFile(t *testing.T) {
|
||||
Name: "test.service",
|
||||
Contents: "testservicefilecontents",
|
||||
},
|
||||
readonly: true,
|
||||
expectErr: true,
|
||||
readonly: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"systemd reload fails": {
|
||||
dbus: stubDbus{
|
||||
@ -157,8 +157,8 @@ func TestWriteSystemdUnitFile(t *testing.T) {
|
||||
Name: "test.service",
|
||||
Contents: "testservicefilecontents",
|
||||
},
|
||||
readonly: false,
|
||||
expectErr: true,
|
||||
readonly: false,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -179,14 +179,14 @@ func TestWriteSystemdUnitFile(t *testing.T) {
|
||||
}
|
||||
err := manager.WriteSystemdUnitFile(context.Background(), tc.unit)
|
||||
|
||||
if tc.expectErr {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
return
|
||||
}
|
||||
require.NoError(err)
|
||||
fileContents, err := afero.ReadFile(fs, fmt.Sprintf("%s/%s", systemdUnitFolder, tc.unit.Name))
|
||||
assert.NoError(err)
|
||||
assert.Equal(tc.expectedFileContents, string(fileContents))
|
||||
assert.Equal(tc.wantFileContents, string(fileContents))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -19,51 +19,51 @@ func TestDeploySSHAuthorizedKey(t *testing.T) {
|
||||
}
|
||||
|
||||
testCases := map[string]struct {
|
||||
fs afero.Fs
|
||||
userCreator *stubUserCreator
|
||||
passwdContents string
|
||||
alreadyDeployed bool
|
||||
readonly bool
|
||||
expectErr bool
|
||||
expectFile bool
|
||||
expectedFileContents string
|
||||
fs afero.Fs
|
||||
userCreator *stubUserCreator
|
||||
passwdContents string
|
||||
alreadyDeployed bool
|
||||
readonly bool
|
||||
wantErr bool
|
||||
wantFile bool
|
||||
wantFileContents string
|
||||
}{
|
||||
"deploy works": {
|
||||
fs: afero.NewMemMapFs(),
|
||||
userCreator: &stubUserCreator{},
|
||||
passwdContents: "user:x:1000:1000:user:/home/user:/bin/bash\n",
|
||||
expectErr: false,
|
||||
expectFile: true,
|
||||
expectedFileContents: "ssh-rsa testkey user\n",
|
||||
fs: afero.NewMemMapFs(),
|
||||
userCreator: &stubUserCreator{},
|
||||
passwdContents: "user:x:1000:1000:user:/home/user:/bin/bash\n",
|
||||
wantErr: false,
|
||||
wantFile: true,
|
||||
wantFileContents: "ssh-rsa testkey user\n",
|
||||
},
|
||||
"appending ssh key works": {
|
||||
fs: memMapFsWithFile("/home/user/.ssh/authorized_keys.d/debugd", "ssh-rsa preexistingkey user\n"),
|
||||
userCreator: &stubUserCreator{},
|
||||
passwdContents: "user:x:1000:1000:user:/home/user:/bin/bash\n",
|
||||
expectErr: false,
|
||||
expectFile: true,
|
||||
expectedFileContents: "ssh-rsa preexistingkey user\nssh-rsa testkey user\n",
|
||||
fs: memMapFsWithFile("/home/user/.ssh/authorized_keys.d/debugd", "ssh-rsa preexistingkey user\n"),
|
||||
userCreator: &stubUserCreator{},
|
||||
passwdContents: "user:x:1000:1000:user:/home/user:/bin/bash\n",
|
||||
wantErr: false,
|
||||
wantFile: true,
|
||||
wantFileContents: "ssh-rsa preexistingkey user\nssh-rsa testkey user\n",
|
||||
},
|
||||
"redeployment avoided": {
|
||||
fs: afero.NewMemMapFs(),
|
||||
userCreator: &stubUserCreator{},
|
||||
passwdContents: "user:x:1000:1000:user:/home/user:/bin/bash\n",
|
||||
expectErr: false,
|
||||
wantErr: false,
|
||||
alreadyDeployed: true,
|
||||
expectFile: false,
|
||||
wantFile: false,
|
||||
},
|
||||
"user does not exist": {
|
||||
fs: afero.NewMemMapFs(),
|
||||
userCreator: &stubUserCreator{},
|
||||
passwdContents: "",
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"readonly fs": {
|
||||
fs: afero.NewMemMapFs(),
|
||||
userCreator: &stubUserCreator{},
|
||||
passwdContents: "user:x:1000:1000:user:/home/user:/bin/bash\n",
|
||||
readonly: true,
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -92,15 +92,15 @@ func TestDeploySSHAuthorizedKey(t *testing.T) {
|
||||
}
|
||||
err := sshAccess.DeploySSHAuthorizedKey(context.Background(), authorizedKey)
|
||||
|
||||
if tc.expectErr {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
return
|
||||
}
|
||||
require.NoError(err)
|
||||
if tc.expectFile {
|
||||
if tc.wantFile {
|
||||
fileContents, err := afero.ReadFile(tc.fs, "/home/user/.ssh/authorized_keys.d/debugd")
|
||||
assert.NoError(err)
|
||||
assert.Equal(tc.expectedFileContents, string(fileContents))
|
||||
assert.Equal(tc.wantFileContents, string(fileContents))
|
||||
} else {
|
||||
exists, err := afero.Exists(tc.fs, "/home/user/.ssh/authorized_keys.d/debugd")
|
||||
assert.NoError(err)
|
||||
|
@ -15,9 +15,9 @@ func TestDiscoverDebugIPs(t *testing.T) {
|
||||
err := errors.New("some err")
|
||||
|
||||
testCases := map[string]struct {
|
||||
meta stubMetadata
|
||||
expectedIPs []string
|
||||
expectErr bool
|
||||
meta stubMetadata
|
||||
wantIPs []string
|
||||
wantErr bool
|
||||
}{
|
||||
"disovery works": {
|
||||
meta: stubMetadata{
|
||||
@ -33,7 +33,7 @@ func TestDiscoverDebugIPs(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
expectedIPs: []string{
|
||||
wantIPs: []string{
|
||||
"192.0.2.1", "192.0.2.2",
|
||||
},
|
||||
},
|
||||
@ -41,7 +41,7 @@ func TestDiscoverDebugIPs(t *testing.T) {
|
||||
meta: stubMetadata{
|
||||
listErr: err,
|
||||
},
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -55,12 +55,12 @@ func TestDiscoverDebugIPs(t *testing.T) {
|
||||
}
|
||||
ips, err := fetcher.DiscoverDebugdIPs(context.Background())
|
||||
|
||||
if tc.expectErr {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
return
|
||||
}
|
||||
require.NoError(err)
|
||||
assert.ElementsMatch(tc.expectedIPs, ips)
|
||||
assert.ElementsMatch(tc.wantIPs, ips)
|
||||
})
|
||||
}
|
||||
}
|
||||
@ -69,9 +69,9 @@ func TestFetchSSHKeys(t *testing.T) {
|
||||
err := errors.New("some err")
|
||||
|
||||
testCases := map[string]struct {
|
||||
meta stubMetadata
|
||||
expectedKeys []ssh.SSHKey
|
||||
expectErr bool
|
||||
meta stubMetadata
|
||||
wantKeys []ssh.SSHKey
|
||||
wantErr bool
|
||||
}{
|
||||
"fetch works": {
|
||||
meta: stubMetadata{
|
||||
@ -81,7 +81,7 @@ func TestFetchSSHKeys(t *testing.T) {
|
||||
SSHKeys: map[string][]string{"bob": {"ssh-rsa bobskey"}},
|
||||
},
|
||||
},
|
||||
expectedKeys: []ssh.SSHKey{
|
||||
wantKeys: []ssh.SSHKey{
|
||||
{
|
||||
Username: "bob",
|
||||
KeyValue: "ssh-rsa bobskey",
|
||||
@ -92,7 +92,7 @@ func TestFetchSSHKeys(t *testing.T) {
|
||||
meta: stubMetadata{
|
||||
selfErr: err,
|
||||
},
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -106,12 +106,12 @@ func TestFetchSSHKeys(t *testing.T) {
|
||||
}
|
||||
keys, err := fetcher.FetchSSHKeys(context.Background())
|
||||
|
||||
if tc.expectErr {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
return
|
||||
}
|
||||
require.NoError(err)
|
||||
assert.ElementsMatch(tc.expectedKeys, keys)
|
||||
assert.ElementsMatch(tc.wantKeys, keys)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -13,12 +13,12 @@ import (
|
||||
|
||||
func TestSchedulerStart(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
fetcher stubFetcher
|
||||
ssh stubSSHDeployer
|
||||
downloader stubDownloader
|
||||
timeout time.Duration
|
||||
expectedSSHKeys []ssh.SSHKey
|
||||
expectedDebugdDownloads []string
|
||||
fetcher stubFetcher
|
||||
ssh stubSSHDeployer
|
||||
downloader stubDownloader
|
||||
timeout time.Duration
|
||||
wantSSHKeys []ssh.SSHKey
|
||||
wantDebugdDownloads []string
|
||||
}{
|
||||
"scheduler works and calls fetcher functions at least once": {},
|
||||
"ssh keys are fetched": {
|
||||
@ -30,7 +30,7 @@ func TestSchedulerStart(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
expectedSSHKeys: []ssh.SSHKey{
|
||||
wantSSHKeys: []ssh.SSHKey{
|
||||
{
|
||||
Username: "test",
|
||||
KeyValue: "testkey",
|
||||
@ -45,14 +45,14 @@ func TestSchedulerStart(t *testing.T) {
|
||||
err: errors.New("download fails"),
|
||||
},
|
||||
|
||||
expectedDebugdDownloads: []string{"192.0.2.1", "192.0.2.2"},
|
||||
wantDebugdDownloads: []string{"192.0.2.1", "192.0.2.2"},
|
||||
},
|
||||
"if download is successful, second download is not attempted": {
|
||||
fetcher: stubFetcher{
|
||||
ips: []string{"192.0.2.1", "192.0.2.2"},
|
||||
},
|
||||
|
||||
expectedDebugdDownloads: []string{"192.0.2.1"},
|
||||
wantDebugdDownloads: []string{"192.0.2.1"},
|
||||
},
|
||||
"endpoint discovery can fail": {
|
||||
fetcher: stubFetcher{
|
||||
@ -82,8 +82,8 @@ func TestSchedulerStart(t *testing.T) {
|
||||
go scheduler.Start(ctx, wg)
|
||||
|
||||
wg.Wait()
|
||||
assert.Equal(tc.expectedSSHKeys, tc.ssh.sshKeys)
|
||||
assert.Equal(tc.expectedDebugdDownloads, tc.downloader.ips)
|
||||
assert.Equal(tc.wantSSHKeys, tc.ssh.sshKeys)
|
||||
assert.Equal(tc.wantDebugdDownloads, tc.downloader.ips)
|
||||
assert.Greater(tc.fetcher.discoverCalls, 0)
|
||||
assert.Greater(tc.fetcher.fetchSSHKeysCalls, 0)
|
||||
})
|
||||
|
@ -23,12 +23,12 @@ func TestUploadAuthorizedKeys(t *testing.T) {
|
||||
endpoint := "192.0.2.1:4000"
|
||||
|
||||
testCases := map[string]struct {
|
||||
ssh stubSSHDeployer
|
||||
serviceManager stubServiceManager
|
||||
request *pb.UploadAuthorizedKeysRequest
|
||||
expectErr bool
|
||||
expectedResponseStatus pb.UploadAuthorizedKeysStatus
|
||||
expectedKeys []ssh.SSHKey
|
||||
ssh stubSSHDeployer
|
||||
serviceManager stubServiceManager
|
||||
request *pb.UploadAuthorizedKeysRequest
|
||||
wantErr bool
|
||||
wantResponseStatus pb.UploadAuthorizedKeysStatus
|
||||
wantKeys []ssh.SSHKey
|
||||
}{
|
||||
"upload authorized keys works": {
|
||||
request: &pb.UploadAuthorizedKeysRequest{
|
||||
@ -39,8 +39,8 @@ func TestUploadAuthorizedKeys(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
expectedResponseStatus: pb.UploadAuthorizedKeysStatus_UPLOAD_AUTHORIZED_KEYS_SUCCESS,
|
||||
expectedKeys: []ssh.SSHKey{
|
||||
wantResponseStatus: pb.UploadAuthorizedKeysStatus_UPLOAD_AUTHORIZED_KEYS_SUCCESS,
|
||||
wantKeys: []ssh.SSHKey{
|
||||
{
|
||||
Username: "testuser",
|
||||
KeyValue: "teskey",
|
||||
@ -56,9 +56,9 @@ func TestUploadAuthorizedKeys(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
ssh: stubSSHDeployer{deployErr: errors.New("ssh key deployment error")},
|
||||
expectedResponseStatus: pb.UploadAuthorizedKeysStatus_UPLOAD_AUTHORIZED_KEYS_FAILURE,
|
||||
expectedKeys: []ssh.SSHKey{
|
||||
ssh: stubSSHDeployer{deployErr: errors.New("ssh key deployment error")},
|
||||
wantResponseStatus: pb.UploadAuthorizedKeysStatus_UPLOAD_AUTHORIZED_KEYS_FAILURE,
|
||||
wantKeys: []ssh.SSHKey{
|
||||
{
|
||||
Username: "testuser",
|
||||
KeyValue: "teskey",
|
||||
@ -86,13 +86,13 @@ func TestUploadAuthorizedKeys(t *testing.T) {
|
||||
|
||||
grpcServ.GracefulStop()
|
||||
|
||||
if tc.expectErr {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
return
|
||||
}
|
||||
require.NoError(err)
|
||||
assert.Equal(tc.expectedResponseStatus, resp.Status)
|
||||
assert.ElementsMatch(tc.ssh.sshKeys, tc.expectedKeys)
|
||||
assert.Equal(tc.wantResponseStatus, resp.Status)
|
||||
assert.ElementsMatch(tc.ssh.sshKeys, tc.wantKeys)
|
||||
})
|
||||
}
|
||||
}
|
||||
@ -101,31 +101,31 @@ func TestUploadCoordinator(t *testing.T) {
|
||||
endpoint := "192.0.2.1:4000"
|
||||
|
||||
testCases := map[string]struct {
|
||||
ssh stubSSHDeployer
|
||||
serviceManager stubServiceManager
|
||||
streamer fakeStreamer
|
||||
uploadChunks [][]byte
|
||||
expectErr bool
|
||||
expectedResponseStatus pb.UploadCoordinatorStatus
|
||||
expectFile bool
|
||||
expectedChunks [][]byte
|
||||
ssh stubSSHDeployer
|
||||
serviceManager stubServiceManager
|
||||
streamer fakeStreamer
|
||||
uploadChunks [][]byte
|
||||
wantErr bool
|
||||
wantResponseStatus pb.UploadCoordinatorStatus
|
||||
wantFile bool
|
||||
wantChunks [][]byte
|
||||
}{
|
||||
"upload works": {
|
||||
uploadChunks: [][]byte{
|
||||
[]byte("test"),
|
||||
},
|
||||
expectFile: true,
|
||||
expectedChunks: [][]byte{
|
||||
wantFile: true,
|
||||
wantChunks: [][]byte{
|
||||
[]byte("test"),
|
||||
},
|
||||
expectedResponseStatus: pb.UploadCoordinatorStatus_UPLOAD_COORDINATOR_SUCCESS,
|
||||
wantResponseStatus: pb.UploadCoordinatorStatus_UPLOAD_COORDINATOR_SUCCESS,
|
||||
},
|
||||
"recv fails": {
|
||||
streamer: fakeStreamer{
|
||||
writeStreamErr: errors.New("recv error"),
|
||||
},
|
||||
expectedResponseStatus: pb.UploadCoordinatorStatus_UPLOAD_COORDINATOR_UPLOAD_FAILED,
|
||||
expectErr: true,
|
||||
wantResponseStatus: pb.UploadCoordinatorStatus_UPLOAD_COORDINATOR_UPLOAD_FAILED,
|
||||
wantErr: true,
|
||||
},
|
||||
"starting coordinator fails": {
|
||||
uploadChunks: [][]byte{
|
||||
@ -134,11 +134,11 @@ func TestUploadCoordinator(t *testing.T) {
|
||||
serviceManager: stubServiceManager{
|
||||
systemdActionErr: errors.New("starting coordinator error"),
|
||||
},
|
||||
expectFile: true,
|
||||
expectedChunks: [][]byte{
|
||||
wantFile: true,
|
||||
wantChunks: [][]byte{
|
||||
[]byte("test"),
|
||||
},
|
||||
expectedResponseStatus: pb.UploadCoordinatorStatus_UPLOAD_COORDINATOR_START_FAILED,
|
||||
wantResponseStatus: pb.UploadCoordinatorStatus_UPLOAD_COORDINATOR_START_FAILED,
|
||||
},
|
||||
}
|
||||
|
||||
@ -164,14 +164,14 @@ func TestUploadCoordinator(t *testing.T) {
|
||||
|
||||
grpcServ.GracefulStop()
|
||||
|
||||
if tc.expectErr {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
return
|
||||
}
|
||||
require.NoError(err)
|
||||
assert.Equal(tc.expectedResponseStatus, resp.Status)
|
||||
if tc.expectFile {
|
||||
assert.Equal(tc.expectedChunks, tc.streamer.writeStreamChunks)
|
||||
assert.Equal(tc.wantResponseStatus, resp.Status)
|
||||
if tc.wantFile {
|
||||
assert.Equal(tc.wantChunks, tc.streamer.writeStreamChunks)
|
||||
assert.Equal("/opt/coordinator", tc.streamer.writeStreamFilename)
|
||||
} else {
|
||||
assert.Empty(tc.streamer.writeStreamChunks)
|
||||
@ -188,8 +188,8 @@ func TestDownloadCoordinator(t *testing.T) {
|
||||
serviceManager stubServiceManager
|
||||
request *pb.DownloadCoordinatorRequest
|
||||
streamer fakeStreamer
|
||||
expectErr bool
|
||||
expectedChunks [][]byte
|
||||
wantErr bool
|
||||
wantChunks [][]byte
|
||||
}{
|
||||
"download works": {
|
||||
request: &pb.DownloadCoordinatorRequest{},
|
||||
@ -198,8 +198,8 @@ func TestDownloadCoordinator(t *testing.T) {
|
||||
[]byte("test"),
|
||||
},
|
||||
},
|
||||
expectErr: false,
|
||||
expectedChunks: [][]byte{
|
||||
wantErr: false,
|
||||
wantChunks: [][]byte{
|
||||
[]byte("test"),
|
||||
},
|
||||
},
|
||||
@ -208,7 +208,7 @@ func TestDownloadCoordinator(t *testing.T) {
|
||||
streamer: fakeStreamer{
|
||||
readStreamErr: errors.New("read coordinator fails"),
|
||||
},
|
||||
expectErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -232,12 +232,12 @@ func TestDownloadCoordinator(t *testing.T) {
|
||||
chunks, err := fakeRead(stream)
|
||||
grpcServ.GracefulStop()
|
||||
|
||||
if tc.expectErr {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
return
|
||||
}
|
||||
require.NoError(err)
|
||||
assert.Equal(tc.expectedChunks, chunks)
|
||||
assert.Equal(tc.wantChunks, chunks)
|
||||
assert.Equal("/opt/coordinator", tc.streamer.readStreamFilename)
|
||||
})
|
||||
}
|
||||
@ -246,12 +246,12 @@ func TestDownloadCoordinator(t *testing.T) {
|
||||
func TestUploadSystemServiceUnits(t *testing.T) {
|
||||
endpoint := "192.0.2.1:4000"
|
||||
testCases := map[string]struct {
|
||||
ssh stubSSHDeployer
|
||||
serviceManager stubServiceManager
|
||||
request *pb.UploadSystemdServiceUnitsRequest
|
||||
expectErr bool
|
||||
expectedResponseStatus pb.UploadSystemdServiceUnitsStatus
|
||||
expectedUnitFiles []deploy.SystemdUnit
|
||||
ssh stubSSHDeployer
|
||||
serviceManager stubServiceManager
|
||||
request *pb.UploadSystemdServiceUnitsRequest
|
||||
wantErr bool
|
||||
wantResponseStatus pb.UploadSystemdServiceUnitsStatus
|
||||
wantUnitFiles []deploy.SystemdUnit
|
||||
}{
|
||||
"upload systemd service units": {
|
||||
request: &pb.UploadSystemdServiceUnitsRequest{
|
||||
@ -262,8 +262,8 @@ func TestUploadSystemServiceUnits(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
expectedResponseStatus: pb.UploadSystemdServiceUnitsStatus_UPLOAD_SYSTEMD_SERVICE_UNITS_SUCCESS,
|
||||
expectedUnitFiles: []deploy.SystemdUnit{
|
||||
wantResponseStatus: pb.UploadSystemdServiceUnitsStatus_UPLOAD_SYSTEMD_SERVICE_UNITS_SUCCESS,
|
||||
wantUnitFiles: []deploy.SystemdUnit{
|
||||
{
|
||||
Name: "test.service",
|
||||
Contents: "testcontents",
|
||||
@ -282,8 +282,8 @@ func TestUploadSystemServiceUnits(t *testing.T) {
|
||||
serviceManager: stubServiceManager{
|
||||
writeSystemdUnitFileErr: errors.New("write error"),
|
||||
},
|
||||
expectedResponseStatus: pb.UploadSystemdServiceUnitsStatus_UPLOAD_SYSTEMD_SERVICE_UNITS_FAILURE,
|
||||
expectedUnitFiles: []deploy.SystemdUnit{
|
||||
wantResponseStatus: pb.UploadSystemdServiceUnitsStatus_UPLOAD_SYSTEMD_SERVICE_UNITS_FAILURE,
|
||||
wantUnitFiles: []deploy.SystemdUnit{
|
||||
{
|
||||
Name: "test.service",
|
||||
Contents: "testcontents",
|
||||
@ -310,14 +310,14 @@ func TestUploadSystemServiceUnits(t *testing.T) {
|
||||
|
||||
grpcServ.GracefulStop()
|
||||
|
||||
if tc.expectErr {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
return
|
||||
}
|
||||
require.NoError(err)
|
||||
require.NotNil(resp.Status)
|
||||
assert.Equal(tc.expectedResponseStatus, resp.Status)
|
||||
assert.ElementsMatch(tc.expectedUnitFiles, tc.serviceManager.unitFiles)
|
||||
assert.Equal(tc.wantResponseStatus, resp.Status)
|
||||
assert.ElementsMatch(tc.wantUnitFiles, tc.serviceManager.unitFiles)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -308,7 +308,7 @@ Y+t5OxL3kL15VzY1Ob0d5cMCAwEAAQ==
|
||||
policyProducer KeyPolicyProducer
|
||||
importKey []byte
|
||||
cleanupRequired bool
|
||||
errExpected bool
|
||||
wantErr bool
|
||||
}{
|
||||
"create new kek successful": {
|
||||
client: &stubAWSClient{createKeyID: "key-id"},
|
||||
@ -317,13 +317,13 @@ Y+t5OxL3kL15VzY1Ob0d5cMCAwEAAQ==
|
||||
"CreateKeyPolicy fails on existing": {
|
||||
client: &stubAWSClient{},
|
||||
policyProducer: &stubKeyPolicyProducer{createKeyPolicyErr: someErr},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"CreateKeyPolicy fails on new": {
|
||||
client: &stubAWSClient{describeKeyErr: &types.NotFoundException{}},
|
||||
policyProducer: &stubKeyPolicyProducer{createKeyPolicyErr: someErr},
|
||||
cleanupRequired: true,
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"PutKeyPolicy fails on new": {
|
||||
client: &stubAWSClient{
|
||||
@ -333,7 +333,7 @@ Y+t5OxL3kL15VzY1Ob0d5cMCAwEAAQ==
|
||||
},
|
||||
policyProducer: &stubKeyPolicyProducer{},
|
||||
cleanupRequired: true,
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"CreateAlias fails on new": {
|
||||
client: &stubAWSClient{
|
||||
@ -343,17 +343,17 @@ Y+t5OxL3kL15VzY1Ob0d5cMCAwEAAQ==
|
||||
},
|
||||
policyProducer: &stubKeyPolicyProducer{},
|
||||
cleanupRequired: true,
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"CreateKey fails on new": {
|
||||
client: &stubAWSClient{describeKeyErr: &types.NotFoundException{}, createKeyErr: someErr},
|
||||
policyProducer: &stubKeyPolicyProducer{},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"DescribeKey fails": {
|
||||
client: &stubAWSClient{describeKeyErr: someErr},
|
||||
policyProducer: &stubKeyPolicyProducer{},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"DescribeKey fails with not found error": {
|
||||
client: &stubAWSClient{describeKeyErr: &types.NotFoundException{}},
|
||||
@ -373,7 +373,7 @@ Y+t5OxL3kL15VzY1Ob0d5cMCAwEAAQ==
|
||||
policyProducer: &stubKeyPolicyProducer{},
|
||||
importKey: importKey,
|
||||
cleanupRequired: true,
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"ImportKeyMaterial fails on new": {
|
||||
client: &stubAWSClient{
|
||||
@ -384,19 +384,19 @@ Y+t5OxL3kL15VzY1Ob0d5cMCAwEAAQ==
|
||||
policyProducer: &stubKeyPolicyProducer{},
|
||||
importKey: importKey,
|
||||
cleanupRequired: true,
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"GetParametersForImport fails on existing": {
|
||||
client: &stubAWSClient{getParametersForImportErr: someErr},
|
||||
policyProducer: &stubKeyPolicyProducer{},
|
||||
importKey: importKey,
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"ImportKeyMaterial fails on existing": {
|
||||
client: &stubAWSClient{importKeyMaterialErr: someErr},
|
||||
policyProducer: &stubKeyPolicyProducer{},
|
||||
importKey: importKey,
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"errors during cleanup don't stop execution": {
|
||||
client: &stubAWSClient{
|
||||
@ -406,7 +406,7 @@ Y+t5OxL3kL15VzY1Ob0d5cMCAwEAAQ==
|
||||
},
|
||||
policyProducer: &stubKeyPolicyProducer{createKeyPolicyErr: someErr},
|
||||
cleanupRequired: true,
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -421,7 +421,7 @@ Y+t5OxL3kL15VzY1Ob0d5cMCAwEAAQ==
|
||||
}
|
||||
|
||||
err := client.CreateKEK(context.Background(), "test-key", tc.importKey)
|
||||
if tc.errExpected {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
if tc.cleanupRequired {
|
||||
assert.True(tc.client.cleanUpCalled, "failed to clean up")
|
||||
|
@ -36,9 +36,9 @@ func TestKMSCreateKEK(t *testing.T) {
|
||||
importKey := []byte("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA")
|
||||
|
||||
testCases := map[string]struct {
|
||||
client *stubAzureClient
|
||||
importKey []byte
|
||||
errExpected bool
|
||||
client *stubAzureClient
|
||||
importKey []byte
|
||||
wantErr bool
|
||||
}{
|
||||
"create new kek successful": {
|
||||
client: &stubAzureClient{},
|
||||
@ -48,13 +48,13 @@ func TestKMSCreateKEK(t *testing.T) {
|
||||
importKey: importKey,
|
||||
},
|
||||
"SetSecret fails on new": {
|
||||
client: &stubAzureClient{setSecretErr: someErr},
|
||||
errExpected: true,
|
||||
client: &stubAzureClient{setSecretErr: someErr},
|
||||
wantErr: true,
|
||||
},
|
||||
"SetSecret fails on import": {
|
||||
client: &stubAzureClient{setSecretErr: someErr},
|
||||
importKey: importKey,
|
||||
errExpected: true,
|
||||
client: &stubAzureClient{setSecretErr: someErr},
|
||||
importKey: importKey,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -68,7 +68,7 @@ func TestKMSCreateKEK(t *testing.T) {
|
||||
}
|
||||
|
||||
err := client.CreateKEK(context.Background(), "test-key", tc.importKey)
|
||||
if tc.errExpected {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
} else {
|
||||
assert.NoError(err)
|
||||
@ -83,9 +83,9 @@ func TestKMSGetDEK(t *testing.T) {
|
||||
wrapKey := []byte("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA")
|
||||
|
||||
testCases := map[string]struct {
|
||||
client kmsClientAPI
|
||||
storage kms.Storage
|
||||
errExpected bool
|
||||
client kmsClientAPI
|
||||
storage kms.Storage
|
||||
wantErr bool
|
||||
}{
|
||||
"successful for new key": {
|
||||
client: &stubAzureClient{secret: wrapKey},
|
||||
@ -97,9 +97,9 @@ func TestKMSGetDEK(t *testing.T) {
|
||||
storage: &stubStorage{key: []byte{0x14, 0x48, 0xC4, 0xEA, 0x4B, 0x4B, 0xCA, 0xE4, 0x5A, 0xD4, 0xCC, 0xE3, 0xF7, 0xDD, 0xD5, 0x78, 0xA5, 0xA9, 0xEF, 0x9A, 0x93, 0x36, 0x09, 0xD6, 0x23, 0x01, 0xF5, 0x5F, 0xE1, 0x20, 0xDD, 0xFC, 0xBC, 0xF3, 0xA9, 0x67, 0x8B, 0x89, 0x54, 0x96}},
|
||||
},
|
||||
"Get from storage fails": {
|
||||
client: &stubAzureClient{},
|
||||
storage: &stubStorage{getErr: someErr},
|
||||
errExpected: true,
|
||||
client: &stubAzureClient{},
|
||||
storage: &stubStorage{getErr: someErr},
|
||||
wantErr: true,
|
||||
},
|
||||
"Put to storage fails": {
|
||||
client: &stubAzureClient{secret: wrapKey},
|
||||
@ -107,27 +107,27 @@ func TestKMSGetDEK(t *testing.T) {
|
||||
getErr: storage.ErrDEKUnset,
|
||||
putErr: someErr,
|
||||
},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"GetSecret fails": {
|
||||
client: &stubAzureClient{getSecretErr: someErr},
|
||||
storage: storage.NewMemMapStorage(),
|
||||
errExpected: true,
|
||||
client: &stubAzureClient{getSecretErr: someErr},
|
||||
storage: storage.NewMemMapStorage(),
|
||||
wantErr: true,
|
||||
},
|
||||
"GetSecret fails with unknown kek": {
|
||||
client: &stubAzureClient{getSecretErr: errors.New("SecretNotFound")},
|
||||
storage: storage.NewMemMapStorage(),
|
||||
errExpected: true,
|
||||
client: &stubAzureClient{getSecretErr: errors.New("SecretNotFound")},
|
||||
storage: storage.NewMemMapStorage(),
|
||||
wantErr: true,
|
||||
},
|
||||
"key wrapping fails": {
|
||||
client: &stubAzureClient{secret: []byte{0x1}},
|
||||
storage: storage.NewMemMapStorage(),
|
||||
errExpected: true,
|
||||
client: &stubAzureClient{secret: []byte{0x1}},
|
||||
storage: storage.NewMemMapStorage(),
|
||||
wantErr: true,
|
||||
},
|
||||
"key unwrapping fails": {
|
||||
client: &stubAzureClient{secret: wrapKey},
|
||||
storage: &stubStorage{key: []byte{0x1}},
|
||||
errExpected: true,
|
||||
client: &stubAzureClient{secret: wrapKey},
|
||||
storage: &stubStorage{key: []byte{0x1}},
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -142,7 +142,7 @@ func TestKMSGetDEK(t *testing.T) {
|
||||
}
|
||||
|
||||
dek, err := client.GetDEK(context.Background(), "test-key", "volume-01", 32)
|
||||
if tc.errExpected {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
} else {
|
||||
assert.Len(dek, 32)
|
||||
|
@ -88,25 +88,25 @@ func TestHSMCreateKEK(t *testing.T) {
|
||||
importKey := []byte("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA")
|
||||
|
||||
testCases := map[string]struct {
|
||||
client *stubHSMClient
|
||||
importKey []byte
|
||||
errExpected bool
|
||||
client *stubHSMClient
|
||||
importKey []byte
|
||||
wantErr bool
|
||||
}{
|
||||
"create new kek successful": {
|
||||
client: &stubHSMClient{},
|
||||
},
|
||||
"CreateOCTKey fails": {
|
||||
client: &stubHSMClient{createOCTKeyErr: someErr},
|
||||
errExpected: true,
|
||||
client: &stubHSMClient{createOCTKeyErr: someErr},
|
||||
wantErr: true,
|
||||
},
|
||||
"import key successful": {
|
||||
client: &stubHSMClient{},
|
||||
importKey: importKey,
|
||||
},
|
||||
"ImportKey fails": {
|
||||
client: &stubHSMClient{importKeyErr: someErr},
|
||||
importKey: importKey,
|
||||
errExpected: true,
|
||||
client: &stubHSMClient{importKeyErr: someErr},
|
||||
importKey: importKey,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -120,7 +120,7 @@ func TestHSMCreateKEK(t *testing.T) {
|
||||
}
|
||||
|
||||
err := client.CreateKEK(context.Background(), "test-key", tc.importKey)
|
||||
if tc.errExpected {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
} else {
|
||||
assert.NoError(err)
|
||||
@ -138,7 +138,7 @@ func TestHSMGetNewDEK(t *testing.T) {
|
||||
client hsmClientAPI
|
||||
storage kms.Storage
|
||||
cryptoClient *stubCryptoClient
|
||||
errExpected bool
|
||||
wantErr bool
|
||||
}{
|
||||
"successful": {
|
||||
client: &stubHSMClient{keyVersion: keyVersion},
|
||||
@ -149,7 +149,7 @@ func TestHSMGetNewDEK(t *testing.T) {
|
||||
client: &stubHSMClient{keyVersion: keyVersion},
|
||||
cryptoClient: &stubCryptoClient{},
|
||||
storage: &stubStorage{getErr: someErr},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"Put to storage fails": {
|
||||
client: &stubHSMClient{keyVersion: keyVersion},
|
||||
@ -158,25 +158,25 @@ func TestHSMGetNewDEK(t *testing.T) {
|
||||
getErr: storage.ErrDEKUnset,
|
||||
putErr: someErr,
|
||||
},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"GetKey fails": {
|
||||
client: &stubHSMClient{getKeyErr: someErr},
|
||||
cryptoClient: &stubCryptoClient{},
|
||||
storage: storage.NewMemMapStorage(),
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"WrapKey fails": {
|
||||
client: &stubHSMClient{keyVersion: keyVersion},
|
||||
cryptoClient: &stubCryptoClient{wrapKeyErr: someErr},
|
||||
storage: storage.NewMemMapStorage(),
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"creating crypto client fails": {
|
||||
client: &stubHSMClient{keyVersion: keyVersion},
|
||||
cryptoClient: &stubCryptoClient{createErr: someErr},
|
||||
storage: storage.NewMemMapStorage(),
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -192,7 +192,7 @@ func TestHSMGetNewDEK(t *testing.T) {
|
||||
}
|
||||
|
||||
dek, err := client.GetDEK(context.Background(), "test-key", "volume-01", 32)
|
||||
if tc.errExpected {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
} else {
|
||||
assert.Len(dek, 32)
|
||||
@ -210,7 +210,7 @@ func TestHSMGetExistingDEK(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
client hsmClientAPI
|
||||
cryptoClient *stubCryptoClient
|
||||
errExpected bool
|
||||
wantErr bool
|
||||
}{
|
||||
"successful": {
|
||||
client: &stubHSMClient{keyVersion: keyVersion},
|
||||
@ -222,17 +222,17 @@ func TestHSMGetExistingDEK(t *testing.T) {
|
||||
getKeyErr: someErr,
|
||||
},
|
||||
cryptoClient: &stubCryptoClient{},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"UnwrapKey fails": {
|
||||
client: &stubHSMClient{keyVersion: keyVersion},
|
||||
cryptoClient: &stubCryptoClient{unwrapKeyErr: someErr},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"creating crypto client fails": {
|
||||
client: &stubHSMClient{keyVersion: keyVersion},
|
||||
cryptoClient: &stubCryptoClient{createErr: someErr},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -253,7 +253,7 @@ func TestHSMGetExistingDEK(t *testing.T) {
|
||||
}
|
||||
|
||||
dek, err := client.GetDEK(context.Background(), "test-key", keyID, len(testKey))
|
||||
if tc.errExpected {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
} else {
|
||||
assert.Len(dek, len(testKey))
|
||||
@ -266,23 +266,23 @@ func TestHSMGetExistingDEK(t *testing.T) {
|
||||
func TestGetKeyVersion(t *testing.T) {
|
||||
testVersion := "test-key-version"
|
||||
testCases := map[string]struct {
|
||||
client *stubHSMClient
|
||||
errExpected bool
|
||||
client *stubHSMClient
|
||||
wantErr bool
|
||||
}{
|
||||
"valid key version": {
|
||||
client: &stubHSMClient{keyVersion: fmt.Sprintf("https://test.managedhsm.azure.net/keys/test-key/%s", testVersion)},
|
||||
},
|
||||
"GetKey fails": {
|
||||
client: &stubHSMClient{getKeyErr: errors.New("error")},
|
||||
errExpected: true,
|
||||
client: &stubHSMClient{getKeyErr: errors.New("error")},
|
||||
wantErr: true,
|
||||
},
|
||||
"key ID is not an URL": {
|
||||
client: &stubHSMClient{keyVersion: string([]byte{0x0, 0x1, 0x2})},
|
||||
errExpected: true,
|
||||
client: &stubHSMClient{keyVersion: string([]byte{0x0, 0x1, 0x2})},
|
||||
wantErr: true,
|
||||
},
|
||||
"invalid key ID URL": {
|
||||
client: &stubHSMClient{keyVersion: "https://test.managedhsm.azure.net/keys/test-key/test-key-version/another-version/and-another-one"},
|
||||
errExpected: true,
|
||||
client: &stubHSMClient{keyVersion: "https://test.managedhsm.azure.net/keys/test-key/test-key-version/another-version/and-another-one"},
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -293,7 +293,7 @@ func TestGetKeyVersion(t *testing.T) {
|
||||
client := HSMClient{client: tc.client}
|
||||
|
||||
keyVersion, err := client.getKeyVersion(context.Background(), "test")
|
||||
if tc.errExpected {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
} else {
|
||||
assert.NoError(err)
|
||||
|
@ -110,9 +110,9 @@ func TestCreateKEK(t *testing.T) {
|
||||
importKey := []byte("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA")
|
||||
|
||||
testCases := map[string]struct {
|
||||
client *stubGCPClient
|
||||
importKey []byte
|
||||
errExpected bool
|
||||
client *stubGCPClient
|
||||
importKey []byte
|
||||
wantErr bool
|
||||
}{
|
||||
"create new kek successful": {
|
||||
client: &stubGCPClient{},
|
||||
@ -129,8 +129,8 @@ func TestCreateKEK(t *testing.T) {
|
||||
importKey: importKey,
|
||||
},
|
||||
"CreateCryptoKey fails": {
|
||||
client: &stubGCPClient{createCryptoKeyErr: someErr},
|
||||
errExpected: true,
|
||||
client: &stubGCPClient{createCryptoKeyErr: someErr},
|
||||
wantErr: true,
|
||||
},
|
||||
"CreatCryptoKey fails on import": {
|
||||
client: &stubGCPClient{
|
||||
@ -142,13 +142,13 @@ func TestCreateKEK(t *testing.T) {
|
||||
State: kmspb.ImportJob_ACTIVE,
|
||||
},
|
||||
},
|
||||
importKey: importKey,
|
||||
errExpected: true,
|
||||
importKey: importKey,
|
||||
wantErr: true,
|
||||
},
|
||||
"CreateImportJob fails": {
|
||||
client: &stubGCPClient{createImportJobErr: someErr},
|
||||
importKey: importKey,
|
||||
errExpected: true,
|
||||
client: &stubGCPClient{createImportJobErr: someErr},
|
||||
importKey: importKey,
|
||||
wantErr: true,
|
||||
},
|
||||
"ImportCryptoKeyVersion fails": {
|
||||
client: &stubGCPClient{
|
||||
@ -160,8 +160,8 @@ func TestCreateKEK(t *testing.T) {
|
||||
},
|
||||
importCryptoKeyVersionErr: someErr,
|
||||
},
|
||||
importKey: importKey,
|
||||
errExpected: true,
|
||||
importKey: importKey,
|
||||
wantErr: true,
|
||||
},
|
||||
"UpdateCryptoKeyPrimaryVersion fails": {
|
||||
client: &stubGCPClient{
|
||||
@ -173,13 +173,13 @@ func TestCreateKEK(t *testing.T) {
|
||||
},
|
||||
updateCryptoKeyPrimaryVersionErr: someErr,
|
||||
},
|
||||
importKey: importKey,
|
||||
errExpected: true,
|
||||
importKey: importKey,
|
||||
wantErr: true,
|
||||
},
|
||||
"GetImportJob fails during waitBackoff": {
|
||||
client: &stubGCPClient{getImportJobErr: someErr},
|
||||
importKey: importKey,
|
||||
errExpected: true,
|
||||
client: &stubGCPClient{getImportJobErr: someErr},
|
||||
importKey: importKey,
|
||||
wantErr: true,
|
||||
},
|
||||
"GetImportJob returns no key": {
|
||||
client: &stubGCPClient{
|
||||
@ -187,8 +187,8 @@ func TestCreateKEK(t *testing.T) {
|
||||
State: kmspb.ImportJob_ACTIVE,
|
||||
},
|
||||
},
|
||||
importKey: importKey,
|
||||
errExpected: true,
|
||||
importKey: importKey,
|
||||
wantErr: true,
|
||||
},
|
||||
"waitBackoff times out": {
|
||||
client: &stubGCPClient{
|
||||
@ -199,12 +199,12 @@ func TestCreateKEK(t *testing.T) {
|
||||
State: kmspb.ImportJob_PENDING_GENERATION,
|
||||
},
|
||||
},
|
||||
importKey: importKey,
|
||||
errExpected: true,
|
||||
importKey: importKey,
|
||||
wantErr: true,
|
||||
},
|
||||
"creating client fails": {
|
||||
client: &stubGCPClient{createErr: someErr},
|
||||
errExpected: true,
|
||||
client: &stubGCPClient{createErr: someErr},
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -222,7 +222,7 @@ func TestCreateKEK(t *testing.T) {
|
||||
}
|
||||
|
||||
err := client.CreateKEK(context.Background(), "test-key", tc.importKey)
|
||||
if tc.errExpected {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
} else {
|
||||
assert.NoError(err)
|
||||
@ -241,9 +241,9 @@ func TestGetDEK(t *testing.T) {
|
||||
testKey := []byte("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA")
|
||||
|
||||
testCases := map[string]struct {
|
||||
client *stubGCPClient
|
||||
storage kmsInterface.Storage
|
||||
errExpected bool
|
||||
client *stubGCPClient
|
||||
storage kmsInterface.Storage
|
||||
wantErr bool
|
||||
}{
|
||||
"GetDEK successful for new key": {
|
||||
client: &stubGCPClient{},
|
||||
@ -254,19 +254,19 @@ func TestGetDEK(t *testing.T) {
|
||||
storage: &stubStorage{key: testKey},
|
||||
},
|
||||
"Get from storage fails": {
|
||||
client: &stubGCPClient{},
|
||||
storage: &stubStorage{getErr: someErr},
|
||||
errExpected: true,
|
||||
client: &stubGCPClient{},
|
||||
storage: &stubStorage{getErr: someErr},
|
||||
wantErr: true,
|
||||
},
|
||||
"Encrypt fails": {
|
||||
client: &stubGCPClient{encryptErr: someErr},
|
||||
storage: &stubStorage{getErr: storage.ErrDEKUnset},
|
||||
errExpected: true,
|
||||
client: &stubGCPClient{encryptErr: someErr},
|
||||
storage: &stubStorage{getErr: storage.ErrDEKUnset},
|
||||
wantErr: true,
|
||||
},
|
||||
"Encrypt fails with notfound error": {
|
||||
client: &stubGCPClient{encryptErr: status.Error(codes.NotFound, "error")},
|
||||
storage: &stubStorage{getErr: storage.ErrDEKUnset},
|
||||
errExpected: true,
|
||||
client: &stubGCPClient{encryptErr: status.Error(codes.NotFound, "error")},
|
||||
storage: &stubStorage{getErr: storage.ErrDEKUnset},
|
||||
wantErr: true,
|
||||
},
|
||||
"Put to storage fails": {
|
||||
client: &stubGCPClient{},
|
||||
@ -274,22 +274,22 @@ func TestGetDEK(t *testing.T) {
|
||||
getErr: storage.ErrDEKUnset,
|
||||
putErr: someErr,
|
||||
},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"Decrypt fails": {
|
||||
client: &stubGCPClient{decryptErr: someErr},
|
||||
storage: &stubStorage{key: testKey},
|
||||
errExpected: true,
|
||||
client: &stubGCPClient{decryptErr: someErr},
|
||||
storage: &stubStorage{key: testKey},
|
||||
wantErr: true,
|
||||
},
|
||||
"Decrypt fails with notfound error": {
|
||||
client: &stubGCPClient{decryptErr: status.Error(codes.NotFound, "error")},
|
||||
storage: &stubStorage{key: testKey},
|
||||
errExpected: true,
|
||||
client: &stubGCPClient{decryptErr: status.Error(codes.NotFound, "error")},
|
||||
storage: &stubStorage{key: testKey},
|
||||
wantErr: true,
|
||||
},
|
||||
"creating client fails": {
|
||||
client: &stubGCPClient{createErr: someErr},
|
||||
storage: &stubStorage{getErr: storage.ErrDEKUnset},
|
||||
errExpected: true,
|
||||
client: &stubGCPClient{createErr: someErr},
|
||||
storage: &stubStorage{getErr: storage.ErrDEKUnset},
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -308,7 +308,7 @@ func TestGetDEK(t *testing.T) {
|
||||
}
|
||||
|
||||
dek, err := client.GetDEK(context.Background(), "test-key", "volume-01", 32)
|
||||
if tc.errExpected {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
} else {
|
||||
assert.NoError(err)
|
||||
@ -321,19 +321,19 @@ func TestGetDEK(t *testing.T) {
|
||||
func TestConnection(t *testing.T) {
|
||||
someErr := errors.New("error")
|
||||
testCases := map[string]struct {
|
||||
client *stubGCPClient
|
||||
errExpected bool
|
||||
client *stubGCPClient
|
||||
wantErr bool
|
||||
}{
|
||||
"success": {
|
||||
client: &stubGCPClient{},
|
||||
},
|
||||
"newClient fails": {
|
||||
client: &stubGCPClient{createErr: someErr},
|
||||
errExpected: true,
|
||||
client: &stubGCPClient{createErr: someErr},
|
||||
wantErr: true,
|
||||
},
|
||||
"GetKeyRing fails": {
|
||||
client: &stubGCPClient{getKeyRingErr: someErr},
|
||||
errExpected: true,
|
||||
client: &stubGCPClient{getKeyRingErr: someErr},
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -351,7 +351,7 @@ func TestConnection(t *testing.T) {
|
||||
}
|
||||
|
||||
err := client.testConnection(context.Background())
|
||||
if tc.errExpected {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
} else {
|
||||
assert.NoError(err)
|
||||
|
@ -11,10 +11,10 @@ func TestWrapKeyAES(t *testing.T) {
|
||||
|
||||
testKEK := []byte{0xD6, 0x8A, 0xED, 0xF5, 0xDB, 0x89, 0x95, 0x66, 0xA9, 0xFF, 0xD9, 0x31, 0x27, 0x4E, 0x30, 0x2D, 0x21, 0xA9, 0x46, 0x21, 0x16, 0x6C, 0x16, 0x17, 0xD1, 0x96, 0x5D, 0xB2, 0xE9, 0x0E, 0x96, 0xD1}
|
||||
testDEK := []byte{0xCB, 0x6E, 0x4B, 0x05, 0x92, 0x6C, 0xE7, 0x38, 0x0C, 0x46, 0x47, 0x06, 0x83, 0xDE, 0x20, 0xFB, 0x73, 0xAA, 0x87, 0xC1, 0x97, 0xE3, 0x7C, 0xE4, 0xF4, 0x0B, 0x96, 0x8D, 0xC5, 0x88, 0xB6, 0xDF}
|
||||
expectedWrap := []byte{0x14, 0x48, 0xC4, 0xEA, 0x4B, 0x4B, 0xCA, 0xE4, 0x5A, 0xD4, 0xCC, 0xE3, 0xF7, 0xDD, 0xD5, 0x78, 0xA5, 0xA9, 0xEF, 0x9A, 0x93, 0x36, 0x09, 0xD6, 0x23, 0x01, 0xF5, 0x5F, 0xE1, 0x20, 0xDD, 0xFC, 0xBC, 0xF3, 0xA9, 0x67, 0x8B, 0x89, 0x54, 0x96}
|
||||
wantWrap := []byte{0x14, 0x48, 0xC4, 0xEA, 0x4B, 0x4B, 0xCA, 0xE4, 0x5A, 0xD4, 0xCC, 0xE3, 0xF7, 0xDD, 0xD5, 0x78, 0xA5, 0xA9, 0xEF, 0x9A, 0x93, 0x36, 0x09, 0xD6, 0x23, 0x01, 0xF5, 0x5F, 0xE1, 0x20, 0xDD, 0xFC, 0xBC, 0xF3, 0xA9, 0x67, 0x8B, 0x89, 0x54, 0x96}
|
||||
res, err := WrapAES(testDEK, testKEK)
|
||||
assert.NoError(err)
|
||||
assert.Equal(expectedWrap, res)
|
||||
assert.Equal(wantWrap, res)
|
||||
|
||||
// Decrypt the Key
|
||||
res, err = UnwrapAES(res, testKEK)
|
||||
|
@ -43,21 +43,21 @@ func (s *stubAWSS3StorageClient) CreateBucket(ctx context.Context, params *s3.Cr
|
||||
|
||||
func TestAWSS3Get(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
client *stubAWSS3StorageClient
|
||||
unsetError bool
|
||||
errExpected bool
|
||||
client *stubAWSS3StorageClient
|
||||
unsetError bool
|
||||
wantErr bool
|
||||
}{
|
||||
"Get successful": {
|
||||
client: &stubAWSS3StorageClient{getObjectOutputData: []byte("test-data")},
|
||||
},
|
||||
"GetObject fails": {
|
||||
client: &stubAWSS3StorageClient{getObjectErr: errors.New("error")},
|
||||
errExpected: true,
|
||||
client: &stubAWSS3StorageClient{getObjectErr: errors.New("error")},
|
||||
wantErr: true,
|
||||
},
|
||||
"GetObject fails with NoSuchKey": {
|
||||
client: &stubAWSS3StorageClient{getObjectErr: &types.NoSuchKey{}},
|
||||
errExpected: true,
|
||||
unsetError: true,
|
||||
client: &stubAWSS3StorageClient{getObjectErr: &types.NoSuchKey{}},
|
||||
wantErr: true,
|
||||
unsetError: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -70,7 +70,7 @@ func TestAWSS3Get(t *testing.T) {
|
||||
}
|
||||
|
||||
out, err := store.Get(context.Background(), "test-key")
|
||||
if tc.errExpected {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
|
||||
if tc.unsetError {
|
||||
@ -89,15 +89,15 @@ func TestAWSS3Get(t *testing.T) {
|
||||
|
||||
func TestAWSS3Put(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
client *stubAWSS3StorageClient
|
||||
errExpected bool
|
||||
client *stubAWSS3StorageClient
|
||||
wantErr bool
|
||||
}{
|
||||
"Put successful": {
|
||||
client: &stubAWSS3StorageClient{},
|
||||
},
|
||||
"PutObject fails": {
|
||||
client: &stubAWSS3StorageClient{putObjectErr: errors.New("error")},
|
||||
errExpected: true,
|
||||
client: &stubAWSS3StorageClient{putObjectErr: errors.New("error")},
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -112,7 +112,7 @@ func TestAWSS3Put(t *testing.T) {
|
||||
testData := []byte{0x1, 0x2, 0x3}
|
||||
|
||||
err := store.Put(context.Background(), "test-key", testData)
|
||||
if tc.errExpected {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
} else {
|
||||
assert.NoError(err)
|
||||
@ -124,23 +124,23 @@ func TestAWSS3Put(t *testing.T) {
|
||||
|
||||
func TestAWSS3CreateBucket(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
client *stubAWSS3StorageClient
|
||||
errExpected bool
|
||||
client *stubAWSS3StorageClient
|
||||
wantErr bool
|
||||
}{
|
||||
"CreateBucket successful": {
|
||||
client: &stubAWSS3StorageClient{},
|
||||
},
|
||||
"CreateBucket fails": {
|
||||
client: &stubAWSS3StorageClient{createBucketErr: errors.New("error")},
|
||||
errExpected: true,
|
||||
client: &stubAWSS3StorageClient{createBucketErr: errors.New("error")},
|
||||
wantErr: true,
|
||||
},
|
||||
"CreateBucket fails with BucketAlreadyExists": {
|
||||
client: &stubAWSS3StorageClient{createBucketErr: &types.BucketAlreadyExists{}},
|
||||
errExpected: false,
|
||||
client: &stubAWSS3StorageClient{createBucketErr: &types.BucketAlreadyExists{}},
|
||||
wantErr: false,
|
||||
},
|
||||
"CreateBucket fails with BucketAlreadyOwnedByYou": {
|
||||
client: &stubAWSS3StorageClient{createBucketErr: &types.BucketAlreadyOwnedByYou{}},
|
||||
errExpected: false,
|
||||
client: &stubAWSS3StorageClient{createBucketErr: &types.BucketAlreadyOwnedByYou{}},
|
||||
wantErr: false,
|
||||
},
|
||||
}
|
||||
|
||||
@ -153,7 +153,7 @@ func TestAWSS3CreateBucket(t *testing.T) {
|
||||
}
|
||||
|
||||
err := store.createBucket(context.Background(), "test-bucket", "test-region")
|
||||
if tc.errExpected {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
} else {
|
||||
assert.NoError(err)
|
||||
|
@ -59,9 +59,9 @@ func TestAzureGet(t *testing.T) {
|
||||
someErr := errors.New("error")
|
||||
|
||||
testCases := map[string]struct {
|
||||
client stubAzureContainerAPI
|
||||
unsetError bool
|
||||
errExpected bool
|
||||
client stubAzureContainerAPI
|
||||
unsetError bool
|
||||
wantErr bool
|
||||
}{
|
||||
"success": {
|
||||
client: stubAzureContainerAPI{
|
||||
@ -69,14 +69,14 @@ func TestAzureGet(t *testing.T) {
|
||||
},
|
||||
},
|
||||
"creating client fails": {
|
||||
client: stubAzureContainerAPI{newClientErr: someErr},
|
||||
errExpected: true,
|
||||
client: stubAzureContainerAPI{newClientErr: someErr},
|
||||
wantErr: true,
|
||||
},
|
||||
"DownloadBlobToBuffer fails": {
|
||||
client: stubAzureContainerAPI{
|
||||
blockBlobAPI: stubAzureBlockBlobAPI{downloadBlobToWriterAtErr: someErr},
|
||||
},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"BlobNotFound error": {
|
||||
client: stubAzureContainerAPI{
|
||||
@ -86,8 +86,8 @@ func TestAzureGet(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
unsetError: true,
|
||||
errExpected: true,
|
||||
unsetError: true,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -103,7 +103,7 @@ func TestAzureGet(t *testing.T) {
|
||||
}
|
||||
|
||||
out, err := client.Get(context.Background(), "test-key")
|
||||
if tc.errExpected {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
|
||||
if tc.unsetError {
|
||||
@ -124,21 +124,21 @@ func TestAzurePut(t *testing.T) {
|
||||
someErr := errors.New("error")
|
||||
|
||||
testCases := map[string]struct {
|
||||
client stubAzureContainerAPI
|
||||
errExpected bool
|
||||
client stubAzureContainerAPI
|
||||
wantErr bool
|
||||
}{
|
||||
"success": {
|
||||
client: stubAzureContainerAPI{},
|
||||
},
|
||||
"creating client fails": {
|
||||
client: stubAzureContainerAPI{newClientErr: someErr},
|
||||
errExpected: true,
|
||||
client: stubAzureContainerAPI{newClientErr: someErr},
|
||||
wantErr: true,
|
||||
},
|
||||
"Upload fails": {
|
||||
client: stubAzureContainerAPI{
|
||||
blockBlobAPI: stubAzureBlockBlobAPI{uploadErr: someErr},
|
||||
},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -157,7 +157,7 @@ func TestAzurePut(t *testing.T) {
|
||||
}
|
||||
|
||||
err := client.Put(context.Background(), "test-key", testData)
|
||||
if tc.errExpected {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
} else {
|
||||
assert.NoError(err)
|
||||
@ -170,8 +170,8 @@ func TestAzurePut(t *testing.T) {
|
||||
func TestCreateContainerOrContinue(t *testing.T) {
|
||||
someErr := errors.New("error")
|
||||
testCases := map[string]struct {
|
||||
client stubAzureContainerAPI
|
||||
errExpected bool
|
||||
client stubAzureContainerAPI
|
||||
wantErr bool
|
||||
}{
|
||||
"success": {
|
||||
client: stubAzureContainerAPI{},
|
||||
@ -180,12 +180,12 @@ func TestCreateContainerOrContinue(t *testing.T) {
|
||||
client: stubAzureContainerAPI{createErr: &azblob.StorageError{ErrorCode: azblob.StorageErrorCodeContainerAlreadyExists}},
|
||||
},
|
||||
"creating client fails": {
|
||||
client: stubAzureContainerAPI{newClientErr: someErr},
|
||||
errExpected: true,
|
||||
client: stubAzureContainerAPI{newClientErr: someErr},
|
||||
wantErr: true,
|
||||
},
|
||||
"Create fails": {
|
||||
client: stubAzureContainerAPI{createErr: someErr},
|
||||
errExpected: true,
|
||||
client: stubAzureContainerAPI{createErr: someErr},
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -202,7 +202,7 @@ func TestCreateContainerOrContinue(t *testing.T) {
|
||||
}
|
||||
|
||||
err := client.createContainerOrContinue(context.Background())
|
||||
if tc.errExpected {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
} else {
|
||||
assert.NoError(err)
|
||||
|
@ -66,25 +66,25 @@ func TestGCPGet(t *testing.T) {
|
||||
someErr := errors.New("error")
|
||||
|
||||
testCases := map[string]struct {
|
||||
client *stubGCPStorageAPI
|
||||
unsetError bool
|
||||
errExpected bool
|
||||
client *stubGCPStorageAPI
|
||||
unsetError bool
|
||||
wantErr bool
|
||||
}{
|
||||
"success": {
|
||||
client: &stubGCPStorageAPI{newReaderOutput: []byte("test-data")},
|
||||
},
|
||||
"creating client fails": {
|
||||
client: &stubGCPStorageAPI{newClientErr: someErr},
|
||||
errExpected: true,
|
||||
client: &stubGCPStorageAPI{newClientErr: someErr},
|
||||
wantErr: true,
|
||||
},
|
||||
"NewReader fails": {
|
||||
client: &stubGCPStorageAPI{newReaderErr: someErr},
|
||||
errExpected: true,
|
||||
client: &stubGCPStorageAPI{newReaderErr: someErr},
|
||||
wantErr: true,
|
||||
},
|
||||
"ErrObjectNotExist error": {
|
||||
client: &stubGCPStorageAPI{newReaderErr: storage.ErrObjectNotExist},
|
||||
unsetError: true,
|
||||
errExpected: true,
|
||||
client: &stubGCPStorageAPI{newReaderErr: storage.ErrObjectNotExist},
|
||||
unsetError: true,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -99,7 +99,7 @@ func TestGCPGet(t *testing.T) {
|
||||
}
|
||||
|
||||
out, err := client.Get(context.Background(), "test-key")
|
||||
if tc.errExpected {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
|
||||
if tc.unsetError {
|
||||
@ -119,9 +119,9 @@ func TestGCPGet(t *testing.T) {
|
||||
func TestGCPPut(t *testing.T) {
|
||||
someErr := errors.New("error")
|
||||
testCases := map[string]struct {
|
||||
client *stubGCPStorageAPI
|
||||
unsetError bool
|
||||
errExpected bool
|
||||
client *stubGCPStorageAPI
|
||||
unsetError bool
|
||||
wantErr bool
|
||||
}{
|
||||
"success": {
|
||||
client: &stubGCPStorageAPI{
|
||||
@ -131,8 +131,8 @@ func TestGCPPut(t *testing.T) {
|
||||
},
|
||||
},
|
||||
"creating client fails": {
|
||||
client: &stubGCPStorageAPI{newClientErr: someErr},
|
||||
errExpected: true,
|
||||
client: &stubGCPStorageAPI{newClientErr: someErr},
|
||||
wantErr: true,
|
||||
},
|
||||
"NewWriter fails": {
|
||||
client: &stubGCPStorageAPI{
|
||||
@ -141,7 +141,7 @@ func TestGCPPut(t *testing.T) {
|
||||
writeErr: someErr,
|
||||
},
|
||||
},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -157,7 +157,7 @@ func TestGCPPut(t *testing.T) {
|
||||
testData := []byte{0x1, 0x2, 0x3}
|
||||
|
||||
err := client.Put(context.Background(), "test-key", testData)
|
||||
if tc.errExpected {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
} else {
|
||||
assert.NoError(err)
|
||||
@ -172,7 +172,7 @@ func TestGCPCreateContainerOrContinue(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
client *stubGCPStorageAPI
|
||||
createNewBucket bool
|
||||
errExpected bool
|
||||
wantErr bool
|
||||
}{
|
||||
"success": {
|
||||
client: &stubGCPStorageAPI{},
|
||||
@ -182,19 +182,19 @@ func TestGCPCreateContainerOrContinue(t *testing.T) {
|
||||
createNewBucket: true,
|
||||
},
|
||||
"creating client fails": {
|
||||
client: &stubGCPStorageAPI{newClientErr: someErr},
|
||||
errExpected: true,
|
||||
client: &stubGCPStorageAPI{newClientErr: someErr},
|
||||
wantErr: true,
|
||||
},
|
||||
"Attrs fails": {
|
||||
client: &stubGCPStorageAPI{attrsErr: someErr},
|
||||
errExpected: true,
|
||||
client: &stubGCPStorageAPI{attrsErr: someErr},
|
||||
wantErr: true,
|
||||
},
|
||||
"CreateBucket fails": {
|
||||
client: &stubGCPStorageAPI{
|
||||
attrsErr: storage.ErrBucketNotExist,
|
||||
createBucketErr: someErr,
|
||||
},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -209,7 +209,7 @@ func TestGCPCreateContainerOrContinue(t *testing.T) {
|
||||
}
|
||||
|
||||
err := client.createContainerOrContinue(context.Background(), nil)
|
||||
if tc.errExpected {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
} else {
|
||||
assert.NoError(err)
|
||||
|
@ -60,24 +60,24 @@ func (c *stubCryptDevice) Wipe(devicePath string, pattern int, offset, length ui
|
||||
|
||||
func TestCloseCryptDevice(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
mapper *stubCryptDevice
|
||||
errExpected bool
|
||||
mapper *stubCryptDevice
|
||||
wantErr bool
|
||||
}{
|
||||
"success": {
|
||||
mapper: &stubCryptDevice{},
|
||||
errExpected: false,
|
||||
mapper: &stubCryptDevice{},
|
||||
wantErr: false,
|
||||
},
|
||||
"error on Init": {
|
||||
mapper: &stubCryptDevice{
|
||||
initErr: errors.New("error"),
|
||||
},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"error on Deactivate": {
|
||||
mapper: &stubCryptDevice{
|
||||
deactivateErr: errors.New("error"),
|
||||
},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -86,7 +86,7 @@ func TestCloseCryptDevice(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
err := closeCryptDevice(tc.mapper, "/dev/some-device", "volume0", "test")
|
||||
if tc.errExpected {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
} else {
|
||||
assert.NoError(err)
|
||||
@ -103,113 +103,113 @@ func TestOpenCryptDevice(t *testing.T) {
|
||||
someErr := errors.New("error")
|
||||
|
||||
testCases := map[string]struct {
|
||||
source string
|
||||
volumeID string
|
||||
dek string
|
||||
integrity bool
|
||||
mapper *stubCryptDevice
|
||||
diskInfo func(disk string) (string, error)
|
||||
errExpected bool
|
||||
source string
|
||||
volumeID string
|
||||
dek string
|
||||
integrity bool
|
||||
mapper *stubCryptDevice
|
||||
diskInfo func(disk string) (string, error)
|
||||
wantErr bool
|
||||
}{
|
||||
"success with Load": {
|
||||
source: "/dev/some-device",
|
||||
volumeID: "volume0",
|
||||
dek: string(testDEK),
|
||||
mapper: &stubCryptDevice{},
|
||||
diskInfo: func(disk string) (string, error) { return "", nil },
|
||||
errExpected: false,
|
||||
source: "/dev/some-device",
|
||||
volumeID: "volume0",
|
||||
dek: string(testDEK),
|
||||
mapper: &stubCryptDevice{},
|
||||
diskInfo: func(disk string) (string, error) { return "", nil },
|
||||
wantErr: false,
|
||||
},
|
||||
"success with error on Load": {
|
||||
source: "/dev/some-device",
|
||||
volumeID: "volume0",
|
||||
dek: string(testDEK),
|
||||
mapper: &stubCryptDevice{loadErr: someErr},
|
||||
diskInfo: func(disk string) (string, error) { return "", nil },
|
||||
errExpected: false,
|
||||
source: "/dev/some-device",
|
||||
volumeID: "volume0",
|
||||
dek: string(testDEK),
|
||||
mapper: &stubCryptDevice{loadErr: someErr},
|
||||
diskInfo: func(disk string) (string, error) { return "", nil },
|
||||
wantErr: false,
|
||||
},
|
||||
"success with integrity": {
|
||||
source: "/dev/some-device",
|
||||
volumeID: "volume0",
|
||||
dek: string(append(testDEK, testDEK[:32]...)),
|
||||
integrity: true,
|
||||
mapper: &stubCryptDevice{loadErr: someErr},
|
||||
diskInfo: func(disk string) (string, error) { return "", nil },
|
||||
errExpected: false,
|
||||
source: "/dev/some-device",
|
||||
volumeID: "volume0",
|
||||
dek: string(append(testDEK, testDEK[:32]...)),
|
||||
integrity: true,
|
||||
mapper: &stubCryptDevice{loadErr: someErr},
|
||||
diskInfo: func(disk string) (string, error) { return "", nil },
|
||||
wantErr: false,
|
||||
},
|
||||
"incorrect key size": {
|
||||
source: "/dev/some-device",
|
||||
volumeID: "volume0",
|
||||
dek: "short",
|
||||
mapper: &stubCryptDevice{},
|
||||
diskInfo: func(disk string) (string, error) { return "", nil },
|
||||
errExpected: true,
|
||||
source: "/dev/some-device",
|
||||
volumeID: "volume0",
|
||||
dek: "short",
|
||||
mapper: &stubCryptDevice{},
|
||||
diskInfo: func(disk string) (string, error) { return "", nil },
|
||||
wantErr: true,
|
||||
},
|
||||
"error on Init": {
|
||||
source: "/dev/some-device",
|
||||
volumeID: "volume0",
|
||||
dek: string(testDEK),
|
||||
mapper: &stubCryptDevice{initErr: someErr},
|
||||
diskInfo: func(disk string) (string, error) { return "", nil },
|
||||
errExpected: true,
|
||||
source: "/dev/some-device",
|
||||
volumeID: "volume0",
|
||||
dek: string(testDEK),
|
||||
mapper: &stubCryptDevice{initErr: someErr},
|
||||
diskInfo: func(disk string) (string, error) { return "", nil },
|
||||
wantErr: true,
|
||||
},
|
||||
"error on Format": {
|
||||
source: "/dev/some-device",
|
||||
volumeID: "volume0",
|
||||
dek: string(testDEK),
|
||||
mapper: &stubCryptDevice{loadErr: someErr, formatErr: someErr},
|
||||
diskInfo: func(disk string) (string, error) { return "", nil },
|
||||
errExpected: true,
|
||||
source: "/dev/some-device",
|
||||
volumeID: "volume0",
|
||||
dek: string(testDEK),
|
||||
mapper: &stubCryptDevice{loadErr: someErr, formatErr: someErr},
|
||||
diskInfo: func(disk string) (string, error) { return "", nil },
|
||||
wantErr: true,
|
||||
},
|
||||
"error on Activate": {
|
||||
source: "/dev/some-device",
|
||||
volumeID: "volume0",
|
||||
dek: string(testDEK),
|
||||
mapper: &stubCryptDevice{activateErr: someErr},
|
||||
diskInfo: func(disk string) (string, error) { return "", nil },
|
||||
errExpected: true,
|
||||
source: "/dev/some-device",
|
||||
volumeID: "volume0",
|
||||
dek: string(testDEK),
|
||||
mapper: &stubCryptDevice{activateErr: someErr},
|
||||
diskInfo: func(disk string) (string, error) { return "", nil },
|
||||
wantErr: true,
|
||||
},
|
||||
"error on diskInfo": {
|
||||
source: "/dev/some-device",
|
||||
volumeID: "volume0",
|
||||
dek: string(testDEK),
|
||||
mapper: &stubCryptDevice{loadErr: someErr},
|
||||
diskInfo: func(disk string) (string, error) { return "", someErr },
|
||||
errExpected: true,
|
||||
source: "/dev/some-device",
|
||||
volumeID: "volume0",
|
||||
dek: string(testDEK),
|
||||
mapper: &stubCryptDevice{loadErr: someErr},
|
||||
diskInfo: func(disk string) (string, error) { return "", someErr },
|
||||
wantErr: true,
|
||||
},
|
||||
"disk is already formatted": {
|
||||
source: "/dev/some-device",
|
||||
volumeID: "volume0",
|
||||
dek: string(testDEK),
|
||||
mapper: &stubCryptDevice{loadErr: someErr},
|
||||
diskInfo: func(disk string) (string, error) { return "ext4", nil },
|
||||
errExpected: true,
|
||||
source: "/dev/some-device",
|
||||
volumeID: "volume0",
|
||||
dek: string(testDEK),
|
||||
mapper: &stubCryptDevice{loadErr: someErr},
|
||||
diskInfo: func(disk string) (string, error) { return "ext4", nil },
|
||||
wantErr: true,
|
||||
},
|
||||
"error with integrity on wipe": {
|
||||
source: "/dev/some-device",
|
||||
volumeID: "volume0",
|
||||
dek: string(append(testDEK, testDEK[:32]...)),
|
||||
integrity: true,
|
||||
mapper: &stubCryptDevice{loadErr: someErr, wipeErr: someErr},
|
||||
diskInfo: func(disk string) (string, error) { return "", nil },
|
||||
errExpected: true,
|
||||
source: "/dev/some-device",
|
||||
volumeID: "volume0",
|
||||
dek: string(append(testDEK, testDEK[:32]...)),
|
||||
integrity: true,
|
||||
mapper: &stubCryptDevice{loadErr: someErr, wipeErr: someErr},
|
||||
diskInfo: func(disk string) (string, error) { return "", nil },
|
||||
wantErr: true,
|
||||
},
|
||||
"error with integrity on activate": {
|
||||
source: "/dev/some-device",
|
||||
volumeID: "volume0",
|
||||
dek: string(append(testDEK, testDEK[:32]...)),
|
||||
integrity: true,
|
||||
mapper: &stubCryptDevice{loadErr: someErr, activateErr: someErr},
|
||||
diskInfo: func(disk string) (string, error) { return "", nil },
|
||||
errExpected: true,
|
||||
source: "/dev/some-device",
|
||||
volumeID: "volume0",
|
||||
dek: string(append(testDEK, testDEK[:32]...)),
|
||||
integrity: true,
|
||||
mapper: &stubCryptDevice{loadErr: someErr, activateErr: someErr},
|
||||
diskInfo: func(disk string) (string, error) { return "", nil },
|
||||
wantErr: true,
|
||||
},
|
||||
"error with integrity on deactivate": {
|
||||
source: "/dev/some-device",
|
||||
volumeID: "volume0",
|
||||
dek: string(append(testDEK, testDEK[:32]...)),
|
||||
integrity: true,
|
||||
mapper: &stubCryptDevice{loadErr: someErr, deactivateErr: someErr},
|
||||
diskInfo: func(disk string) (string, error) { return "", nil },
|
||||
errExpected: true,
|
||||
source: "/dev/some-device",
|
||||
volumeID: "volume0",
|
||||
dek: string(append(testDEK, testDEK[:32]...)),
|
||||
integrity: true,
|
||||
mapper: &stubCryptDevice{loadErr: someErr, deactivateErr: someErr},
|
||||
diskInfo: func(disk string) (string, error) { return "", nil },
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -218,7 +218,7 @@ func TestOpenCryptDevice(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
out, err := openCryptDevice(tc.mapper, tc.source, tc.volumeID, tc.dek, tc.integrity, tc.diskInfo)
|
||||
if tc.errExpected {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
} else {
|
||||
assert.NoError(err)
|
||||
|
@ -22,16 +22,16 @@ func (c *stubVPNClient) GetDataKey(context.Context, *vpnproto.GetDataKeyRequest,
|
||||
|
||||
func TestConstellationKMS(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
vpn *stubVPNClient
|
||||
errExpected bool
|
||||
vpn *stubVPNClient
|
||||
wantErr bool
|
||||
}{
|
||||
"GetDataKey success": {
|
||||
vpn: &stubVPNClient{dataKey: []byte{0x1, 0x2, 0x3}},
|
||||
errExpected: false,
|
||||
vpn: &stubVPNClient{dataKey: []byte{0x1, 0x2, 0x3}},
|
||||
wantErr: false,
|
||||
},
|
||||
"GetDataKey error": {
|
||||
vpn: &stubVPNClient{getDataKeyErr: errors.New("error")},
|
||||
errExpected: true,
|
||||
vpn: &stubVPNClient{getDataKeyErr: errors.New("error")},
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -48,7 +48,7 @@ func TestConstellationKMS(t *testing.T) {
|
||||
}
|
||||
res, err := kms.GetDEK(context.Background(), "data-key", 64)
|
||||
|
||||
if tc.errExpected {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
assert.Nil(res)
|
||||
} else {
|
||||
|
@ -29,7 +29,7 @@ func TestRequestKeyLoop(t *testing.T) {
|
||||
|
||||
testCases := map[string]struct {
|
||||
server *stubAPIServer
|
||||
expectedCalls int
|
||||
wantCalls int
|
||||
listResponse []core.Instance
|
||||
dontStartServer bool
|
||||
}{
|
||||
@ -111,9 +111,9 @@ func TestRequestKeyLoop(t *testing.T) {
|
||||
|
||||
func TestPushStateDiskKey(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
testAPI *KeyAPI
|
||||
request *keyproto.PushStateDiskKeyRequest
|
||||
errExpected bool
|
||||
testAPI *KeyAPI
|
||||
request *keyproto.PushStateDiskKeyRequest
|
||||
wantErr bool
|
||||
}{
|
||||
"success": {
|
||||
testAPI: &KeyAPI{keyReceived: make(chan struct{}, 1)},
|
||||
@ -124,13 +124,13 @@ func TestPushStateDiskKey(t *testing.T) {
|
||||
keyReceived: make(chan struct{}, 1),
|
||||
key: []byte("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"),
|
||||
},
|
||||
request: &keyproto.PushStateDiskKeyRequest{StateDiskKey: []byte("BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB")},
|
||||
errExpected: true,
|
||||
request: &keyproto.PushStateDiskKeyRequest{StateDiskKey: []byte("BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB")},
|
||||
wantErr: true,
|
||||
},
|
||||
"incorrect size of pushed key": {
|
||||
testAPI: &KeyAPI{keyReceived: make(chan struct{}, 1)},
|
||||
request: &keyproto.PushStateDiskKeyRequest{StateDiskKey: []byte("AAAAAAAAAAAAAAAA")},
|
||||
errExpected: true,
|
||||
testAPI: &KeyAPI{keyReceived: make(chan struct{}, 1)},
|
||||
request: &keyproto.PushStateDiskKeyRequest{StateDiskKey: []byte("AAAAAAAAAAAAAAAA")},
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -139,7 +139,7 @@ func TestPushStateDiskKey(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
_, err := tc.testAPI.PushStateDiskKey(context.Background(), tc.request)
|
||||
if tc.errExpected {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
} else {
|
||||
assert.NoError(err)
|
||||
|
@ -26,7 +26,7 @@ func TestPrepareExistingDisk(t *testing.T) {
|
||||
mounter *stubMounter
|
||||
openTPM vtpm.TPMOpenFunc
|
||||
missingState bool
|
||||
errExpected bool
|
||||
wantErr bool
|
||||
}{
|
||||
"success": {
|
||||
fs: afero.Afero{Fs: afero.NewMemMapFs()},
|
||||
@ -36,12 +36,12 @@ func TestPrepareExistingDisk(t *testing.T) {
|
||||
openTPM: vtpm.OpenNOPTPM,
|
||||
},
|
||||
"WaitForDecryptionKey fails": {
|
||||
fs: afero.Afero{Fs: afero.NewMemMapFs()},
|
||||
keyWaiter: &stubKeyWaiter{waitErr: someErr},
|
||||
mapper: &stubMapper{uuid: "test"},
|
||||
mounter: &stubMounter{},
|
||||
openTPM: vtpm.OpenNOPTPM,
|
||||
errExpected: true,
|
||||
fs: afero.Afero{Fs: afero.NewMemMapFs()},
|
||||
keyWaiter: &stubKeyWaiter{waitErr: someErr},
|
||||
mapper: &stubMapper{uuid: "test"},
|
||||
mounter: &stubMounter{},
|
||||
openTPM: vtpm.OpenNOPTPM,
|
||||
wantErr: true,
|
||||
},
|
||||
"MapDisk fails causes a repeat": {
|
||||
fs: afero.Afero{Fs: afero.NewMemMapFs()},
|
||||
@ -51,41 +51,41 @@ func TestPrepareExistingDisk(t *testing.T) {
|
||||
mapDiskErr: someErr,
|
||||
mapDiskRepeatedCalls: 2,
|
||||
},
|
||||
mounter: &stubMounter{},
|
||||
openTPM: vtpm.OpenNOPTPM,
|
||||
errExpected: false,
|
||||
mounter: &stubMounter{},
|
||||
openTPM: vtpm.OpenNOPTPM,
|
||||
wantErr: false,
|
||||
},
|
||||
"MkdirAll fails": {
|
||||
fs: afero.Afero{Fs: afero.NewMemMapFs()},
|
||||
keyWaiter: &stubKeyWaiter{},
|
||||
mapper: &stubMapper{uuid: "test"},
|
||||
mounter: &stubMounter{mkdirAllErr: someErr},
|
||||
openTPM: vtpm.OpenNOPTPM,
|
||||
errExpected: true,
|
||||
fs: afero.Afero{Fs: afero.NewMemMapFs()},
|
||||
keyWaiter: &stubKeyWaiter{},
|
||||
mapper: &stubMapper{uuid: "test"},
|
||||
mounter: &stubMounter{mkdirAllErr: someErr},
|
||||
openTPM: vtpm.OpenNOPTPM,
|
||||
wantErr: true,
|
||||
},
|
||||
"Mount fails": {
|
||||
fs: afero.Afero{Fs: afero.NewMemMapFs()},
|
||||
keyWaiter: &stubKeyWaiter{},
|
||||
mapper: &stubMapper{uuid: "test"},
|
||||
mounter: &stubMounter{mountErr: someErr},
|
||||
openTPM: vtpm.OpenNOPTPM,
|
||||
errExpected: true,
|
||||
fs: afero.Afero{Fs: afero.NewMemMapFs()},
|
||||
keyWaiter: &stubKeyWaiter{},
|
||||
mapper: &stubMapper{uuid: "test"},
|
||||
mounter: &stubMounter{mountErr: someErr},
|
||||
openTPM: vtpm.OpenNOPTPM,
|
||||
wantErr: true,
|
||||
},
|
||||
"Unmount fails": {
|
||||
fs: afero.Afero{Fs: afero.NewMemMapFs()},
|
||||
keyWaiter: &stubKeyWaiter{},
|
||||
mapper: &stubMapper{uuid: "test"},
|
||||
mounter: &stubMounter{unmountErr: someErr},
|
||||
openTPM: vtpm.OpenNOPTPM,
|
||||
errExpected: true,
|
||||
fs: afero.Afero{Fs: afero.NewMemMapFs()},
|
||||
keyWaiter: &stubKeyWaiter{},
|
||||
mapper: &stubMapper{uuid: "test"},
|
||||
mounter: &stubMounter{unmountErr: someErr},
|
||||
openTPM: vtpm.OpenNOPTPM,
|
||||
wantErr: true,
|
||||
},
|
||||
"MarkNodeAsInitialized fails": {
|
||||
fs: afero.Afero{Fs: afero.NewMemMapFs()},
|
||||
keyWaiter: &stubKeyWaiter{},
|
||||
mapper: &stubMapper{uuid: "test"},
|
||||
mounter: &stubMounter{unmountErr: someErr},
|
||||
openTPM: failOpener,
|
||||
errExpected: true,
|
||||
fs: afero.Afero{Fs: afero.NewMemMapFs()},
|
||||
keyWaiter: &stubKeyWaiter{},
|
||||
mapper: &stubMapper{uuid: "test"},
|
||||
mounter: &stubMounter{unmountErr: someErr},
|
||||
openTPM: failOpener,
|
||||
wantErr: true,
|
||||
},
|
||||
"no state file": {
|
||||
fs: afero.Afero{Fs: afero.NewMemMapFs()},
|
||||
@ -94,7 +94,7 @@ func TestPrepareExistingDisk(t *testing.T) {
|
||||
mounter: &stubMounter{},
|
||||
openTPM: vtpm.OpenNOPTPM,
|
||||
missingState: true,
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -110,7 +110,7 @@ func TestPrepareExistingDisk(t *testing.T) {
|
||||
setupManager := New("test", tc.fs, tc.keyWaiter, tc.mapper, tc.mounter, tc.openTPM)
|
||||
|
||||
err := setupManager.PrepareExistingDisk()
|
||||
if tc.errExpected {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
} else {
|
||||
assert.NoError(err)
|
||||
@ -131,18 +131,18 @@ func failOpener() (io.ReadWriteCloser, error) {
|
||||
func TestPrepareNewDisk(t *testing.T) {
|
||||
someErr := errors.New("error")
|
||||
testCases := map[string]struct {
|
||||
fs afero.Afero
|
||||
mapper *stubMapper
|
||||
errExpected bool
|
||||
fs afero.Afero
|
||||
mapper *stubMapper
|
||||
wantErr bool
|
||||
}{
|
||||
"success": {
|
||||
fs: afero.Afero{Fs: afero.NewMemMapFs()},
|
||||
mapper: &stubMapper{uuid: "test"},
|
||||
},
|
||||
"creating directory fails": {
|
||||
fs: afero.Afero{Fs: afero.NewReadOnlyFs(afero.NewMemMapFs())},
|
||||
mapper: &stubMapper{},
|
||||
errExpected: true,
|
||||
fs: afero.Afero{Fs: afero.NewReadOnlyFs(afero.NewMemMapFs())},
|
||||
mapper: &stubMapper{},
|
||||
wantErr: true,
|
||||
},
|
||||
"FormatDisk fails": {
|
||||
fs: afero.Afero{Fs: afero.NewMemMapFs()},
|
||||
@ -150,7 +150,7 @@ func TestPrepareNewDisk(t *testing.T) {
|
||||
uuid: "test",
|
||||
formatDiskErr: someErr,
|
||||
},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"MapDisk fails": {
|
||||
fs: afero.Afero{Fs: afero.NewMemMapFs()},
|
||||
@ -159,7 +159,7 @@ func TestPrepareNewDisk(t *testing.T) {
|
||||
mapDiskErr: someErr,
|
||||
mapDiskRepeatedCalls: 1,
|
||||
},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -170,7 +170,7 @@ func TestPrepareNewDisk(t *testing.T) {
|
||||
setupManager := New("test", tc.fs, nil, tc.mapper, nil, nil)
|
||||
|
||||
err := setupManager.PrepareNewDisk()
|
||||
if tc.errExpected {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
} else {
|
||||
assert.NoError(err)
|
||||
@ -187,11 +187,11 @@ func TestPrepareNewDisk(t *testing.T) {
|
||||
|
||||
func TestReadInitSecrets(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
fs afero.Afero
|
||||
ownerID string
|
||||
clusterID string
|
||||
writeFile bool
|
||||
errExpected bool
|
||||
fs afero.Afero
|
||||
ownerID string
|
||||
clusterID string
|
||||
writeFile bool
|
||||
wantErr bool
|
||||
}{
|
||||
"success": {
|
||||
fs: afero.Afero{Fs: afero.NewMemMapFs()},
|
||||
@ -200,25 +200,25 @@ func TestReadInitSecrets(t *testing.T) {
|
||||
writeFile: true,
|
||||
},
|
||||
"no state file": {
|
||||
fs: afero.Afero{Fs: afero.NewMemMapFs()},
|
||||
errExpected: true,
|
||||
fs: afero.Afero{Fs: afero.NewMemMapFs()},
|
||||
wantErr: true,
|
||||
},
|
||||
"missing ownerID": {
|
||||
fs: afero.Afero{Fs: afero.NewMemMapFs()},
|
||||
clusterID: "clusterID",
|
||||
writeFile: true,
|
||||
errExpected: true,
|
||||
fs: afero.Afero{Fs: afero.NewMemMapFs()},
|
||||
clusterID: "clusterID",
|
||||
writeFile: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"missing clusterID": {
|
||||
fs: afero.Afero{Fs: afero.NewMemMapFs()},
|
||||
ownerID: "ownerID",
|
||||
writeFile: true,
|
||||
errExpected: true,
|
||||
fs: afero.Afero{Fs: afero.NewMemMapFs()},
|
||||
ownerID: "ownerID",
|
||||
writeFile: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"no IDs": {
|
||||
fs: afero.Afero{Fs: afero.NewMemMapFs()},
|
||||
writeFile: true,
|
||||
errExpected: true,
|
||||
fs: afero.Afero{Fs: afero.NewMemMapFs()},
|
||||
writeFile: true,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@ -236,7 +236,7 @@ func TestReadInitSecrets(t *testing.T) {
|
||||
setupManager := New("test", tc.fs, nil, nil, nil, nil)
|
||||
|
||||
ownerID, clusterID, err := setupManager.readInitSecrets("/tmp/test-state.json")
|
||||
if tc.errExpected {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
} else {
|
||||
assert.NoError(err)
|
||||
|
@ -24,18 +24,18 @@ import (
|
||||
|
||||
func TestGetVerifyPeerCertificateFunc(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
rawCerts [][]byte
|
||||
errExpected bool
|
||||
rawCerts [][]byte
|
||||
wantErr bool
|
||||
}{
|
||||
"no certificates": {
|
||||
rawCerts: nil,
|
||||
errExpected: true,
|
||||
rawCerts: nil,
|
||||
wantErr: true,
|
||||
},
|
||||
"invalid certificate": {
|
||||
rawCerts: [][]byte{
|
||||
{0x1, 0x2, 0x3},
|
||||
},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"no extension": {
|
||||
rawCerts: [][]byte{
|
||||
@ -43,7 +43,7 @@ func TestGetVerifyPeerCertificateFunc(t *testing.T) {
|
||||
SerialNumber: big.NewInt(123),
|
||||
}),
|
||||
},
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"certificate with attestation": {
|
||||
rawCerts: [][]byte{
|
||||
@ -58,7 +58,7 @@ func TestGetVerifyPeerCertificateFunc(t *testing.T) {
|
||||
},
|
||||
}),
|
||||
},
|
||||
errExpected: false,
|
||||
wantErr: false,
|
||||
},
|
||||
}
|
||||
|
||||
@ -71,7 +71,7 @@ func TestGetVerifyPeerCertificateFunc(t *testing.T) {
|
||||
verify := getVerifyPeerCertificateFunc(attDoc)
|
||||
|
||||
err := verify(tc.rawCerts, nil)
|
||||
if tc.errExpected {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
} else {
|
||||
require.NoError(err)
|
||||
@ -96,9 +96,9 @@ func mustGenerateTestCert(t *testing.T, template *x509.Certificate) []byte {
|
||||
|
||||
func TestExportToFile(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
pcrs map[uint32][]byte
|
||||
fs *afero.Afero
|
||||
errExpected bool
|
||||
pcrs map[uint32][]byte
|
||||
fs *afero.Afero
|
||||
wantErr bool
|
||||
}{
|
||||
"file not writeable": {
|
||||
pcrs: map[uint32][]byte{
|
||||
@ -106,8 +106,8 @@ func TestExportToFile(t *testing.T) {
|
||||
1: {0x1, 0x2, 0x3},
|
||||
2: {0x1, 0x2, 0x3},
|
||||
},
|
||||
fs: &afero.Afero{Fs: afero.NewReadOnlyFs(afero.NewMemMapFs())},
|
||||
errExpected: true,
|
||||
fs: &afero.Afero{Fs: afero.NewReadOnlyFs(afero.NewMemMapFs())},
|
||||
wantErr: true,
|
||||
},
|
||||
"file writeable": {
|
||||
pcrs: map[uint32][]byte{
|
||||
@ -115,8 +115,8 @@ func TestExportToFile(t *testing.T) {
|
||||
1: {0x1, 0x2, 0x3},
|
||||
2: {0x1, 0x2, 0x3},
|
||||
},
|
||||
fs: &afero.Afero{Fs: afero.NewMemMapFs()},
|
||||
errExpected: false,
|
||||
fs: &afero.Afero{Fs: afero.NewMemMapFs()},
|
||||
wantErr: false,
|
||||
},
|
||||
}
|
||||
|
||||
@ -127,7 +127,7 @@ func TestExportToFile(t *testing.T) {
|
||||
|
||||
path := "test-file"
|
||||
err := exportToFile(path, tc.pcrs, tc.fs)
|
||||
if tc.errExpected {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
} else {
|
||||
assert.NoError(err)
|
||||
@ -146,22 +146,22 @@ func TestExportToFile(t *testing.T) {
|
||||
|
||||
func TestValidatePCRAttDoc(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
attDocRaw []byte
|
||||
errExpected bool
|
||||
attDocRaw []byte
|
||||
wantErr bool
|
||||
}{
|
||||
"invalid attestation document": {
|
||||
attDocRaw: []byte{0x1, 0x2, 0x3},
|
||||
errExpected: true,
|
||||
attDocRaw: []byte{0x1, 0x2, 0x3},
|
||||
wantErr: true,
|
||||
},
|
||||
"nil attestation": {
|
||||
attDocRaw: mustMarshalAttDoc(t, vtpm.AttestationDocument{}),
|
||||
errExpected: true,
|
||||
attDocRaw: mustMarshalAttDoc(t, vtpm.AttestationDocument{}),
|
||||
wantErr: true,
|
||||
},
|
||||
"nil quotes": {
|
||||
attDocRaw: mustMarshalAttDoc(t, vtpm.AttestationDocument{
|
||||
Attestation: &attest.Attestation{},
|
||||
}),
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"invalid PCRs": {
|
||||
attDocRaw: mustMarshalAttDoc(t, vtpm.AttestationDocument{
|
||||
@ -178,7 +178,7 @@ func TestValidatePCRAttDoc(t *testing.T) {
|
||||
},
|
||||
},
|
||||
}),
|
||||
errExpected: true,
|
||||
wantErr: true,
|
||||
},
|
||||
"valid PCRs": {
|
||||
attDocRaw: mustMarshalAttDoc(t, vtpm.AttestationDocument{
|
||||
@ -195,7 +195,7 @@ func TestValidatePCRAttDoc(t *testing.T) {
|
||||
},
|
||||
},
|
||||
}),
|
||||
errExpected: false,
|
||||
wantErr: false,
|
||||
},
|
||||
}
|
||||
|
||||
@ -205,7 +205,7 @@ func TestValidatePCRAttDoc(t *testing.T) {
|
||||
require := require.New(t)
|
||||
|
||||
pcrs, err := validatePCRAttDoc(tc.attDocRaw)
|
||||
if tc.errExpected {
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
} else {
|
||||
require.NoError(err)
|
||||
|
Loading…
x
Reference in New Issue
Block a user