2020-07-12 02:23:07 +08:00
## 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/.
2016-04-04 23:41:54 +08:00
##
2020-03-10 22:39:56 +08:00
## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved.
2016-04-04 23:41:54 +08:00
defmodule SetParameterCommandTest do
use ExUnit.Case , async : false
import TestHelper
2016-06-10 07:54:22 +08:00
@command RabbitMQ.CLI.Ctl.Commands.SetParameterCommand
2016-04-04 23:41:54 +08:00
@vhost " test1 "
@root " / "
@component_name " federation-upstream "
@key " reconnect-delay "
2020-06-26 10:55:57 +08:00
@value " { \" uri \" : \" amqp://127.0.0.1:5672 \" } "
2016-04-04 23:41:54 +08:00
setup_all do
2016-11-15 01:52:08 +08:00
RabbitMQ.CLI.Core.Distribution . start ( )
2017-06-07 23:21:53 +08:00
2016-04-04 23:41:54 +08:00
add_vhost @vhost
2016-09-07 20:28:06 +08:00
enable_federation_plugin ( )
2016-04-04 23:41:54 +08:00
on_exit ( [ ] , fn ->
delete_vhost @vhost
end )
2020-06-26 10:55:57 +08:00
# featured in a definitions file imported by other tests
clear_parameter ( " / " , " federation-upstream " , " up-1 " )
2016-04-04 23:41:54 +08:00
:ok
end
setup context do
on_exit ( context , fn ->
clear_parameter context [ :vhost ] , context [ :component_name ] , context [ :key ]
end )
{
:ok ,
opts : %{
2017-01-24 20:33:48 +08:00
node : get_rabbit_hostname ( ) ,
2017-12-09 01:50:18 +08:00
vhost : context [ :vhost ]
2016-04-04 23:41:54 +08:00
}
}
end
2016-05-24 19:33:11 +08:00
@tag component_name : @component_name , key : @key , value : @value , vhost : @root
2016-05-27 19:10:11 +08:00
test " merge_defaults: a well-formed command with no vhost runs against the default " do
2016-06-10 07:54:22 +08:00
assert match? ( { _ , %{ vhost : " / " } } , @command . merge_defaults ( [ ] , %{ } ) )
2017-12-09 01:50:18 +08:00
assert match? ( { _ , %{ vhost : " non_default " } } , @command . merge_defaults ( [ ] , %{ vhost : " non_default " } ) )
2016-05-24 19:33:11 +08:00
end
test " validate: wrong number of arguments leads to an arg count error " do
2016-06-10 07:54:22 +08:00
assert @command . validate ( [ ] , %{ } ) == { :validation_failure , :not_enough_args }
assert @command . validate ( [ " insufficient " ] , %{ } ) == { :validation_failure , :not_enough_args }
assert @command . validate ( [ " not " , " enough " ] , %{ } ) == { :validation_failure , :not_enough_args }
assert @command . validate ( [ " this " , " is " , " too " , " many " ] , %{ } ) == { :validation_failure , :too_many_args }
2016-04-04 23:41:54 +08:00
end
@tag component_name : @component_name , key : @key , value : @value , vhost : @vhost
2016-05-24 19:33:11 +08:00
test " run: a well-formed, host-specific command returns okay " , context do
2016-05-20 00:28:27 +08:00
vhost_opts = Map . merge ( context [ :opts ] , %{ vhost : context [ :vhost ] } )
2016-05-03 05:53:18 +08:00
2016-06-10 07:54:22 +08:00
assert @command . run (
2016-05-24 19:33:11 +08:00
[ context [ :component_name ] , context [ :key ] , context [ :value ] ] ,
vhost_opts
) == :ok
2016-04-04 23:41:54 +08:00
2016-04-13 21:46:03 +08:00
assert_parameter_fields ( context )
2016-04-04 23:41:54 +08:00
end
2016-12-17 02:16:07 +08:00
test " run: throws a badrpc when instructed to contact an unreachable RabbitMQ node " do
2019-04-12 06:27:15 +08:00
opts = %{ node : :jake @thedog , vhost : " / " , timeout : 200 }
2017-06-07 23:21:53 +08:00
2019-04-12 06:27:15 +08:00
assert match? ( { :badrpc , _ } , @command . run ( [ @component_name , @key , @value ] , opts ) )
2016-04-04 23:41:54 +08:00
end
@tag component_name : " bad-component-name " , key : @key , value : @value , vhost : @root
2016-05-24 19:33:11 +08:00
test " run: an invalid component_name returns a validation failed error " , context do
2016-06-10 07:54:22 +08:00
assert @command . run (
2016-05-24 19:33:11 +08:00
[ context [ :component_name ] , context [ :key ] , context [ :value ] ] ,
context [ :opts ]
) == { :error_string , ' Validation failed \n \n component #{ context [ :component_name ] } not found \n ' }
2016-04-04 23:41:54 +08:00
assert list_parameters ( context [ :vhost ] ) == [ ]
end
@tag component_name : @component_name , key : @key , value : @value , vhost : " bad-vhost "
2016-05-24 19:33:11 +08:00
test " run: an invalid vhost returns a no-such-vhost error " , context do
2016-05-20 00:28:27 +08:00
vhost_opts = Map . merge ( context [ :opts ] , %{ vhost : context [ :vhost ] } )
2016-05-03 05:53:18 +08:00
2016-06-10 07:54:22 +08:00
assert @command . run (
2016-05-24 19:33:11 +08:00
[ context [ :component_name ] , context [ :key ] , context [ :value ] ] ,
vhost_opts
) == { :error , { :no_such_vhost , context [ :vhost ] } }
2016-04-04 23:41:54 +08:00
end
@tag component_name : @component_name , key : @key , value : " bad-value " , vhost : @root
2016-05-24 19:33:11 +08:00
test " run: an invalid value returns a JSON decoding error " , context do
2017-02-02 05:13:11 +08:00
assert match? ( { :error_string , _ } ,
@command . run ( [ context [ :component_name ] , context [ :key ] , context [ :value ] ] ,
context [ :opts ] ) )
2016-04-04 23:41:54 +08:00
assert list_parameters ( context [ :vhost ] ) == [ ]
end
@tag component_name : @component_name , key : @key , value : " {} " , vhost : @root
2016-05-24 19:33:11 +08:00
test " run: an empty JSON object value returns a key \" uri \" not found error " , context do
2016-06-10 07:54:22 +08:00
assert @command . run (
2016-05-24 19:33:11 +08:00
[ context [ :component_name ] , context [ :key ] , context [ :value ] ] ,
context [ :opts ]
) == { :error_string , ' Validation failed \n \n Key "uri" not found in reconnect-delay \n ' }
2016-04-04 23:41:54 +08:00
assert list_parameters ( context [ :vhost ] ) == [ ]
end
2016-04-13 21:46:03 +08:00
2016-05-03 05:53:18 +08:00
@tag component_name : @component_name , key : @key , value : @value , vhost : @vhost
2016-05-24 19:33:11 +08:00
test " banner " , context do
2016-05-20 00:28:27 +08:00
vhost_opts = Map . merge ( context [ :opts ] , %{ vhost : context [ :vhost ] } )
2016-05-03 05:53:18 +08:00
2016-06-10 07:54:22 +08:00
assert @command . banner ( [ context [ :component_name ] , context [ :key ] , context [ :value ] ] , vhost_opts )
2019-09-20 17:41:33 +08:00
=~ ~r/ Setting runtime parameter \" #{ context [ :key ] } \" for component \" #{ context [ :component_name ] } \" to \" #{ context [ :value ] } \" in vhost \" #{ context [ :vhost ] } \" \. \. \. /
2016-05-03 05:53:18 +08:00
end
2016-04-13 21:46:03 +08:00
# Checks each element of the first parameter against the expected context values
defp assert_parameter_fields ( context ) do
result_param = context [ :vhost ] |> list_parameters |> List . first
assert result_param [ :value ] == context [ :value ]
assert result_param [ :component ] == context [ :component_name ]
assert result_param [ :name ] == context [ :key ]
end
2016-04-04 23:41:54 +08:00
end