You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by ja...@apache.org on 2022/10/12 23:11:41 UTC

[couchdb] 01/01: Quiet prometheus end to end tests

This is an automated email from the ASF dual-hosted git repository.

jaydoane pushed a commit to branch quiet-prometheus-e2e-tests
in repository https://gitbox.apache.org/repos/asf/couchdb.git

commit 44ba966ecd89c17c9d67b1a313226098b1aadc16
Author: Jay Doane <ja...@apache.org>
AuthorDate: Wed Oct 12 15:19:01 2022 -0700

    Quiet prometheus end to end tests
    
    Currently, applications are restarted during individual tests,
    resulting in noisy output:
    
    module 'couch_prometheus_e2e_tests'
      Prometheus E2E Tests
        couch_prometheus_e2e_tests:66: node_call_chttpd...ok
    Application config was left running!
    Application asn1 was left running!
    Application b64url was left running!
    Application public_key was left running!
    Application ssl was left running!
    Application couch_dist was left running!
    Application couch_epi was left running!
    Application khash was left running!
    Application couch_event was left running!
    Application folsom was left running!
    Application couch_stats was left running!
    Application hyper was left running!
    Application ibrowse was left running!
    Application inets was left running!
    Application hqueue was left running!
    Application compiler was left running!
    Application syntax_tools was left running!
    Application xmerl was left running!
    Application mochiweb was left running!
    Application sasl was left running!
    Application ets_lru was left running!
    Application rexi was left running!
    Application fabric was left running!
        couch_prometheus_e2e_tests:98: node_call_prometheus_http...ok
    Application config was left running!
    Application asn1 was left running!
    Application b64url was left running!
    Application public_key was left running!
    Application ssl was left running!
    Application couch_dist was left running!
    Application couch_epi was left running!
    Application khash was left running!
    Application couch_event was left running!
    Application folsom was left running!
    Application couch_stats was left running!
    Application hyper was left running!
    Application ibrowse was left running!
    Application inets was left running!
    Application hqueue was left running!
    Application compiler was left running!
    Application syntax_tools was left running!
    Application xmerl was left running!
    Application mochiweb was left running!
    Application sasl was left running!
    Application ets_lru was left running!
    Application rexi was left running!
    Application fabric was left running!
        couch_prometheus_e2e_tests:109: deny_prometheus_http...ok
        couch_prometheus_e2e_tests:82: node_see_updated_metrics...ok
        [done in 6.595 s]
      [done in 8.739 s]
    
    This factors the current test generator into two, one where a
    separate prometheus HTTP server is started on a dedicated port, and
    one where it is not, which prevents the noise.
    
    Additionally, tests have been changed to use the TDEF_FE macro,
    cleaned up, and renamed to make them easier to understand.
---
 .../test/eunit/couch_prometheus_e2e_tests.erl      | 138 ++++++++++++---------
 1 file changed, 78 insertions(+), 60 deletions(-)

diff --git a/src/couch_prometheus/test/eunit/couch_prometheus_e2e_tests.erl b/src/couch_prometheus/test/eunit/couch_prometheus_e2e_tests.erl
index f986fc6b0..a1f987aaa 100644
--- a/src/couch_prometheus/test/eunit/couch_prometheus_e2e_tests.erl
+++ b/src/couch_prometheus/test/eunit/couch_prometheus_e2e_tests.erl
@@ -21,52 +21,95 @@
 -define(PROM_PORT, "17986").
 -define(CONTENT_JSON, {"Content-Type", "application/json"}).
 
-start() ->
-    test_util:start_couch([chttpd, couch_prometheus]).
-
-setup() ->
-    Hashed = couch_passwords:hash_admin_password(?PASS),
-    ok = config:set("admins", ?USER, ?b2l(Hashed), _Persist = false),
-    ok = config:set_integer("stats", "interval", 2),
-    ok = config:set_integer("couch_prometheus", "interval", 1),
-    Port = mochiweb_socket_server:get(chttpd, port),
-    construct_url(Port).
-
-teardown(_) ->
-    ok.
+e2e_test_() ->
+    {
+        "Prometheus started with dedicated port",
+        {
+            setup,
+            fun() ->
+                setup_prometheus(true)
+            end,
+            fun(Ctx) ->
+                test_util:stop_couch(Ctx)
+            end,
+            {
+                foreach,
+                fun() ->
+                    mochiweb_socket_server:get(chttpd, port)
+                end,
+                [
+                    ?TDEF_FE(chttpd_port),
+                    ?TDEF_FE(prometheus_port),
+                    ?TDEF_FE(metrics_updated)
+                ]
+            }
+        }
+    }.
 
-couch_prometheus_e2e_test_() ->
+reject_test_() ->
     {
-        "Prometheus E2E Tests",
+        "Prometheus started without dedicated port",
         {
             setup,
-            fun start/0,
-            fun test_util:stop_couch/1,
+            fun() ->
+                setup_prometheus(false)
+            end,
+            fun(Ctx) ->
+                test_util:stop_couch(Ctx)
+            end,
             {
                 foreach,
-                fun setup/0,
-                fun teardown/1,
+                fun() ->
+                    ?PROM_PORT
+                end,
                 [
-                    fun node_call_chttpd/1,
-                    fun node_call_prometheus_http/1,
-                    fun deny_prometheus_http/1,
-                    fun node_see_updated_metrics/1
+                    ?TDEF_FE(reject_prometheus_port)
                 ]
             }
         }
     }.
 
-% normal chttpd path via cluster port
-node_call_chttpd(Url) ->
+setup_prometheus(WithAdditionalPort) ->
+    Ctx = test_util:start_couch([chttpd]),
+    Persist = false,
+    Hashed = couch_passwords:hash_admin_password(?PASS),
+    ok = config:set("admins", ?USER, ?b2l(Hashed), Persist),
+    ok = config:set_integer("stats", "interval", 2, Persist),
+    ok = config:set_integer("prometheus", "interval", 1, Persist),
+    ok = config:set_boolean(
+        "prometheus",
+        "additional_port",
+        WithAdditionalPort,
+        Persist
+    ),
+    % it's started by default, so restart to pick up config
+    ok = application:stop(couch_prometheus),
+    ok = application:start(couch_prometheus),
+    Ctx.
+    
+chttpd_port(ChttpdPort) ->
     {ok, RC1, _, _} = test_request:get(
-        Url,
+        node_local_url(ChttpdPort),
         [?CONTENT_JSON, ?AUTH],
         []
     ),
-    ?_assertEqual(200, RC1).
+    ?assertEqual(200, RC1).
+
+prometheus_port(_) ->
+    Url = node_local_url(?PROM_PORT),
+    {ok, RC1, _, _} = test_request:get(
+        Url,
+        [?CONTENT_JSON, ?AUTH]
+    ),
+    ?assertEqual(200, RC1),
+    % since this port doesn't require auth, this should work
+    {ok, RC2, _, _} = test_request:get(
+        Url,
+        [?CONTENT_JSON]
+    ),
+    ?assertEqual(200, RC2).
 
-% normal chttpd path via cluster port
-node_see_updated_metrics(Url) ->
+metrics_updated(ChttpdPort) ->
     TmpDb = ?tempdb(),
     Addr = config:get("chttpd", "bind_address", "127.0.0.1"),
     Port = mochiweb_socket_server:get(chttpd, port),
@@ -74,49 +117,24 @@ node_see_updated_metrics(Url) ->
     create_db(DbUrl),
     [create_doc(DbUrl, "testdoc" ++ integer_to_binary(I)) || I <- lists:seq(1, 100)],
     delete_db(DbUrl),
+    Url = node_local_url(ChttpdPort),
     InitMetrics = wait_for_metrics(Url, "couchdb_httpd_requests_total 0", 5000),
     UpdatedMetrics = wait_for_metrics(Url, "couchdb_httpd_requests_total", 10000),
     % since the puts happen so fast, we can't have an exact
     % total requests given the scraping interval. so we just want to acknowledge
     % a change as occurred
-    ?_assertNotEqual(InitMetrics, UpdatedMetrics).
-
-% normal chttpd path via cluster port
-node_call_prometheus_http(_) ->
-    maybe_start_http_server("true"),
-    Url = construct_url(?PROM_PORT),
-    {ok, RC1, _, _} = test_request:get(
-        Url,
-        [?CONTENT_JSON, ?AUTH]
-    ),
-    % since this port doesn't require auth, this should work
-    {ok, RC2, _, _} = test_request:get(
-        Url,
-        [?CONTENT_JSON]
-    ),
-    delete_db(Url),
-    ?_assertEqual({200, 200}, {RC1, RC2}).
+    ?assertNotEqual(InitMetrics, UpdatedMetrics).
 
 % we don't start the http server
-deny_prometheus_http(_) ->
-    maybe_start_http_server("false"),
-    Url = construct_url(?PROM_PORT),
+reject_prometheus_port(PrometheusPort) ->
     Response = test_request:get(
-        Url,
+        node_local_url(PrometheusPort),
         [?CONTENT_JSON, ?AUTH],
         []
     ),
-    ?_assertEqual({error, {conn_failed, {error, econnrefused}}}, Response).
-
-maybe_start_http_server(Additional) ->
-    test_util:stop_applications([couch_prometheus, chttpd]),
-    Hashed = couch_passwords:hash_admin_password(?PASS),
-    ok = config:set("admins", ?USER, ?b2l(Hashed), _Persist = false),
-    ok = config:set("prometheus", "additional_port", Additional),
-    ok = config:set("prometheus", "port", ?PROM_PORT),
-    test_util:start_applications([couch_prometheus, chttpd]).
+    ?assertEqual({error, {conn_failed, {error, econnrefused}}}, Response).
 
-construct_url(Port) ->
+node_local_url(Port) ->
     Addr = config:get("chttpd", "bind_address", "127.0.0.1"),
     lists:concat(["http://", Addr, ":", Port, "/_node/_local/_prometheus"]).