public async Task TestSteps_ForBuildToDockerRegistry_MultipleTagsAsync() { await BuildRegistryImageAsync( ImageReference.Of("gcr.io", "distroless/java", DISTROLESS_DIGEST), ImageReference.Of("localhost:5000", "testimage", "testtag"), new[] { "testtag2", "testtag3" }).ConfigureAwait(false); progressChecker.CheckCompletion(); const string imageReference = "localhost:5000/testimage:testtag"; localRegistry.Pull(imageReference); AssertDockerInspect(imageReference); Assert.AreEqual( "Hello, world. An argument.\n", new Command("docker", "run", "--rm", imageReference).Run()); const string imageReference2 = "localhost:5000/testimage:testtag2"; localRegistry.Pull(imageReference2); AssertDockerInspect(imageReference2); Assert.AreEqual( "Hello, world. An argument.\n", new Command("docker", "run", "--rm", imageReference2).Run()); const string imageReference3 = "localhost:5000/testimage:testtag3"; localRegistry.Pull(imageReference3); AssertDockerInspect(imageReference3); Assert.AreEqual( "Hello, world. An argument.\n", new Command("docker", "run", "--rm", imageReference3).Run()); }
public async Task TestTag_fail() { DockerClient testDockerClient = new DockerClient( subcommand => { Assert.AreEqual(new[] { "tag", "original", "new" }, subcommand); return(mockProcessBuilder); }); Mock.Get(mockProcess).Setup(m => m.WaitFor()).Returns(1); Mock.Get(mockProcess).Setup(m => m.GetErrorStream()).Returns(new MemoryStream(Encoding.UTF8.GetBytes("error"))); try { ImageReference originalImageReference = ImageReference.Of(null, "original", null); ImageReference newImageReference = ImageReference.Parse("new"); await testDockerClient.TagAsync(originalImageReference, newImageReference).ConfigureAwait(false); Assert.Fail("docker tag should have failed"); } catch (IOException ex) { Assert.AreEqual("'docker tag' command failed with error: error", ex.Message); } }
public void TestOf_smoke() { const string expectedRegistry = "someregistry"; const string expectedRepository = "somerepository"; const string expectedTag = "sometag"; Assert.AreEqual( expectedRegistry, ImageReference.Of(expectedRegistry, expectedRepository, expectedTag).GetRegistry()); Assert.AreEqual( expectedRepository, ImageReference.Of(expectedRegistry, expectedRepository, expectedTag).GetRepository()); Assert.AreEqual( expectedTag, ImageReference.Of(expectedRegistry, expectedRepository, expectedTag).GetTag()); Assert.AreEqual( "registry-1.docker.io", ImageReference.Of(null, expectedRepository, expectedTag).GetRegistry()); Assert.AreEqual( "registry-1.docker.io", ImageReference.Of(null, expectedRepository, null).GetRegistry()); Assert.AreEqual( "latest", ImageReference.Of(expectedRegistry, expectedRepository, null).GetTag()); Assert.AreEqual("latest", ImageReference.Of(null, expectedRepository, null).GetTag()); Assert.AreEqual( expectedRepository, ImageReference.Of(null, expectedRepository, null).GetRepository()); }
public void TestToString() { Assert.AreEqual("someimage", ImageReference.Of(null, "someimage", null).ToString()); Assert.AreEqual("someimage", ImageReference.Of("", "someimage", "").ToString()); Assert.AreEqual( "someotherimage", ImageReference.Of(null, "library/someotherimage", null).ToString()); Assert.AreEqual( "someregistry/someotherimage", ImageReference.Of("someregistry", "someotherimage", null).ToString()); Assert.AreEqual( "anotherregistry/anotherimage:sometag", ImageReference.Of("anotherregistry", "anotherimage", "sometag").ToString()); Assert.AreEqual( "someimage@sha256:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", ImageReference.Of( null, "someimage", "sha256:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa").ToString()); Assert.AreEqual( "gcr.io/distroless/java@sha256:b430543bea1d8326e767058bdab3a2482ea45f59d7af5c5c61334cd29ede88a1", ImageReference.Parse( "gcr.io/distroless/java@sha256:b430543bea1d8326e767058bdab3a2482ea45f59d7af5c5c61334cd29ede88a1") .ToString()); }
public void TestTo() { RegistryImage registryImage = RegistryImage.Named(ImageReference.Of(null, "repository", null)); DockerDaemonImage dockerDaemonImage = DockerDaemonImage.Named(ImageReference.Of(null, "repository", null)); TarImage tarImage = TarImage.Named(ImageReference.Of(null, "repository", null)).SaveTo(Paths.Get("ignored")); VerifyTo(Containerizer.To(registryImage)); VerifyTo(Containerizer.To(dockerDaemonImage)); VerifyTo(Containerizer.To(tarImage)); }
public void TestGetRegistry() { Assert.AreEqual( "registry-1.docker.io", ImageReference.Of(null, "someimage", null).GetRegistry()); Assert.AreEqual( "registry-1.docker.io", ImageReference.Of("docker.io", "someimage", null).GetRegistry()); Assert.AreEqual( "index.docker.io", ImageReference.Of("index.docker.io", "someimage", null).GetRegistry()); Assert.AreEqual( "registry.hub.docker.com", ImageReference.Of("registry.hub.docker.com", "someimage", null).GetRegistry()); Assert.AreEqual("gcr.io", ImageReference.Of("gcr.io", "someimage", null).GetRegistry()); }
public async Task TestBuildToDockerDaemonAsync() { await BuildDockerDaemonImageAsync( ImageReference.Of("gcr.io", "distroless/java", DISTROLESS_DIGEST), ImageReference.Of(null, "testdocker", null), new List <string>()).ConfigureAwait(false); progressChecker.CheckCompletion(); AssertDockerInspect("testdocker"); AssertLayerSizer(7, "testdocker"); Assert.AreEqual( "Hello, world. An argument.\n", new Command("docker", "run", "--rm", "testdocker").Run()); }
public async Task TestBuildToDockerRegistry_DockerHubBaseImageAsync() { await BuildRegistryImageAsync( ImageReference.Parse("openjdk:8-jre-alpine"), ImageReference.Of("localhost:5000", "testimage", "testtag"), new List <string>()).ConfigureAwait(false); progressChecker.CheckCompletion(); const string imageReference = "localhost:5000/testimage:testtag"; new Command("docker", "pull", imageReference).Run(); Assert.AreEqual( "Hello, world. An argument.\n", new Command("docker", "run", "--rm", imageReference).Run()); }
public void TestIsTagDigest() { Assert.IsFalse(ImageReference.Of(null, "someimage", null).IsTagDigest()); Assert.IsFalse(ImageReference.Of(null, "someimage", "latest").IsTagDigest()); Assert.IsTrue( ImageReference.Of( null, "someimage", "sha256:b430543bea1d8326e767058bdab3a2482ea45f59d7af5c5c61334cd29ede88a1") .IsTagDigest()); Assert.IsTrue( ImageReference.Parse( "someimage@sha256:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa") .IsTagDigest()); }
public void TestToStringWithTag() { Assert.AreEqual( "someimage:latest", ImageReference.Of(null, "someimage", null).ToStringWithTag()); Assert.AreEqual( "someimage:latest", ImageReference.Of("", "someimage", "").ToStringWithTag()); Assert.AreEqual( "someotherimage:latest", ImageReference.Of(null, "library/someotherimage", null).ToStringWithTag()); Assert.AreEqual( "someregistry/someotherimage:latest", ImageReference.Of("someregistry", "someotherimage", null).ToStringWithTag()); Assert.AreEqual( "anotherregistry/anotherimage:sometag", ImageReference.Of("anotherregistry", "anotherimage", "sometag").ToStringWithTag()); }
public async Task TestTag() { DockerClient testDockerClient = new DockerClient( subcommand => { Assert.AreEqual(new[] { "tag", "original", "new" }, subcommand); return(mockProcessBuilder); }); Mock.Get(mockProcess).Setup(m => m.WaitFor()).Returns(0); ImageReference originalImageReference = ImageReference.Of(null, "original", null); ImageReference newImageReference = ImageReference.Parse("new"); await testDockerClient.TagAsync(originalImageReference, newImageReference).ConfigureAwait(false); }
public void TestDockerConfig() { CredentialRetrieverFactory credentialRetrieverFactory = CredentialRetrieverFactory.ForImage( ImageReference.Of("registry", "repository", null), mockLogger); Mock.Get(mockDockerConfigCredentialRetriever).Setup(m => m.Retrieve(mockLogger)).Returns(Maybe.Of(fakeCredentials)); Assert.AreEqual( fakeCredentials, credentialRetrieverFactory .DockerConfig(mockDockerConfigCredentialRetriever) .Retrieve() .OrElseThrow(() => new AssertionException(""))); Mock.Get(mockLogger).Verify(m => m(LogEvent.Info("Using credentials from Docker config for registry"))); }
public async Task TestBuildTarballAsync() { SystemPath outputPath = temporaryFolder.NewFolder().ToPath().Resolve("test.tar"); await BuildTarImageAsync( ImageReference.Of("gcr.io", "distroless/java", DISTROLESS_DIGEST), ImageReference.Of(null, "testtar", null), outputPath, new List <string>()).ConfigureAwait(false); progressChecker.CheckCompletion(); new Command("docker", "load", "--input", outputPath.ToString()).Run(); AssertLayerSizer(7, "testtar"); Assert.AreEqual( "Hello, world. An argument.\n", new Command("docker", "run", "--rm", "testtar").Run()); }
public void TestDockerCredentialHelper() { CredentialRetrieverFactory credentialRetrieverFactory = new CredentialRetrieverFactory( ImageReference.Of("registry", "repository", null), mockLogger, GetTestFactory( "registry", Paths.Get("docker-credential-helper"), mockDockerCredentialHelper)); Assert.AreEqual( fakeCredentials, credentialRetrieverFactory .DockerCredentialHelper(Paths.Get("docker-credential-helper")) .Retrieve() .OrElseThrow(() => new AssertionException(""))); Mock.Get(mockLogger).Verify(m => m(LogEvent.Info("Using docker-credential-helper for registry"))); }
public void TestWithAdditionalTag() { DockerDaemonImage dockerDaemonImage = DockerDaemonImage.Named(ImageReference.Of(null, "repository", null)); Containerizer containerizer = Containerizer.To(dockerDaemonImage); containerizer.WithAdditionalTag("tag"); try { containerizer.WithAdditionalTag("+invalid+"); Assert.Fail(); } catch (ArgumentException ex) { Assert.AreEqual("invalid tag '+invalid+'", ex.Message); } }
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); }
public void TestRetrieveManifest_v22() { SystemPath cacheDirectory = temporaryFolder.NewFolder().ToPath(); SetupCachedMetadataV22(cacheDirectory); CacheStorageFiles cacheStorageFiles = new CacheStorageFiles(cacheDirectory); CacheStorageReader cacheStorageReader = new CacheStorageReader(cacheStorageFiles); V22ManifestTemplate manifestTemplate = (V22ManifestTemplate) cacheStorageReader .RetrieveMetadata(ImageReference.Of("test", "image", "tag")) .Get() .GetManifest(); Assert.AreEqual(2, manifestTemplate.SchemaVersion); }
public void TestRetrieveContainerConfiguration() { SystemPath cacheDirectory = temporaryFolder.NewFolder().ToPath(); SetupCachedMetadataV22(cacheDirectory); CacheStorageFiles cacheStorageFiles = new CacheStorageFiles(cacheDirectory); CacheStorageReader cacheStorageReader = new CacheStorageReader(cacheStorageFiles); ContainerConfigurationTemplate configurationTemplate = cacheStorageReader .RetrieveMetadata(ImageReference.Of("test", "image", "tag")) .Get() .GetConfig() .Get(); Assert.AreEqual("wasm", configurationTemplate.Architecture); Assert.AreEqual("js", configurationTemplate.Os); }
public void TestBuilder_default() { // These are required and don't have defaults. const string expectedBaseImageServerUrl = "someserver"; const string expectedBaseImageName = "baseimage"; const string expectedBaseImageTag = "baseimagetag"; const string expectedTargetServerUrl = "someotherserver"; const string expectedTargetImageName = "targetimage"; const string expectedTargetTag = "targettag"; ImageConfiguration baseImageConfiguration = ImageConfiguration.CreateBuilder( ImageReference.Of( expectedBaseImageServerUrl, expectedBaseImageName, expectedBaseImageTag)) .Build(); ImageConfiguration targetImageConfiguration = ImageConfiguration.CreateBuilder( ImageReference.Of( expectedTargetServerUrl, expectedTargetImageName, expectedTargetTag)) .Build(); BuildConfiguration.Builder buildConfigurationBuilder = BuildConfiguration.CreateBuilder() .SetBaseImageConfiguration(baseImageConfiguration) .SetTargetImageConfiguration(targetImageConfiguration) .SetBaseImageLayersCacheDirectory(Paths.Get("ignored")) .SetApplicationLayersCacheDirectory(Paths.Get("ignored")); BuildConfiguration buildConfiguration = buildConfigurationBuilder.Build(); Assert.AreEqual(ImmutableHashSet.Create("targettag"), buildConfiguration.GetAllTargetImageTags()); Assert.AreEqual(ManifestFormat.V22, buildConfiguration.GetTargetFormat()); Assert.IsNotNull(buildConfigurationBuilder.GetApplicationLayersCacheDirectory()); Assert.AreEqual( Paths.Get("ignored"), buildConfigurationBuilder.GetApplicationLayersCacheDirectory()); Assert.IsNotNull(buildConfigurationBuilder.GetBaseImageLayersCacheDirectory()); Assert.AreEqual( Paths.Get("ignored"), buildConfigurationBuilder.GetBaseImageLayersCacheDirectory()); Assert.IsNull(buildConfiguration.GetContainerConfiguration()); Assert.IsFalse(buildConfiguration.GetAllowInsecureRegistries()); Assert.AreEqual(new List <LayerConfiguration>(), buildConfiguration.GetLayerConfigurations()); Assert.AreEqual(null, buildConfiguration.GetToolName()); Assert.AreEqual(null, buildConfiguration.GetToolVersion()); }
public void TestInferCredentialHelper_info() { CredentialRetrieverFactory credentialRetrieverFactory = new CredentialRetrieverFactory( ImageReference.Of("something.amazonaws.com", "repository", null), mockLogger, GetTestFactory( "something.amazonaws.com", Paths.Get("docker-credential-ecr-login"), mockNonexistentDockerCredentialHelper)); Mock.Get(mockNonexistentDockerCredentialHelper) .Setup(m => m.Retrieve()) .Throws(new CredentialHelperNotFoundException("warning", new IOException("the root cause"))); Assert.IsFalse(credentialRetrieverFactory.InferCredentialHelper().Retrieve().IsPresent()); Mock.Get(mockLogger).Verify(m => m(LogEvent.Info("warning"))); Mock.Get(mockLogger).Verify(m => m(LogEvent.Info(" Caused by: the root cause"))); }
public void TestToJson() { // Loads the expected JSON string. SystemPath jsonFile = Paths.Get(TestResources.GetResource("core/json/loadmanifest.json").ToURI()); string expectedJson = Encoding.UTF8.GetString(Files.ReadAllBytes(jsonFile)); DockerLoadManifestEntryTemplate template = new DockerLoadManifestEntryTemplate(); template.SetRepoTags( ImageReference.Of("testregistry", "testrepo", "testtag").ToStringWithTag()); template.AddLayerFile("layer1.tar.gz"); template.AddLayerFile("layer2.tar.gz"); template.AddLayerFile("layer3.tar.gz"); List <DockerLoadManifestEntryTemplate> loadManifest = new List <DockerLoadManifestEntryTemplate> { template }; Assert.AreEqual(expectedJson, JsonTemplateMapper.ToUtf8String(loadManifest)); }
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 TestSteps_ForBuildToDockerRegistryAsync() { Stopwatch s = Stopwatch.StartNew(); FibContainer image1 = await BuildRegistryImageAsync( ImageReference.Of("gcr.io", "distroless/java", DISTROLESS_DIGEST), ImageReference.Of("localhost:5000", "testimage", "testtag"), new List <string>()).ConfigureAwait(false); progressChecker.CheckCompletion(); logger.Info("Initial build time: " + s.Elapsed); s.Restart(); FibContainer image2 = await BuildRegistryImageAsync( ImageReference.Of("gcr.io", "distroless/java", DISTROLESS_DIGEST), ImageReference.Of("localhost:5000", "testimage", "testtag"), new List <string>()).ConfigureAwait(false); logger.Info("Secondary build time: " + s.Elapsed); Assert.AreEqual(image1, image2); const string imageReference = "localhost:5000/testimage:testtag"; localRegistry.Pull(imageReference); AssertDockerInspect(imageReference); AssertLayerSizer(7, imageReference); Assert.AreEqual( "Hello, world. An argument.\n", new Command("docker", "run", "--rm", imageReference).Run()); string imageReferenceByDigest = "localhost:5000/testimage@" + image1.GetDigest(); localRegistry.Pull(imageReferenceByDigest); AssertDockerInspect(imageReferenceByDigest); Assert.AreEqual( "Hello, world. An argument.\n", new Command("docker", "run", "--rm", imageReferenceByDigest).Run()); }
public async Task TestBuildToDockerDaemon_MultipleTagsAsync() { const string imageReference = "testdocker"; await BuildDockerDaemonImageAsync( ImageReference.Of("gcr.io", "distroless/java", DISTROLESS_DIGEST), ImageReference.Of(null, imageReference, null), new[] { "testtag2", "testtag3" }).ConfigureAwait(false); progressChecker.CheckCompletion(); AssertDockerInspect(imageReference); Assert.AreEqual( "Hello, world. An argument.\n", new Command("docker", "run", "--rm", imageReference).Run()); AssertDockerInspect(imageReference + ":testtag2"); Assert.AreEqual( "Hello, world. An argument.\n", new Command("docker", "run", "--rm", imageReference + ":testtag2").Run()); AssertDockerInspect(imageReference + ":testtag3"); Assert.AreEqual( "Hello, world. An argument.\n", new Command("docker", "run", "--rm", imageReference + ":testtag3").Run()); }
public void TestBuilder() { const string expectedBaseImageServerUrl = "someserver"; const string expectedBaseImageName = "baseimage"; const string expectedBaseImageTag = "baseimagetag"; const string expectedTargetServerUrl = "someotherserver"; const string expectedTargetImageName = "targetimage"; const string expectedTargetTag = "targettag"; ISet <string> additionalTargetImageTags = ImmutableHashSet.Create("tag1", "tag2", "tag3"); ISet <string> expectedTargetImageTags = ImmutableHashSet.Create("targettag", "tag1", "tag2", "tag3"); IList <CredentialRetriever> credentialRetrievers = new List <CredentialRetriever> { () => Maybe.Of(Credential.From("username", "password")) }; Instant expectedCreationTime = Instant.FromUnixTimeSeconds(10000); IList <string> expectedEntrypoint = new[] { "some", "entrypoint" }; IList <string> expectedProgramArguments = new[] { "arg1", "arg2" }; IDictionary <string, string> expectedEnvironment = ImmutableDic.Of("key", "value"); ImmutableHashSet <Port> expectedExposedPorts = ImmutableHashSet.Create(Port.Tcp(1000), Port.Tcp(2000)); IDictionary <string, string> expectedLabels = ImmutableDic.Of("key1", "value1", "key2", "value2"); const ManifestFormat expectedTargetFormat = ManifestFormat.OCI; SystemPath expectedApplicationLayersCacheDirectory = Paths.Get("application/layers"); SystemPath expectedBaseImageLayersCacheDirectory = Paths.Get("base/image/layers"); IList <ILayerConfiguration> expectedLayerConfigurations = new List <ILayerConfiguration> { LayerConfiguration.CreateBuilder() .AddEntry(Paths.Get("sourceFile"), AbsoluteUnixPath.Get("/path/in/container")) .Build() }; const string expectedCreatedBy = "createdBy"; ImageConfiguration baseImageConfiguration = ImageConfiguration.CreateBuilder( ImageReference.Of( expectedBaseImageServerUrl, expectedBaseImageName, expectedBaseImageTag)) .Build(); ImageConfiguration targetImageConfiguration = ImageConfiguration.CreateBuilder( ImageReference.Of( expectedTargetServerUrl, expectedTargetImageName, expectedTargetTag)) .SetCredentialRetrievers(credentialRetrievers) .Build(); ContainerConfiguration containerConfiguration = ContainerConfiguration.CreateBuilder() .SetCreationTime(expectedCreationTime) .SetEntrypoint(expectedEntrypoint) .SetProgramArguments(expectedProgramArguments) .SetEnvironment(expectedEnvironment) .SetExposedPorts(expectedExposedPorts) .SetLabels(expectedLabels) .Build(); BuildConfiguration.Builder buildConfigurationBuilder = BuildConfiguration.CreateBuilder() .SetBaseImageConfiguration(baseImageConfiguration) .SetTargetImageConfiguration(targetImageConfiguration) .SetAdditionalTargetImageTags(additionalTargetImageTags) .SetContainerConfiguration(containerConfiguration) .SetApplicationLayersCacheDirectory(expectedApplicationLayersCacheDirectory) .SetBaseImageLayersCacheDirectory(expectedBaseImageLayersCacheDirectory) .SetTargetFormat(ImageFormat.OCI) .SetAllowInsecureRegistries(true) .SetLayerConfigurations(expectedLayerConfigurations) .SetToolName(expectedCreatedBy); BuildConfiguration buildConfiguration = buildConfigurationBuilder.Build(); Assert.IsNotNull(buildConfiguration.GetContainerConfiguration()); Assert.AreEqual( expectedCreationTime, buildConfiguration.GetContainerConfiguration().GetCreationTime()); Assert.AreEqual( expectedBaseImageServerUrl, buildConfiguration.GetBaseImageConfiguration().GetImageRegistry()); Assert.AreEqual( expectedBaseImageName, buildConfiguration.GetBaseImageConfiguration().GetImageRepository()); Assert.AreEqual( expectedBaseImageTag, buildConfiguration.GetBaseImageConfiguration().GetImageTag()); Assert.AreEqual( expectedTargetServerUrl, buildConfiguration.GetTargetImageConfiguration().GetImageRegistry()); Assert.AreEqual( expectedTargetImageName, buildConfiguration.GetTargetImageConfiguration().GetImageRepository()); Assert.AreEqual( expectedTargetTag, buildConfiguration.GetTargetImageConfiguration().GetImageTag()); Assert.AreEqual(expectedTargetImageTags, buildConfiguration.GetAllTargetImageTags()); Assert.AreEqual( Credential.From("username", "password"), buildConfiguration .GetTargetImageConfiguration() .GetCredentialRetrievers() [0] .Retrieve() .OrElseThrow(() => new AssertionException(""))); Assert.AreEqual( expectedProgramArguments, buildConfiguration.GetContainerConfiguration().GetProgramArguments()); Assert.AreEqual( expectedEnvironment, buildConfiguration.GetContainerConfiguration().GetEnvironmentMap()); Assert.AreEqual( expectedExposedPorts, buildConfiguration.GetContainerConfiguration().GetExposedPorts()); Assert.AreEqual(expectedLabels, buildConfiguration.GetContainerConfiguration().GetLabels()); Assert.AreEqual(expectedTargetFormat, buildConfiguration.GetTargetFormat()); Assert.AreEqual( expectedApplicationLayersCacheDirectory, buildConfigurationBuilder.GetApplicationLayersCacheDirectory()); Assert.AreEqual( expectedBaseImageLayersCacheDirectory, buildConfigurationBuilder.GetBaseImageLayersCacheDirectory()); Assert.IsTrue(buildConfiguration.GetAllowInsecureRegistries()); Assert.AreEqual(expectedLayerConfigurations, buildConfiguration.GetLayerConfigurations()); Assert.AreEqual( expectedEntrypoint, buildConfiguration.GetContainerConfiguration().GetEntrypoint()); Assert.AreEqual(expectedCreatedBy, buildConfiguration.GetToolName()); }
public void TestIsScratch() { Assert.IsTrue(ImageReference.Scratch().IsScratch()); Assert.IsFalse(ImageReference.Of("", "scratch", "").IsScratch()); Assert.IsFalse(ImageReference.Of(null, "scratch", null).IsScratch()); }
public void TestToBuildConfiguration() { RegistryImage targetImage = RegistryImage.Named(ImageReference.Of("gcr.io", "my-project/my-app", null)) .AddCredential("username", "password"); IContainerizer containerizer = Containerizer.To(targetImage) .SetBaseImageLayersCache(Paths.Get("base/image/layers")) .SetApplicationLayersCache(Paths.Get("application/layers")) .AddEventHandler(mockFibEventConsumer); RegistryImage baseImage = RegistryImage.Named("base/image").AddCredentialRetriever(mockCredentialRetriever); FibContainerBuilder fibContainerBuilder = new FibContainerBuilder(baseImage, buildConfigurationBuilder) .SetLayers(new[] { mockLayerConfiguration1, mockLayerConfiguration2 }); BuildConfiguration buildConfiguration = fibContainerBuilder.ToBuildConfiguration( containerizer); Assert.AreEqual( buildConfigurationBuilder.Build().GetContainerConfiguration(), buildConfiguration.GetContainerConfiguration()); Assert.AreEqual( "base/image", buildConfiguration.GetBaseImageConfiguration().GetImage().ToString()); Assert.AreEqual( new[] { mockCredentialRetriever }, buildConfiguration.GetBaseImageConfiguration().GetCredentialRetrievers()); Assert.AreEqual( "gcr.io/my-project/my-app", buildConfiguration.GetTargetImageConfiguration().GetImage().ToString()); Assert.AreEqual( 1, buildConfiguration.GetTargetImageConfiguration().GetCredentialRetrievers().Length); Assert.AreEqual( Credential.From("username", "password"), buildConfiguration .GetTargetImageConfiguration() .GetCredentialRetrievers() [0] .Retrieve() .OrElseThrow(() => new AssertionException(""))); Assert.AreEqual(ImmutableHashSet.Create("latest"), buildConfiguration.GetAllTargetImageTags()); Assert.AreEqual( new[] { mockLayerConfiguration1, mockLayerConfiguration2 }, buildConfiguration.GetLayerConfigurations()); buildConfiguration.GetEventHandlers().Dispatch(mockFibEvent); Mock.Get(mockFibEventConsumer).Verify(m => m(mockFibEvent)); Assert.AreEqual("fibdotnet-core", buildConfiguration.GetToolName()); Assert.AreEqual(ManifestFormat.V22, buildConfiguration.GetTargetFormat()); Assert.AreEqual("fibdotnet-core", buildConfiguration.GetToolName()); // Changes fibContainerBuilder. buildConfiguration = fibContainerBuilder .SetFormat(ImageFormat.OCI) .ToBuildConfiguration( containerizer .WithAdditionalTag("tag1") .WithAdditionalTag("tag2") .SetToolName("toolName")); Assert.AreEqual(ManifestFormat.OCI, buildConfiguration.GetTargetFormat()); Assert.AreEqual( ImmutableHashSet.Create("latest", "tag1", "tag2"), buildConfiguration.GetAllTargetImageTags()); Assert.AreEqual("toolName", buildConfiguration.GetToolName()); }
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()); }