|
44 | 44 | -include("couch_db_int.hrl"). |
45 | 45 | -include("couch_bt_engine.hrl"). |
46 | 46 |
|
47 | | --record(test_context, {mocked = [], started = [], module}). |
| 47 | +-record(test_context, {mocked = [], started = [], module, dir}). |
48 | 48 |
|
49 | 49 | -define(DEFAULT_APPS, [inets, ibrowse, ssl, config, couch_epi, couch_event, couch]). |
50 | 50 |
|
@@ -81,21 +81,58 @@ start_couch() -> |
81 | 81 | start_couch(ExtraApps) -> |
82 | 82 | start_couch(?CONFIG_CHAIN, ExtraApps). |
83 | 83 |
|
| 84 | +% This function starts CouchDB with optional extra apps in a dedicated |
| 85 | +% directory under tmp/couchdb-tests/<uuid>/ — That is, all instances |
| 86 | +% run isolated so that if more than one runs at a time, it does not |
| 87 | +% interfere with any of the others. |
| 88 | +% The sub-directories here are etc/ log/ and data/ for configuration |
| 89 | +% logs and databases and view indexes respectively. |
| 90 | +% The function copies the initial bespoke test-ini files to the new |
| 91 | +% random directory and configures CouchDB to use those. |
| 92 | +% The two pieces of state created here are the random dir in the file system and |
| 93 | +% the process env var {config, ini_files} that is read by couch_config on |
| 94 | +% startup. These need to be reset at the right points in time. |
| 95 | +% the random dir is deleted and the env var reset when CouchDB is stopped. |
| 96 | +% Note: there is currently a case where stop_couch/0 could be called and |
| 97 | +% the file system cleanup can’t be run because we don’t get passed the test |
| 98 | +% context with the random directory value. At the moment stop_couch/0 is not |
| 99 | +% used anywhere, tho. Maybe we should remove it? |
84 | 100 | start_couch(IniFiles, ExtraApps) -> |
| 101 | + RandomDir = filename:join([builddir(), "tmp", "couchdb-tests", couch_uuids:random()]), |
| 102 | + RandomEtcDir = filename:join([RandomDir, "etc"]), |
| 103 | + RandomDataDir = ?b2l(filename:join([RandomDir, "data"])), |
| 104 | + RandomLogFile = ?b2l(filename:join([RandomDir, "log", "couch.log"])), |
| 105 | + |
| 106 | + ok = filelib:ensure_path(RandomDir), |
| 107 | + ok = filelib:ensure_path(RandomEtcDir), |
| 108 | + ok = filelib:ensure_path(RandomDataDir), |
| 109 | + ok = filelib:ensure_dir(RandomLogFile), |
| 110 | + |
| 111 | + RandomIniFiles = lists:map(fun(SourceFile) -> |
| 112 | + TargetFileName = lists:last(filename:split(SourceFile)), |
| 113 | + TargetFile = filename:join([RandomEtcDir, TargetFileName]), |
| 114 | + {ok, _} = file:copy(SourceFile, TargetFile), |
| 115 | + ?b2l(TargetFile) |
| 116 | + end, IniFiles), |
85 | 117 | load_applications_with_stats(), |
86 | | - ok = application:set_env(config, ini_files, IniFiles), |
| 118 | + ok = application:set_env(config, ini_files, RandomIniFiles), |
87 | 119 | Apps = start_applications(?DEFAULT_APPS ++ ExtraApps), |
| 120 | + |
| 121 | + ok = config:set("couchdb", "database_dir", RandomDataDir, false), |
| 122 | + ok = config:set("couchdb", "view_index_dir", RandomDataDir, false), |
| 123 | + ok = config:set("log", "file", RandomLogFile, false), |
88 | 124 | ok = config:delete("compactions", "_default", false), |
89 | | - #test_context{started = Apps}. |
| 125 | + #test_context{started = Apps, dir = RandomDir}. |
90 | 126 |
|
91 | 127 | stop_couch() -> |
92 | 128 | ok = stop_applications(?DEFAULT_APPS). |
93 | 129 |
|
94 | | -stop_couch(#test_context{started = Apps}) -> |
| 130 | +stop_couch(#test_context{started = Apps, dir = RandomDir }) -> |
| 131 | + file:del_dir_r(RandomDir), |
95 | 132 | stop_applications(Apps); |
| 133 | + |
96 | 134 | stop_couch(_) -> |
97 | 135 | stop_couch(). |
98 | | - |
99 | 136 | with_couch_server_restart(Fun) -> |
100 | 137 | Servers = couch_server:names(), |
101 | 138 | test_util:with_processes_restart(Servers, Fun). |
@@ -125,6 +162,7 @@ start_applications([App | Apps], Acc) -> |
125 | 162 |
|
126 | 163 | stop_applications(Apps) -> |
127 | 164 | [application:stop(App) || App <- lists:reverse(Apps)], |
| 165 | + ok = application:unset_env(config, ini_files), |
128 | 166 | ok. |
129 | 167 |
|
130 | 168 | start_config(Chain) -> |
|
0 commit comments