-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Using the common deduplication function, and unit testing azure recon…
…ciliation
- Loading branch information
Showing
6 changed files
with
222 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
189 changes: 189 additions & 0 deletions
189
lib/srv/discovery/fetchers/azure-sync/reconcile_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,189 @@ | ||
/* | ||
* Teleport | ||
* Copyright (C) 2024 Gravitational, Inc. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
package azure_sync | ||
|
||
import ( | ||
accessgraphv1alpha "github.com/gravitational/teleport/gen/proto/go/accessgraph/v1alpha" | ||
"github.com/stretchr/testify/require" | ||
"testing" | ||
) | ||
|
||
func TestReconcileResults(t *testing.T) { | ||
principals := generatePrincipals() | ||
roleDefs := generateRoleDefs() | ||
roleAssigns := generateRoleAssigns() | ||
vms := generateVms() | ||
|
||
tests := []struct { | ||
oldResults *Resources | ||
newResults *Resources | ||
expectedUpserts *accessgraphv1alpha.AzureResourceList | ||
expectedDeletes *accessgraphv1alpha.AzureResourceList | ||
}{ | ||
// Overlapping old and new results | ||
{ | ||
oldResults: &Resources{ | ||
Principals: principals[0:2], | ||
RoleDefinitions: roleDefs[0:2], | ||
RoleAssignments: roleAssigns[0:2], | ||
VirtualMachines: vms[0:2], | ||
}, | ||
newResults: &Resources{ | ||
Principals: principals[1:3], | ||
RoleDefinitions: roleDefs[1:3], | ||
RoleAssignments: roleAssigns[1:3], | ||
VirtualMachines: vms[1:3], | ||
}, | ||
expectedUpserts: generateExpected(principals[2:3], roleDefs[2:3], roleAssigns[2:3], vms[2:3]), | ||
expectedDeletes: generateExpected(principals[0:1], roleDefs[0:1], roleAssigns[0:1], vms[0:1]), | ||
}, | ||
// Completely new results | ||
{ | ||
oldResults: &Resources{ | ||
Principals: nil, | ||
RoleDefinitions: nil, | ||
RoleAssignments: nil, | ||
VirtualMachines: nil, | ||
}, | ||
newResults: &Resources{ | ||
Principals: principals[1:3], | ||
RoleDefinitions: roleDefs[1:3], | ||
RoleAssignments: roleAssigns[1:3], | ||
VirtualMachines: vms[1:3], | ||
}, | ||
expectedUpserts: generateExpected(principals[1:3], roleDefs[1:3], roleAssigns[1:3], vms[1:3]), | ||
expectedDeletes: generateExpected(nil, nil, nil, nil), | ||
}, | ||
// No new results | ||
{ | ||
oldResults: &Resources{ | ||
Principals: principals[1:3], | ||
RoleDefinitions: roleDefs[1:3], | ||
RoleAssignments: roleAssigns[1:3], | ||
VirtualMachines: vms[1:3], | ||
}, | ||
newResults: &Resources{ | ||
Principals: nil, | ||
RoleDefinitions: nil, | ||
RoleAssignments: nil, | ||
VirtualMachines: nil, | ||
}, | ||
expectedUpserts: generateExpected(nil, nil, nil, nil), | ||
expectedDeletes: generateExpected(principals[1:3], roleDefs[1:3], roleAssigns[1:3], vms[1:3]), | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
upserts, deletes := ReconcileResults(test.oldResults, test.newResults) | ||
require.ElementsMatch(t, upserts.Resources, test.expectedUpserts.Resources) | ||
require.ElementsMatch(t, deletes.Resources, test.expectedDeletes.Resources) | ||
} | ||
|
||
} | ||
|
||
func generateExpected( | ||
principals []*accessgraphv1alpha.AzurePrincipal, | ||
roleDefs []*accessgraphv1alpha.AzureRoleDefinition, | ||
roleAssigns []*accessgraphv1alpha.AzureRoleAssignment, | ||
vms []*accessgraphv1alpha.AzureVirtualMachine, | ||
) *accessgraphv1alpha.AzureResourceList { | ||
resList := &accessgraphv1alpha.AzureResourceList{ | ||
Resources: make([]*accessgraphv1alpha.AzureResource, 0), | ||
} | ||
for _, principal := range principals { | ||
resList.Resources = append(resList.Resources, azurePrincipalsWrap(principal)) | ||
} | ||
for _, roleDef := range roleDefs { | ||
resList.Resources = append(resList.Resources, azureRoleDefWrap(roleDef)) | ||
} | ||
for _, roleAssign := range roleAssigns { | ||
resList.Resources = append(resList.Resources, azureRoleAssignWrap(roleAssign)) | ||
} | ||
for _, vm := range vms { | ||
resList.Resources = append(resList.Resources, azureVmWrap(vm)) | ||
} | ||
return resList | ||
} | ||
|
||
func generatePrincipals() []*accessgraphv1alpha.AzurePrincipal { | ||
return []*accessgraphv1alpha.AzurePrincipal{ | ||
{ | ||
Id: "/principals/foo", | ||
DisplayName: "userFoo", | ||
}, | ||
{ | ||
Id: "/principals/bar", | ||
DisplayName: "userBar", | ||
}, | ||
{ | ||
Id: "/principals/charles", | ||
DisplayName: "userCharles", | ||
}, | ||
} | ||
} | ||
|
||
func generateRoleDefs() []*accessgraphv1alpha.AzureRoleDefinition { | ||
return []*accessgraphv1alpha.AzureRoleDefinition{ | ||
{ | ||
Id: "/roledefinitions/foo", | ||
Name: "roleFoo", | ||
}, | ||
{ | ||
Id: "/roledefinitions/bar", | ||
Name: "roleBar", | ||
}, | ||
{ | ||
Id: "/roledefinitions/charles", | ||
Name: "roleCharles", | ||
}, | ||
} | ||
} | ||
|
||
func generateRoleAssigns() []*accessgraphv1alpha.AzureRoleAssignment { | ||
return []*accessgraphv1alpha.AzureRoleAssignment{ | ||
{ | ||
Id: "/roleassignments/foo", | ||
PrincipalId: "userFoo", | ||
}, | ||
{ | ||
Id: "/roleassignments/bar", | ||
PrincipalId: "userBar", | ||
}, | ||
{ | ||
Id: "/roleassignments/charles", | ||
PrincipalId: "userCharles", | ||
}, | ||
} | ||
} | ||
|
||
func generateVms() []*accessgraphv1alpha.AzureVirtualMachine { | ||
return []*accessgraphv1alpha.AzureVirtualMachine{ | ||
{ | ||
Id: "/vms/foo", | ||
Name: "userFoo", | ||
}, | ||
{ | ||
Id: "/vms/bar", | ||
Name: "userBar", | ||
}, | ||
{ | ||
Id: "/vms/charles", | ||
Name: "userCharles", | ||
}, | ||
} | ||
} |