public void ContainerRegistryReplicationTest()
        {
            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 and replication
                var registry    = ContainerRegistryTestUtilities.CreateManagedContainerRegistry(registryClient, resourceGroup.Name, nonDefaultLocation);
                var replication = ContainerRegistryTestUtilities.CreatedContainerRegistryReplication(registryClient, resourceGroup.Name, registry.Name, resourceGroup.Location);

                // Validate the created replication
                ContainerRegistryTestUtilities.ValidateResourceDefaultTags(replication);
                Assert.Equal(ProvisioningState.Succeeded, replication.ProvisioningState);
                Assert.NotNull(replication.Status);
                Assert.NotNull(replication.Status.DisplayStatus);

                // List replications by container registry
                var replications = registryClient.Replications.List(resourceGroup.Name, registry.Name);
                replication = replications.First(
                    r => StringComparer.OrdinalIgnoreCase.Equals(r.Name, replication.Name));
                Assert.Equal(2, replications.Count()); // 2 because a replication in home region is auto created
                ContainerRegistryTestUtilities.ValidateResourceDefaultTags(replication);

                // Get the replication
                replication = registryClient.Replications.Get(resourceGroup.Name, registry.Name, replication.Name);
                ContainerRegistryTestUtilities.ValidateResourceDefaultTags(replication);

                // Update the replication
                replication = registryClient.Replications.Update(resourceGroup.Name, registry.Name, replication.Name, ContainerRegistryTestUtilities.DefaultNewTags);

                // Validate the updated replication
                ContainerRegistryTestUtilities.ValidateResourceDefaultNewTags(replication);
                Assert.Equal(ProvisioningState.Succeeded, replication.ProvisioningState);
                Assert.NotNull(replication.Status);
                Assert.NotNull(replication.Status.DisplayStatus);

                // Delete the replication
                registryClient.Replications.Delete(resourceGroup.Name, registry.Name, replication.Name);

                // Delete the replication again
                registryClient.Replications.Delete(resourceGroup.Name, registry.Name, replication.Name);

                // Delete the container registry
                registryClient.Registries.Delete(resourceGroup.Name, registry.Name);
            }
        }
        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 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);
            }
        }