summaryrefslogtreecommitdiff
path: root/deps/rabbitmq_cli/test/ctl/forget_cluster_node_command_test.exs
blob: 0f09e4fee81d1650f031c64c6f763c7763e5348f (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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
## 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) 2016-2020 VMware, Inc. or its affiliates.  All rights reserved.

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

  @command RabbitMQ.CLI.Ctl.Commands.ForgetClusterNodeCommand

  setup_all do
    RabbitMQ.CLI.Core.Distribution.start()
    node = get_rabbit_hostname()

    start_rabbitmq_app()

    {:ok, plugins_dir} =
      :rabbit_misc.rpc_call(node, :application, :get_env, [:rabbit, :plugins_dir])

    rabbitmq_home = :rabbit_misc.rpc_call(node, :code, :lib_dir, [:rabbit])
    mnesia_dir = :rabbit_misc.rpc_call(node, :rabbit_mnesia, :dir, [])

    feature_flags_file =
      :rabbit_misc.rpc_call(node, :rabbit_feature_flags, :enabled_feature_flags_list_file, [])

    on_exit([], fn ->
      start_rabbitmq_app()
    end)

    {:ok,
     opts: %{
       rabbitmq_home: rabbitmq_home,
       plugins_dir: plugins_dir,
       mnesia_dir: mnesia_dir,
       feature_flags_file: feature_flags_file,
       offline: false
     }}
  end

  setup context do
    {:ok, opts: Map.merge(context[:opts], %{node: get_rabbit_hostname()})}
  end

  test "validate: specifying no target node is reported as an error", context do
    assert @command.validate([], context[:opts]) ==
             {:validation_failure, :not_enough_args}
  end

  test "validate: specifying multiple target nodes is reported as an error", context do
    assert @command.validate(["a", "b", "c"], context[:opts]) ==
             {:validation_failure, :too_many_args}
  end

  test "validate_execution_environment: offline request to a running node fails", context do
    assert match?(
             {:validation_failure, :node_running},
             @command.validate_execution_environment(
               ["other_node@localhost"],
               Map.merge(context[:opts], %{offline: true})
             )
           )
  end

  test "validate_execution_environment: offline forget without mnesia dir fails", context do
    offline_opts =
      Map.merge(
        context[:opts],
        %{offline: true, node: :non_exist@localhost}
      )

    opts_without_mnesia = Map.delete(offline_opts, :mnesia_dir)
    Application.put_env(:mnesia, :dir, "/tmp")
    on_exit(fn -> Application.delete_env(:mnesia, :dir) end)

    assert match?(
             :ok,
             @command.validate_execution_environment(
               ["other_node@localhost"],
               opts_without_mnesia
             )
           )

    Application.delete_env(:mnesia, :dir)
    System.put_env("RABBITMQ_MNESIA_DIR", "/tmp")
    on_exit(fn -> System.delete_env("RABBITMQ_MNESIA_DIR") end)

    assert match?(
             :ok,
             @command.validate_execution_environment(
               ["other_node@localhost"],
               opts_without_mnesia
             )
           )

    System.delete_env("RABBITMQ_MNESIA_DIR")

    assert match?(
             :ok,
             @command.validate_execution_environment(["other_node@localhost"], offline_opts)
           )
  end

  test "validate_execution_environment: online mode does not fail is mnesia is not loaded",
       context do
    opts_without_mnesia = Map.delete(context[:opts], :mnesia_dir)

    assert match?(
             :ok,
             @command.validate_execution_environment(
               ["other_node@localhost"],
               opts_without_mnesia
             )
           )
  end

  test "run: online request to a non-existent node returns a badrpc", context do
    assert match?(
             {:badrpc, :nodedown},
             @command.run(
               [context[:opts][:node]],
               Map.merge(context[:opts], %{node: :non_exist@localhost})
             )
           )
  end

  test "banner", context do
    assert @command.banner(["a"], context[:opts]) =~
             ~r/Removing node a from the cluster/
  end
end