summaryrefslogtreecommitdiff
path: root/deps/rabbitmq_cli/test/ctl/import_definitions_command_test.exs
blob: fb7f975ec5a56ddb1170d759a58110701c28d959 (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
## 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) 2007-2020 VMware, Inc. or its affiliates.  All rights reserved.


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

  @command RabbitMQ.CLI.Ctl.Commands.ImportDefinitionsCommand

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

    :ok
  end

  setup context do
    {:ok, opts: %{
        node: get_rabbit_hostname(),
        timeout: context[:test_timeout] || 30000,
        format: context[:format] || "json"
      }}
  end

  test "merge_defaults: defaults to JSON for format" do
    assert @command.merge_defaults([valid_file_path()], %{}) ==
             {[valid_file_path()], %{format: "json"}}
  end

  test "merge_defaults: defaults to --silent if target is stdout" do
    assert @command.merge_defaults(["-"], %{}) == {["-"], %{format: "json", silent: true}}
  end

  test "merge_defaults: format is case insensitive" do
    assert @command.merge_defaults([valid_file_path()], %{format: "JSON"}) ==
             {[valid_file_path()], %{format: "json"}}
    assert @command.merge_defaults([valid_file_path()], %{format: "Erlang"}) ==
             {[valid_file_path()], %{format: "erlang"}}
  end

  test "merge_defaults: format can be overridden" do
    assert @command.merge_defaults([valid_file_path()], %{format: "erlang"}) ==
             {[valid_file_path()], %{format: "erlang"}}
  end

  test "validate: accepts a file path argument", context do
    assert @command.validate([valid_file_path()], context[:opts]) == :ok
  end

  test "validate: unsupported format fails validation", context do
    assert match?({:validation_failure, {:bad_argument, _}},
                  @command.validate([valid_file_path()], Map.merge(context[:opts], %{format: "yolo"})))
  end

  test "validate: more than one positional argument fails validation", context do
    assert @command.validate([valid_file_path(), "extra-arg"], context[:opts]) ==
             {:validation_failure, :too_many_args}
  end

  test "validate: supports JSON and Erlang formats", context do
    assert @command.validate([valid_file_path()], Map.merge(context[:opts], %{format: "json"})) == :ok
    assert @command.validate([valid_file_path()], Map.merge(context[:opts], %{format: "erlang"})) == :ok
  end

  @tag test_timeout: 3000
  test "run: targeting an unreachable node throws a badrpc", context do
    result = @command.run([valid_file_path()],
                          %{node: :jake@thedog,
                            timeout: context[:test_timeout],
                            format: "json"})
    assert match?({:badrpc, _}, result)
  end

  @tag format: "json"
  test "run: imports definitions from a file", context do
    assert :ok == @command.run([valid_file_path()], context[:opts])

    # clean up the state we've modified
    clear_parameter("/", "federation-upstream", "up-1")
  end

  defp valid_file_path() do
    Path.join([File.cwd!(), "test", "fixtures", "files", "definitions.json"])
  end
end