|
| 1 | +% Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 2 | +% use this file except in compliance with the License. You may obtain a copy of |
| 3 | +% the License at |
| 4 | +% |
| 5 | +% http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | +% |
| 7 | +% Unless required by applicable law or agreed to in writing, software |
| 8 | +% distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 9 | +% WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 10 | +% License for the specific language governing permissions and limitations under |
| 11 | +% the License. |
| 12 | + |
| 13 | +-module(nouveau_index_upgrader_tests). |
| 14 | + |
| 15 | +-include_lib("couch/include/couch_eunit.hrl"). |
| 16 | +-include_lib("couch/include/couch_db.hrl"). |
| 17 | +-include_lib("nouveau/include/nouveau.hrl"). |
| 18 | + |
| 19 | +-define(PLUGIN, nouveau_index_upgrader). |
| 20 | + |
| 21 | +nouveau_index_upgrader_test_() -> |
| 22 | + { |
| 23 | + foreach, |
| 24 | + fun setup/0, |
| 25 | + fun teardown/1, |
| 26 | + [ |
| 27 | + ?TDEF_FE(t_upgrade_legacy_index, 10), |
| 28 | + ?TDEF_FE(t_dont_upgrade_latest_index, 10) |
| 29 | + ] |
| 30 | + }. |
| 31 | + |
| 32 | +setup() -> |
| 33 | + {module, _} = code:ensure_loaded(?PLUGIN), |
| 34 | + meck:new(?PLUGIN, [passthrough]), |
| 35 | + meck:new(couch_scanner_server, [passthrough]), |
| 36 | + meck:new(couch_scanner_util, [passthrough]), |
| 37 | + meck:new(nouveau_api, [passthrough]), |
| 38 | + meck:expect(nouveau_api, supported_lucene_versions, fun() -> |
| 39 | + {ok, [?LEGACY_LUCENE_VERSION, ?TARGET_LUCENE_VERSION]} |
| 40 | + end), |
| 41 | + Ctx = test_util:start_couch([fabric, couch_scanner]), |
| 42 | + DbName = ?tempdb(), |
| 43 | + ok = fabric:create_db(DbName, [{q, "2"}, {n, "1"}]), |
| 44 | + config:set(atom_to_list(?PLUGIN), "max_batch_items", "1", false), |
| 45 | + reset_stats(), |
| 46 | + {Ctx, DbName}. |
| 47 | + |
| 48 | +teardown({Ctx, DbName}) -> |
| 49 | + config_delete_section("couch_scanner"), |
| 50 | + config_delete_section("couch_scanner_plugins"), |
| 51 | + config_delete_section(atom_to_list(?PLUGIN)), |
| 52 | + couch_scanner:reset_checkpoints(), |
| 53 | + couch_scanner:resume(), |
| 54 | + fabric:delete_db(DbName), |
| 55 | + test_util:stop_couch(Ctx), |
| 56 | + meck:unload(). |
| 57 | + |
| 58 | +t_upgrade_legacy_index({_, DbName}) -> |
| 59 | + DDocId = <<"_design/foo">>, |
| 60 | + IndexName = <<"bar">>, |
| 61 | + ok = add_ddoc(DbName, DDocId, IndexName, ?LEGACY_LUCENE_VERSION), |
| 62 | + meck:reset(couch_scanner_server), |
| 63 | + meck:reset(?PLUGIN), |
| 64 | + meck:new(nouveau_fabric_search, [passthrough]), |
| 65 | + meck:expect(nouveau_fabric_search, go, fun(_DbName, _Args, _Index) -> {ok, []} end), |
| 66 | + config:set("couch_scanner_plugins", atom_to_list(?PLUGIN), "true", false), |
| 67 | + wait_exit(10000), |
| 68 | + ?assertEqual(1, num_calls(start, 2)), |
| 69 | + ?assertEqual(1, num_calls(complete, 1)), |
| 70 | + ?assertEqual(?TARGET_LUCENE_VERSION, get_lucene_version(DbName, DDocId, IndexName)), |
| 71 | + ok. |
| 72 | + |
| 73 | +t_dont_upgrade_latest_index({_, DbName}) -> |
| 74 | + DDocId = <<"_design/foo">>, |
| 75 | + IndexName = <<"bar">>, |
| 76 | + ok = add_ddoc(DbName, DDocId, IndexName, ?TARGET_LUCENE_VERSION), |
| 77 | + meck:reset(couch_scanner_server), |
| 78 | + meck:reset(?PLUGIN), |
| 79 | + config:set("couch_scanner_plugins", atom_to_list(?PLUGIN), "true", false), |
| 80 | + wait_exit(10000), |
| 81 | + ?assertEqual(1, num_calls(start, 2)), |
| 82 | + ?assertEqual(1, num_calls(complete, 1)), |
| 83 | + ?assertEqual(?TARGET_LUCENE_VERSION, get_lucene_version(DbName, DDocId, IndexName)), |
| 84 | + ok. |
| 85 | + |
| 86 | +reset_stats() -> |
| 87 | + Counters = [ |
| 88 | + [couchdb, query_server, process_error_exits], |
| 89 | + [couchdb, query_server, process_errors], |
| 90 | + [couchdb, query_server, process_exits] |
| 91 | + ], |
| 92 | + [reset_counter(C) || C <- Counters]. |
| 93 | + |
| 94 | +reset_counter(Counter) -> |
| 95 | + case couch_stats:sample(Counter) of |
| 96 | + 0 -> |
| 97 | + ok; |
| 98 | + N when is_integer(N), N > 0 -> |
| 99 | + couch_stats:decrement_counter(Counter, N) |
| 100 | + end. |
| 101 | + |
| 102 | +config_delete_section(Section) -> |
| 103 | + [config:delete(K, V, false) || {K, V} <- config:get(Section)]. |
| 104 | + |
| 105 | +add_ddoc(DbName, DDocId, IndexName, LuceneVersion) -> |
| 106 | + {ok, _} = fabric:update_doc(DbName, mkddoc(DDocId, IndexName, LuceneVersion), [?ADMIN_CTX]), |
| 107 | + ok. |
| 108 | + |
| 109 | +get_lucene_version(DbName, DDocId, IndexName) -> |
| 110 | + {ok, #doc{body = {Props}}} = fabric:open_doc(DbName, DDocId, [?ADMIN_CTX]), |
| 111 | + {Indexes} = couch_util:get_value(<<"nouveau">>, Props), |
| 112 | + {Index} = couch_util:get_value(IndexName, Indexes), |
| 113 | + couch_util:get_value(<<"lucene_version">>, Index). |
| 114 | + |
| 115 | +mkddoc(DocId, IndexName, LuceneVersion) -> |
| 116 | + Body = #{ |
| 117 | + <<"_id">> => DocId, |
| 118 | + <<"nouveau">> => #{ |
| 119 | + IndexName => #{ |
| 120 | + <<"lucene_version">> => LuceneVersion, |
| 121 | + <<"index">> => <<"function(doc){}">> |
| 122 | + } |
| 123 | + } |
| 124 | + }, |
| 125 | + jiffy:decode(jiffy:encode(Body)). |
| 126 | + |
| 127 | +num_calls(Fun, Args) -> |
| 128 | + meck:num_calls(?PLUGIN, Fun, Args). |
| 129 | + |
| 130 | +wait_exit(MSec) -> |
| 131 | + meck:wait(couch_scanner_server, handle_info, [{'EXIT', '_', '_'}, '_'], MSec). |
0 commit comments