Example #1
0
        public static RepoInfo Create(
            Repo model,
            string registry,
            string modelRegistryName,
            ManifestFilter manifestFilter,
            IManifestOptionsInfo options,
            VariableHelper variableHelper,
            string baseDirectory)
        {
            RepoInfo repoInfo = new RepoInfo();

            repoInfo.Model         = model;
            repoInfo.FullModelName = (string.IsNullOrEmpty(modelRegistryName) ? string.Empty : $"{modelRegistryName}/") + model.Name;
            repoInfo.Id            = model.Id ?? model.Name;

            if (options.RepoOverrides.TryGetValue(model.Name, out string nameOverride))
            {
                repoInfo.Name = nameOverride;
            }
            else
            {
                registry      = string.IsNullOrEmpty(registry) ? string.Empty : $"{registry}/";
                repoInfo.Name = registry + options.RepoPrefix + model.Name;
            }

            repoInfo.AllImages = model.Images
                                 .Select(image => ImageInfo.Create(image, repoInfo.FullModelName, repoInfo.Name, manifestFilter, variableHelper, baseDirectory))
                                 .ToArray();

            repoInfo.FilteredImages = repoInfo.AllImages
                                      .Where(image => image.FilteredPlatforms.Any())
                                      .ToArray();

            return(repoInfo);
        }
Example #2
0
        public static RepoInfo Create(
            Repo model,
            string registry,
            string modelRegistryName,
            ManifestFilter manifestFilter,
            IManifestOptionsInfo options,
            VariableHelper variableHelper,
            string baseDirectory)
        {
            RepoInfo repoInfo = new RepoInfo
            {
                Model         = model,
                FullModelName = (string.IsNullOrEmpty(modelRegistryName) ? string.Empty : $"{modelRegistryName}/") + model.Name,
                Id            = model.Id ?? model.Name
            };

            registry = string.IsNullOrEmpty(registry) ? string.Empty : $"{registry}/";
            repoInfo.QualifiedName = registry + options.RepoPrefix + model.Name;

            repoInfo.AllImages = model.Images
                                 .Select(image => ImageInfo.Create(image, repoInfo.FullModelName, repoInfo.QualifiedName, manifestFilter, variableHelper, baseDirectory))
                                 .ToArray();

            repoInfo.FilteredImages = repoInfo.AllImages
                                      .Where(image => image.FilteredPlatforms.Any())
                                      .ToArray();

            return(repoInfo);
        }
Example #3
0
        public void Load_Include_Repos()
        {
            const string includeManifestPath1 = "manifest.custom1.json";
            const string includeManifestPath2 = "manifest.custom2.json";
            string       manifest             =
                $@"
{{
  ""includes"": [
    ""{includeManifestPath1}"",
    ""{includeManifestPath2}""
  ]
}}";

            string includeManifest1 =
                $@"
{{
  ""repos"": [
    {CreateRepo("testRepo1", s_dockerfilePath, "testTag1")},
    {CreateRepo("testRepo2", s_dockerfilePath)}
  ]
}}";

            string includeManifest2 =
                $@"
{{
  ""repos"": [
    {CreateRepo("testRepo1", s_dockerfilePath, "testTag2")},
    {CreateRepo("testRepo3", s_dockerfilePath)}
  ]
}}";

            using TempFolderContext tempFolderContext = TestHelper.UseTempFolder();

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

            File.WriteAllText(manifestPath, manifest);

            File.WriteAllText(Path.Combine(tempFolderContext.Path, includeManifestPath1), includeManifest1);
            File.WriteAllText(Path.Combine(tempFolderContext.Path, includeManifestPath2), includeManifest2);

            DockerfileHelper.CreateDockerfile(s_dockerfilePath, tempFolderContext);

            IManifestOptionsInfo manifestOptions = ManifestHelper.GetManifestOptions(manifestPath);
            ManifestInfo         manifestInfo    = ManifestInfo.Load(manifestOptions);

            Assert.Equal(3, manifestInfo.Model.Repos.Length);
            Assert.Equal("testRepo1", manifestInfo.Model.Repos[0].Name);
            Assert.Equal("testRepo2", manifestInfo.Model.Repos[1].Name);
            Assert.Equal("testRepo3", manifestInfo.Model.Repos[2].Name);

            Assert.Equal(2, manifestInfo.Model.Repos[0].Images.Length);
            Assert.Equal(1, manifestInfo.Model.Repos[1].Images.Length);
            Assert.Equal(1, manifestInfo.Model.Repos[2].Images.Length);
        }
Example #4
0
        public static ManifestInfo Load(IManifestOptionsInfo options)
        {
            Logger.WriteHeading("READING MANIFEST");

            ManifestInfo manifest = ManifestInfo.Create(
                options.Manifest,
                options.GetManifestFilter(),
                options);

            if (options.IsVerbose)
            {
                Logger.WriteMessage(JsonConvert.SerializeObject(manifest, Formatting.Indented));
            }

            return(manifest);
        }
Example #5
0
        private static ManifestInfo LoadManifestInfo(string manifest, string includeManifestPath = null, string includeManifest = null)
        {
            using TempFolderContext tempFolderContext = TestHelper.UseTempFolder();

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

            File.WriteAllText(manifestPath, manifest);

            if (includeManifestPath != null)
            {
                string fullIncludeManifestPath = Path.Combine(tempFolderContext.Path, includeManifestPath);
                File.WriteAllText(fullIncludeManifestPath, includeManifest);
            }

            DockerfileHelper.CreateDockerfile(s_dockerfilePath, tempFolderContext);

            IManifestOptionsInfo manifestOptions = ManifestHelper.GetManifestOptions(manifestPath);

            return(ManifestInfo.Load(manifestOptions));
        }
Example #6
0
        public VariableHelper(Manifest manifest, IManifestOptionsInfo options, Func <string, RepoInfo> getRepoById)
        {
            GetRepoById = getRepoById;
            Manifest    = manifest;
            Options     = options;

            if (Manifest.Variables is not null)
            {
                foreach (KeyValuePair <string, string?> kvp in Manifest.Variables)
                {
                    string?variableValue;
                    if (Options.Variables is not null && Options.Variables.TryGetValue(kvp.Key, out string?overridenValue))
                    {
                        variableValue = overridenValue;
                    }
                    else
                    {
                        variableValue = kvp.Value;
                    }

                    variableValue = SubstituteValues(variableValue);
                    ResolvedVariables.Add(kvp.Key, variableValue);
                }
        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());

            // Create manifest
            Manifest manifest = ManifestHelper.CreateManifest(
                ManifestHelper.CreateRepo(RepoName,
                                          new Image[]
            {
                ManifestHelper.CreateImage(
                    ManifestHelper.CreatePlatform(dockerfileRelativePath, new string[] { TagName }))
            },
                                          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>();
            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);

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

            string branchOrSha = sourceRepoBranch ?? DockerfileSha;

            Assert.Equal($"{SourceRepoUrl}/blob/{branchOrSha}/{DockerfileDir}/Dockerfile",
                         tagsMetadata.Repos[0].TagGroups[0].Dockerfile);
        }
Example #8
0
 public VariableHelper(Manifest manifest, IManifestOptionsInfo options, Func <string, RepoInfo> getRepoById)
 {
     GetRepoById = getRepoById;
     Manifest    = manifest;
     Options     = options;
 }
Example #9
0
        private static ManifestInfo Create(string manifestPath, ManifestFilter manifestFilter, IManifestOptionsInfo options)
        {
            string   manifestDirectory = PathHelper.GetNormalizedDirectory(manifestPath);
            Manifest model             = LoadModel(manifestPath, manifestDirectory);

            model.Validate(manifestDirectory);

            ManifestInfo manifestInfo = new ManifestInfo
            {
                Model     = model,
                Registry  = options.RegistryOverride ?? model.Registry,
                Directory = manifestDirectory
            };

            manifestInfo.VariableHelper = new VariableHelper(model, options, manifestInfo.GetRepoById);
            manifestInfo.AllRepos       = manifestInfo.Model.Repos
                                          .Select(repo => RepoInfo.Create(
                                                      repo,
                                                      manifestInfo.Registry,
                                                      model.Registry,
                                                      manifestFilter,
                                                      options,
                                                      manifestInfo.VariableHelper,
                                                      manifestInfo.Directory))
                                          .ToArray();

            if (model.Readme != null)
            {
                manifestInfo.ReadmePath = Path.Combine(manifestInfo.Directory, model.Readme.Path);
                if (model.Readme.TemplatePath != null)
                {
                    manifestInfo.ReadmeTemplatePath = Path.Combine(manifestInfo.Directory, model.Readme.TemplatePath);
                }
            }

            IEnumerable <string> repoNames = manifestInfo.AllRepos.Select(repo => repo.QualifiedName).ToArray();

            foreach (PlatformInfo platform in manifestInfo.GetAllPlatforms())
            {
                platform.Initialize(repoNames, manifestInfo.Registry);
            }

            IEnumerable <Repo> filteredRepoModels = manifestFilter.GetRepos(manifestInfo.Model);

            manifestInfo.FilteredRepos = manifestInfo.AllRepos
                                         .Where(repo => filteredRepoModels.Contains(repo.Model))
                                         .ToArray();

            return(manifestInfo);
        }
Example #10
0
        private static ManifestInfo Create(string manifestPath, ManifestFilter manifestFilter, IManifestOptionsInfo options)
        {
            string   manifestJson      = File.ReadAllText(manifestPath);
            Manifest model             = JsonConvert.DeserializeObject <Manifest>(manifestJson);
            string   manifestDirectory = PathHelper.GetNormalizedDirectory(manifestPath);

            model.Validate(manifestDirectory);

            ManifestInfo manifestInfo = new ManifestInfo
            {
                Model     = model,
                Registry  = options.RegistryOverride ?? model.Registry,
                Directory = manifestDirectory
            };

            manifestInfo.VariableHelper = new VariableHelper(model, options, manifestInfo.GetRepoById);
            manifestInfo.AllRepos       = manifestInfo.Model.Repos
                                          .Select(repo => RepoInfo.Create(
                                                      repo,
                                                      manifestInfo.Registry,
                                                      model.Registry,
                                                      manifestFilter,
                                                      options,
                                                      manifestInfo.VariableHelper,
                                                      manifestInfo.Directory))
                                          .ToArray();

            IEnumerable <string> repoNames = manifestInfo.AllRepos.Select(repo => repo.QualifiedName).ToArray();

            foreach (PlatformInfo platform in manifestInfo.GetAllPlatforms())
            {
                platform.Initialize(repoNames, manifestInfo.Registry);
            }

            IEnumerable <Repo> filteredRepoModels = manifestFilter.GetRepos(manifestInfo.Model);

            manifestInfo.FilteredRepos = manifestInfo.AllRepos
                                         .Where(repo => filteredRepoModels.Contains(repo.Model))
                                         .ToArray();

            return(manifestInfo);
        }
Example #11
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);
        }
Example #12
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);
        }