Skip to content

Commit

Permalink
Unit tests added
Browse files Browse the repository at this point in the history
Signed-off-by: jan shahid shaik <jashaik@progress.com>
  • Loading branch information
jashaik committed Sep 25, 2024
1 parent 6d026e2 commit 5570d74
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 9 deletions.
24 changes: 19 additions & 5 deletions src/oc_erchef/apps/chef_license/src/chef_license_worker.erl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
test_license/0
]).

%%-ifdef(TEST).
-export([refresh_license/0]).
%%-endif.

-export([
init/1,
handle_call/3,
Expand All @@ -26,7 +30,7 @@
message
}).

-define(DEFAULT_LICENSE_SCAN_INTERVAL, 3000). %milli seconds
-define(DEFAULT_LICENSE_SCAN_INTERVAL, 30000). %milli seconds

-define(DEFAULT_FILE_PATH, "/tmp/lic").

Expand All @@ -41,14 +45,17 @@ start_link() ->
gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).

get_license()->
gen_server:handle_call(?MODULE, get_license).
gen_server:call(?MODULE, get_license).

refresh_license()->
erlang:send(?MODULE, check_license).

%%% ======================================
%%% Gen Server callbacks
%%% ======================================
init(_Config) ->
State = check_license(#state{}),
gen_server:send_after(?DEFAULT_LICENSE_SCAN_INTERVAL, check_license),
erlang:send_after(?DEFAULT_LICENSE_SCAN_INTERVAL, self(), check_license),
{ok, State}.

handle_call(get_license, _From, #state{license_cache = Lic, grace_period = GracePeriod, message = Msg} = State) ->
Expand All @@ -61,8 +68,9 @@ handle_cast(_Message, State) ->
{noreply, State}.

handle_info(check_license, State)->
io:format("Refershing the license in state~n"),
check_license(State),
gen_server:send_after(?DEFAULT_LICENSE_SCAN_INTERVAL, check_license),
erlang:send_after(?DEFAULT_LICENSE_SCAN_INTERVAL, self(), check_license),
{noreply,State};

handle_info(_Message, State) ->
Expand All @@ -78,7 +86,13 @@ terminate(_Reason, _State) ->
%%% Internal functions
%%% =====================
check_license(State) ->
JsonStr = get_license_info(),
JsonStr =
case catch get_license_info() of
Result when is_list(Result) -> Result;
{'EXIT', _} -> <<"">>
end,
io:format("Json raw: ~p~n",[JsonStr]),
io:format("Process license:~p~n",[process_license(JsonStr)]),
case process_license(JsonStr) of
{ok, valid_license} -> State#state{license_cache=valid, scanned_time = erlang:timestamp()};
{ok, expired} -> State#state{license_cache=expired, scanned_time = erlang:timestamp()};
Expand Down
68 changes: 64 additions & 4 deletions src/oc_erchef/apps/chef_license/test/chef_license_worker_test.erl
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,70 @@

-export([]).

-define(DEFAULT_FILE_PATH, "/tmp/lic").

-define(DEFAULT_CONFIG, []).

get_commercial_license()->
<<"{\"command\":\"chef-automate license status --result-json /tmp/string3\",\"status\":\"OK\",\"error_code\":0,\"error_description\":\"\",\"error_cause\":\"\",\"error_stack_trace\":\"\",\"error_recovery\":\"\",\"error_type\":\"\",\"result\":{\"set\":true,\"license_id\":\"6541d90a-2ed0-4d64-9861-c20fc21a3093\",\"customer_name\":\"janshahid.shaik@progress.com\",\"expiration_date\":{\"seconds\":1835689599},\"deployment_id\":\"fa7cce37-d8b3-4542-ad38-2e587a05faec\",\"deployment_type\":\"Standalone\",\"license_type\":\"commercial\",\"deployment_at\":{\"seconds\":1727169083}}}">>.

get_commercial_license_expired()->
<<"{\"command\":\"chef-automate license status --result-json /tmp/string3\",\"status\":\"OK\",\"error_code\":0,\"error_description\":\"\",\"error_cause\":\"\",\"error_stack_trace\":\"\",\"error_recovery\":\"\",\"error_type\":\"\",\"result\":{\"set\":true,\"license_id\":\"6541d90a-2ed0-4d64-9861-c20fc21a3093\",\"customer_name\":\"janshahid.shaik@progress.com\",\"expiration_date\":{\"seconds\":1635689599},\"deployment_id\":\"fa7cce37-d8b3-4542-ad38-2e587a05faec\",\"deployment_type\":\"Standalone\",\"license_type\":\"commercial\",\"deployment_at\":{\"seconds\":1727169083}}}">>.

get_commercial_grace_license()->
<<"{\"command\":\"chef-automate license status --result-json /tmp/string3\",\"status\":\"OK\",\"error_code\":0,\"error_description\":\"\",\"error_cause\":\"\",\"error_stack_trace\":\"\",\"error_recovery\":\"\",\"error_type\":\"\",\"result\":{\"set\":true,\"grace_period\":true,\"license_id\":\"6541d90a-2ed0-4d64-9861-c20fc21a3093\",\"customer_name\":\"janshahid.shaik@progress.com\",\"expiration_date\":{\"seconds\":1635689599},\"deployment_id\":\"fa7cce37-d8b3-4542-ad38-2e587a05faec\",\"deployment_type\":\"Standalone\",\"license_type\":\"commercial\",\"deployment_at\":{\"seconds\":1727169083}}}">>.

get_trail_license()->
<<"{\"command\":\"chef-automate license status --result-json /tmp/string3\",\"status\":\"OK\",\"error_code\":0,\"error_description\":\"\",\"error_cause\":\"\",\"error_stack_trace\":\"\",\"error_recovery\":\"\",\"error_type\":\"\",\"result\":{\"set\":true,\"grace_period\":true,\"license_id\":\"6541d90a-2ed0-4d64-9861-c20fc21a3093\",\"customer_name\":\"janshahid.shaik@progress.com\",\"expiration_date\":{\"seconds\":1835689599},\"deployment_id\":\"fa7cce37-d8b3-4542-ad38-2e587a05faec\",\"deployment_type\":\"Standalone\",\"license_type\":\"trail\",\"deployment_at\":{\"seconds\":1727169083}}}">>.

get_trail_license_expired()->
<<"{\"command\":\"chef-automate license status --result-json /tmp/string3\",\"status\":\"OK\",\"error_code\":0,\"error_description\":\"\",\"error_cause\":\"\",\"error_stack_trace\":\"\",\"error_recovery\":\"\",\"error_type\":\"\",\"result\":{\"set\":true,\"grace_period\":true,\"license_id\":\"6541d90a-2ed0-4d64-9861-c20fc21a3093\",\"customer_name\":\"janshahid.shaik@progress.com\",\"expiration_date\":{\"seconds\":1635689599},\"deployment_id\":\"fa7cce37-d8b3-4542-ad38-2e587a05faec\",\"deployment_type\":\"Standalone\",\"license_type\":\"trail\",\"deployment_at\":{\"seconds\":1727169083}}}">>.

license_test()->
CJson = get_commercial_license(),
TJson = get_trail_license(),
CJsonGrace = get_commercial_grace_license(),
CJsonExp = get_commercial_license_expired(),
application:start(chef_license),
file:write_file(?DEFAULT_FILE_PATH,get_commercial_license()),
timer:sleep(200),
refresh_license(),
timer:sleep(500),
Result = chef_license_worker:get_license(),
?assertEqual({valid,undefined, undefined}, Result),
os:cmd("rm -rf " ++ ?DEFAULT_FILE_PATH),
timer:sleep(200),

file:write_file(?DEFAULT_FILE_PATH,get_commercial_license_expired()),
timer:sleep(200),
refresh_license(),
timer:sleep(5000),
Result1 = chef_license_worker:get_license(),
?assertEqual({expired,undefined, undefined}, Result1),
os:cmd("rm -rf " ++ ?DEFAULT_FILE_PATH),
timer:sleep(200),

file:write_file(?DEFAULT_FILE_PATH,get_commercial_grace_license()),
timer:sleep(200),
refresh_license(),
timer:sleep(5000),
Result2 = chef_license_worker:get_license(),
?assertEqual({expired,true,undefined}, Result2),
os:cmd("rm -rf " ++ ?DEFAULT_FILE_PATH),
timer:sleep(200),

file:write_file(?DEFAULT_FILE_PATH,get_trail_license()),
timer:sleep(200),
refresh_license(),
timer:sleep(5000),
Result3 = chef_license_worker:get_license(),
?assertEqual({valid, undefined, undefined}, Result3),
os:cmd("rm -rf " ++ ?DEFAULT_FILE_PATH),
timer:sleep(200),

file:write_file(?DEFAULT_FILE_PATH,get_trail_license_expired()),
timer:sleep(200),
refresh_license(),
timer:sleep(5000),
Result4 = chef_license_worker:get_license(),
?assertEqual({expired, undefined, undefined}, Result4),
os:cmd("rm -rf " ++ ?DEFAULT_FILE_PATH).

refresh_license()->
erlang:send(chef_license_worker, check_license).

0 comments on commit 5570d74

Please sign in to comment.