Пример #1
0
        public async Task WriteToAsync(Stream @out)
        {
            TarStreamBuilder tarStreamBuilder = new TarStreamBuilder();
            DockerLoadManifestEntryTemplate manifestTemplate = new DockerLoadManifestEntryTemplate();

            // Adds all the layers to the tarball and manifest.
            foreach (ILayer layer in image.GetLayers())
            {
                string layerName = layer.GetBlobDescriptor().GetDigest().GetHash() + LAYER_FILE_EXTENSION;

                tarStreamBuilder.AddBlobEntry(
                    layer.GetBlob(), layer.GetBlobDescriptor().GetSize(), layerName);
                manifestTemplate.AddLayerFile(layerName);
            }

            // Adds the container configuration to the tarball.
            ContainerConfigurationTemplate containerConfiguration =
                new ImageToJsonTranslator(image).GetContainerConfiguration();

            tarStreamBuilder.AddByteEntry(
                JsonTemplateMapper.ToByteArray(containerConfiguration),
                CONTAINER_CONFIGURATION_JSON_FILE_NAME);

            // Adds the manifest to tarball.
            manifestTemplate.SetRepoTags(imageReference.ToStringWithTag());
            tarStreamBuilder.AddByteEntry(
                JsonTemplateMapper.ToByteArray(new List <DockerLoadManifestEntryTemplate> {
                manifestTemplate
            }),
                MANIFEST_JSON_FILE_NAME);

            await tarStreamBuilder.WriteAsTarArchiveToAsync(@out).ConfigureAwait(false);
        }
Пример #2
0
        public async Task TestToBlob_multiByteAsync()
        {
            testTarStreamBuilder.AddByteEntry(Encoding.UTF8.GetBytes("日本語"), "test");
            testTarStreamBuilder.AddByteEntry(Encoding.UTF8.GetBytes("asdf"), "crepecake");
            testTarStreamBuilder.AddBlobEntry(
                Blobs.From("fib"), Encoding.UTF8.GetBytes("fib").Length, "fib");

            // Writes the BLOB and captures the output.
            MemoryStream tarByteOutputStream = new MemoryStream();

            using (Stream compressorStream = new GZipStream(tarByteOutputStream, CompressionMode.Compress))
            {
                await testTarStreamBuilder.WriteAsTarArchiveToAsync(compressorStream).ConfigureAwait(false);
            }

            // Rearrange the output into input for verification.
            MemoryStream byteArrayInputStream =
                new MemoryStream(tarByteOutputStream.ToArray());
            Stream tarByteInputStream = new GZipStream(byteArrayInputStream, CompressionMode.Decompress);

            using (TarInputStream tarArchiveInputStream = new TarInputStream(tarByteInputStream))
            {
                // Verify multi-byte characters are written/read correctly
                TarEntry headerFile = tarArchiveInputStream.GetNextEntry();
                Assert.AreEqual("test", headerFile.Name);
                Assert.AreEqual(
                    "日本語", Encoding.UTF8.GetString(ByteStreams.ToByteArray(tarArchiveInputStream)));

                headerFile = tarArchiveInputStream.GetNextEntry();
                Assert.AreEqual("crepecake", headerFile.Name);
                Assert.AreEqual(
                    "asdf", Encoding.UTF8.GetString(ByteStreams.ToByteArray(tarArchiveInputStream)));

                headerFile = tarArchiveInputStream.GetNextEntry();
                Assert.AreEqual("fib", headerFile.Name);
                Assert.AreEqual(
                    "fib", Encoding.UTF8.GetString(ByteStreams.ToByteArray(tarArchiveInputStream)));

                Assert.IsNull(tarArchiveInputStream.GetNextEntry());
            }
        }