summaryrefslogtreecommitdiff
path: root/deps/rabbitmq_cli/test/ctl/enable_feature_flag_test.exs
blob: f8a3e62920d7a142ddae0a103b8aee55ede86a51 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
## This Source Code Form is subject to the terms of the Mozilla Public
## License, v. 2.0. If a copy of the MPL was not distributed with this
## file, You can obtain one at https://mozilla.org/MPL/2.0/.
##
## Copyright (c) 2018-2020 VMware, Inc. or its affiliates.  All rights reserved.


defmodule EnableFeatureFlagCommandTest do
  use ExUnit.Case, async: false
  import TestHelper

  @command RabbitMQ.CLI.Ctl.Commands.EnableFeatureFlagCommand
  @feature_flag :ff_from_enable_ff_testsuite

  setup_all do
    RabbitMQ.CLI.Core.Distribution.start()

    # Define an arbitrary feature flag for the test.
    node = get_rabbit_hostname()
    new_feature_flags = %{
      @feature_flag =>
      %{desc: "My feature flag",
        provided_by: :EnableFeatureFlagCommandTest,
        stability: :stable}}
    :ok = :rabbit_misc.rpc_call(
      node, :rabbit_feature_flags, :initialize_registry, [new_feature_flags])

    {
      :ok,
      opts: %{node: get_rabbit_hostname()},
      feature_flag: @feature_flag
    }
  end

  test "validate: wrong number of arguments results in arg count errors" do
    assert @command.validate([], %{}) == {:validation_failure, :not_enough_args}
    assert @command.validate(["ff_from_enable_ff_testsuite", "whoops"], %{}) == {:validation_failure, :too_many_args}
  end

  test "validate: passing an empty string for feature_flag name is an arg error", context do
    assert match?({:validation_failure, {:bad_argument, _}}, @command.validate([""], context[:opts]))
  end

  test "run: passing a valid feature_flag name to a running RabbitMQ node succeeds", context do
    assert @command.run([Atom.to_string(context[:feature_flag])], context[:opts]) == :ok
    assert list_feature_flags(:enabled) |> Map.has_key?(context[:feature_flag])
  end

  test "run: attempt to use an unreachable node returns a nodedown" do
    opts = %{node: :jake@thedog, timeout: 200}
    assert match?({:badrpc, _}, @command.run(["na"], opts))
  end

  test "run: enabling the same feature flag twice is idempotent", context do
    enable_feature_flag context[:feature_flag]
    assert @command.run([Atom.to_string(context[:feature_flag])], context[:opts]) == :ok
    assert list_feature_flags(:enabled) |> Map.has_key?(context[:feature_flag])
  end

  test "run: enabling all feature flags succeeds", context do
    enable_feature_flag context[:feature_flag]
    assert @command.run(["all"], context[:opts]) == :ok
    assert list_feature_flags(:enabled) |> Map.has_key?(context[:feature_flag])
  end

  test "banner", context do
    assert @command.banner([context[:feature_flag]], context[:opts])
      =~ ~r/Enabling feature flag \"#{context[:feature_flag]}\" \.\.\./
  end
end