|
| 1 | +%% This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | +%% License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | +%% file, You can obtain one at https://mozilla.org/MPL/2.0/. |
| 4 | +%% |
| 5 | +%% Copyright (c) 2007-2024 Broadcom. All Rights Reserved. The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. All rights reserved. |
| 6 | +%% |
| 7 | + |
| 8 | +-module(ibmmq_ct_helpers). |
| 9 | + |
| 10 | +-include_lib("common_test/include/ct.hrl"). |
| 11 | + |
| 12 | +-export([setup_steps/0, |
| 13 | + teardown_steps/0, |
| 14 | + init_config/1, |
| 15 | + start_ibmmq_server/1, |
| 16 | + stop_ibmmq_server/1]). |
| 17 | + |
| 18 | +setup_steps() -> |
| 19 | + [fun init_config/1, |
| 20 | + fun start_ibmmq_server/1 |
| 21 | + ]. |
| 22 | + |
| 23 | +teardown_steps() -> |
| 24 | + [ |
| 25 | + fun stop_ibmmq_server/1 |
| 26 | + ]. |
| 27 | + |
| 28 | +init_config(Config) -> |
| 29 | + NodeConfig = [{tcp_port_amqp, 5672}], |
| 30 | + rabbit_ct_helpers:set_config(Config, [ {rmq_nodes, [NodeConfig]}, |
| 31 | + {rmq_hostname, "localhost"}, |
| 32 | + {tcp_hostname_amqp, "localhost"}, |
| 33 | + {sasl, {plain, <<"app">>, <<"passw0rd">>}} ]). |
| 34 | + |
| 35 | +start_ibmmq_server(Config) -> |
| 36 | + IBMmqCmd = filename:join([?config(data_dir, Config), "ibmmq_runner"]), |
| 37 | + Cmd = [IBMmqCmd, "start"], |
| 38 | + ct:log("Running command ~p", [Cmd]), |
| 39 | + case rabbit_ct_helpers:exec(Cmd, []) of |
| 40 | + {ok, _} -> wait_for_ibmmq_nodes(Config); |
| 41 | + Error -> ct:pal("Error: ~tp", [Error]), |
| 42 | + {skip, "Failed to start IBM MQ"} |
| 43 | + end. |
| 44 | + |
| 45 | +wait_for_ibmmq_nodes(Config) -> |
| 46 | + Hostname = ?config(rmq_hostname, Config), |
| 47 | + Ports = rabbit_ct_broker_helpers:get_node_configs(Config, tcp_port_amqp), |
| 48 | + wait_for_ibmmq_ports(Config, Hostname, Ports). |
| 49 | + |
| 50 | +wait_for_ibmmq_ports(Config, Hostname, [Port | Rest]) -> |
| 51 | + ct:log("Waiting for IBM MQ on port ~b", [Port]), |
| 52 | + case wait_for_ibmmq_port(Hostname, Port, 60) of |
| 53 | + ok -> |
| 54 | + ct:log("IBM MQ ready on port ~b", [Port]), |
| 55 | + wait_for_ibmmq_ports(Config, Hostname, Rest); |
| 56 | + {error, _} -> |
| 57 | + Msg = lists:flatten( |
| 58 | + io_lib:format( |
| 59 | + "Failed to start IBM MQ on port ~b; see IBM MQ logs", |
| 60 | + [Port])), |
| 61 | + ct:pal(?LOW_IMPORTANCE, Msg, []), |
| 62 | + {skip, Msg} |
| 63 | + end; |
| 64 | +wait_for_ibmmq_ports(Config, _, []) -> |
| 65 | + Config. |
| 66 | + |
| 67 | +wait_for_ibmmq_port(_, _, 0) -> |
| 68 | + {error, econnrefused}; |
| 69 | +wait_for_ibmmq_port(Hostname, Port, Retries) -> |
| 70 | + case gen_tcp:connect(Hostname, Port, []) of |
| 71 | + {ok, Connection} -> |
| 72 | + gen_tcp:close(Connection), |
| 73 | + ok; |
| 74 | + {error, econnrefused} -> |
| 75 | + timer:sleep(1000), |
| 76 | + wait_for_ibmmq_port(Hostname, Port, Retries - 1); |
| 77 | + Error -> |
| 78 | + Error |
| 79 | + end. |
| 80 | + |
| 81 | +stop_ibmmq_server(Config) -> |
| 82 | + IBMmqCmd = filename:join([?config(data_dir, Config), "ibmmq_runner"]), |
| 83 | + Cmd = [IBMmqCmd, "stop"], |
| 84 | + ct:log("Running command ~p", [Cmd]), |
| 85 | + case rabbit_ct_helpers:exec(Cmd, []) of |
| 86 | + {ok, _} -> Config; |
| 87 | + Error -> ct:pal("Error: ~tp", [Error]), |
| 88 | + {skip, "Failed to stop IBM MQ"} |
| 89 | + end. |
0 commit comments