@@ -39,6 +39,45 @@ func TestNewAlzLibCustomOptions(t *testing.T) {
3939 assert .False (t , az .Options .UniqueRoleDefinitions )
4040}
4141
42+ func TestAddPolicyAndRoleAssetsAllowsDuplicateVersions (t * testing.T ) {
43+ testCases := []struct {
44+ name string
45+ allowOverwrite bool
46+ }{
47+ {name : "disallow overwrite" , allowOverwrite : false },
48+ {name : "allow overwrite" , allowOverwrite : true },
49+ }
50+
51+ for _ , tc := range testCases {
52+ t .Run (tc .name , func (t * testing.T ) {
53+ az := NewAlzLib (& Options {
54+ AllowOverwrite : tc .allowOverwrite ,
55+ Parallelism : defaultParallelism ,
56+ UniqueRoleDefinitions : defaultUniqueRoleDefinitions ,
57+ })
58+
59+ existingPolicyDefs := assets .NewPolicyDefinitionVersions ()
60+ require .NoError (t , existingPolicyDefs .Add (testPolicyDefinition (t , "dup-policy" , "1.0.0" ), false ))
61+ az .policyDefinitions ["dup-policy" ] = existingPolicyDefs
62+
63+ existingPolicySetDefs := assets .NewPolicySetDefinitionVersions ()
64+ require .NoError (t , existingPolicySetDefs .Add (testPolicySetDefinition (t , "dup-policy-set" , "1.0.0" ), false ))
65+ az .policySetDefinitions ["dup-policy-set" ] = existingPolicySetDefs
66+
67+ res := processor .NewResult ()
68+ dupPolicyDefs := assets .NewPolicyDefinitionVersions ()
69+ require .NoError (t , dupPolicyDefs .Add (testPolicyDefinition (t , "dup-policy" , "1.0.0" ), false ))
70+ res .PolicyDefinitions ["dup-policy" ] = dupPolicyDefs
71+
72+ dupPolicySetDefs := assets .NewPolicySetDefinitionVersions ()
73+ require .NoError (t , dupPolicySetDefs .Add (testPolicySetDefinition (t , "dup-policy-set" , "1.0.0" ), false ))
74+ res .PolicySetDefinitions ["dup-policy-set" ] = dupPolicySetDefs
75+
76+ require .NoError (t , az .addPolicyAndRoleAssets (res ))
77+ })
78+ }
79+ }
80+
4281// Test_NewAlzLib_noDir tests the creation of a new AlzLib when supplied with a path
4382// that does not exist.
4483// The error details are checked for the expected error message.
@@ -1087,3 +1126,43 @@ func TestIntegrationGetDefinitionsFromAzure(t *testing.T) {
10871126 assert .True (t , az .PolicySetDefinitionExists ("1a5bb27d-173f-493e-9568-eb56638dde4d" , to .Ptr ("16.*.*" )))
10881127 assert .True (t , az .PolicySetDefinitionExists ("0a2ebd47-3fb9-4735-a006-b7f31ddadd9f" , nil ))
10891128}
1129+
1130+ func testPolicyDefinition (t * testing.T , name , version string ) * assets.PolicyDefinition {
1131+ t .Helper ()
1132+
1133+ desc := name + " description"
1134+
1135+ return & assets.PolicyDefinition {
1136+ Definition : armpolicy.Definition {
1137+ Name : to .Ptr (name ),
1138+ Properties : & armpolicy.DefinitionProperties {
1139+ DisplayName : to .Ptr (name ),
1140+ Description : & desc ,
1141+ Metadata : map [string ]any {},
1142+ PolicyRule : map [string ]any {"if" : map [string ]any {}, "then" : map [string ]any {}},
1143+ Version : to .Ptr (version ),
1144+ },
1145+ },
1146+ }
1147+ }
1148+
1149+ func testPolicySetDefinition (t * testing.T , name , version string ) * assets.PolicySetDefinition {
1150+ t .Helper ()
1151+
1152+ desc := name + " description"
1153+
1154+ return & assets.PolicySetDefinition {
1155+ SetDefinition : armpolicy.SetDefinition {
1156+ Name : to .Ptr (name ),
1157+ Properties : & armpolicy.SetDefinitionProperties {
1158+ DisplayName : to .Ptr (name ),
1159+ Description : & desc ,
1160+ Metadata : map [string ]any {},
1161+ PolicyDefinitions : []* armpolicy.DefinitionReference {},
1162+ Parameters : map [string ]* armpolicy.ParameterDefinitionsValue {},
1163+ PolicyType : to .Ptr (armpolicy .PolicyTypeCustom ),
1164+ Version : to .Ptr (version ),
1165+ },
1166+ },
1167+ }
1168+ }
0 commit comments