public void TestGetAzureRmPolicyAliasByNamespaceNoAliases()
        {
            var providers = new ProviderListBuilder();

            providers.AddProvider("Name1");
            providers.AddProvider("Name2");
            providers.AddProvider("Name3");

            var listResult = providers.List;

            this.SetupAliasListResult(listResult);

            // no args provided, no aliases, so no matching results
            this.commandRuntimeMock
            .Setup(m => m.WriteObject(It.IsAny <object>(), It.IsAny <bool>()))
            .Callback((object obj, bool listAll) => { this.AssertResult(obj, listResult, 0); });

            this.cmdlet.ExecuteCmdlet();
            this.VerifyListCallPatternAndReset();

            // list providers with namespace matching, none have aliases, so no matching results
            this.cmdlet.NamespaceMatch = "name";

            this.commandRuntimeMock
            .Setup(m => m.WriteObject(It.IsAny <object>(), It.IsAny <bool>()))
            .Callback((object obj, bool listAll) => { this.AssertResult(obj, listResult, 0); });

            this.cmdlet.ExecuteCmdlet();
            this.VerifyListCallPatternAndReset();

            // adding resource type matching shouldn't change the results
            this.cmdlet.ResourceTypeMatch = "resource";

            this.commandRuntimeMock
            .Setup(m => m.WriteObject(It.IsAny <object>(), It.IsAny <bool>()))
            .Callback((object obj, bool listAll) => { this.AssertResult(obj, listResult, 0); });

            this.cmdlet.ExecuteCmdlet();
            this.VerifyListCallPatternAndReset();

            // only resource type matching shouldn't change the results
            this.cmdlet.NamespaceMatch = null;

            this.commandRuntimeMock
            .Setup(m => m.WriteObject(It.IsAny <object>(), It.IsAny <bool>()))
            .Callback((object obj, bool listAll) => { this.AssertResult(obj, listResult, 0); });

            this.cmdlet.ExecuteCmdlet();
            this.VerifyListCallPatternAndReset();

            // repeat with the list available switch, none should come back since none have resource types
            this.cmdlet.ListAvailable = true;

            this.commandRuntimeMock
            .Setup(m => m.WriteObject(It.IsAny <object>(), It.IsAny <bool>()))
            .Callback((object obj, bool listAll) => { this.AssertResult(obj, listResult, 0); });

            this.cmdlet.ExecuteCmdlet();
            this.VerifyListCallPatternAndReset();
        }
예제 #2
0
        public void TestGetAzureRmPolicyAliasWithPathMetadata()
        {
            var providers     = new ProviderListBuilder();
            var provider      = providers.AddProvider("Provider1");
            var resourceTypes = provider.AddResourceType("ResourceType1");
            var alias         = resourceTypes.AddAlias("Alias1");

            alias.AddDefaultAliasPathMetadata(AliasPathAttributes.Modifiable, AliasPathTokenType.String);
            alias.AddAliasPath("properties.alias1", new List <string> {
                "2020-01-01"
            }, new AliasPathMetadata(AliasPathAttributes.Modifiable, AliasPathTokenType.Object));

            var listResult = providers.List;

            this.SetupAliasListResult(listResult);

            this.commandRuntimeMock
            .Setup(m => m.WriteObject(It.IsAny <object>(), It.IsAny <bool>()))
            .Callback((object obj, bool listAll) => { this.AssertResult(obj, listResult, 1); });

            this.cmdlet.ExecuteCmdlet();
            this.VerifyListCallPatternAndReset();
        }
        public void TestGetAzureRmPolicyAliasAllParameters()
        {
            var providers = new ProviderListBuilder();

            providers.AddProvider("Provider1");
            providers.AddProvider("Provider2").AddResourceType("ResourceType2");
            providers.AddProvider("Provider3").AddResourceType("ResourceType3", new[] { "ApiVersion3" }).AddAlias("Alias3");
            providers.AddProvider("Provider4").AddResourceType("ResourceType4", new[] { "ApiVersion4" }, new[] { "Location4" }).AddAlias("Alias4");
            providers.AddProvider("Provider5").AddResourceType("ResourceType5").AddAlias("Alias5");
            providers.AddProvider("Provider6").AddResourceType("ResourceType6").AddAlias("Alias6").AddAliasPath("AliasPath6");
            providers.AddProvider("Provider7").AddResourceType("ResourceType7").AddAlias("Alias7").AddAliasPath("AliasPath7", new[] { "ApiVersion7" });

            var listResult = providers.List;

            this.SetupAliasListResult(listResult);

            // should just match the last one
            this.cmdlet.ApiVersionMatch = "7";

            this.commandRuntimeMock
            .Setup(m => m.WriteObject(It.IsAny <object>(), It.IsAny <bool>()))
            .Callback((object obj, bool listAll) => { this.AssertResult(obj, listResult, 1); });

            this.cmdlet.ExecuteCmdlet();
            this.VerifyListCallPatternAndReset();

            // should match the last 2
            this.cmdlet.PathMatch = "6";

            this.commandRuntimeMock
            .Setup(m => m.WriteObject(It.IsAny <object>(), It.IsAny <bool>()))
            .Callback((object obj, bool listAll) => { this.AssertResult(obj, listResult, 2); });

            this.cmdlet.ExecuteCmdlet();
            this.VerifyListCallPatternAndReset();

            // should match the last 3
            this.cmdlet.AliasMatch = "5";

            this.commandRuntimeMock
            .Setup(m => m.WriteObject(It.IsAny <object>(), It.IsAny <bool>()))
            .Callback((object obj, bool listAll) => { this.AssertResult(obj, listResult, 3); });

            this.cmdlet.ExecuteCmdlet();
            this.VerifyListCallPatternAndReset();

            // should match the last 4
            this.cmdlet.LocationMatch = "4";

            this.commandRuntimeMock
            .Setup(m => m.WriteObject(It.IsAny <object>(), It.IsAny <bool>()))
            .Callback((object obj, bool listAll) => { this.AssertResult(obj, listResult, 4); });

            this.cmdlet.ExecuteCmdlet();
            this.VerifyListCallPatternAndReset();

            // should match 3 through 6
            this.cmdlet.ApiVersionMatch = "3";

            this.commandRuntimeMock
            .Setup(m => m.WriteObject(It.IsAny <object>(), It.IsAny <bool>()))
            .Callback((object obj, bool listAll) => { this.AssertResult(obj, listResult, 4); });

            this.cmdlet.ExecuteCmdlet();
            this.VerifyListCallPatternAndReset();

            // with list all switch match all other providers with resource types (6)
            this.cmdlet.ListAvailable = true;

            this.commandRuntimeMock
            .Setup(m => m.WriteObject(It.IsAny <object>(), It.IsAny <bool>()))
            .Callback((object obj, bool listAll) => { this.AssertResult(obj, listResult, 6); });

            this.cmdlet.ExecuteCmdlet();
            this.VerifyListCallPatternAndReset();
        }
        public void TestGetAzureRmPolicyAliasByAlias()
        {
            var providers    = new ProviderListBuilder();
            var resourceType = providers.AddProvider("Provider1").AddResourceType("ResourceType1");

            resourceType.AddAlias("Alias22").AddAliasPath("Properties.Alias22", Enumerable.Empty <string>());
            resourceType.AddAlias("Alias21");
            resourceType.AddAlias("Alias12").AddAliasPath("Properties.Alias12", new[] { "2018-01-01", "2016-01-01" });
            resourceType.AddAlias("Alias11");

            var listResult = providers.List;

            this.SetupAliasListResult(listResult);

            // should match just the second alias
            this.cmdlet.AliasMatch = "12";

            this.commandRuntimeMock
            .Setup(m => m.WriteObject(It.IsAny <object>(), It.IsAny <bool>()))
            .Callback((object obj, bool listAll) => { this.AssertResult(obj, listResult, 1); });

            this.cmdlet.ExecuteCmdlet();
            this.VerifyListCallPatternAndReset();

            // should match both aliases now
            this.cmdlet.PathMatch = "Properties.Alias22";

            this.commandRuntimeMock
            .Setup(m => m.WriteObject(It.IsAny <object>(), It.IsAny <bool>()))
            .Callback((object obj, bool listAll) => { this.AssertResult(obj, listResult, 1); });

            this.cmdlet.ExecuteCmdlet();
            this.VerifyListCallPatternAndReset();

            // now should match just the second alias
            this.cmdlet.PathMatch = "Properties.Alias12";

            this.commandRuntimeMock
            .Setup(m => m.WriteObject(It.IsAny <object>(), It.IsAny <bool>()))
            .Callback((object obj, bool listAll) => { this.AssertResult(obj, listResult, 1); });

            this.cmdlet.ExecuteCmdlet();
            this.VerifyListCallPatternAndReset();

            // add api version, should still match second alias
            this.cmdlet.ApiVersionMatch = "2018";

            this.commandRuntimeMock
            .Setup(m => m.WriteObject(It.IsAny <object>(), It.IsAny <bool>()))
            .Callback((object obj, bool listAll) => { this.AssertResult(obj, listResult, 1); });

            this.cmdlet.ExecuteCmdlet();
            this.VerifyListCallPatternAndReset();

            // should still match because of alias and path match
            this.cmdlet.ApiVersionMatch = "2017";

            this.commandRuntimeMock
            .Setup(m => m.WriteObject(It.IsAny <object>(), It.IsAny <bool>()))
            .Callback((object obj, bool listAll) => { this.AssertResult(obj, listResult, 1); });

            this.cmdlet.ExecuteCmdlet();
            this.VerifyListCallPatternAndReset();

            // just the api version match
            this.cmdlet.AliasMatch = null;
            this.cmdlet.PathMatch  = null;

            this.commandRuntimeMock
            .Setup(m => m.WriteObject(It.IsAny <object>(), It.IsAny <bool>()))
            .Callback((object obj, bool listAll) => { this.AssertResult(obj, listResult, 0); });

            this.cmdlet.ExecuteCmdlet();
            this.VerifyListCallPatternAndReset();
        }
        public void TestGetAzureRmPolicyAliasByResourceType()
        {
            var providers = new ProviderListBuilder();
            var provider1 = providers.AddProvider("Provider1");

            provider1.AddResourceType("ResourceType11").AddAlias("Alias22");
            provider1.AddResourceType("ResourceType12").AddAlias("Alias21");
            provider1.AddResourceType("ResourceType21").AddAlias("Alias12");
            provider1.AddResourceType("ResourceType22").AddAlias("Alias11");

            var listResult = providers.List;

            this.SetupAliasListResult(listResult);

            // should match just the second resource type
            this.cmdlet.ResourceTypeMatch = "12";

            this.commandRuntimeMock
            .Setup(m => m.WriteObject(It.IsAny <object>(), It.IsAny <bool>()))
            .Callback((object obj, bool listAll) => { this.AssertResult(obj, listResult, 1); });

            this.cmdlet.ExecuteCmdlet();
            this.VerifyListCallPatternAndReset();

            // this should match the first 3
            this.cmdlet.ResourceTypeMatch = "1";
            listResult = providers.List;
            this.SetupAliasListResult(listResult);

            this.commandRuntimeMock
            .Setup(m => m.WriteObject(It.IsAny <object>(), It.IsAny <bool>()))
            .Callback((object obj, bool listAll) => { this.AssertResult(obj, listResult, 3); });

            this.cmdlet.ExecuteCmdlet();
            this.VerifyListCallPatternAndReset();

            // adding alias match of 11 should eliminate any matches
            this.cmdlet.AliasMatch = "11";
            listResult             = providers.List;
            this.SetupAliasListResult(listResult);

            this.commandRuntimeMock
            .Setup(m => m.WriteObject(It.IsAny <object>(), It.IsAny <bool>()))
            .Callback((object obj, bool listAll) => { this.AssertResult(obj, listResult, 0); });

            this.cmdlet.ExecuteCmdlet();
            this.VerifyListCallPatternAndReset();

            // removing resource type match should result in one match
            this.cmdlet.ResourceTypeMatch = null;
            listResult = providers.List;
            this.SetupAliasListResult(listResult);

            this.commandRuntimeMock
            .Setup(m => m.WriteObject(It.IsAny <object>(), It.IsAny <bool>()))
            .Callback((object obj, bool listAll) => { this.AssertResult(obj, listResult, 1); });

            this.cmdlet.ExecuteCmdlet();
            this.VerifyListCallPatternAndReset();

            // less restrictive alias match should result in 3 matches
            this.cmdlet.AliasMatch = "1";
            listResult             = providers.List;
            this.SetupAliasListResult(listResult);

            this.commandRuntimeMock
            .Setup(m => m.WriteObject(It.IsAny <object>(), It.IsAny <bool>()))
            .Callback((object obj, bool listAll) => { this.AssertResult(obj, listResult, 3); });

            this.cmdlet.ExecuteCmdlet();
            this.VerifyListCallPatternAndReset();
        }
        public void TestGetAzureRmPolicyAliasByNamespace()
        {
            var providers = new ProviderListBuilder();

            providers.AddProvider("Provider1").AddResourceType("ResourceType1").AddAlias("Alias1");
            var resourceType22 = providers.AddProvider("Provider2").AddResourceType("ResourceType2");
            var resourceType33 = providers.AddProvider("Provider12").AddResourceType("ResourceType3");

            var listResult = providers.List;

            this.SetupAliasListResult(listResult);

            // list available should find all 3
            this.cmdlet.ListAvailable = true;

            this.commandRuntimeMock
            .Setup(m => m.WriteObject(It.IsAny <object>(), It.IsAny <bool>()))
            .Callback((object obj, bool listAll) => { this.AssertResult(obj, listResult, 3); });

            this.cmdlet.ExecuteCmdlet();
            this.VerifyListCallPatternAndReset();

            // list by provider match first and third provider should match, but only first has an alias
            this.cmdlet.ListAvailable  = false;
            this.cmdlet.NamespaceMatch = "1";

            this.commandRuntimeMock
            .Setup(m => m.WriteObject(It.IsAny <object>(), It.IsAny <bool>()))
            .Callback((object obj, bool listAll) => { this.AssertResult(obj, listResult, 1); });

            this.cmdlet.ExecuteCmdlet();
            this.VerifyListCallPatternAndReset();

            // with aliases on all providers, should match first and third now
            var alias21 = resourceType22.AddAlias("Alias1");

            alias21.AddAliasPath("Properties.Alias1Path");

            var alias22 = resourceType22.AddAlias("Alias2");

            alias22.AddAliasPath("Properties.Alias2Path");

            var alias3 = resourceType33.AddAlias("Alias3");

            alias3.AddAliasPath("Properties.Alias3Path");

            listResult = providers.List;
            this.SetupAliasListResult(listResult);

            this.commandRuntimeMock
            .Setup(m => m.WriteObject(It.IsAny <object>(), It.IsAny <bool>()))
            .Callback((object obj, bool listAll) => { this.AssertResult(obj, listResult, 2); });

            this.cmdlet.ExecuteCmdlet();
            this.VerifyListCallPatternAndReset();

            // match resource type that doesn't exist in matching providers, so no results
            this.cmdlet.ResourceTypeMatch = "2";

            this.commandRuntimeMock
            .Setup(m => m.WriteObject(It.IsAny <object>(), It.IsAny <bool>()))
            .Callback((object obj, bool listAll) => { this.AssertResult(obj, listResult, 0); });

            this.cmdlet.ExecuteCmdlet();
            this.VerifyListCallPatternAndReset();

            // matching all resource types should give 2 result matches
            this.cmdlet.ResourceTypeMatch = "type";

            this.commandRuntimeMock
            .Setup(m => m.WriteObject(It.IsAny <object>(), It.IsAny <bool>()))
            .Callback((object obj, bool listAll) => { this.AssertResult(obj, listResult, 2); });

            this.cmdlet.ExecuteCmdlet();
            this.VerifyListCallPatternAndReset();

            // resource types in the third provider should give 1 result match
            this.cmdlet.ResourceTypeMatch = "3";

            this.commandRuntimeMock
            .Setup(m => m.WriteObject(It.IsAny <object>(), It.IsAny <bool>()))
            .Callback((object obj, bool listAll) => { this.AssertResult(obj, listResult, 1); });

            this.cmdlet.ExecuteCmdlet();
            this.VerifyListCallPatternAndReset();
        }