summaryrefslogtreecommitdiffstats
path: root/src/util.erl
diff options
context:
space:
mode:
authorTommy Carpenter <tommy@research.att.com>2017-09-11 16:16:02 -0400
committerTommy Carpenter <tommy@research.att.com>2017-09-11 17:25:25 -0400
commit3080e069b14f58dcc4609721cad6ea24ee19a6e9 (patch)
tree660163fe393ff4e5d77fd083be04d263db84a168 /src/util.erl
parentce25370b6e0d8ca806c47e36222547c4edd6c6b5 (diff)
Improve Unit Tests
Issue-ID: DCAEGEN2-99 Change-Id: I228b449cd22a215cf9e16b44ea07f54cefd74d7e Signed-off-by: Tommy Carpenter <tommy@research.att.com>
Diffstat (limited to 'src/util.erl')
-rw-r--r--src/util.erl54
1 files changed, 27 insertions, 27 deletions
diff --git a/src/util.erl b/src/util.erl
index d96675b..520b071 100644
--- a/src/util.erl
+++ b/src/util.erl
@@ -6,9 +6,9 @@
% Licensed under the Apache License, Version 2.0 (the "License");
% you may not use this file except in compliance with the License.
% You may obtain a copy of the License at
-%
+%
% http://www.apache.org/licenses/LICENSE-2.0
-%
+%
% Unless required by applicable law or agreed to in writing, software
% distributed under the License is distributed on an "AS IS" BASIS,
% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -21,8 +21,8 @@
-module(util).
-include("application.hrl").
--export([concat/1,
- get_platform_envs_and_config/0,
+-export([concat/1,
+ get_platform_envs_and_config/0,
resolve_cbs/2,
initialize_database/0,
get_all_appnames_from_db/0,
@@ -39,10 +39,7 @@
%http://stackoverflow.com/questions/39757020/erlang-drying-up-stringbinary-concatenation
%NOTE! Does not work or bomb when an element in the list is an atom. Must be a string or binary. Maybe add a check for this
-to_string(Value) when is_binary(Value) -> binary_to_list(Value);
-to_string(Value) -> Value.
-concat(List) ->
- lists:flatten(lists:map(fun to_string/1, List)).
+concat(List) -> lists:flatten(lists:map(fun to_str/1, List)).
resolve_cbs(XER, ConsulURL) ->
%Ideally this function would dissapear if we get real DNS. This essentially is doing an SRV record lookup every time someone needs the bindng URL
@@ -52,30 +49,30 @@ resolve_cbs(XER, ConsulURL) ->
concat(["http://", IP, ":", integer_to_binary(Port)]).
get_platform_envs_and_config() ->
- %Get platform envs needed for broker operation, then fetch my config.
+ %Get platform envs needed for broker operation, then fetch my config.
%If something critical fails, returns [], else [ConsulURL, CDAPUrl, BoundConfigMap]
MyName = os:getenv("HOSTNAME"),
ConsulHost = os:getenv("CONSUL_HOST"),
- case MyName == false orelse ConsulHost == false of
+ case MyName == false orelse ConsulHost == false of
true -> [];
- false ->
+ false ->
%build Consul URL
ConsulURL = concat(["http://", ConsulHost, ":8500"]),
-
+
%Bind my own config map
%generate my own XER here
XER = gen_uuid(),
{200, BoundConfig} = consul_interface:consul_bind_config(XER, MyName, ConsulURL),
BoundConfigMap = jiffy:decode(jiffy:encode(BoundConfig), [return_maps]), %kind of an interesting way to turn an erlang proplist into a map
-
+
%Here, we waterfall looking for "CDAP_CLUSTER_TO_MANAGE".
%First, we will check for environmnental variables for a cluster *NAME*
%If that is not found, then we will check out bound config for a fully bound URL
%If that is also not found, let it crash baby.
- CDAPURL = case os:getenv("CDAP_CLUSTER_TO_MANAGE") of
- false ->
+ CDAPURL = case os:getenv("CDAP_CLUSTER_TO_MANAGE") of
+ false ->
list_to_binary(concat(["http://", lists:nth(1, maps:get(<<"cdap_cluster_to_manage">>, BoundConfigMap))])); %cbs returns ip:port. need http:// or will get "no adaptors found" error
- CDAPName ->
+ CDAPName ->
{IP, Port} = consul_interface:consul_get_service_ip_port(XER, CDAPName, ConsulURL),
list_to_binary(concat(["http://", IP, ":", integer_to_binary(Port)]))
end,
@@ -84,13 +81,13 @@ get_platform_envs_and_config() ->
initialize_database() ->
%Create the database (currently MNesia) if it does not exist, and the application table.
- %Or, do nothing.
+ %Or, do nothing.
N = node(),
lager:info(io_lib:format("Initializing database. My node name is ~s", [N])),
%set MNesia dir
application:set_env(mnesia, dir, "/var/mnesia/"),
-
+
%stop if running, can't create schema if it is. Dont check status, OK if stopped
mnesia:stop(),
@@ -98,16 +95,16 @@ initialize_database() ->
%erlang:display(mnesia:delete_schema([N])),
mnesia:create_schema([N]),
%start MNesia, assert it works
-
+
ok = mnesia:start(), %start MNesia, bomb if alreay started, should not happen
lager:info("Mnesia started"),
-
+
%try to create the table, or if it exists, do nothing
%erlang:display(mnesia:delete_table(application)),
case mnesia:create_table(application, [{attributes, record_info(fields, application)}, {disc_copies, [N]}]) of
{aborted,{already_exists,application}} ->
lager:info("Application table already exists");
- {atomic,ok} ->
+ {atomic,ok} ->
lager:info(io_lib:format("Created application table on ~s", [N]))
end,
@@ -116,14 +113,14 @@ initialize_database() ->
case mnesia:create_table(prog_flow_supp, [{attributes, record_info(fields, prog_flow_supp)}, {disc_copies, [N]}]) of
{aborted,{already_exists, prog_flow_supp}} ->
lager:info("prog_flow_supp table already exists");
- {atomic,ok} ->
+ {atomic,ok} ->
lager:info(io_lib:format("Created prog_flow_supp table on ~s", [N]))
end,
%wait up to 30s for the table to come up. Usually instantaneous. If it takes more crash abd burn
ok = mnesia:wait_for_tables([application, prog_flow_supp], 30000),
ok.
-
+
get_all_appnames_from_db() ->
{atomic, Apps} = mnesia:transaction(fun() -> mnesia:match_object(application, #application{_ = '_'}, read) end),
lists:map(fun(X) -> {application, Appname,_,_,_,_,_,_,_,_} = X,
@@ -159,7 +156,10 @@ iso_elapsed(Endtime, Starttime) ->
Egs - Sgs.
to_str("") -> "";
-to_str(Term) -> lists:flatten(io_lib:format("~p", [Term])).
+to_str(Term) when is_list(Term) -> Term;
+to_str(Term) when is_binary(Term) -> binary_to_list(Term);
+to_str(Term) ->
+ lists:flatten(io_lib:format("~p", [Term])).
-spec ip_to_str({inet:ip_address(), inet:port_number()}) -> binary().
%nasty.. I miss pythons x <= Foo <= Y syntax.. or something mathematical like Foo in [X..Y].. erlang not good 4 math
@@ -176,9 +176,9 @@ update_with_new_config_map(NewConfig, OldConfig) ->
%This is very similar to the maps:merge/2 builtin but that will inject keys of newconfig that were not in oldconfig. We need a "RIGHT JOIN"
NCKeys = maps:keys(NewConfig),
ConfigOverlaps = [X || X <- NCKeys, maps:is_key(X, OldConfig)],
- case ConfigOverlaps of
+ case ConfigOverlaps of
[] -> nooverlap;
- _ ->
+ _ ->
%we have an entry that should be in app config
%build a new map with just the keys to update
Pred = fun(X,_) -> lists:member(X, ConfigOverlaps) end,
@@ -187,6 +187,6 @@ update_with_new_config_map(NewConfig, OldConfig) ->
end.
ejson_to_map(E) ->
- %takes the jiffy "ejson: format of {[{<<"foo">>, <<"bar">>}, {<<"foo2">>, <<"bar2">>}]} and turns it into a map,
+ %takes the jiffy "ejson: format of {[{<<"foo">>, <<"bar">>}, {<<"foo2">>, <<"bar2">>}]} and turns it into a map,
%usefu because ejsons do not appear to be order-independent-comparable, but maps are (e.g., two maps are equal if all their k+v are equal but agnostic to order)
jiffy:decode(jiffy:encode(E), [return_maps]).