Exemplo n.º 1
0
        public void TestMethodLoadFromString()
        {
            var manifest = ManifestHelper.LoadFromString("<?xml version=\"1.0\" encoding=\"utf-8\" ?><package>  <name>PackageName</name>  <description>Das ist eine Beschreigung des Packages</description>  <version>1.0</version>  <authors></authors>  <owners></owners>  <projectUrl></projectUrl>  <tags>    <tag>tag</tag>    <tag>tag2</tag>  </tags></package>");

            Assert.IsNotNull(manifest);
        }
Exemplo n.º 2
0
        public async Task CopyAcrImagesCommand_CustomDockerfileName()
        {
            const string subscriptionId = "my subscription";

            using (TempFolderContext tempFolderContext = TestHelper.UseTempFolder())
            {
                Mock <IRegistriesOperations> registriesOperationsMock = AzureHelper.CreateRegistriesOperationsMock();
                IAzure azure = AzureHelper.CreateAzureMock(registriesOperationsMock);
                Mock <IAzureManagementFactory> azureManagementFactoryMock =
                    AzureHelper.CreateAzureManagementFactoryMock(subscriptionId, azure);

                CopyAcrImagesCommand command = new CopyAcrImagesCommand(azureManagementFactoryMock.Object, Mock.Of <ILoggerService>());
                command.Options.Manifest         = Path.Combine(tempFolderContext.Path, "manifest.json");
                command.Options.Subscription     = subscriptionId;
                command.Options.ResourceGroup    = "my resource group";
                command.Options.SourceRepoPrefix = command.Options.RepoPrefix = "test/";
                command.Options.ImageInfoPath    = "image-info.json";

                const string runtimeRelativeDir = "1.0/runtime/os";
                Directory.CreateDirectory(Path.Combine(tempFolderContext.Path, runtimeRelativeDir));
                string dockerfileRelativePath = Path.Combine(runtimeRelativeDir, "Dockerfile.custom");
                File.WriteAllText(Path.Combine(tempFolderContext.Path, dockerfileRelativePath), "FROM repo:tag");

                Manifest manifest = ManifestHelper.CreateManifest(
                    ManifestHelper.CreateRepo("runtime",
                                              ManifestHelper.CreateImage(
                                                  ManifestHelper.CreatePlatform(dockerfileRelativePath, new string[] { "tag1", "tag2" })))
                    );
                manifest.Registry = "mcr.microsoft.com";

                File.WriteAllText(Path.Combine(tempFolderContext.Path, command.Options.Manifest), JsonConvert.SerializeObject(manifest));

                RepoData runtimeRepo;

                ImageArtifactDetails imageArtifactDetails = new ImageArtifactDetails
                {
                    Repos =
                    {
                        {
                            runtimeRepo = new RepoData
                            {
                                Repo   = "runtime",
                                Images =
                                {
                                    new ImageData
                                    {
                                        Platforms =
                                        {
                                            CreatePlatform(
                                                PathHelper.NormalizePath(dockerfileRelativePath),
                                                simpleTags: new List <string>
                                            {
                                                "tag1",
                                                "tag2"
                                            })
                                        }
                                    }
                                }
                            }
                        }
                    }
                };

                File.WriteAllText(command.Options.ImageInfoPath, JsonConvert.SerializeObject(imageArtifactDetails));

                command.LoadManifest();
                await command.ExecuteAsync();

                IList <string> expectedTags = runtimeRepo.Images.First().Platforms.First().SimpleTags
                                              .Select(tag => $"{command.Options.RepoPrefix}{runtimeRepo.Repo}:{tag}")
                                              .ToList();

                foreach (string expectedTag in expectedTags)
                {
                    registriesOperationsMock
                    .Verify(o => o.ImportImageWithHttpMessagesAsync(
                                command.Options.ResourceGroup,
                                manifest.Registry,
                                It.Is <ImportImageParametersInner>(parameters =>
                                                                   VerifyImportImageParameters(parameters, new List <string> {
                        expectedTag
                    })),
                                It.IsAny <Dictionary <string, List <string> > >(),
                                It.IsAny <CancellationToken>()));
                }
            }
        }
Exemplo n.º 3
0
        public void TestMethodLoadFromPath()
        {
            var manifest = ManifestHelper.Load("Ressources\\Manifest.xml");

            Assert.IsNotNull(manifest);
        }
Exemplo n.º 4
0
        public void DockerfileUrl(string sourceRepoBranch)
        {
            using TempFolderContext tempFolderContext = TestHelper.UseTempFolder();

            const string SourceRepoUrl = "https://www.github.com/dotnet/dotnet-docker";
            const string RepoName      = "repo";
            const string TagName       = "tag";

            // Create Dockerfile
            string DockerfileDir = $"1.0/{RepoName}/os";

            Directory.CreateDirectory(Path.Combine(tempFolderContext.Path, DockerfileDir));
            string dockerfileRelativePath = Path.Combine(DockerfileDir, "Dockerfile");
            string dockerfileFullPath     = PathHelper.NormalizePath(Path.Combine(tempFolderContext.Path, dockerfileRelativePath));

            File.WriteAllText(dockerfileFullPath, "FROM base:tag");

            // Create MCR tags metadata template file
            StringBuilder tagsMetadataTemplateBuilder = new StringBuilder();

            tagsMetadataTemplateBuilder.AppendLine($"$(McrTagsYmlRepo:{RepoName})");
            tagsMetadataTemplateBuilder.Append($"$(McrTagsYmlTagGroup:{TagName})");
            string tagsMetadataTemplatePath = Path.Combine(tempFolderContext.Path, "tags.yaml");

            File.WriteAllText(tagsMetadataTemplatePath, tagsMetadataTemplateBuilder.ToString());

            string emptyFileName = "emptyFile.md";
            string emptyFilePath = Path.Combine(tempFolderContext.Path, emptyFileName);

            File.WriteAllText(emptyFilePath, string.Empty);

            // Create manifest
            Manifest manifest = ManifestHelper.CreateManifest(
                ManifestHelper.CreateRepo(RepoName,
                                          new Image[]
            {
                ManifestHelper.CreateImage(
                    ManifestHelper.CreatePlatform(dockerfileRelativePath, new string[] { TagName }))
            },
                                          readme: emptyFileName,
                                          readmeTemplate: emptyFileName,
                                          mcrTagsMetadataTemplate: Path.GetFileName(tagsMetadataTemplatePath))
                );
            string manifestPath = Path.Combine(tempFolderContext.Path, "manifest.json");

            File.WriteAllText(manifestPath, JsonConvert.SerializeObject(manifest));

            // Load manifest
            IManifestOptionsInfo manifestOptions = ManifestHelper.GetManifestOptions(manifestPath);
            ManifestInfo         manifestInfo    = ManifestInfo.Load(manifestOptions);
            RepoInfo             repo            = manifestInfo.AllRepos.First();

            Mock <IGitService> gitServiceMock = new Mock <IGitService>();
            const string       DockerfileSha  = "random_sha";

            if (sourceRepoBranch == null)
            {
                gitServiceMock
                .Setup(o => o.GetCommitSha(dockerfileFullPath, true))
                .Returns(DockerfileSha);
            }

            // Execute generator
            string result = McrTagsMetadataGenerator.Execute(
                gitServiceMock.Object, manifestInfo, repo, SourceRepoUrl, sourceRepoBranch);

            TagsMetadata tagsMetadata = new DeserializerBuilder()
                                        .WithNamingConvention(CamelCaseNamingConvention.Instance)
                                        .Build()
                                        .Deserialize <TagsMetadata>(result);

            string branchOrSha = sourceRepoBranch ?? DockerfileSha;

            Assert.Equal($"{SourceRepoUrl}/blob/{branchOrSha}/{DockerfileDir}/Dockerfile",
                         tagsMetadata.Repos[0].TagGroups[0].Dockerfile);
        }
Exemplo n.º 5
0
        public void DuplicatedPlatform()
        {
            using TempFolderContext tempFolderContext = TestHelper.UseTempFolder();

            const string SourceRepoUrl = "https://www.github.com/dotnet/dotnet-docker";
            const string RepoName      = "repo";
            const string SourceBranch  = "branch";

            // Create MCR tags metadata template file
            StringBuilder tagsMetadataTemplateBuilder = new StringBuilder();

            tagsMetadataTemplateBuilder.AppendLine($"$(McrTagsYmlRepo:{RepoName})");
            tagsMetadataTemplateBuilder.AppendLine($"$(McrTagsYmlTagGroup:concreteTagA)");
            string tagsMetadataTemplatePath = Path.Combine(tempFolderContext.Path, "tags.yaml");

            File.WriteAllText(tagsMetadataTemplatePath, tagsMetadataTemplateBuilder.ToString());

            string emptyFileName = "emptyFile.md";
            string emptyFilePath = Path.Combine(tempFolderContext.Path, emptyFileName);

            File.WriteAllText(emptyFilePath, string.Empty);

            // Create manifest
            Manifest manifest = ManifestHelper.CreateManifest(
                ManifestHelper.CreateRepo(RepoName,
                                          new Image[]
            {
                ManifestHelper.CreateImage(
                    new Platform[]
                {
                    ManifestHelper.CreatePlatform(
                        DockerfileHelper.CreateDockerfile($"1.0/{RepoName}/os", tempFolderContext),
                        new string[] { "concreteTagZ", "concreteTagA" })
                },
                    sharedTags: new Dictionary <string, Tag>
                {
                    { "shared1", new Tag() },
                    { "latest", new Tag() },
                }),
                ManifestHelper.CreateImage(
                    new Platform[]
                {
                    ManifestHelper.CreatePlatform(
                        DockerfileHelper.CreateDockerfile($"1.0/{RepoName}/os", tempFolderContext),
                        Array.Empty <string>())
                },
                    sharedTags: new Dictionary <string, Tag>
                {
                    { "shared2", new Tag() }
                })
            },
                                          readme: emptyFileName,
                                          readmeTemplate: emptyFileName,
                                          mcrTagsMetadataTemplate: Path.GetFileName(tagsMetadataTemplatePath))
                );

            string manifestPath = Path.Combine(tempFolderContext.Path, "manifest.json");

            File.WriteAllText(manifestPath, JsonConvert.SerializeObject(manifest));

            // Load manifest
            IManifestOptionsInfo manifestOptions = ManifestHelper.GetManifestOptions(manifestPath);
            ManifestInfo         manifestInfo    = ManifestInfo.Load(manifestOptions);
            RepoInfo             repo            = manifestInfo.AllRepos.First();

            Mock <IGitService> gitServiceMock = new Mock <IGitService>();

            // Execute generator
            string result = McrTagsMetadataGenerator.Execute(
                gitServiceMock.Object, manifestInfo, repo, SourceRepoUrl, SourceBranch);

            TagsMetadata tagsMetadata = new DeserializerBuilder()
                                        .WithNamingConvention(CamelCaseNamingConvention.Instance)
                                        .Build()
                                        .Deserialize <TagsMetadata>(result);

            // Verify the output only contains the platform with the documented tag
            Assert.Single(tagsMetadata.Repos[0].TagGroups);
            Assert.Equal(
                $"{SourceRepoUrl}/blob/{SourceBranch}/1.0/{RepoName}/os/Dockerfile",
                tagsMetadata.Repos[0].TagGroups[0].Dockerfile);

            List <string> expectedTags = new List <string>
            {
                "concreteTagZ",
                "concreteTagA",
                "shared2",
                "shared1",
                "latest"
            };

            Assert.Equal(expectedTags, tagsMetadata.Repos[0].TagGroups[0].Tags);
        }
Exemplo n.º 6
0
        public void HandlesUndocumentedPlatform()
        {
            using TempFolderContext tempFolderContext = TestHelper.UseTempFolder();

            const string SourceRepoUrl = "https://www.github.com/dotnet/dotnet-docker";
            const string RepoName      = "repo";
            const string SourceBranch  = "branch";

            // Create MCR tags metadata template file
            StringBuilder tagsMetadataTemplateBuilder = new StringBuilder();

            tagsMetadataTemplateBuilder.AppendLine($"$(McrTagsYmlRepo:{RepoName})");
            tagsMetadataTemplateBuilder.AppendLine($"$(McrTagsYmlTagGroup:tag1a)");
            string tagsMetadataTemplatePath = Path.Combine(tempFolderContext.Path, "tags.yaml");

            File.WriteAllText(tagsMetadataTemplatePath, tagsMetadataTemplateBuilder.ToString());

            Platform platform = ManifestHelper.CreatePlatform(
                DockerfileHelper.CreateDockerfile($"1.0/{RepoName}/os", tempFolderContext),
                Array.Empty <string>());

            platform.Tags = new Dictionary <string, Tag>
            {
                {
                    "tag2",
                    new Tag
                    {
                        IsUndocumented = true
                    }
                }
            };

            // Create manifest
            Manifest manifest = ManifestHelper.CreateManifest(
                ManifestHelper.CreateRepo(RepoName,
                                          new Image[]
            {
                ManifestHelper.CreateImage(
                    platform,
                    ManifestHelper.CreatePlatform(
                        DockerfileHelper.CreateDockerfile($"1.0/{RepoName}/os2", tempFolderContext),
                        new string[] { "tag1a", "tag1b" }))
            },
                                          mcrTagsMetadataTemplatePath: Path.GetFileName(tagsMetadataTemplatePath))
                );
            string manifestPath = Path.Combine(tempFolderContext.Path, "manifest.json");

            File.WriteAllText(manifestPath, JsonConvert.SerializeObject(manifest));

            // Load manifest
            IManifestOptionsInfo manifestOptions = GetManifestOptions(manifestPath);
            ManifestInfo         manifestInfo    = ManifestInfo.Load(manifestOptions);
            RepoInfo             repo            = manifestInfo.AllRepos.First();

            Mock <IGitService> gitServiceMock = new Mock <IGitService>();

            // Execute generator
            string result = McrTagsMetadataGenerator.Execute(
                gitServiceMock.Object, manifestInfo, repo, SourceRepoUrl, SourceBranch);

            Models.Mcr.McrTagsMetadata tagsMetadata = new DeserializerBuilder()
                                                      .WithNamingConvention(CamelCaseNamingConvention.Instance)
                                                      .Build()
                                                      .Deserialize <Models.Mcr.McrTagsMetadata>(result);

            // Verify the output only contains the platform with the documented tag
            Assert.Single(tagsMetadata.Repos[0].TagGroups);
            Assert.Equal(
                $"{SourceRepoUrl}/blob/{SourceBranch}/1.0/{RepoName}/os2/Dockerfile",
                tagsMetadata.Repos[0].TagGroups[0].Dockerfile);
            Assert.Equal(new string[] { "tag1a", "tag1b" }, tagsMetadata.Repos[0].TagGroups[0].Tags);
        }
Exemplo n.º 7
0
        public static async Task <object> Run([HttpTrigger("post", WebHookType = "genericJson")] HttpRequestMessage req, TraceWriter log, ExecutionContext executionContext)
        {
            log.Info("GenerateManifest requested.");

            var gmRequest = await req.Content.ReadAsAsync <GenerateManifestRequest>();

            // Sanity checks
            #region Sanity checks
            if (gmRequest == null || gmRequest.AssetId == null)
            {
                return(req.CreateResponse(HttpStatusCode.BadRequest, new
                {
                    error = "Invalid generate manifest request."
                }));
            }
            #endregion


            // Create and cache the Media Services credentials in a static class variable
            _cachedCredentials = new MediaServicesCredentials(_mediaServicesAccountName, _mediaServicesAccountKey);

            // Used the cached credentials to create CloudMediaContext
            _context = new CloudMediaContext(_cachedCredentials);

            // Get the asset
            var asset = _context.Assets.Where(a => a.Id == gmRequest.AssetId).FirstOrDefault();
            if (asset == null)
            {
                return(req.CreateResponse(HttpStatusCode.NotFound, new
                {
                    error = $"Asset {gmRequest.AssetId} doesn't exist."
                }));
            }

            log.Info("Found the asset. Generating manifest.ism");
            try
            {
                var manifestFilePath = executionContext.FunctionDirectory + @"\..\bin\shared\Manifest.ism";
                var smildata         = ManifestHelper.LoadAndUpdateManifestTemplate(asset, manifestFilePath);
                var smilXMLDocument  = XDocument.Parse(smildata.Content);

                // Check if the manifest file exists
                if (asset.AssetFiles.Where(af => af.Name == smildata.FileName).FirstOrDefault() != null)
                {
                    // Do nothing
                    log.Info("Manifest already exists.");
                    return(req.CreateResponse(HttpStatusCode.OK, new
                    {
                        message = "Manifest already exists"
                    }));
                }

                var smildataAssetFile = asset.AssetFiles.Create(smildata.FileName);

                var stream = new MemoryStream(); // Create a stream
                smilXMLDocument.Save(stream);    // Save XDocument into the stream
                stream.Position = 0;             // Rewind the stream ready to read from it elsewhere
                smildataAssetFile.Upload(stream);

                // Update the asset to set the primary file as the ism file
                ManifestHelper.SetFileAsPrimary(asset, smildata.FileName);
            }
            catch (Exception ex)
            {
                log.Error("Could not generate the manifest.");
                log.Error(ex.ToString());

                return(req.CreateResponse(HttpStatusCode.InternalServerError, new
                {
                    error = $"Could not generate the manifest. Are you sure the asset contains media files (mp4, m4a)?."
                }));
            }

            // Fetching the name from the path parameter in the request URL
            return(req.CreateResponse(HttpStatusCode.Created, new
            {
                message = "Manifest created"
            }));
        }