diff options
author | Tommy Carpenter <tommy@research.att.com> | 2017-09-06 22:08:30 -0400 |
---|---|---|
committer | Tommy Carpenter <tommy@research.att.com> | 2017-09-08 14:28:28 -0400 |
commit | b5f293ece50abeb188d8245a2d803e5047fdcdbb (patch) | |
tree | a974b88603a779cddbd67ba17a19e015ea6150d5 /test/apitest/apitest_SUITE.erl | |
parent | ca2c3ada0487ced88ebba5cf5b39af4ec578b13e (diff) |
Clean up resource handler.
Refactors some resource_handler functions to split between pure and impure.
Adds more unit tests and removes integration_tests
IssueID: DCAEGEN2-99
Change-Id: I667e3cdeb7346b87b5e8a54dcfbf5058858f10a6
Signed-off-by: Tommy Carpenter <tommy@research.att.com>
Diffstat (limited to 'test/apitest/apitest_SUITE.erl')
-rw-r--r-- | test/apitest/apitest_SUITE.erl | 143 |
1 files changed, 60 insertions, 83 deletions
diff --git a/test/apitest/apitest_SUITE.erl b/test/apitest/apitest_SUITE.erl index 971204c..1f7a3b2 100644 --- a/test/apitest/apitest_SUITE.erl +++ b/test/apitest/apitest_SUITE.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,7 +21,7 @@ -include_lib("common_test/include/ct.hrl"). -include("../../src/application.hrl"). -export([all/0, groups/0, init_per_suite/1, end_per_suite/1]). --export([server_health_test/1, app_deploy/1, hydrator_deploy/1, app_teardown/1, app_test/1, app_reconfigure/1, test_failures/1, app_botch_flows/1, app_botch_delete/1, app_botch_consul_delete/1, invalid_reconfigure/1, delete_all/1, +-export([server_health_test/1, app_deploy/1, hydrator_deploy/1, app_teardown/1, app_test/1, app_reconfigure/1, test_failures/1, app_botch_flows/1, app_botch_delete/1, app_botch_consul_delete/1, delete_all/1, hydrator_app_teardown/1, hydrator_test/1, hydrator_wdeps_deploy/1, hydrator_wdeps_test/1, @@ -30,7 +30,7 @@ %lazy shorthands (yay C style macros! miss these in python) -define(SC(L), util:concat(L)). --define(PLG(K, PL), proplists:get_value(K, PL)). +-define(PLG(K, PL), proplists:get_value(K, PL)). -define(XER, "testing-XER"). -define(D(X), erlang:display(X)). @@ -40,7 +40,6 @@ all() -> [ {group, apibotchedflows}, {group, apibotcheddeleted}, {group, apibotchedconsuldeleted}, - {group, invalidreconfig}, {group, apideleteall} ]. groups() -> [ @@ -91,15 +90,7 @@ groups() -> [ app_botch_consul_delete, app_teardown ]}, - {invalidreconfig, %call reconfigure on an app that DNE - [], - [ - server_health_test, - app_deploy, - invalid_reconfigure, - app_teardown - ]}, - {apideleteall, + {apideleteall, [], [ server_health_test, @@ -119,9 +110,9 @@ setup_rels(Config, D) -> % 4 Broker binds config % 5 Broker pushes bound config to CDAP % Between state 1 and 3 consul is in an inconsistent state where it has only the rels key but not the config key. Not so sure about this. They seem to be a pair. Maybe the rels key should be pushed to the source node to be dealt with. - % #Here, we are mocking step 1 + % #Here, we are mocking step 1 URL = ?SC([?PLG(consul_url, Config), "/v1/kv/", ?PLG(appname, Config), ":rel"]), - case D of + case D of setup -> {200,"true"} = httpabs:put(?XER, URL, "application/json", jiffy:encode([<<"666_fake_testing_service">>])); teardown -> {200,"true"} = httpabs:delete(?XER, URL) end, @@ -131,7 +122,7 @@ setup_fake_testing_service(Config, D) -> %register a fake testing service to test that the CDAP app recieved it's bound configuration properly Name = <<"666_fake_testing_service">>, SrvURL = ?SC([?PLG(consul_url, Config), "/v1/catalog/service/", Name]), - case D of + case D of setup -> URL = ?SC([?PLG(consul_url, Config), "/v1/agent/service/register"]), Body = {[{<<"name">>, Name}, @@ -147,10 +138,10 @@ setup_fake_testing_service(Config, D) -> httpabs:get(?XER, SrvURL) end. -get_config_consul(C) -> +get_config_consul(C) -> %get config from consul. returns the code too for tests testing for a 404 - {RC, RB} = consul_interface:consul_get_configuration(?XER, ?PLG(appname, C), ?PLG(consul_url, C)), - case RC of + {RC, RB} = consul_interface:consul_get_configuration(?XER, ?PLG(appname, C), ?PLG(consul_url, C)), + case RC of 200 -> {RC, util:ejson_to_map(RB)}; _ -> {RC, RB} end. @@ -158,7 +149,7 @@ get_config_consul(C) -> get_config_cdap(C) -> {RC, RB} = cdap_interface:get_app_config(?XER, ?PLG(appname, C), ?PLG(namespace, C),?PLG(cdap_url, C)), case RC of - 200 -> + 200 -> %I think CDAP is DOUBLY encoding JSON!! {RC, jiffy:decode(jiffy:decode(jiffy:encode(RB)), [return_maps])}; _ -> {RC, RB} @@ -171,10 +162,10 @@ get_preferences_cdap(C) -> _ -> {RC, RB} end. -get_preferences_consul(C) -> +get_preferences_consul(C) -> %get preferences from consul. returns the code too for tests testing for a 404 {RC, RB} = consul_interface:consul_get_preferences(?XER, ?PLG(appname, C), ?PLG(consul_url, C)), - case RC of + case RC of 200 -> {RC, util:ejson_to_map(RB)}; _ -> {RC, RB} end. @@ -189,7 +180,7 @@ valid_deploy_body(C) -> {<<"artifact_version">>, ?PLG(art_ver, C)}, {<<"app_config">>, ?PLG(init_config, C)}, {<<"app_preferences">>, ?PLG(init_preferences, C)}, - {<<"services">>, [{[{<<"service_name">>, <<"Greeting">>}, + {<<"services">>, [{[{<<"service_name">>, <<"Greeting">>}, {<<"service_endpoint">>, <<"greet">>}, {<<"endpoint_method">>, <<"GET">>}]}]}, {<<"programs">>, [ @@ -198,8 +189,8 @@ valid_deploy_body(C) -> {[{<<"program_type">>, <<"services">>}, {<<"program_id">>, <<"Greeting">>}]}]}, {<<"program_preferences">>, [ - {[{<<"program_type">>,<<"flows">>}, - {<<"program_id">>, <<"WhoFlow">>}, + {[{<<"program_type">>,<<"flows">>}, + {<<"program_id">>, <<"WhoFlow">>}, {<<"program_pref">>, ?PLG(whoflowpref, C)}]} ]} ]}. @@ -209,7 +200,7 @@ valid_deploy_body(C) -> init_per_suite(_C) -> %get platform ENVs [MyName, ConsulURL, _, _] = util:get_platform_envs_and_config(), - + BrokerUrl = case os:getenv("BROKER_TEST_TYPE") of false -> %no env variable means start the broker on localhost %start a local broker @@ -229,7 +220,7 @@ init_per_suite(_C) -> {200, RB} = httpabs:get(?XER, BrokerUrl), CDAPUrl = maps:get(<<"managed cdap url">>, jiffy:decode(RB, [return_maps])), - + %set properties that are shared between program-flowlet and hydrator Namespace = <<"testns">>, CDAPUrlNS = ?SC([CDAPUrl, "/v3/namespaces/", Namespace]), @@ -242,14 +233,14 @@ init_per_suite(_C) -> HydratorAppname = <<"hydratortest">>, HydratorAppURL = ?SC([CDAPUrlNS, "/apps/", HydratorAppname]), HydratorStreamname = <<"s1">>, %horrible name but not made by me - + HydratorWDepsAppname = <<"hydratorwdepstest">>, HydratorWDepsAppURL = ?SC([CDAPUrlNS, "/apps/", HydratorWDepsAppname]), HydratorWDepsStreamname = <<"t1">>, %horrible name but not made by me - %Set up this test suites configuration - [{broker_url, BrokerUrl}, - {cdap_url, CDAPUrl}, + %Set up this test suites configuration + [{broker_url, BrokerUrl}, + {cdap_url, CDAPUrl}, {cdap_ns_url, CDAPUrlNS}, {jar_url, ?SC([Nexus, "/jar_files/HelloWorld-3.4.3.jar"])}, {consul_url, ConsulURL}, @@ -314,7 +305,7 @@ server_health_test(C) -> app_deploy(C) -> %C == Config %Deploy the test application - + %Deploy the rel key {200, _} = setup_rels(C, setup), @@ -324,7 +315,7 @@ app_deploy(C) -> %C == Config ExpectedBoundConfg = maps:from_list([ {<<"services_calls">> , [<<"666.666.666.666:13">>]}, - {<<"streams_produces">>, [<<"666.666.666.666:13">>]}, + {<<"streams_produces">>, [<<"666.666.666.666:13">>]}, {<<"donotresolveme">> , <<"donotabsolveme">>} ]), @@ -346,13 +337,13 @@ app_deploy(C) -> %C == Config %assert the current appliccation list does not contain our test app {200,RB0} = httpabs:get(?XER, ?SC([?PLG(broker_url, C), "/application"])), true = lists:all(fun(X) -> X /= ?PLG(appname, C) end, jiffy:decode(RB0)), - + %deploy the app Body = valid_deploy_body(C), {200, RB} = httpabs:put(?XER, ?PLG(broker_app_url, C), "application/json", jiffy:encode(Body)), - + %The CDAP APIs return the config as a JSON dumped to a string, so we need to get that back into a real JSON to have key-order-independent equality testing - Fix = fun(X) -> + Fix = fun(X) -> RBMap = jiffy:decode(X, [return_maps]), maps:update(<<"bound_config">>, jiffy:decode(maps:get(<<"bound_config">>, RBMap), [return_maps]), RBMap) end, @@ -370,10 +361,10 @@ app_deploy(C) -> %C == Config %make sure it is in CDAP {200, _} = httpabs:get(?XER, ?PLG(app_url, C)), - + %check metrics {200, _} = httpabs:get(?XER, ?SC([?PLG(broker_app_url, C), "/metrics"])), - + %check healthcheck {200, _} = httpabs:get(?XER,?SC([?PLG(broker_app_url, C), "/healthcheck"])), @@ -383,7 +374,7 @@ app_deploy(C) -> %C == Config %check that the UNbound config is correct true = {200, util:ejson_to_map(?PLG(init_config, C))} == get_config_consul(C), - + %check that the preferences in Consul is correct InitPrefMap = util:ejson_to_map(?PLG(init_preferences, C)), true = {200, InitPrefMap} == get_preferences_consul(C), @@ -402,7 +393,7 @@ app_deploy(C) -> %C == Config true = ExpectedBoundConfg == CDAPConfig, %try to put the same app again and assert you get a 400 - {400,"State: Bad Request. Return Body: Put recieved on /application/:appname but appname is already registered. Call /application/:appname/reconfigure if trying to reconfigure or delete first"} = + {400,"State: Bad Request. Return Body: Put recieved on /application/:appname but appname is already registered. Call /application/:appname/reconfigure if trying to reconfigure or delete first"} = httpabs:put(?XER, ?PLG(broker_app_url, C), "application/json", jiffy:encode(Body)). hydrator_deploy(C) -> @@ -426,7 +417,7 @@ hydrator_deploy(C) -> %assert the current appliccation list does not contain our test app {200,RB0} = httpabs:get(?XER, ?SC([?PLG(broker_url, C), "/application"])), true = lists:all(fun(X) -> X /= ?PLG(hydrator_appname, C) end, jiffy:decode(RB0)), - + %try the deploy {200, RB1} = httpabs:put(?XER, ?PLG(broker_hydrator_app_url, C), "application/json", jiffy:encode(Body)), true = jiffy:decode(RB1, [return_maps]) == Expected, @@ -437,17 +428,17 @@ hydrator_deploy(C) -> %make sure it is in CDAP {200, _} = httpabs:get(?XER, ?PLG(hydrator_app_url, C)), - + %assert the current application list now includes our new app {200, RB3} = httpabs:get(?XER, ?SC([?PLG(broker_url, C), "/application"])), true = lists:any(fun(X) -> X == ?PLG(hydrator_appname, C) end, jiffy:decode(RB3)), - + %check healthcheck {200, _} = httpabs:get(?XER,?SC([?PLG(broker_hydrator_app_url, C), "/healthcheck"])), - + %check metrics {200, _} = httpabs:get(?XER,?SC([?PLG(broker_hydrator_app_url, C), "/metrics"])), - + %make sure that the service is registered. TODO! Could get more fancy by manually checking a healthcheck {200, RBHC} = httpabs:get(?XER,?PLG(consul_hydrator_app_url, C)), true = jiffy:decode(RBHC) /= [] @@ -486,7 +477,7 @@ hydrator_wdeps_deploy(C) -> %try the deploy {200, RB1} = httpabs:put(?XER, ?PLG(broker_hydrator_wdeps_app_url, C), "application/json", jiffy:encode(Body)), true = jiffy:decode(RB1, [return_maps]) == Expected, - + %make sure properties are loaded, test artifact {200, _} = httpabs:get(?XER,?SC([?PLG(cdap_ns_url, C), "/artifacts/", ?PLG(hydrator_wdeps_artname, C), "/versions/", ?PLG(hydrator_wdeps_artver, C), "/properties"])), @@ -496,22 +487,22 @@ hydrator_wdeps_deploy(C) -> %make sure it is in CDAP {200, _} = httpabs:get(?XER,?PLG(hydrator_wdeps_app_url, C)), - + %assert the current application list now includes our new app {200, RB3} = httpabs:get(?XER,?SC([?PLG(broker_url, C), "/application"])), true = lists:any(fun(X) -> X == ?PLG(hydrator_wdeps_appname, C) end, jiffy:decode(RB3)), - + %check healthcheck {200, _} = httpabs:get(?XER,?SC([?PLG(broker_hydrator_wdeps_app_url, C), "/healthcheck"])), - + %check metrics {200, _} = httpabs:get(?XER,?SC([?PLG(broker_hydrator_wdeps_app_url, C), "/metrics"])), - + %make sure that the service is registered. TODO! Could get more fancy by manually checking a healthcheck {200, RBHC} = httpabs:get(?XER,?PLG(consul_hydrator_wdeps_app_url, C)), true = jiffy:decode(RBHC) /= [] . - + hydrator_test(C) -> %test te app by injecting some data into the stream and getting it out %Sleeping since HTTP services may still be booting up: see https://issues.cask.co/browse/CDAP-812 @@ -554,7 +545,7 @@ app_reconfigure(C) -> true = {200, ReconfigMap} == get_config_consul(C), %test new config right in cdap true = {200, ReconfigMap} == get_config_cdap(C), - + %Test preferences reconfiguration %check that the preferences in Consul is correct InitMap = util:ejson_to_map(?PLG(init_preferences, C)), @@ -594,8 +585,8 @@ app_reconfigure(C) -> true = {200, ExpectedNewPreferencesBoth} == get_preferences_cdap(C), true = {200, ExpectedNewConfigBoth} == get_config_consul(C), true = {200, ExpectedNewConfigBoth} == get_config_cdap(C), - - %try to give it a smart where there are no overlaps + + %try to give it a smart where there are no overlaps SmartReconfigNone = {[{<<"EMPTY">>, <<"LIKE YOUR SOUL">>}]}, {400, _} = httpabs:put(?XER, ?SC([?PLG(broker_app_url, C), "/reconfigure"]), "application/json", jiffy:encode({[{<<"reconfiguration_type">>, <<"program-flowlet-smart">>},{<<"config">>, SmartReconfigNone}]})), true = {200, ExpectedNewPreferencesBoth} == get_preferences_consul(C), @@ -609,18 +600,18 @@ app_botch_flows(C) -> {200, _} = httpabs:get(?XER,?SC([?PLG(broker_app_url, C), "/healthcheck"])), %purposely shut down a flow "manually" to test that undeploy works with a "partially deployed" app - {200, []} = cdap_interface:exec_programs(?XER, ?PLG(appname, C), ?PLG(namespace, C), ?PLG(cdap_url, C), + {200, []} = cdap_interface:exec_programs(?XER, ?PLG(appname, C), ?PLG(namespace, C), ?PLG(cdap_url, C), [#program{type = <<"flows">>, id = <<"WhoFlow">>}, #program{type = <<"services">>, id = <<"Greeting">>}], "stop"), %make sure healthcheck now fails {400, _} = httpabs:get(?XER,?SC([?PLG(broker_app_url, C), "/healthcheck"])) . app_botch_delete(C) -> - %purposely shut down flows and then delete the app from the CDAP api to test undeploy works with a [gone] app - {200, []} = cdap_interface:exec_programs(?XER, ?PLG(appname, C), ?PLG(namespace, C), ?PLG(cdap_url, C), + %purposely shut down flows and then delete the app from the CDAP api to test undeploy works with a [gone] app + {200, []} = cdap_interface:exec_programs(?XER, ?PLG(appname, C), ?PLG(namespace, C), ?PLG(cdap_url, C), [#program{type = <<"flows">>, id = <<"WhoFlow">>}, #program{type = <<"services">>, id = <<"Greeting">>}], "stop"), {200, []} = cdap_interface:delete_app(?XER, ?PLG(appname, C), ?PLG(namespace, C), ?PLG(cdap_url, C)), - + %make sure healthcheck now fails {400, _} = httpabs:get(?XER,?SC([?PLG(broker_app_url, C), "/healthcheck"])) . @@ -636,7 +627,7 @@ app_teardown(C) -> %teardown the test application {200, []} = httpabs:delete(?XER, ?PLG(broker_app_url, C)), - + %make sure the broker deleted the config from Consul {404, _} = get_config_consul(C), @@ -645,17 +636,17 @@ app_teardown(C) -> %make sure the broker app url no longer exists {404, _ } = httpabs:get(?XER,?PLG(broker_app_url, C)), - + %teardown the testing rels {404, _} = setup_rels(C, teardown), - + %teardown the fake service and make sure it is gone {200, Srv} = setup_fake_testing_service(C, teardown), true = Srv == "[]", - + %cdap app gone {404,"State: Not Found. Return Body: 'application:testns.hwtest.-SNAPSHOT' was not found."} = httpabs:get(?XER,?PLG(app_url, C)), - + %make sure that the service is not registered. TODO! Could get more fancy by manually checking a healthcheck {200, RBHC} = httpabs:get(?XER,?PLG(consul_app_url, C)), true = jiffy:decode(RBHC) == []. @@ -727,9 +718,9 @@ hydrator_wdeps_teardown(C) -> test_failures(C) -> %test things that should fail %delete a non-existent app - {404, "State: Not Found. Return Body: Tried to delete an application that was not registered"} = + {404, "State: Not Found. Return Body: Tried to delete an application that was not registered"} = httpabs:delete(?XER, ?SC([?PLG(broker_app_url, C), "MYFRIENDOFMISERY"])), - + %malformed Broker put URL = ?SC([?PLG(broker_app_url, C), "FAILURETEST"]), Body = {[ @@ -747,7 +738,7 @@ test_failures(C) -> {<<"artifact_version">>, ?PLG(art_ver, C)}, {<<"app_config">>, ?PLG(init_config, C)}, {<<"app_preferences">>, ?PLG(init_preferences, C)}, - {<<"services">>, [{[{<<"service_name">>, <<"Greeting">>}, + {<<"services">>, [{[{<<"service_name">>, <<"Greeting">>}, {<<"service_endpoint">>, <<"greet">>}, {<<"endpoint_method">>, <<"GET">>}]}]}, {<<"programs">>, [ @@ -762,7 +753,7 @@ test_failures(C) -> {404,_} = httpabs:put(?XER, URL, "application/json", jiffy:encode(Body2)), %make sure the rollback happened {200, "[]"} = httpabs:get(?XER,?SC([?PLG(broker_url, C), "/application"])), - + %try to deploy with a bad URL where bad means nonexistent (504) Body3 = {[ {<<"cdap_application_type">>, <<"program-flowlet">>}, @@ -796,20 +787,6 @@ test_failures(C) -> {400,"State: Bad Request. Return Body: ERROR: The following URL is malformed: THIS IS NOT EVEN A URL WHAT ARE YOU DOING TO ME"} = httpabs:put(?XER, URL, "application/json", jiffy:encode(Body4)) . -invalid_reconfigure(C) -> - %test reconfiguring an app that does not exist despite put body being correct - {404,"State: Not Found. Return Body: Reconfigure recieved but the app is not registered"} = httpabs:put(?XER, ?SC([?PLG(broker_app_url, C), "THE_VOID", "/reconfigure"]), "application/json", jiffy:encode({[{<<"reconfiguration_type">>, <<"program-flowlet-app-config>">>}, {<<"config">>, {[{<<"foo">>, <<"bar">>}]}}]})), - - %test reconfiguring with an invalid PUT body (missing "reconfiguration_type") - {400,"State: Bad Request. Return Body: Invalid PUT Reconfigure Body: key 'reconfiguration_type' is missing"} = httpabs:put(?XER, ?SC([?PLG(broker_app_url,C), "/reconfigure"]), "application/json", jiffy:encode({[{<<"config">>, <<"bar">>}]})), - - %test reconfiguring with an invalid PUT body (missing app_config) - {400,"State: Bad Request. Return Body: Invalid PUT Reconfigure Body: key 'config' is missing"} = httpabs:put(?XER, ?SC([?PLG(broker_app_url,C), "/reconfigure"]), "application/json", jiffy:encode({[{<<"reconfiguration_type">>, <<"program-flowlet-app-config">>}, {<<"foo">>, <<"bar">>}]})), - - %test reconfiguring an invalid (unimplemented) type - {501, "State: Not Implemented. Return Body: This type (EMPTINESS) of reconfiguration is not implemented"} = httpabs:put(?XER, ?SC([?PLG(broker_app_url,C), "/reconfigure"]), "application/json", jiffy:encode({[{<<"config">>, <<"bar">>}, {<<"reconfiguration_type">>, <<"EMPTINESS">>}]})) - . - delete_all(C) -> %test invalid key Body1 = jiffy:encode({[{<<"ids">>, [<<"hwtest">>]}]}), @@ -823,4 +800,4 @@ delete_all(C) -> %teardown the fake service and make sure it is gone {200, Srv} = setup_fake_testing_service(C, teardown), true = Srv == "[]". - + |