public async Task TestScratchAsync() { ImageReference targetImageReference = ImageReference.Of("localhost:5002", "fibdotnet-core", "basic-scratch"); await FibContainerBuilder.FromScratch() .ContainerizeAsync( Containerizer.To( RegistryImage.Named(targetImageReference) .AddCredentialRetriever( () => Maybe.Of(Credential.From("username", "password")))) .SetAllowInsecureRegistries(true)).ConfigureAwait(false); // Check that resulting image has no layers localRegistry.Pull(targetImageReference.ToString()); string inspectOutput = new Command("docker", "inspect", targetImageReference.ToString()).Run(); Assert.That(inspectOutput, Does.Not.Contain("\"Layers\": ["), "docker inspect output contained layers: " + inspectOutput); }
/** * Tags the image referenced by {@code originalImageReference} with a new image reference {@code * newImageReference}. * * @param originalImageReference the existing image reference on the Docker daemon * @param newImageReference the new image reference * @see <a * href="https://docs.docker.com/engine/reference/commandline/tag/">https://docs.docker.com/engine/reference/commandline/tag/</a> * @throws InterruptedException if the 'docker tag' process is interrupted. * @throws IOException if an I/O exception occurs or {@code docker tag} failed */ public async Task TagAsync(IImageReference originalImageReference, ImageReference newImageReference) { originalImageReference = originalImageReference ?? throw new ArgumentNullException(nameof(originalImageReference)); newImageReference = newImageReference ?? throw new ArgumentNullException(nameof(newImageReference)); // Runs 'docker tag'. IProcess dockerProcess = Docker("tag", originalImageReference.ToString(), newImageReference.ToString()); if (dockerProcess.WaitFor() != 0) { using (StreamReader stderr = new StreamReader(dockerProcess.GetErrorStream(), Encoding.UTF8)) { string errorMessage = await stderr.ReadToEndAsync().ConfigureAwait(false); throw new IOException( "'docker tag' command failed with error: " + errorMessage); } } }
public async Task TestBasic_HelloWorldAsync() { ImageReference targetImageReference = ImageReference.Of("localhost:5002", "fibdotnet-core", "basic-helloworld"); FibContainer fibContainer = await FibContainerBuilder.From("busybox") .SetEntrypoint("echo", "Hello World") .ContainerizeAsync( Containerizer.To( RegistryImage.Named(targetImageReference) .AddCredentialRetriever( () => Maybe.Of(Credential.From("username", "password")))) .SetAllowInsecureRegistries(true) .AddEventHandler <IFibEvent>(e => TestContext.Out.WriteLine(e))).ConfigureAwait(false); Assert.AreEqual("Hello World\n", PullAndRunBuiltImage(targetImageReference.ToString())); Assert.AreEqual( "Hello World\n", PullAndRunBuiltImage( targetImageReference.WithTag(fibContainer.GetDigest().ToString()).ToString())); }
public async Task TestOfflineAsync() { SystemPath cacheDirectory = cacheFolder.GetRoot().ToPath(); ImageReference targetImageReferenceOnline = ImageReference.Of("localhost:5001", "fibdotnet-core", "basic-online"); ImageReference targetImageReferenceOffline = ImageReference.Of("localhost:5001", "fibdotnet-core", "basic-offline"); FibContainerBuilder fibContainerBuilder = FibContainerBuilder.From("localhost:5001/busybox").SetEntrypoint("echo", "Hello World"); // Should fail since Fib can't build to registry offline try { await fibContainerBuilder.ContainerizeAsync( Containerizer.To(RegistryImage.Named(targetImageReferenceOffline)).SetOfflineMode(true)).ConfigureAwait(false); Assert.Fail(); } catch (InvalidOperationException ex) { Assert.AreEqual("Cannot build to a container registry in offline mode", ex.Message); } // Should fail since Fib hasn't cached the base image yet try { await fibContainerBuilder.ContainerizeAsync( Containerizer.To(DockerDaemonImage.Named(targetImageReferenceOffline)) .SetBaseImageLayersCache(cacheDirectory) .SetOfflineMode(true)).ConfigureAwait(false); Assert.Fail(); } catch (IOException ex) { Assert.AreEqual( "Cannot run Fib in offline mode; localhost:5001/busybox not found in local Fib cache", ex.Message); } using (LocalRegistry tempRegistry = new LocalRegistry(5001)) { await tempRegistry.StartAsync().ConfigureAwait(false); tempRegistry.PullAndPushToLocal("busybox", "busybox"); // Run online to cache the base image await fibContainerBuilder.ContainerizeAsync( Containerizer.To(DockerDaemonImage.Named(targetImageReferenceOnline)) .SetBaseImageLayersCache(cacheDirectory) .SetAllowInsecureRegistries(true)).ConfigureAwait(false); } // Run again in offline mode, should succeed this time await fibContainerBuilder.ContainerizeAsync( Containerizer.To(DockerDaemonImage.Named(targetImageReferenceOffline)) .SetBaseImageLayersCache(cacheDirectory) .SetOfflineMode(true)).ConfigureAwait(false); // Verify output Assert.AreEqual( "Hello World\n", new Command("docker", "run", "--rm", targetImageReferenceOffline.ToString()).Run()); }