Ejemplo n.º 1
0
        public static void Initialize(TestContext ctx)
        {
            if (CommandExtensions.IsNative() || CommandExtensions.IsEmulatedNative())
            {
                _docker.LinuxMode();
                return;
            }

            var machineName = "test-machine";

            var machines = Machine.Ls();

            if (machines.Success && machines.Data.Any(x => x.Name == "default"))
            {
                machineName = "default";
            }
            else
            {
                machineName.Create(1024, 20000, 1);
                _createdTestMachine = true;
            }

            machineName.Start();
            var inspect = machineName.Inspect().Data;

            _docker       = machineName.Uri();
            _certificates = new CertificatePaths
            {
                CaCertificate     = inspect.AuthConfig.CaCertPath,
                ClientCertificate = inspect.AuthConfig.ClientCertPath,
                ClientKey         = inspect.AuthConfig.ClientKeyPath
            };

            _docker.LinuxMode(_certificates);
        }
Ejemplo n.º 2
0
        public IList <IHostService> Discover(bool preferNative = false)
        {
            var list = new List <IHostService>();

            if (CommandExtensions.IsEmulatedNative() || CommandExtensions.IsNative())
            {
                list.Add(new DockerHostService("native", true, false,
                                               null,
                                               Environment.GetEnvironmentVariable(DockerHostService.DockerCertPath)));
            }

            if (list.Count > 0 && preferNative)
            {
                return(list);
            }

            if (Machine.IsPresent())
            {
                var ls = Machine.Ls();
                if (ls.Success)
                {
                    list.AddRange(from machine in ls.Data
                                  let inspect = machine.Name.Inspect()
                                                select
                                                new DockerHostService(machine.Name, false, false, machine.Docker?.ToString(),
                                                                      inspect.Data.AuthConfig.CertDir));
                }
            }

            return(list);
        }
Ejemplo n.º 3
0
        public void CustomResolverForContainerShallWork()
        {
            bool executedCustomResolver = false;

            using (
                var container =
                    Fd.UseContainer()
                    .UseImage("postgres:9.6-alpine")
                    .WithEnvironment("POSTGRES_PASSWORD=mysecretpassword")
                    .ExposePort(5432)
                    .UseCustomResolver((
                                           ports, portAndProto, dockerUri) =>
            {
                executedCustomResolver = true;

                if (null == ports || string.IsNullOrEmpty(portAndProto))
                {
                    return(null);
                }

                if (!ports.TryGetValue(portAndProto, out var endpoints))
                {
                    return(null);
                }

                if (null == endpoints || endpoints.Length == 0)
                {
                    return(null);
                }

                if (CommandExtensions.IsNative())
                {
                    return(endpoints[0]);
                }

                if (CommandExtensions.IsEmulatedNative())
                {
                    return(CommandExtensions.IsDockerDnsAvailable()
                  ? new IPEndPoint(CommandExtensions.EmulatedNativeAddress(), endpoints[0].Port)
                  : new IPEndPoint(IPAddress.Loopback, endpoints[0].Port));
                }

                if (Equals(endpoints[0].Address, IPAddress.Any) && null != dockerUri)
                {
                    return(new IPEndPoint(IPAddress.Parse(dockerUri.Host), endpoints[0].Port));
                }

                return(endpoints[0]);
            })
                    .WaitForPort("5432/tcp", 30000 /*30s*/)
                    .Build()
                    .Start())
            {
                var state = container.GetConfiguration(true /*force*/).State.ToServiceState();
                Assert.AreEqual(ServiceRunningState.Running, state);
                Assert.IsTrue(executedCustomResolver);
            }
        }
Ejemplo n.º 4
0
        public IHostService Native()
        {
            if (CommandExtensions.IsEmulatedNative() || CommandExtensions.IsNative())
            {
                return(new DockerHostService("native", true, false, null,
                                             Environment.GetEnvironmentVariable(DockerHostService.DockerCertPath)));
            }

            return(null);
        }
        public void DiscoverShouldReturnNativeWhenSuchIsPresent()
        {
            if (!(CommandExtensions.IsEmulatedNative() || CommandExtensions.IsNative()))
            {
                return;
            }

            var services = new Hosts().Discover();

            Assert.AreEqual(1, services.Count);

            var native = services.First();

            Assert.AreEqual("native", native.Name);
            Assert.AreEqual(true, native.IsNative);
        }
Ejemplo n.º 6
0
        /// <summary>
        ///   Translates a docker exposed port and protocol (on format 'port/proto' e.g. '534/tcp') to a
        ///   host endpoint that can be contacted outside the container.
        /// </summary>
        /// <param name="ports">The ports from the <see cref="ContainerNetworkSettings.Ports" /> property.</param>
        /// <param name="portAndProto">The port and protocol string.</param>
        /// <param name="dockerUri">Optional docker uri to use when the adress is 0.0.0.0 in the endpoint.</param>
        /// <returns>A endpoint of the host exposed ip and port into the container port. If none is found, null is returned.</returns>
        public static IPEndPoint ToHostPort(this Dictionary <string, HostIpEndpoint[]> ports, string portAndProto,
                                            Uri dockerUri = null)
        {
            if (null == ports || string.IsNullOrEmpty(portAndProto))
            {
                return(null);
            }

            HostIpEndpoint[] endpoints;
            if (!ports.TryGetValue(portAndProto, out endpoints))
            {
                return(null);
            }

            if (null == endpoints || endpoints.Length == 0)
            {
                return(null);
            }

            if (CommandExtensions.IsNative())
            {
                return(endpoints[0]);
            }

            if (CommandExtensions.IsEmulatedNative())
            {
                if (CommandExtensions.IsDockerDnsAvailable())
                {
                    return(new IPEndPoint(CommandExtensions.EmulatedNativeAdress(), endpoints[0].Port));
                }
                return(new IPEndPoint(IPAddress.Loopback, endpoints[0].Port));
            }

            if (Equals(endpoints[0].Address, IPAddress.Any) && null != dockerUri)
            {
                return(new IPEndPoint(IPAddress.Parse(dockerUri.Host), endpoints[0].Port));
            }

            return(endpoints[0]);
        }