1
1
local t = require (' luatest' )
2
+ local fun = require (' fun' )
2
3
local json = require (' json' )
4
+ local yaml = require (' yaml' )
3
5
local treegen = require (' test.treegen' )
4
6
local justrun = require (' test.justrun' )
5
7
local source_file = require (' internal.config.source.file' ).new ()
8
+ local server = require (' test.luatest_helpers.server' )
6
9
7
10
local g = t .group ()
8
11
@@ -14,6 +17,13 @@ g.after_all(function()
14
17
treegen .clean (g )
15
18
end )
16
19
20
+ g .after_each (function (g )
21
+ if g .server ~= nil then
22
+ g .server :stop ()
23
+ g .server = nil
24
+ end
25
+ end )
26
+
17
27
g .test_source_file = function ()
18
28
local config = {_config_file = ' doc/examples/config/single.yaml' }
19
29
source_file :sync (config , {})
@@ -49,15 +59,14 @@ g.test_source_env = function()
49
59
local dir = treegen .prepare_directory (g , {}, {})
50
60
local script = [[
51
61
local json = require('json')
52
- local source_env = require('internal.config.source.env').new()
62
+ local source_env = require('internal.config.source.env').new({
63
+ env_var_suffix = arg[1],
64
+ })
53
65
source_env:sync({}, {})
54
66
print(json.encode(source_env:get()))
55
67
]]
56
68
treegen .write_script (dir , ' main.lua' , script )
57
69
58
- local env = {TT_LOG_LEVEL = ' info' , TT_MEMTX_MEMORY = 1000000 }
59
- local opts = {nojson = true , stderr = false }
60
- local res = justrun .tarantool (dir , env , {' main.lua' }, opts )
61
70
local exp = {
62
71
config = {
63
72
version = ' dev' ,
@@ -69,6 +78,104 @@ g.test_source_env = function()
69
78
memory = 1000000
70
79
},
71
80
}
72
- t .assert_equals (res .exit_code , 0 )
73
- t .assert_equals (json .decode (res .stdout ), exp )
81
+
82
+ local cases = {
83
+ {
84
+ name = ' env' ,
85
+ env_var_suffix = nil ,
86
+ env = {
87
+ TT_LOG_LEVEL = ' info' ,
88
+ TT_MEMTX_MEMORY = 1000000 ,
89
+ },
90
+ },
91
+ {
92
+ name = ' env default' ,
93
+ env_var_suffix = ' default' ,
94
+ env = {
95
+ TT_LOG_LEVEL_DEFAULT = ' info' ,
96
+ TT_MEMTX_MEMORY_DEFAULT = 1000000 ,
97
+ },
98
+ },
99
+ }
100
+ local opts = {nojson = true , stderr = false }
101
+ for _ , case in ipairs (cases ) do
102
+ local comment = (' case: %s' ):format (case .name )
103
+ local args = {' main.lua' , case .env_var_suffix }
104
+ local res = justrun .tarantool (dir , case .env , args , opts )
105
+ t .assert_equals (res .exit_code , 0 , comment )
106
+ t .assert_equals (json .decode (res .stdout ), exp , comment )
107
+ end
108
+ end
109
+
110
+ -- Verify priority of configuration sources.
111
+ --
112
+ -- 1. env (TT_*)
113
+ -- 2. file
114
+ -- 3. env default (TT_*_DEFAULT)
115
+ --
116
+ -- Several string options from the instance config are chosen for
117
+ -- testing purposes, their meaning is irrelevant for the test.
118
+ --
119
+ -- The table below shows where the given option is set (which
120
+ -- source defines it) and what we expect as a result.
121
+ --
122
+ -- | option | env | file | env default | result |
123
+ -- | ------------------- | --- | ---- | ----------- | ----------- |
124
+ -- | process.title | | + | + | file |
125
+ -- | log.file | + | + | | env |
126
+ -- | log.pipe | + | | + | env |
127
+ -- | log.syslog.identity | | | + | env default |
128
+ g .test_sources_priority = function (g )
129
+ local dir = treegen .prepare_directory (g , {}, {})
130
+ local config = {
131
+ credentials = {
132
+ users = {
133
+ guest = {
134
+ roles = {' super' },
135
+ },
136
+ },
137
+ },
138
+ iproto = {
139
+ listen = ' unix/:./{{ instance_name }}.iproto' ,
140
+ },
141
+ process = {
142
+ title = ' from file' ,
143
+ },
144
+ log = {
145
+ file = ' from file' ,
146
+ },
147
+ groups = {
148
+ [' group-001' ] = {
149
+ replicasets = {
150
+ [' replicaset-001' ] = {
151
+ instances = {
152
+ [' instance-001' ] = {},
153
+ },
154
+ },
155
+ },
156
+ },
157
+ },
158
+ }
159
+ local config_file = treegen .write_script (dir , ' config.yaml' ,
160
+ yaml .encode (config ))
161
+ local opts = {
162
+ config_file = config_file ,
163
+ chdir = dir ,
164
+ env = {
165
+ TT_PROCESS_TITLE_DEFAULT = ' from env default' ,
166
+ TT_LOG_FILE = ' from env' ,
167
+ TT_LOG_PIPE = ' from env' ,
168
+ TT_LOG_PIPE_DEFAULT = ' from env default' ,
169
+ TT_LOG_SYSLOG_IDENTITY_DEFAULT = ' from env default' ,
170
+ },
171
+ }
172
+ g .server = server :new (fun .chain (opts , {alias = ' instance-001' }):tomap ())
173
+ g .server :start ()
174
+ g .server :exec (function ()
175
+ local config = require (' config' )
176
+ t .assert_equals (config :get (' process.title' ), ' from file' )
177
+ t .assert_equals (config :get (' log.file' ), ' from env' )
178
+ t .assert_equals (config :get (' log.pipe' ), ' from env' )
179
+ t .assert_equals (config :get (' log.syslog.identity' ), ' from env default' )
180
+ end )
74
181
end
0 commit comments