Exemplo n.º 1
0
        public void TestFromPath_windows()
        {
            Assume.That(RuntimeInformation.IsOSPlatform(OSPlatform.Windows));

            Assert.AreEqual(
                "/absolute/path", AbsoluteUnixPath.FromPath(Paths.Get("T:\\absolute\\path")).ToString());
        }
Exemplo n.º 2
0
        public static async Task CreateImageAsync()
        {
            SystemPath root      = imageLocation.GetRoot().ToPath();
            SystemPath fileA     = Files.CreateFile(root.Resolve("fileA.txt"));
            SystemPath fileB     = Files.CreateFile(root.Resolve("fileB.txt"));
            SystemPath fileC     = Files.CreateFile(root.Resolve("fileC.txt"));
            SystemPath subdir    = Files.CreateDirectory(root.Resolve("dir"));
            SystemPath subsubdir = Files.CreateDirectory(subdir.Resolve("subdir"));

            Files.CreateFile(subdir.Resolve("fileD.txt"));
            Files.CreateFile(subsubdir.Resolve("fileE.txt"));

            imageTar = new FileInfo(Path.Combine(imageLocation.GetRoot().FullName, "image.tar"));
            Containerizer containerizer =
                Containerizer.To(TarImage.Named("fibdotnet-core/reproducible").SaveTo(imageTar.ToPath()));

            await FibContainerBuilder.FromScratch()
            .SetEntrypoint("echo", "Hello World")
            .AddLayer(ImmutableArray.Create(fileA), AbsoluteUnixPath.Get("/app"))
            // layer with out-of-order files
            .AddLayer(ImmutableArray.Create(fileC, fileB), "/app")
            .AddLayer(
                LayerConfiguration.CreateBuilder()
                .AddEntryRecursive(subdir, AbsoluteUnixPath.Get("/app"))
                .Build())
            .ContainerizeAsync(containerizer).ConfigureAwait(false);
        }
Exemplo n.º 3
0
        public async Task TestBuild_timestampNonDefaultAsync()
        {
            SystemPath file = CreateFile(temporaryFolder.GetRoot().ToPath(), "fileA", "some content", 54321);

            IBlob blob =
                new ReproducibleLayerBuilder(
                    ImmutableArray.Create(
                        new LayerEntry(
                            file,
                            AbsoluteUnixPath.Get("/fileA"),
                            FilePermissions.DefaultFilePermissions,
                            Instant.FromUnixTimeSeconds(123))))
                .Build();

            SystemPath tarFile = temporaryFolder.NewFile().ToPath();

            using (Stream @out = new BufferedStream(Files.NewOutputStream(tarFile)))
            {
                await blob.WriteToAsync(@out).ConfigureAwait(false);
            }

            // Reads the file back.
            using (TarInputStream @in = new TarInputStream(Files.NewInputStream(tarFile)))
            {
                Assert.AreEqual(
                    (Instant.FromUnixTimeSeconds(0) + Duration.FromSeconds(123)).ToDateTimeUtc(),
                    @in.GetNextEntry().TarHeader.ModTime);
            }
        }
Exemplo n.º 4
0
        public void TestVolumeMapToList()
        {
            ImmutableSortedDictionary <string, IDictionary <object, object> > input =
                new Dictionary <string, IDictionary <object, object> >
            {
                ["/var/job-result-data"] = ImmutableDictionary.Create <object, object>(),
                ["/var/log/my-app-logs"] = ImmutableDictionary.Create <object, object>()
            }.ToImmutableSortedDictionary();
            ImmutableHashSet <AbsoluteUnixPath> expected =
                ImmutableHashSet.Create(
                    AbsoluteUnixPath.Get("/var/job-result-data"),
                    AbsoluteUnixPath.Get("/var/log/my-app-logs"));

            Assert.AreEqual(expected, JsonToImageTranslator.VolumeMapToSet(input));

            ImmutableArray <IDictionary <string, IDictionary <object, object> > > badInputs =
                ImmutableArray.Create <IDictionary <string, IDictionary <object, object> > >(
                    ImmutableDic.Of <string, IDictionary <object, object> >("var/job-result-data", ImmutableDictionary.Create <object, object>()),
                    ImmutableDic.Of <string, IDictionary <object, object> >("log", ImmutableDictionary.Create <object, object>()),
                    ImmutableDic.Of <string, IDictionary <object, object> >("C:/udp", ImmutableDictionary.Create <object, object>()));

            foreach (IDictionary <string, IDictionary <object, object> > badInput in badInputs)
            {
                try
                {
                    JsonToImageTranslator.VolumeMapToSet(badInput);
                    Assert.Fail();
                }
                catch (BadContainerConfigurationFormatException)
                {
                }
            }
        }
Exemplo n.º 5
0
        public void TestAddEntryRecursive_defaults()
        {
            SystemPath testDirectory = TestResources.GetResource("core/layer");
            SystemPath testFile      = TestResources.GetResource("core/fileA");

            ILayerConfiguration layerConfiguration =
                LayerConfiguration.CreateBuilder()
                .AddEntryRecursive(testDirectory, AbsoluteUnixPath.Get("/app/layer/"))
                .AddEntryRecursive(testFile, AbsoluteUnixPath.Get("/app/fileA"))
                .Build();

            ImmutableHashSet <LayerEntry> expectedLayerEntries =
                ImmutableHashSet.Create(
                    DefaultLayerEntry(testDirectory, AbsoluteUnixPath.Get("/app/layer/")),
                    DefaultLayerEntry(testDirectory.Resolve("a"), AbsoluteUnixPath.Get("/app/layer/a/")),
                    DefaultLayerEntry(
                        testDirectory.Resolve("a/b"), AbsoluteUnixPath.Get("/app/layer/a/b/")),
                    DefaultLayerEntry(
                        testDirectory.Resolve("a/b/bar"), AbsoluteUnixPath.Get("/app/layer/a/b/bar/")),
                    DefaultLayerEntry(testDirectory.Resolve("c/"), AbsoluteUnixPath.Get("/app/layer/c")),
                    DefaultLayerEntry(
                        testDirectory.Resolve("c/cat/"), AbsoluteUnixPath.Get("/app/layer/c/cat")),
                    DefaultLayerEntry(testDirectory.Resolve("foo"), AbsoluteUnixPath.Get("/app/layer/foo")),
                    DefaultLayerEntry(testFile, AbsoluteUnixPath.Get("/app/fileA")));

            CollectionAssert.AreEquivalent(
                expectedLayerEntries, ImmutableHashSet.CreateRange(layerConfiguration.LayerEntries));
        }
Exemplo n.º 6
0
        public async Task SetUpAsync()
        {
            SystemPath directory = temporaryFolder.NewFolder().ToPath();

            Files.CreateDirectory(directory.Resolve("source"));
            Files.CreateFile(directory.Resolve("source/file"));
            Files.CreateDirectories(directory.Resolve("another/source"));
            Files.CreateFile(directory.Resolve("another/source/file"));

            layerBlob1   = Blobs.From("layerBlob1");
            layerDigest1 = await DigestOfAsync(Compress(layerBlob1)).ConfigureAwait(false);

            layerDiffId1 = await DigestOfAsync(layerBlob1).ConfigureAwait(false);

            layerSize1 = await SizeOfAsync(Compress(layerBlob1)).ConfigureAwait(false);

            layerEntries1 =
                ImmutableArray.Create(
                    DefaultLayerEntry(
                        directory.Resolve("source/file"), AbsoluteUnixPath.Get("/extraction/path")),
                    DefaultLayerEntry(
                        directory.Resolve("another/source/file"),
                        AbsoluteUnixPath.Get("/another/extraction/path")));

            layerBlob2   = Blobs.From("layerBlob2");
            layerDigest2 = await DigestOfAsync(Compress(layerBlob2)).ConfigureAwait(false);

            layerDiffId2 = await DigestOfAsync(layerBlob2).ConfigureAwait(false);

            layerSize2 = await SizeOfAsync(Compress(layerBlob2)).ConfigureAwait(false);

            layerEntries2 = ImmutableArray.Create <LayerEntry>();
        }
Exemplo n.º 7
0
        public void Test_smokeTest()
        {
            Image image =
                Image.CreateBuilder(ManifestFormat.V22)
                .SetCreated(Instant.FromUnixTimeSeconds(10000))
                .AddEnvironmentVariable("crepecake", "is great")
                .AddEnvironmentVariable("VARIABLE", "VALUE")
                .SetEntrypoint(new[] { "some", "command" })
                .SetProgramArguments(new[] { "arg1", "arg2" })
                .AddExposedPorts(ImmutableHashSet.Create(Port.Tcp(1000), Port.Tcp(2000)))
                .AddVolumes(
                    ImmutableHashSet.Create(
                        AbsoluteUnixPath.Get("/a/path"), AbsoluteUnixPath.Get("/another/path")))
                .SetUser("john")
                .AddLayer(mockLayer)
                .Build();

            Assert.AreEqual(ManifestFormat.V22, image.GetImageFormat());
            Assert.AreEqual(
                mockDescriptorDigest, image.GetLayers()[0].GetBlobDescriptor().GetDigest());
            Assert.AreEqual(Instant.FromUnixTimeSeconds(10000), image.GetCreated());
            Assert.AreEqual(
                ImmutableDic.Of("crepecake", "is great", "VARIABLE", "VALUE"), image.GetEnvironment());
            Assert.AreEqual(new[] { "some", "command" }, image.GetEntrypoint());
            Assert.AreEqual(new[] { "arg1", "arg2" }, image.GetProgramArguments());
            Assert.AreEqual(ImmutableHashSet.Create(Port.Tcp(1000), Port.Tcp(2000)), image.GetExposedPorts());
            Assert.AreEqual(
                ImmutableHashSet.Create(AbsoluteUnixPath.Get("/a/path"), AbsoluteUnixPath.Get("/another/path")),
                image.GetVolumes());
            Assert.AreEqual("john", image.GetUser());
        }
Exemplo n.º 8
0
        public void TestBuilder_workingDirectory()
        {
            ContainerConfiguration configuration =
                ContainerConfiguration.CreateBuilder().SetWorkingDirectory(AbsoluteUnixPath.Get("/path")).Build();

            Assert.AreEqual(AbsoluteUnixPath.Get("/path"), configuration.GetWorkingDirectory());
        }
Exemplo n.º 9
0
 private static LayerEntry DefaultLayerEntry(SystemPath source, AbsoluteUnixPath destination)
 {
     return(new LayerEntry(
                source,
                destination,
                LayerConfiguration.DefaultFilePermissionsProvider(source, destination),
                LayerConfiguration.DefaultModifiedTime));
 }
Exemplo n.º 10
0
        public void TestEquals()
        {
            AbsoluteUnixPath absoluteUnixPath1 = AbsoluteUnixPath.Get("/absolute/path");
            AbsoluteUnixPath absoluteUnixPath2 = AbsoluteUnixPath.Get("/absolute/path/");
            AbsoluteUnixPath absoluteUnixPath3 = AbsoluteUnixPath.Get("/another/path");

            Assert.AreEqual(absoluteUnixPath1, absoluteUnixPath2);
            Assert.AreNotEqual(absoluteUnixPath1, absoluteUnixPath3);
        }
Exemplo n.º 11
0
        public void TestBuilder_nullValues()
        {
            // Java arguments element should not be null.
            try
            {
                ContainerConfiguration.CreateBuilder().SetProgramArguments(new[] { "first", null });
                Assert.Fail("The IllegalArgumentException should be thrown.");
            }
            catch (ArgumentException ex)
            {
                Assert.That(ex.Message, Contains.Substring(Resources.NullProgramArgument));
            }

            // Entrypoint element should not be null.
            try
            {
                ContainerConfiguration.CreateBuilder().SetEntrypoint(new[] { "first", null });
                Assert.Fail("The IllegalArgumentException should be thrown.");
            }
            catch (ArgumentException ex)
            {
                Assert.That(ex.Message, Contains.Substring(Resources.NullEntrypointArgument));
            }

            // Exposed ports element should not be null.
            ISet <Port> badPorts = new HashSet <Port> {
                Port.Tcp(1000), null
            };

            try
            {
                ContainerConfiguration.CreateBuilder().SetExposedPorts(badPorts);
                Assert.Fail("The IllegalArgumentException should be thrown.");
            }
            catch (ArgumentException ex)
            {
                Assert.That(ex.Message, Contains.Substring(Resources.NullPort));
            }

            // Volume element should not be null.
            ISet <AbsoluteUnixPath> badVolumes =
                new HashSet <AbsoluteUnixPath> {
                AbsoluteUnixPath.Get("/"), null
            };

            try
            {
                ContainerConfiguration.CreateBuilder().SetVolumes(badVolumes);
                Assert.Fail("The IllegalArgumentException should be thrown.");
            }
            catch (ArgumentException ex)
            {
                Assert.That(ex.Message, Contains.Substring(Resources.NullVolume));
            }
        }
Exemplo n.º 12
0
        public async Task TestBuildAsync()
        {
            SystemPath layerDirectory = Paths.Get(TestResources.GetResource("core/layer").ToURI());
            SystemPath blobA          = Paths.Get(TestResources.GetResource("core/blobA").ToURI());

            ReproducibleLayerBuilder layerBuilder =
                new ReproducibleLayerBuilder(
                    LayerConfiguration.CreateBuilder()
                    .AddEntryRecursive(
                        layerDirectory, AbsoluteUnixPath.Get("/extract/here/apple/layer"))
                    .AddEntry(blobA, AbsoluteUnixPath.Get("/extract/here/apple/blobA"))
                    .AddEntry(blobA, AbsoluteUnixPath.Get("/extract/here/banana/blobA"))
                    .Build()
                    .LayerEntries);

            // Writes the layer tar to a temporary file.
            IBlob      unwrittenBlob = layerBuilder.Build();
            SystemPath temporaryFile = temporaryFolder.NewFile().ToPath();

            using (Stream temporaryFileOutputStream =
                       new BufferedStream(Files.NewOutputStream(temporaryFile)))
            {
                await unwrittenBlob.WriteToAsync(temporaryFileOutputStream).ConfigureAwait(false);
            }

            // Reads the file back.
            TarInputStream tarArchiveInputStream =
                new TarInputStream(Files.NewInputStream(temporaryFile));

            VerifyNextTarArchiveEntryIsDirectory(tarArchiveInputStream, "extract/");
            VerifyNextTarArchiveEntryIsDirectory(tarArchiveInputStream, "extract/here/");
            VerifyNextTarArchiveEntryIsDirectory(tarArchiveInputStream, "extract/here/apple/");
            VerifyNextTarArchiveEntry(tarArchiveInputStream, "extract/here/apple/blobA", blobA);
            VerifyNextTarArchiveEntryIsDirectory(tarArchiveInputStream, "extract/here/apple/layer/");
            VerifyNextTarArchiveEntryIsDirectory(tarArchiveInputStream, "extract/here/apple/layer/a/");
            VerifyNextTarArchiveEntryIsDirectory(tarArchiveInputStream, "extract/here/apple/layer/a/b/");
            VerifyNextTarArchiveEntry(
                tarArchiveInputStream,
                "extract/here/apple/layer/a/b/bar",
                Paths.Get(TestResources.GetResource("core/layer/a/b/bar").ToURI()));
            VerifyNextTarArchiveEntryIsDirectory(tarArchiveInputStream, "extract/here/apple/layer/c/");
            VerifyNextTarArchiveEntry(
                tarArchiveInputStream,
                "extract/here/apple/layer/c/cat",
                Paths.Get(TestResources.GetResource("core/layer/c/cat").ToURI()));
            VerifyNextTarArchiveEntry(
                tarArchiveInputStream,
                "extract/here/apple/layer/foo",
                Paths.Get(TestResources.GetResource("core/layer/foo").ToURI()));
            VerifyNextTarArchiveEntryIsDirectory(tarArchiveInputStream, "extract/here/banana/");
            VerifyNextTarArchiveEntry(tarArchiveInputStream, "extract/here/banana/blobA", blobA);
        }
Exemplo n.º 13
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());
        }
Exemplo n.º 14
0
 public void TestGet_notAbsolute()
 {
     try
     {
         AbsoluteUnixPath.Get("not/absolute");
         Assert.Fail();
     }
     catch (ArgumentException ex)
     {
         Assert.AreEqual(
             "Path does not start with forward slash (/): not/absolute", ex.Message);
     }
 }
Exemplo n.º 15
0
 public void TestResolve_Path_notRelative()
 {
     try
     {
         AbsoluteUnixPath.Get("/").Resolve(Paths.Get("/not/relative"));
         Assert.Fail();
     }
     catch (ArgumentException ex)
     {
         Assert.AreEqual(
             "Cannot resolve against absolute Path: " + Paths.Get("/not/relative"), ex.Message);
     }
 }
Exemplo n.º 16
0
        public void TestResolve_relativeUnixPath()
        {
            AbsoluteUnixPath absoluteUnixPath1 = AbsoluteUnixPath.Get("/");

            Assert.AreEqual(absoluteUnixPath1, absoluteUnixPath1.Resolve(""));
            Assert.AreEqual("/file", absoluteUnixPath1.Resolve("file").ToString());
            Assert.AreEqual("/relative/path", absoluteUnixPath1.Resolve("relative/path").ToString());

            AbsoluteUnixPath absoluteUnixPath2 = AbsoluteUnixPath.Get("/some/path");

            Assert.AreEqual(absoluteUnixPath2, absoluteUnixPath2.Resolve(""));
            Assert.AreEqual("/some/path/file", absoluteUnixPath2.Resolve("file").ToString());
            Assert.AreEqual(
                "/some/path/relative/path", absoluteUnixPath2.Resolve("relative/path").ToString());
        }
Exemplo n.º 17
0
        public void TestVolumeListToMap()
        {
            ImmutableHashSet <AbsoluteUnixPath> input =
                ImmutableHashSet.Create(
                    AbsoluteUnixPath.Get("/var/job-result-data"),
                    AbsoluteUnixPath.Get("/var/log/my-app-logs"));
            ImmutableSortedDictionary <string, IDictionary <object, object> > expected =
                new Dictionary <string, IDictionary <object, object> >
            {
                ["/var/job-result-data"] = ImmutableDictionary.Create <object, object>(),
                ["/var/log/my-app-logs"] = ImmutableDictionary.Create <object, object>()
            }.ToImmutableSortedDictionary();

            Assert.AreEqual(expected, ImageToJsonTranslator.VolumesSetToMap(input));
        }
Exemplo n.º 18
0
        //private static DescriptorDigest fakeDigest = DescriptorDigest.fromHash(new string('a', 64));

        private void SetUp(ManifestFormat imageFormat)
        {
            Image.Builder testImageBuilder =
                Image.CreateBuilder(imageFormat)
                .SetCreated(Instant.FromUnixTimeSeconds(20))
                .SetArchitecture("wasm")
                .SetOs("js")
                .AddEnvironmentVariable("VAR1", "VAL1")
                .AddEnvironmentVariable("VAR2", "VAL2")
                .SetEntrypoint(new[] { "some", "entrypoint", "command" })
                .SetProgramArguments(new[] { "arg1", "arg2" })
                .SetHealthCheck(
                    DockerHealthCheck.FromCommand(ImmutableArray.Create("CMD-SHELL", "/checkhealth"))
                    .SetInterval(Duration.FromSeconds(3))
                    .SetTimeout(Duration.FromSeconds(1))
                    .SetStartPeriod(Duration.FromSeconds(2))
                    .SetRetries(3)
                    .Build())
                .AddExposedPorts(ImmutableHashSet.Create(Port.Tcp(1000), Port.Tcp(2000), Port.Udp(3000)))
                .AddVolumes(
                    ImmutableHashSet.Create(
                        AbsoluteUnixPath.Get("/var/job-result-data"),
                        AbsoluteUnixPath.Get("/var/log/my-app-logs")))
                .AddLabels(ImmutableDic.Of("key1", "value1", "key2", "value2"))
                .SetWorkingDirectory("/some/workspace")
                .SetUser("tomcat");

            DescriptorDigest fakeDigest =
                DescriptorDigest.FromDigest(
                    "sha256:8c662931926fa990b41da3c9f42663a537ccd498130030f9149173a0493832ad");

            testImageBuilder.AddLayer(
                new FakeLayer(fakeDigest));
            testImageBuilder.AddHistory(
                HistoryEntry.CreateBuilder()
                .SetCreationTimestamp(Instant.FromUnixTimeSeconds(0))
                .SetAuthor("Bazel")
                .SetCreatedBy("bazel build ...")
                .SetEmptyLayer(true)
                .Build());
            testImageBuilder.AddHistory(
                HistoryEntry.CreateBuilder()
                .SetCreationTimestamp(Instant.FromUnixTimeSeconds(20))
                .SetAuthor("Fib")
                .SetCreatedBy("fib")
                .Build());
            imageToJsonTranslator = new ImageToJsonTranslator(testImageBuilder.Build());
        }
Exemplo n.º 19
0
 /**
  * Lists the files in the {@code resourcePath} resources directory and builds a {@link
  * LayerConfiguration} from those files.
  */
 private static ILayerConfiguration MakeLayerConfiguration(
     string resourcePath, string pathInContainer)
 {
     IEnumerable <SystemPath> fileStream =
         Files.List(Paths.Get(TestResources.GetResource(resourcePath).ToURI()));
     {
         LayerConfiguration.Builder layerConfigurationBuilder = LayerConfiguration.CreateBuilder();
         foreach (SystemPath i in fileStream)
         {
             ((Func <SystemPath, LayerConfiguration.Builder>)(sourceFile =>
                                                              layerConfigurationBuilder.AddEntry(
                                                                  sourceFile, AbsoluteUnixPath.Get(pathInContainer + sourceFile.GetFileName()))))(i);
         }
         return(layerConfigurationBuilder.Build());
     }
 }
Exemplo n.º 20
0
        public async Task TestContainerize_configuredExecutorAsync()
        {
            FibContainerBuilder fibContainerBuilder =
                new FibContainerBuilder(RegistryImage.Named("base/image"), buildConfigurationBuilder)
                .SetEntrypoint(new[] { "entry", "point" })
                .SetEnvironment(ImmutableDic.Of("name", "value"))
                .SetExposedPorts(ImmutableHashSet.Create(Port.Tcp(1234), Port.Udp(5678)))
                .SetLabels(ImmutableDic.Of("key", "value"))
                .SetProgramArguments(new[] { "program", "arguments" })
                .SetCreationTime(DateTimeOffset.FromUnixTimeMilliseconds(1000))
                .SetUser("user")
                .SetWorkingDirectory(AbsoluteUnixPath.Get("/working/directory"));
            IContainerizer mockContainerizer = CreateMockContainerizer();

            await fibContainerBuilder.ContainerizeAsync(mockContainerizer).ConfigureAwait(false);
        }
Exemplo n.º 21
0
        public async Task TestBuild_permissionsAsync()
        {
            SystemPath testRoot = temporaryFolder.GetRoot().ToPath();
            SystemPath folder   = Files.CreateDirectories(testRoot.Resolve("files1"));
            SystemPath fileA    = CreateFile(testRoot, "fileA", "abc", 54321);
            SystemPath fileB    = CreateFile(testRoot, "fileB", "def", 54321);

            IBlob blob =
                new ReproducibleLayerBuilder(
                    ImmutableArray.Create(
                        DefaultLayerEntry(fileA, AbsoluteUnixPath.Get("/somewhere/fileA")),
                        new LayerEntry(
                            fileB,
                            AbsoluteUnixPath.Get("/somewhere/fileB"),
                            FilePermissions.FromOctalString("123"),
                            LayerConfiguration.DefaultModifiedTime),
                        new LayerEntry(
                            folder,
                            AbsoluteUnixPath.Get("/somewhere/folder"),
                            FilePermissions.FromOctalString("456"),
                            LayerConfiguration.DefaultModifiedTime)))
                .Build();

            SystemPath tarFile = temporaryFolder.NewFile().ToPath();

            using (Stream @out = new BufferedStream(Files.NewOutputStream(tarFile)))
            {
                await blob.WriteToAsync(@out).ConfigureAwait(false);
            }

            using (TarInputStream @in = new TarInputStream(Files.NewInputStream(tarFile)))
            {
                // Root folder (default folder permissions)
                TarEntry rootEntry = @in.GetNextEntry();
                // fileA (default file permissions)
                TarEntry fileAEntry = @in.GetNextEntry();
                // fileB (custom file permissions)
                TarEntry fileBEntry = @in.GetNextEntry();
                // folder (custom folder permissions)
                TarEntry folderEntry = @in.GetNextEntry();
                Assert.AreEqual("755", rootEntry.GetMode().ToOctalString());
                Assert.AreEqual("644", fileAEntry.GetMode().ToOctalString());
                Assert.AreEqual("123", fileBEntry.GetMode().ToOctalString());
                Assert.AreEqual("456", folderEntry.GetMode().ToOctalString());
            }
        }
Exemplo n.º 22
0
        public void SetUp()
        {
            SystemPath folder = temporaryFolder.NewFolder().ToPath();
            SystemPath file1  = Files.CreateDirectory(folder.Resolve("files"));
            SystemPath file2  = Files.CreateFile(folder.Resolve("files").Resolve("two"));
            SystemPath file3  = Files.CreateFile(folder.Resolve("gile"));

            LayerEntry testLayerEntry1 = DefaultLayerEntry(file1, AbsoluteUnixPath.Get("/extraction/path"));
            LayerEntry testLayerEntry2 = DefaultLayerEntry(file2, AbsoluteUnixPath.Get("/extraction/path"));
            LayerEntry testLayerEntry3 = DefaultLayerEntry(file3, AbsoluteUnixPath.Get("/extraction/path"));
            LayerEntry testLayerEntry4 =
                new LayerEntry(
                    file3,
                    AbsoluteUnixPath.Get("/extraction/path"),
                    FilePermissions.FromOctalString("755"),
                    LayerConfiguration.DefaultModifiedTime);
            LayerEntry testLayerEntry5 =
                DefaultLayerEntry(file3, AbsoluteUnixPath.Get("/extraction/patha"));
            LayerEntry testLayerEntry6 =
                new LayerEntry(
                    file3,
                    AbsoluteUnixPath.Get("/extraction/patha"),
                    FilePermissions.FromOctalString("755"),
                    LayerConfiguration.DefaultModifiedTime);

            outOfOrderLayerEntries =
                ImmutableArray.Create(
                    testLayerEntry4,
                    testLayerEntry2,
                    testLayerEntry6,
                    testLayerEntry3,
                    testLayerEntry1,
                    testLayerEntry5);
            inOrderLayerEntries =
                ImmutableArray.Create(
                    testLayerEntry1,
                    testLayerEntry2,
                    testLayerEntry3,
                    testLayerEntry4,
                    testLayerEntry5,
                    testLayerEntry6);
        }
Exemplo n.º 23
0
        public async Task TestGenerateSelector_fileModifiedAsync()
        {
            SystemPath layerFile = temporaryFolder.NewFolder("testFolder").ToPath().Resolve("file");

            Files.Write(layerFile, Encoding.UTF8.GetBytes("hello"));
            Files.SetLastModifiedTime(layerFile, FileTime.From(Instant.FromUnixTimeSeconds(0)));
            LayerEntry       layerEntry       = DefaultLayerEntry(layerFile, AbsoluteUnixPath.Get("/extraction/path"));
            DescriptorDigest expectedSelector =
                await GenerateSelectorAsync(ImmutableArray.Create(layerEntry)).ConfigureAwait(false);

            // Verify that changing modified time generates a different selector
            Files.SetLastModifiedTime(layerFile, FileTime.From(Instant.FromUnixTimeSeconds(1)));
            Assert.AreNotEqual(
                expectedSelector, await GenerateSelectorAsync(ImmutableArray.Create(layerEntry)).ConfigureAwait(false));

            // Verify that changing modified time back generates same selector
            Files.SetLastModifiedTime(layerFile, FileTime.From(Instant.FromUnixTimeSeconds(0)));
            Assert.AreEqual(
                expectedSelector,
                await GenerateSelectorAsync(ImmutableArray.Create(layerEntry)).ConfigureAwait(false));
        }
Exemplo n.º 24
0
 private ContainerConfiguration(
     Instant creationTime,
     ImmutableArray <string>?entrypoint,
     ImmutableArray <string>?programArguments,
     ImmutableDictionary <string, string> environmentMap,
     ImmutableHashSet <Port> exposedPorts,
     ImmutableHashSet <AbsoluteUnixPath> volumes,
     ImmutableDictionary <string, string> labels,
     string user,
     AbsoluteUnixPath workingDirectory)
 {
     this.creationTime     = creationTime;
     this.entrypoint       = entrypoint;
     this.programArguments = programArguments;
     this.environmentMap   = environmentMap;
     this.exposedPorts     = exposedPorts;
     this.volumes          = volumes;
     this.labels           = labels;
     this.user             = user;
     this.workingDirectory = workingDirectory;
 }
Exemplo n.º 25
0
        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());
        }
Exemplo n.º 26
0
        /**
         * Converts a map of volumes strings to a set of {@link AbsoluteUnixPath}s (e.g. {@code {@code
         * {"/var/log/my-app-logs":{}}} => AbsoluteUnixPath().get("/var/log/my-app-logs")}).
         *
         * @param volumeMap the map to convert
         * @return a set of {@link AbsoluteUnixPath}s
         */

        public static ImmutableHashSet <AbsoluteUnixPath> VolumeMapToSet(IDictionary <string, IDictionary <object, object> > volumeMap)
        {
            if (volumeMap == null)
            {
                return(ImmutableHashSet.Create <AbsoluteUnixPath>());
            }

            ImmutableHashSet <AbsoluteUnixPath> .Builder volumeList = ImmutableHashSet.CreateBuilder <AbsoluteUnixPath>();
            foreach (string volume in volumeMap.Keys)
            {
                try
                {
                    volumeList.Add(AbsoluteUnixPath.Get(volume));
                }
                catch (ArgumentException)
                {
                    throw new BadContainerConfigurationFormatException("Invalid volume path: " + volume);
                }
            }

            return(volumeList.ToImmutable());
        }
Exemplo n.º 27
0
        public async Task TestToBlob_reproducibilityAsync()
        {
            SystemPath testRoot = temporaryFolder.GetRoot().ToPath();
            SystemPath root1    = Files.CreateDirectories(testRoot.Resolve("files1"));
            SystemPath root2    = Files.CreateDirectories(testRoot.Resolve("files2"));

            // TODO: Currently this test only covers variation in order and modified time, even though
            // TODO: the code is designed to clean up userid/groupid, this test does not check that yet.
            const string contentA = "abcabc";
            SystemPath   fileA1   = CreateFile(root1, "fileA", contentA, 10000);
            SystemPath   fileA2   = CreateFile(root2, "fileA", contentA, 20000);
            const string contentB = "yumyum";
            SystemPath   fileB1   = CreateFile(root1, "fileB", contentB, 10000);
            SystemPath   fileB2   = CreateFile(root2, "fileB", contentB, 20000);

            // check if modified times are off
            Assert.AreNotEqual(Files.GetLastModifiedTime(fileA1), Files.GetLastModifiedTime(fileA2));
            Assert.AreNotEqual(Files.GetLastModifiedTime(fileB1), Files.GetLastModifiedTime(fileB2));

            // create layers of exact same content but ordered differently and with different timestamps
            IBlob layer =
                new ReproducibleLayerBuilder(
                    ImmutableArray.Create(
                        DefaultLayerEntry(fileA1, AbsoluteUnixPath.Get("/somewhere/fileA")),
                        DefaultLayerEntry(fileB1, AbsoluteUnixPath.Get("/somewhere/fileB"))))
                .Build();
            IBlob reproduced =
                new ReproducibleLayerBuilder(
                    ImmutableArray.Create(
                        DefaultLayerEntry(fileB2, AbsoluteUnixPath.Get("/somewhere/fileB")),
                        DefaultLayerEntry(fileA2, AbsoluteUnixPath.Get("/somewhere/fileA"))))
                .Build();

            byte[] layerContent = await Blobs.WriteToByteArrayAsync(layer).ConfigureAwait(false);

            byte[] reproducedLayerContent = await Blobs.WriteToByteArrayAsync(reproduced).ConfigureAwait(false);

            Assert.AreEqual(layerContent, reproducedLayerContent);
        }
Exemplo n.º 28
0
        public void TestGenerateSelector_permissionsModified()
        {
            SystemPath layerFile = temporaryFolder.NewFolder("testFolder").ToPath().Resolve("file");

            Files.Write(layerFile, Encoding.UTF8.GetBytes("hello"));
            LayerEntry layerEntry111 =
                new LayerEntry(
                    layerFile,
                    AbsoluteUnixPath.Get("/extraction/path"),
                    FilePermissions.FromOctalString("111"),
                    LayerConfiguration.DefaultModifiedTime);
            LayerEntry layerEntry222 =
                new LayerEntry(
                    layerFile,
                    AbsoluteUnixPath.Get("/extraction/path"),
                    FilePermissions.FromOctalString("222"),
                    LayerConfiguration.DefaultModifiedTime);

            // Verify that changing permissions generates a different selector
            Assert.AreNotEqual(
                GenerateSelectorAsync(ImmutableArray.Create(layerEntry111)),
                GenerateSelectorAsync(ImmutableArray.Create(layerEntry222)));
        }
Exemplo n.º 29
0
 /**
  * Sets the working directory in the container.
  *
  * @param workingDirectory the working directory
  * @return this
  */
 public Builder SetWorkingDirectory(AbsoluteUnixPath workingDirectory)
 {
     this.workingDirectory = workingDirectory;
     return(this);
 }
Exemplo n.º 30
0
 public void AddVolume(AbsoluteUnixPath volume)
 {
     (volumes ?? (volumes = new HashSet <AbsoluteUnixPath>())).Add(volume);
 }