couchdb-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dav...@apache.org
Subject [couchdb] 01/02: Add support for a config tag
Date Thu, 07 Dec 2017 00:29:45 GMT
This is an automated email from the ASF dual-hosted git repository.

davisp pushed a commit to branch elixir-suite-davisp
in repository https://gitbox.apache.org/repos/asf/couchdb.git

commit a9123ae4d15d3e1a9beb6bf79854eeeda23aa891
Author: Paul J. Davis <paul.joseph.davis@gmail.com>
AuthorDate: Wed Dec 6 18:28:16 2017 -0600

    Add support for a config tag
    
    This allows tests to use a config tag to mimic the JavaScript
    "run_on_modified_server" function. The value of a config tag should be
    set to a list of 3-tuples that contain the section/key/value to set. The
    previous value is restored automatically after the test is finished.
---
 elixir_suite/test/test_helper.exs | 43 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 43 insertions(+)

diff --git a/elixir_suite/test/test_helper.exs b/elixir_suite/test/test_helper.exs
index b8adb52..ecd88e5 100644
--- a/elixir_suite/test/test_helper.exs
+++ b/elixir_suite/test/test_helper.exs
@@ -15,6 +15,12 @@ defmodule CouchTestCase do
       use ExUnit.Case
 
       setup context do
+        {:ok, db_context} = set_db_context(context)
+        {:ok, cfg_context} = set_config_context(context)
+        {:ok, db_context ++ cfg_context}
+      end
+
+      def set_db_context(context) do
         db_name = if context[:with_db] != nil or context[:with_db_name] != nil do
           if context[:with_db] != nil and context[:with_db] != true do
             context[:with_db]
@@ -36,12 +42,49 @@ defmodule CouchTestCase do
         {:ok, db_name: db_name}
       end
 
+      def set_config_context(context) do
+        if is_list(context[:config]) do
+          Enum.each(context[:config], fn cfg ->
+            set_config(cfg)
+          end)
+        end
+        {:ok, []}
+      end
+
       def random_db_name do
         time = :erlang.monotonic_time()
         umi = :erlang.unique_integer([:monotonic])
         "random-test-db-#{time}-#{umi}"
       end
 
+      def set_config({section, key, value}) do
+        resp = Couch.get("/_membership")
+        existing = Enum.map(resp.body["all_nodes"], fn node ->
+          url = "/_node/#{node}/_config/#{section}/#{key}"
+          headers = ["X-Couch-Persist": "false"]
+          body = :jiffy.encode(value)
+          resp = Couch.put(url, headers: headers, body: body)
+          assert resp.status_code == 200
+          {node, resp.body}
+        end)
+        on_exit(fn ->
+          Enum.each(existing, fn {node, prev_value} ->
+            if prev_value != "" do
+              url = "/_node/#{node}/_config/#{section}/#{key}"
+              headers = ["X-Couch-Persist": "false"]
+              body = :jiffy.encode(prev_value)
+              resp = Couch.put(url, headers: headers, body: body)
+              assert resp.status_code == 200
+            else
+              url = "/_node/#{node}/_config/#{section}/#{key}"
+              headers = ["X-Couch-Persist": "false"]
+              resp = Couch.delete(url, headers: headers)
+              assert resp.status_code == 200
+            end
+          end)
+        end)
+      end
+
       def create_db(db_name) do
         resp = Couch.put("/#{db_name}")
         assert resp.status_code == 201

-- 
To stop receiving notification emails like this one, please contact
"commits@couchdb.apache.org" <commits@couchdb.apache.org>.

Mime
View raw message