public void ContainerRegistryTaskRunTest()
        {
            var handler = new RecordedDelegatingHandler {
                StatusCodeToReturn = HttpStatusCode.OK
            };

            using (MockContext context = MockContext.Start(this.GetType()))
            {
                var resourceClient = ContainerRegistryTestUtilities.GetResourceManagementClient(context, handler);
                var registryClient = ContainerRegistryTestUtilities.GetContainerRegistryManagementClient(context, handler);

                // Create resource group
                var resourceGroup      = ContainerRegistryTestUtilities.CreateResourceGroup(resourceClient);
                var nonDefaultLocation = ContainerRegistryTestUtilities.GetNonDefaultRegistryLocation(resourceClient, resourceGroup.Location);

                // Create container registry
                var registry = ContainerRegistryTestUtilities.CreateManagedContainerRegistry(registryClient, resourceGroup.Name, nonDefaultLocation);

                string taskString =
                    @"
version: v1.1.0
steps:
  - cmd: docker images";

                // Crete task
                var taskRun = registryClient.TaskRuns.Create(
                    resourceGroup.Name,
                    registry.Name,
                    TestUtilities.GenerateName("acrtaskrun"),
                    new TaskRun(
                        location: registry.Location,
                        identity: new IdentityProperties {
                    Type = Microsoft.Azure.Management.ContainerRegistry.Models.ResourceIdentityType.UserAssigned,
                    UserAssignedIdentities = new Dictionary <string, UserIdentityProperties> {
                        ["/subscriptions/84c559c6-30a0-417c-ba06-8a2253b388c3/resourceGroups/sdk-test/providers/Microsoft.ManagedIdentity/userAssignedIdentities/acrsdktestidentity"]
                            = new UserIdentityProperties()
                    }
                },
                        runRequest: new EncodedTaskRunRequest(
                            encodedTaskContent: Convert.ToBase64String(Encoding.UTF8.GetBytes(taskString)),
                            platform: new PlatformProperties {
                    Architecture = Architecture.Amd64, Os = OS.Linux
                },
                            credentials: new Credentials {
                    SourceRegistry = new SourceRegistryCredentials {
                        LoginMode = SourceRegistryLoginMode.Default
                    },
                    CustomRegistries = new Dictionary <string, CustomRegistryCredentials> {
                        ["acrsdktestregistry.azurecr.io"] = new CustomRegistryCredentials {
                            Identity = "84962bda-7c5e-403e-8515-5100e4ede735"         // client id of acrsdktestidentity
                        }
                    }
                },
                            encodedValuesContent: null,
                            values: null,
                            timeout: 600,
                            sourceLocation: null)));

                Assert.NotNull(taskRun);
                Assert.Equal("ca1", taskRun.RunResult.RunId);

                // List task
                var taskRunList = registryClient.TaskRuns.List(resourceGroup.Name, registry.Name);
                Assert.Single(taskRunList);

                // Delete the task
                registryClient.TaskRuns.Delete(resourceGroup.Name, registry.Name, taskRun.Name);

                // Delete the container registry
                registryClient.Registries.Delete(resourceGroup.Name, registry.Name);
            }
        }
        public void ContainerRegistryAgentPoolTest()
        {
            var handler = new RecordedDelegatingHandler {
                StatusCodeToReturn = HttpStatusCode.OK
            };

            using (MockContext context = MockContext.Start(this.GetType()))
            {
                var resourceClient = ContainerRegistryTestUtilities.GetResourceManagementClient(context, handler);
                var registryClient = ContainerRegistryTestUtilities.GetContainerRegistryManagementClient(context, handler);

                // Create resource group
                var resourceGroup = ContainerRegistryTestUtilities.CreateResourceGroup(resourceClient);
                var locaton       = "eastus2euap";

                // Create container registry
                var registry = ContainerRegistryTestUtilities.CreateManagedContainerRegistry(registryClient, resourceGroup.Name, locaton);

                // Crete a new agentpool
                var agentPool = registryClient.AgentPools.Create(
                    resourceGroup.Name,
                    registry.Name,
                    TestUtilities.GenerateName("acrap"),
                    new AgentPool(
                        location: registry.Location,
                        count: 1,
                        tier: "S2",
                        os: OS.Linux
                        ));

                Assert.NotNull(agentPool);

                // List agentpool
                var agentPoolList = registryClient.AgentPools.List(resourceGroup.Name, registry.Name);
                Assert.Single(agentPoolList);

                // Update agentpool
                agentPool = registryClient.AgentPools.Update(resourceGroup.Name, registry.Name, agentPool.Name, 2);
                Assert.Equal(2, agentPool.Count);

                // Schedule a task run on the agentpool
                string taskString =
                    @"
version: v1.1.0
steps:
  - cmd: docker images";
                var run4 = registryClient.Registries.ScheduleRun(resourceGroup.Name, registry.Name,
                                                                 new EncodedTaskRunRequest(
                                                                     encodedTaskContent: Convert.ToBase64String(Encoding.UTF8.GetBytes(taskString)),
                                                                     agentPoolName: agentPool.Name,
                                                                     platform: new PlatformProperties {
                    Architecture = Architecture.Amd64, Os = OS.Linux
                },
                                                                     isArchiveEnabled: false,
                                                                     encodedValuesContent: null,
                                                                     values: null,
                                                                     timeout: 600,
                                                                     sourceLocation: null));

                // List runs
                var runList = registryClient.Runs.List(resourceGroup.Name, registry.Name);
                Assert.Single(runList);

                Assert.Equal(agentPool.Name, runList.Single().AgentPoolName);

                // Delete the container registry
                registryClient.Registries.Delete(resourceGroup.Name, registry.Name);
            }
        }
        public void ContainerRegistryTaskTest()
        {
            var handler = new RecordedDelegatingHandler {
                StatusCodeToReturn = HttpStatusCode.OK
            };

            using (MockContext context = MockContext.Start(this.GetType()))
            {
                var resourceClient = ContainerRegistryTestUtilities.GetResourceManagementClient(context, handler);
                var registryClient = ContainerRegistryTestUtilities.GetContainerRegistryManagementClient(context, handler);

                // Create resource group
                var resourceGroup      = ContainerRegistryTestUtilities.CreateResourceGroup(resourceClient);
                var nonDefaultLocation = ContainerRegistryTestUtilities.GetNonDefaultRegistryLocation(resourceClient, resourceGroup.Location);

                // Create container registry
                var registry = ContainerRegistryTestUtilities.CreateManagedContainerRegistry(registryClient, resourceGroup.Name, nonDefaultLocation);

                // Crete task
                var task = registryClient.Tasks.Create(
                    resourceGroup.Name,
                    registry.Name,
                    TestUtilities.GenerateName("acrtask"),
                    new Task(
                        location: registry.Location,
                        platform: new PlatformProperties {
                    Architecture = Architecture.Amd64, Os = OS.Linux
                },
                        step: new DockerBuildStep(
                            dockerFilePath: "Dockerfile",
                            baseImageDependencies: null,
                            contextPath: "https://github.com/azure/acr-builder.git",
                            imageNames: new List <string> {
                    "image:{{.Run.ID}}", "image:latest"
                },
                            isPushEnabled: true,
                            noCache: true,
                            arguments: null),
                        agentConfiguration: new AgentProperties(cpu: 2),
                        status: "Enabled",
                        timeout: 600,
                        trigger: new TriggerProperties(
                            sourceTriggers: null,
                            baseImageTrigger: new BaseImageTrigger(
                                baseImageTriggerType: BaseImageTriggerType.Runtime,
                                name: "defaultBaseimageTriggerName",
                                status: TriggerStatus.Enabled))
                        ));

                Assert.NotNull(task);

                // List task
                var taskList = registryClient.Tasks.List(resourceGroup.Name, registry.Name);
                Assert.Single(taskList);

                // Update task
                task = registryClient.Tasks.Update(resourceGroup.Name, registry.Name, task.Name, new TaskUpdateParameters(
                                                       timeout: 900
                                                       ));

                Assert.Equal(900, task.Timeout);

                // Schedule a run from task
                var run1 = registryClient.Registries.ScheduleRun(resourceGroup.Name, registry.Name,
                                                                 new TaskRunRequest(
                                                                     taskId: task.Id,
                                                                     overrideTaskStepProperties: new OverrideTaskStepProperties(values: new List <SetValue> {
                    new SetValue("key1", "value1"), new SetValue("key2", "value2", isSecret: true)
                })));

                Assert.Equal("ca1", run1.RunId);

                // Cancel the run
                registryClient.Runs.Cancel(resourceGroup.Name, registry.Name, run1.RunId);

                // Schedule a docker build run
                var run2 = registryClient.Registries.ScheduleRun(resourceGroup.Name, registry.Name,
                                                                 new DockerBuildRequest(
                                                                     dockerFilePath: "Dockerfile",
                                                                     platform: new PlatformProperties {
                    Architecture = Architecture.Amd64, Os = OS.Linux
                },
                                                                     isArchiveEnabled: false,
                                                                     imageNames: new List <string> {
                    "testimage1:tag1", "testimage2:tag2"
                },
                                                                     isPushEnabled: false,
                                                                     noCache: true,
                                                                     arguments: new List <Argument> {
                    new Argument("param1", "value1", isSecret: true)
                },
                                                                     timeout: 600,
                                                                     agentConfiguration: new AgentProperties(cpu: 2),
                                                                     sourceLocation: "https://github.com/azure/acr-builder.git"));

                Assert.Equal("ca2", run2.RunId);

                // Schedule a file based task run
                var run3 = registryClient.Registries.ScheduleRun(resourceGroup.Name, registry.Name,
                                                                 new FileTaskRunRequest(
                                                                     taskFilePath: "acb.yaml",
                                                                     platform: new PlatformProperties {
                    Architecture = Architecture.Amd64, Os = OS.Linux
                },
                                                                     isArchiveEnabled: false,
                                                                     valuesFilePath: null,
                                                                     values: new List <SetValue> {
                    new SetValue("key1", "value1"), new SetValue("key2", "value2", isSecret: true)
                },
                                                                     timeout: 600,
                                                                     agentConfiguration: new AgentProperties(cpu: 2),
                                                                     sourceLocation: "https://github.com/azure/acr-builder.git"));

                Assert.Equal("ca3", run3.RunId);

                // Schedule an encoded task run
                string taskString =
                    @"
steps:
  - build: . -t acb:linux-{{.Run.ID}}";
                string valuesString =
                    @"
key1: value1
key2: value2
";
                var run4 = registryClient.Registries.ScheduleRun(resourceGroup.Name, registry.Name,
                                                                 new EncodedTaskRunRequest(
                                                                     encodedTaskContent: Convert.ToBase64String(Encoding.UTF8.GetBytes(taskString)),
                                                                     platform: new PlatformProperties {
                    Architecture = Architecture.Amd64, Os = OS.Linux
                },
                                                                     isArchiveEnabled: false,
                                                                     encodedValuesContent: Convert.ToBase64String(Encoding.UTF8.GetBytes(valuesString)),
                                                                     values: null,
                                                                     timeout: 600,
                                                                     agentConfiguration: new AgentProperties(cpu: 2),
                                                                     sourceLocation: "https://github.com/azure/acr-builder.git"));

                Assert.Equal("ca4", run4.RunId);

                // List runs
                var runList = registryClient.Runs.List(resourceGroup.Name, registry.Name);
                Assert.Equal(4, runList.Count());

                // Delete the task
                registryClient.Tasks.Delete(resourceGroup.Name, registry.Name, task.Name);

                // Delete the container registry
                registryClient.Registries.Delete(resourceGroup.Name, registry.Name);
            }
        }
        public void ContainerRegistryWebhookTest()
        {
            var handler = new RecordedDelegatingHandler {
                StatusCodeToReturn = HttpStatusCode.OK
            };

            using (MockContext context = MockContext.Start(this.GetType()))
            {
                var resourceClient = ContainerRegistryTestUtilities.GetResourceManagementClient(context, handler);
                var registryClient = ContainerRegistryTestUtilities.GetContainerRegistryManagementClient(context, handler);

                // Create resource group
                var resourceGroup = ContainerRegistryTestUtilities.CreateResourceGroup(resourceClient);

                // Create container registry and webhook
                var registry = ContainerRegistryTestUtilities.CreateManagedContainerRegistry(registryClient, resourceGroup.Name, resourceGroup.Location);
                var webhook  = ContainerRegistryTestUtilities.CreatedContainerRegistryWebhook(registryClient, resourceGroup.Name, registry.Name, resourceGroup.Location);

                // Validate the created webhook
                ContainerRegistryTestUtilities.ValidateResourceDefaultTags(webhook);
                Assert.Equal(WebhookStatus.Enabled, webhook.Status);
                Assert.True(string.IsNullOrEmpty(webhook.Scope));
                Assert.Equal(1, webhook.Actions.Count);
                Assert.True(webhook.Actions.Contains(WebhookAction.Push));
                Assert.Equal(ProvisioningState.Succeeded, webhook.ProvisioningState);

                // List webhooks by container registry
                var webhooks = registryClient.Webhooks.List(resourceGroup.Name, registry.Name);
                webhook = webhooks.First(
                    w => StringComparer.OrdinalIgnoreCase.Equals(w.Name, webhook.Name));
                Assert.Single(webhooks);
                ContainerRegistryTestUtilities.ValidateResourceDefaultTags(webhook);

                // Get the webhook
                webhook = registryClient.Webhooks.Get(resourceGroup.Name, registry.Name, webhook.Name);
                ContainerRegistryTestUtilities.ValidateResourceDefaultTags(webhook);

                // Update the webhook
                webhook = registryClient.Webhooks.Update(resourceGroup.Name, registry.Name, webhook.Name, new WebhookUpdateParameters
                {
                    Tags    = ContainerRegistryTestUtilities.DefaultNewTags,
                    Status  = WebhookStatus.Disabled,
                    Scope   = ContainerRegistryTestUtilities.DefaultWebhookScope,
                    Actions = new List <string>
                    {
                        WebhookAction.Push, WebhookAction.Delete
                    },
                    CustomHeaders = new Dictionary <string, string>
                    {
                        { "key", "value" }
                    }
                });

                // Validate the updated webhook
                ContainerRegistryTestUtilities.ValidateResourceDefaultNewTags(webhook);
                Assert.Equal(WebhookStatus.Disabled, webhook.Status);
                Assert.Equal(ContainerRegistryTestUtilities.DefaultWebhookScope, webhook.Scope);
                Assert.Equal(2, webhook.Actions.Count);
                Assert.True(webhook.Actions.Contains(WebhookAction.Push));
                Assert.True(webhook.Actions.Contains(WebhookAction.Delete));
                Assert.Equal(ProvisioningState.Succeeded, webhook.ProvisioningState);

                // Get the webhook call back config
                var webhookConfig = registryClient.Webhooks.GetCallbackConfig(resourceGroup.Name, registry.Name, webhook.Name);
                Assert.NotNull(webhookConfig);
                Assert.Equal(ContainerRegistryTestUtilities.DefaultWebhookServiceUri, webhookConfig.ServiceUri);
                Assert.Equal(1, webhookConfig.CustomHeaders.Count);
                Assert.Equal("value", webhookConfig.CustomHeaders["key"]);

                // Ping the webhook
                var eventInfo = registryClient.Webhooks.Ping(resourceGroup.Name, registry.Name, webhook.Name);
                Assert.NotNull(eventInfo);
                Assert.NotNull(eventInfo.Id);

                var events = registryClient.Webhooks.ListEvents(resourceGroup.Name, registry.Name, webhook.Name);
                Assert.NotNull(events);

                // Delete the webhook
                registryClient.Webhooks.Delete(resourceGroup.Name, registry.Name, webhook.Name);

                // Delete the webhook again
                registryClient.Webhooks.Delete(resourceGroup.Name, registry.Name, webhook.Name);

                // Delete the container registry
                registryClient.Registries.Delete(resourceGroup.Name, registry.Name);
            }
        }
        public static ContainerRegistryManagementClient GetContainerRegistryManagementClient(MockContext context, RecordedDelegatingHandler handler)
        {
            handler.IsPassThrough = true;
            ContainerRegistryManagementClient client = context.GetServiceClient <ContainerRegistryManagementClient>(handlers: handler);

            return(client);
        }
        public static ResourceManagementClient GetResourceManagementClient(MockContext context, RecordedDelegatingHandler handler)
        {
            handler.IsPassThrough = true;
            ResourceManagementClient client = context.GetServiceClient <ResourceManagementClient>(handlers: handler);

            return(client);
        }