Skip to content

Commit 4495235

Browse files
committed
testing add/remove users REST API in integration tests
1 parent 218394d commit 4495235

4 files changed

Lines changed: 84 additions & 5 deletions

File tree

ci/run.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@ run ./ci/test_distribute_scenario.sh
1515
run ./ci/test_run_scenario.sh
1616
run ./ci/test_add_new_node.sh
1717
run ./ci/test_update_settings.sh
18+
run ./ci/test_add_and_remove_users.sh
1819
run ./ci/stop_demo_cluster.sh

ci/test_add_and_remove_users.sh

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#!/bin/bash
2+
3+
source "$(dirname "$0")/helper.sh"
4+
enable_strict_mode
5+
6+
#############################
7+
## amoc REST API functions ##
8+
#############################
9+
10+
function node_list() {
11+
local nodes=( "$@" ) nodes_json="" node=
12+
if [ "${#nodes[@]}" -gt "0" ]; then
13+
nodes_json+='"nodes": ["'"${nodes[0]}"'"'
14+
for node in "${nodes[@]:1}"; do
15+
nodes_json+=', "'"$node"'"'
16+
done
17+
nodes_json+='], '
18+
fi
19+
echo -n "$nodes_json"
20+
}
21+
22+
function add_users() {
23+
local port="$(amoc_container_port "$1")" # $1 - where to send request
24+
local n="$2" # $2 - number of users to add
25+
shift 2
26+
local nodes="$(node_list "$@")"
27+
local json_body="{ ${nodes} \"users\": $n}"
28+
curl -X PATCH --header 'Content-Type: application/json' --header 'Accept: application/json' \
29+
-s -w " %{http_code}" -d "$json_body" "http://localhost:${port}/execution/add_users"
30+
}
31+
32+
function remove_users() {
33+
local port="$(amoc_container_port "$1")" # $1 - where to send request
34+
local n="$2" # $2 - number of users to add
35+
shift 2
36+
local nodes="$(node_list "$@")"
37+
local json_body="{ ${nodes} \"users\": $n}"
38+
curl -X PATCH --header 'Content-Type: application/json' --header 'Accept: application/json' \
39+
-s -w " %{http_code}" -d "$json_body" "http://localhost:${port}/execution/remove_users"
40+
}
41+
42+
43+
echo "adding users to nodes"
44+
add_users amoc-master 6 "amoc_arsenal@amoc-worker-3" | contains 200
45+
add_users amoc-master 2 "amoc_arsenal@"{"amoc-worker-1","amoc-worker-2"} | contains 200
46+
add_users amoc-master 15 | contains 200 ## add 5 users on each node
47+
add_users amoc-worker-3 15 | contains 500 '"error":"not_a_master"'
48+
49+
echo "checking status of the nodes"
50+
sleep 1 ## 1 second is enough to start 11
51+
get_status amoc-master | contains '"amoc_status":"up"' \
52+
'"status":"disabled"'
53+
worker_status=( '"amoc_status":"up"'
54+
'"status":"running"'
55+
'"scenario":"dummy_scenario"'
56+
'"test":"<<\\"test_value\\">>"'
57+
'"interarrival":"50"' )
58+
get_status amoc-worker-1 | contains "${worker_status[@]}" '"number_of_users":11'
59+
get_status amoc-worker-2 | contains "${worker_status[@]}" '"number_of_users":11'
60+
get_status amoc-worker-3 | contains "${worker_status[@]}" '"number_of_users":11'
61+
62+
echo "removing users from nodes"
63+
## remove all the users from amoc-worker-3
64+
remove_users amoc-master 11 "amoc_arsenal@amoc-worker-3" | contains 200
65+
remove_users amoc-master 2 "amoc_arsenal@"{"amoc-worker-1","amoc-worker-2"} | contains 200
66+
sleep 3 ## two seconds is a shutdown period for a forced users removal.
67+
## so we must wait before removing the next chunk of users,
68+
## otherwise the same users can be selected for removal twice.
69+
remove_users amoc-master 15 | contains 200 ## remove 5 users from each node
70+
remove_users amoc-worker-3 15 | contains 500 '"error":"not_a_master"'
71+
72+
echo "checking status of the nodes"
73+
sleep 3 ## 2 seconds is a shutdown period for a forced users removal.
74+
get_status amoc-master | contains '"amoc_status":"up"' \
75+
'"status":"disabled"'
76+
get_status amoc-worker-1 | contains "${worker_status[@]}" '"number_of_users":5'
77+
get_status amoc-worker-2 | contains "${worker_status[@]}" '"number_of_users":5'
78+
get_status amoc-worker-3 | contains "${worker_status[@]}" '"number_of_users":0'

src/rest_api/amoc_api_helpers_execution.erl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,9 @@ add_users(_) ->
6565

6666
-spec remove_users(body()) -> ret_value().
6767
remove_users(#{<<"users">> := Users, <<"nodes">> := Nodes}) ->
68-
amoc_dist:remove(Users, false, read_nodes(Nodes));
68+
amoc_dist:remove(Users, true, read_nodes(Nodes));
6969
remove_users(#{<<"users">> := Users}) ->
70-
amoc_dist:remove(Users, false);
70+
amoc_dist:remove(Users, true);
7171
remove_users(_) ->
7272
{error, invalid_body}.
7373

test/amoc_api_execution_handler_SUITE.erl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,13 @@ setup_meck(add_users_on_nodes) ->
9292
meck:expect(amoc_dist, add, fun(10, [node1@host1, node2@host2]) -> {ok, mocked} end);
9393
setup_meck(remove_users) ->
9494
meck:expect(amoc_dist, get_state, fun() -> running end),
95-
meck:expect(amoc_dist, remove, fun(10, false) -> {ok, mocked} end);
95+
meck:expect(amoc_dist, remove, fun(10, true) -> {ok, mocked} end);
9696
setup_meck(fail_to_remove_users_when_amoc_dist_fails) ->
9797
meck:expect(amoc_dist, get_state, fun() -> running end),
98-
meck:expect(amoc_dist, remove, fun(10, false) -> {error, mocked} end);
98+
meck:expect(amoc_dist, remove, fun(10, true) -> {error, mocked} end);
9999
setup_meck(remove_users_on_nodes) ->
100100
meck:expect(amoc_dist, get_state, fun() -> running end),
101-
meck:expect(amoc_dist, remove, fun(10, false, [node1@host1, node2@host2]) -> {ok, mocked} end);
101+
meck:expect(amoc_dist, remove, fun(10, true, [node1@host1, node2@host2]) -> {ok, mocked} end);
102102
setup_meck(update_settings) ->
103103
meck:expect(amoc_dist, get_state, fun() -> running end),
104104
meck:expect(amoc_dist, update_settings,

0 commit comments

Comments
 (0)