public void TestToBuildConfiguration_containerConfigurationAdd()
        {
            FibContainerBuilder fibContainerBuilder =
                new FibContainerBuilder(RegistryImage.Named("base/image"), buildConfigurationBuilder)
                .SetEntrypoint("entry", "point")
                .SetEnvironment(ImmutableDic.Of("name", "value"))
                .AddEnvironmentVariable("environment", "variable")
                .SetExposedPorts(Port.Tcp(1234), Port.Udp(5678))
                .AddExposedPort(Port.Tcp(1337))
                .SetLabels(ImmutableDic.Of("key", "value"))
                .AddLabel("added", "label")
                .SetProgramArguments("program", "arguments");

            BuildConfiguration buildConfiguration =
                fibContainerBuilder.ToBuildConfiguration(
                    Containerizer.To(RegistryImage.Named("target/image")));
            IContainerConfiguration containerConfiguration = buildConfiguration.GetContainerConfiguration();

            Assert.AreEqual(new[] { "entry", "point" }, containerConfiguration.GetEntrypoint());
            Assert.AreEqual(
                ImmutableDic.Of("name", "value", "environment", "variable"),
                containerConfiguration.GetEnvironmentMap());
            Assert.AreEqual(
                ImmutableHashSet.Create(Port.Tcp(1234), Port.Udp(5678), Port.Tcp(1337)),
                containerConfiguration.GetExposedPorts());
            Assert.AreEqual(
                ImmutableDic.Of("key", "value", "added", "label"), containerConfiguration.GetLabels());
            Assert.AreEqual(
                new[] { "program", "arguments" }, containerConfiguration.GetProgramArguments());
            Assert.AreEqual(Instant.FromUnixTimeSeconds(0), containerConfiguration.GetCreationTime());
        }
Exemple #2
0
        /**
         * Computes the image program arguments. If {@link ContainerConfiguration#getEntrypoint()} and
         * {@link ContainerConfiguration#getProgramArguments()} are null, the program arguments are
         * inherited from the base image. Otherwise {@link ContainerConfiguration#getProgramArguments()}
         * is returned.
         *
         * @param baseImage the base image
         * @param containerConfiguration the container configuration
         * @return the container program arguments
         */
        private ImmutableArray <string>?ComputeProgramArguments(
            Image baseImage, IContainerConfiguration containerConfiguration)
        {
            bool shouldInherit =
                baseImage.GetProgramArguments() != null
                // Inherit CMD only when inheriting ENTRYPOINT.
                && containerConfiguration.GetEntrypoint() == null &&
                containerConfiguration.GetProgramArguments() == null;

            ImmutableArray <string>?programArgumentsToUse =
                shouldInherit
                    ? baseImage.GetProgramArguments()
                    : containerConfiguration.GetProgramArguments();

            if (programArgumentsToUse != null)
            {
                string logSuffix = shouldInherit ? " (inherited from base image)" : "";
                string message   = "Container program arguments set to " +
                                   $"[{string.Join(", ", programArgumentsToUse?.Select(ToJavascriptString))}]" +
                                   logSuffix;
                buildConfiguration.GetEventHandlers().Dispatch(LogEvent.Lifecycle(message));
            }

            return(programArgumentsToUse);
        }
Exemple #3
0
        /**
         * Computes the image entrypoint. If {@link ContainerConfiguration#getEntrypoint()} is null, the
         * entrypoint is inherited from the base image. Otherwise {@link
         * ContainerConfiguration#getEntrypoint()} is returned.
         *
         * @param baseImage the base image
         * @param containerConfiguration the container configuration
         * @return the container entrypoint
         */
        private ImmutableArray <string>?ComputeEntrypoint(
            Image baseImage, IContainerConfiguration containerConfiguration)
        {
            bool shouldInherit =
                baseImage.GetEntrypoint() != null && containerConfiguration.GetEntrypoint() == null;

            ImmutableArray <string>?entrypointToUse =
                shouldInherit ? baseImage.GetEntrypoint() : containerConfiguration.GetEntrypoint();

            if (entrypointToUse != null)
            {
                string logSuffix = shouldInherit ? " (inherited from base image)" : "";
                string message   = "Container entrypoint set to " +
                                   $"[{string.Join(", ", entrypointToUse?.Select(ToJavascriptString))}]" +
                                   logSuffix;
                buildConfiguration.GetEventHandlers().Dispatch(LogEvent.Lifecycle(""));
                buildConfiguration.GetEventHandlers().Dispatch(LogEvent.Lifecycle(message));
            }

            return(entrypointToUse);
        }
Exemple #4
0
        public void TestGetContainerBuilder_BaseImage()
        {
            LayerConfiguration[] layerConfigurations = new[] { new LayerConfiguration("appLayer", ImmutableArray <LayerEntry> .Empty) };
            var cliConfiguration = new FibCliConfiguration
            {
                ImageFormat = ImageFormat.OCI,
                ImageLayers = layerConfigurations,
                BaseImage   = "reg.io/base-image:tag",
                Entrypoint  = new[] { "Entrypoint" },
                Cmd         = new[] { "Program", "Arguments" },
                Environment = new Dictionary <string, string> {
                    ["Var"] = "Value"
                },
                ImageWorkingDirectory = AbsoluteUnixPath.Get("/working/dir"),
                ImageUser             = "******",
                Ports   = new[] { Port.Tcp(5000) },
                Volumes = new[] { AbsoluteUnixPath.Get("/volume") },
                Labels  = new Dictionary <string, string> {
                    ["Label"] = "data"
                },
                ReproducableBuild = true
            };

            var builder = cliConfiguration.GetContainerBuilder();

            var configuration = builder.ToBuildConfiguration(Containerizer.To(DockerDaemonImage.Named("target-image")));

            Assert.AreEqual(ManifestFormat.OCI, configuration.GetTargetFormat());
            Assert.AreEqual(layerConfigurations, configuration.GetLayerConfigurations());

            ImageConfiguration imageConfiguration = configuration.GetBaseImageConfiguration();

            Assert.AreEqual("reg.io", imageConfiguration.GetImageRegistry());
            Assert.AreEqual("base-image", imageConfiguration.GetImageRepository());
            Assert.AreEqual("tag", imageConfiguration.GetImageTag());

            IContainerConfiguration containerConfiguration = configuration.GetContainerConfiguration();

            Assert.AreEqual(new[] { "Entrypoint" }, containerConfiguration.GetEntrypoint());
            Assert.AreEqual(new[] { "Program", "Arguments" }, containerConfiguration.GetProgramArguments());
            Assert.AreEqual(new Dictionary <string, string> {
                ["Var"] = "Value"
            }, containerConfiguration.GetEnvironmentMap());
            Assert.AreEqual(AbsoluteUnixPath.Get("/working/dir"), containerConfiguration.GetWorkingDirectory());
            Assert.AreEqual("user", containerConfiguration.GetUser());
            Assert.AreEqual(new[] { Port.Tcp(5000) }, containerConfiguration.GetExposedPorts());
            Assert.AreEqual(new[] { AbsoluteUnixPath.Get("/volume") }, containerConfiguration.GetVolumes());
            Assert.AreEqual(new Dictionary <string, string> {
                ["Label"] = "data"
            }, containerConfiguration.GetLabels());
            Assert.AreEqual(ContainerConfiguration.DefaultCreationTime, containerConfiguration.GetCreationTime());
        }
        public void TestToBuildConfiguration_containerConfigurationSet()
        {
            FibContainerBuilder fibContainerBuilder =
                new FibContainerBuilder(RegistryImage.Named("base/image"), buildConfigurationBuilder)
                .SetEntrypoint(new[] { "entry", "point" })
                .SetEnvironment(ImmutableDictionary.CreateRange(new Dictionary <string, string> {
                ["name"] = "value"
            }))
                .SetExposedPorts(ImmutableHashSet.Create(Port.Tcp(1234), Port.Udp(5678)))
                .SetLabels(ImmutableDictionary.CreateRange(new Dictionary <string, string> {
                ["key"] = "value"
            }))
                .SetProgramArguments(new[] { "program", "arguments" })
                .SetCreationTime(DateTimeOffset.FromUnixTimeMilliseconds(1000))
                .SetUser("user")
                .SetWorkingDirectory(AbsoluteUnixPath.Get("/working/directory"));

            BuildConfiguration buildConfiguration =
                fibContainerBuilder.ToBuildConfiguration(
                    Containerizer.To(RegistryImage.Named("target/image")));
            IContainerConfiguration containerConfiguration = buildConfiguration.GetContainerConfiguration();

            Assert.AreEqual(new[] { "entry", "point" }, containerConfiguration.GetEntrypoint());
            Assert.AreEqual(
                ImmutableDictionary.CreateRange(new Dictionary <string, string> {
                ["name"] = "value"
            }), containerConfiguration.GetEnvironmentMap());
            Assert.AreEqual(
                ImmutableHashSet.Create(Port.Tcp(1234), Port.Udp(5678)), containerConfiguration.GetExposedPorts());
            Assert.AreEqual(ImmutableDictionary.CreateRange(new Dictionary <string, string> {
                ["key"] = "value"
            }), containerConfiguration.GetLabels());
            Assert.AreEqual(
                new[] { "program", "arguments" }, containerConfiguration.GetProgramArguments());
            Assert.AreEqual(Instant.FromUnixTimeMilliseconds(1000), containerConfiguration.GetCreationTime());
            Assert.AreEqual("user", containerConfiguration.GetUser());
            Assert.AreEqual(
                AbsoluteUnixPath.Get("/working/directory"), containerConfiguration.GetWorkingDirectory());
        }