/// <summary> /// <para> /// Starts a Couchbase container if it's not already running. You'll generally want /// to call this in your test class constructor instead of <see cref="ITestFixture.Start(Action)"/>. /// </para> /// <note> /// You'll need to call <see cref="StartAsComposed(CouchbaseSettings, string, string, string[], string, string, bool, string, ContainerLimits)"/> /// instead when this fixture is being added to a <see cref="ComposedFixture"/>. /// </note> /// </summary> /// <param name="settings">Optional Couchbase settings.</param> /// <param name="image"> /// Optionally specifies the Couchbase container image. This defaults to /// <b>ghcr.io/neonrelease/couchbase-dev:latest</b> or <b>ghcr.io/neonrelease-dev/couchbase-dev:latest</b> /// depending on whether the assembly was built from a git release branch or not. /// </param> /// <param name="name">Optionally specifies the Couchbase container name (defaults to <c>cb-test</c>).</param> /// <param name="env">Optional environment variables to be passed to the Couchbase container, formatted as <b>NAME=VALUE</b> or just <b>NAME</b>.</param> /// <param name="username">Optional Couchbase username (defaults to <b>Administrator</b>).</param> /// <param name="password">Optional Couchbase password (defaults to <b>password</b>).</param> /// <param name="noPrimary">Optionally disable creation of the primary bucket index.</param> /// <param name="hostInterface"> /// Optionally specifies the host interface where the container public ports will be /// published. This defaults to <see cref="ContainerFixture.DefaultHostInterface"/> /// but may be customized. This needs to be an IPv4 address. /// </param> /// <param name="limits"> /// Optionally specifies the Docker container limits to use for hosting Couchbase. Note that /// this method will use reasonable default limits when this is <c>null</c>. /// </param> /// <returns> /// <see cref="TestFixtureStatus.Started"/> if the fixture wasn't previously started and /// this method call started it or <see cref="TestFixtureStatus.AlreadyRunning"/> if the /// fixture was already running. /// </returns> /// <remarks> /// <note> /// Some of the <paramref name="settings"/> properties will be ignored including /// <see cref="CouchbaseSettings.Servers"/>. This will be replaced by the local /// endpoint for the Couchbase container. Also, the fixture will connect to the /// <b>test</b> bucket by default (unless another is specified). /// </note> /// <para> /// This method creates a primary index by default because it's very common for /// unit tests to require a primary index. You can avoid creating a primary index /// by passing <paramref name="noPrimary"/><c>=true</c>. /// </para> /// <para> /// There are three basic patterns for using this fixture. /// </para> /// <list type="table"> /// <item> /// <term><b>initialize once</b></term> /// <description> /// <para> /// The basic idea here is to have your test class initialize Couchbase /// once within the test class constructor inside of the initialize action /// with common state that all of the tests can access. /// </para> /// <para> /// This will be quite a bit faster than reconfiguring Couchbase at the /// beginning of every test and can work well for many situations. /// </para> /// </description> /// </item> /// <item> /// <term><b>initialize every test</b></term> /// <description> /// For scenarios where Couchbase must be cleared before every test, /// you can use the <see cref="Clear()"/> method to reset its /// state within each test method, populate the database as necessary, /// and then perform your tests. /// </description> /// </item> /// <item> /// <term><b>docker integrated</b></term> /// <description> /// The <see cref="CouchbaseFixture"/> can also be added to the <see cref="DockerFixture"/> /// and used within a swarm. This is useful for testing multiple services and /// also has the advantage of ensure that swarm/node state is fully reset /// before the database container is started. /// </description> /// </item> /// </list> /// </remarks> public TestFixtureStatus Start( CouchbaseSettings settings = null, string image = null, string name = "cb-test", string[] env = null, string username = "******", string password = "******", bool noPrimary = false, string hostInterface = null, ContainerLimits limits = null) { return(base.Start( () => { StartAsComposed(settings, image, name, env, username, password, noPrimary, hostInterface, limits); })); }
public Test_ContainerFixtureLimits(ContainerFixture fixture) { TestHelper.ResetDocker(this.GetType()); this.fixture = fixture; var limits = new ContainerLimits() { Memory = "50 MiB", MemoryReservation = "40 MiB", MemorySwap = "100 MiB", MemorySwappiness = 50, KernelMemory = "250 MiB", OomKillDisable = true }; fixture.Start("neon-unit-test-container", $"{NeonHelper.NeonLibraryBranchRegistry}/test:latest", limits: limits); }
/// <summary> /// Used to start the fixture within a <see cref="ComposedFixture"/>. /// </summary> /// <param name="settings">Optional Couchbase settings.</param> /// <param name="image"> /// Optionally specifies the Couchbase container image. This defaults to /// <b>ghcr.io/neonrelease/couchbase-dev:latest</b> or <b>ghcr.io/neonrelease-dev/couchbase-dev:latest</b> /// depending on whether the assembly was built from a git release branch or not. /// </param> /// <param name="name">Optionally specifies the Couchbase container name (defaults to <c>cb-test</c>).</param> /// <param name="env">Optional environment variables to be passed to the Couchbase container, formatted as <b>NAME=VALUE</b> or just <b>NAME</b>.</param> /// <param name="username">Optional Couchbase username (defaults to <b>Administrator</b>).</param> /// <param name="password">Optional Couchbase password (defaults to <b>password</b>).</param> /// <param name="noPrimary">Optionally disable creation of thea primary bucket index.</param> /// <param name="hostInterface"> /// Optionally specifies the host interface where the container public ports will be /// published. This defaults to <see cref="ContainerFixture.DefaultHostInterface"/> /// but may be customized. This needs to be an IPv4 address. /// </param> /// <param name="limits"> /// Optionally specifies the Docker container limits to use for hosting Couchbase. Note that /// this method will use reasonably small default limits when this is <c>null</c>. /// </param> public void StartAsComposed( CouchbaseSettings settings = null, string image = null, string name = "cb-test", string[] env = null, string username = "******", string password = "******", bool noPrimary = false, string hostInterface = null, ContainerLimits limits = null) { image = image ?? $"{NeonHelper.NeonLibraryBranchRegistry}/couchbase-dev:latest"; base.CheckWithinAction(); createPrimaryIndex = !noPrimary; if (!IsRunning) { // Use reasonable default limits. limits = limits ?? new ContainerLimits() { Memory = "1 GiB" }; base.StartAsComposed(name, image, new string[] { "--detach", "-p", $"{GetHostInterface(hostInterface)}:4369:4369", "-p", $"{GetHostInterface(hostInterface)}:8091-8096:8091-8096", "-p", $"{GetHostInterface(hostInterface)}:9100-9105:9100-9105", "-p", $"{GetHostInterface(hostInterface)}:9110-9118:9110-9118", "-p", $"{GetHostInterface(hostInterface)}:9120-9122:9120-9122", "-p", $"{GetHostInterface(hostInterface)}:9999:9999", "-p", $"{GetHostInterface(hostInterface)}:11207:11207", "-p", $"{GetHostInterface(hostInterface)}:11209-11211:11209-11211", "-p", $"{GetHostInterface(hostInterface)}:18091-18096:18091-18096", "-p", $"{GetHostInterface(hostInterface)}:21100-21299:21100-21299" }, env: env, limits: limits); Thread.Sleep(warmupDelay); settings = settings ?? new CouchbaseSettings(); settings.Servers.Clear(); settings.Servers.Add(new Uri($"http://{GetHostInterface(hostInterface, forConnection: true)}:8091")); if (settings.Bucket == null) { settings.Bucket = "test"; } Bucket = null; Settings = settings; Username = username; Password = password; jsonClient = new JsonClient(); jsonClient.BaseAddress = new Uri($"http://{GetHostInterface(hostInterface, forConnection: true)}:8094"); jsonClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue( "Basic", Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes($"{username}:{password}"))); } ConnectBucket(); }