Esempio n. 1
0
        public void SetUp()
        {
            mockProcessBuilder = Mock.Of <IProcessBuilder>();
            mockProcess        = Mock.Of <IProcess>();
            imageTarball       = Mock.Of <IImageTarball>();
            Mock.Get(mockProcessBuilder).Setup(m => m.Start()).Returns(mockProcess);

            Mock.Get(imageTarball).Setup(i => i.WriteToAsync(It.IsAny <Stream>()))
            .Returns <Stream>(async s => await s.WriteAsync(Encoding.UTF8.GetBytes("fib")));
        }
Esempio n. 2
0
        /**
         * Loads an image tarball into the Docker daemon.
         *
         * @see <a
         *     href="https://docs.docker.com/engine/reference/commandline/load/">https://docs.docker.com/engine/reference/commandline/load</a>
         * @param imageTarball the built container tarball.
         * @return stdout from {@code docker}.
         * @throws InterruptedException if the 'docker load' process is interrupted.
         * @throws IOException if streaming the blob to 'docker load' fails.
         */
        public async Task <string> LoadAsync(IImageTarball imageTarball)
        {
            imageTarball = imageTarball ?? throw new ArgumentNullException(nameof(imageTarball));
            // Runs 'docker load'.
            IProcess dockerProcess = Docker("load");

            using (Stream stdin = dockerProcess.GetOutputStream())
            {
                try
                {
                    await imageTarball.WriteToAsync(stdin).ConfigureAwait(false);
                }
                catch (IOException ex)
                {
                    // Tries to read from stderr.
                    string error;
                    try
                    {
                        using (TextReader stderr = dockerProcess.GetErrorReader())
                        {
                            error = await stderr.ReadToEndAsync().ConfigureAwait(false);
                        }
                    }
                    catch (IOException e)
                    {
                        // This ignores exceptions from reading stderr and throws the original exception from
                        // writing to stdin.
                        Debug.WriteLine(e);
                        error = null;
                    }
                    if (error is null)
                    {
                        throw;
                    }
                    throw new IOException("'docker load' command failed with error: " + error, ex);
                }
            }

            using (Stream stdoutStream = dockerProcess.GetInputStream())
                using (StreamReader stdout = new StreamReader(stdoutStream, Encoding.UTF8))
                {
                    string output = await stdout.ReadToEndAsync().ConfigureAwait(false);

                    if (dockerProcess.WaitFor() != 0)
                    {
                        string errMessage = await GetErrorMessageAsync(dockerProcess).ConfigureAwait(false);

                        throw new IOException("'docker load' command failed with output: " + errMessage);
                    }

                    return(output);
                }
        }