summaryrefslogtreecommitdiffstats
path: root/libnetdata/dyn_conf/tests/test_dyncfg.rb
blob: 1b4b3a068a9b5b90ec5895e5fa8a7947a72cfd17 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#!/usr/bin/env ruby

require 'json'
require 'httparty'
require 'pastel'
require 'securerandom'

ARGV.length == 1 or raise "Usage: #{$0} <config file>"
config_file = ARGV[0]

File.exist?(config_file) or raise "File not found: #{config_file}"

$config = JSON.parse(File.read(config_file), symbolize_names: true)

$plugin_name = $config[:global][:test_plugin_name]
$pastel = Pastel.new

class TestRunner
    attr_reader :stats
    def initialize
        @stats = {
            :suites => 0,
            :tests  => 0,
            :assertions => 0
        }
        @test = nil
    end
    def add_assertion()
        @stats[:assertions] += 1
    end
    def FAIL(msg, exception = nil, loc = nil)
        puts $pastel.red.bold(" ✕ FAIL")
        STDERR.print "    "
        if loc
            STDERR.print $pastel.yellow("@#{loc.path}:#{loc.lineno}: ")
        else
            STDERR.print $pastel.yellow("@#{caller_locations(1, 1).first.path}:#{caller_locations(1, 1).first.lineno}: ")
        end
        STDERR.puts msg
        STDERR.puts exception.full_message(:highlight => true) if exception
        STDERR.puts $pastel.yellow("    Backtrace:")
        caller.each do |line|
            STDERR.puts "        #{line}"
        end
        exit 1
    end
    def PASS()
        STDERR.puts $pastel.green.bold(" ✓ PASS")
        @stats[:tests] += 1
        @test = nil
    end
    def TEST_SUITE(name)
        puts $pastel.bold("• TEST SUITE: \"#{name}\"")
        @stats[:suites] += 1
    end
    def assert_no_test_running()
        unless @test.nil?
            STDERR.puts $pastel.red("\nFATAL: Test \"#{@test}\" did not call PASS() or FAIL()!")
            exit 1
        end
    end
    def TEST(name, description = nil)
        assert_no_test_running()
        @test = name
        col = 0
        txt = "  ├─ T: #{name} "
        col += txt.length
        print $pastel.bold(txt)

        tab = 50
        rem = tab - (col % tab)
        rem.times do putc ' ' end
        col += rem

        if (description)
            txt = " - #{description} "
            col += txt.length
            print txt

            tab = 180
            rem = tab - (col % tab)
            rem.times do putc '.' end
        end
    end
    def FINALIZE()
        assert_no_test_running()
    end
end

$test_runner = TestRunner.new
def FAIL(msg, exception = nil, loc = nil)
    $test_runner.FAIL(msg, exception, loc)
end
def PASS()
    $test_runner.PASS()
end
def TEST_SUITE(name)
    $test_runner.TEST_SUITE(name)
end
def TEST(name, description = nil)
    $test_runner.TEST(name, description)
end

def assert_eq(got, expected, msg = nil)
    unless got == expected
        FAIL("Expected #{expected}, got #{got} #{msg ? "(#{msg})" : ""}", nil, caller_locations(1, 1).first)
    end
    $test_runner.add_assertion()
end
def assert_eq_http_code(got, expected, msg = nil)
    unless got.code == expected
        FAIL("Expected #{expected}, got #{got}. Server \"#{got.parsed_response}\" #{msg ? "(#{msg})" : ""}", nil, caller_locations(1, 1).first)
    end
    $test_runner.add_assertion()
end
def assert_eq_str(got, expected, msg = nil)
    unless got == expected
        FAIL("Strings do not match #{msg ? "(#{msg})" : ""}", nil, caller_locations(1, 1).first)
    end
    $test_runner.add_assertion()
end
def assert_not_eq_str(got, expected, msg = nil)
    unless got != expected
        FAIL("Strings shoud not match #{msg ? "(#{msg})" : ""}", nil, caller_locations(1, 1).first)
    end
    $test_runner.add_assertion()
end
def assert_nothing_raised()
    begin
        yield
    rescue Exception => e
        FAIL("Unexpected exception of type #{e.class} raised. Msg: \"#{e.message}\"", e, caller_locations(1, 1).first)
    end
    $test_runner.add_assertion()
end
def assert_has_key?(hash, key)
    unless hash.has_key?(key)
        FAIL("Expected key \"#{key}\" in hash", nil, caller_locations(1, 1).first)
    end
    $test_runner.add_assertion()
end
def assert_array_include?(array, value)
    unless array.include?(value)
        FAIL("Expected array to include \"#{value}\"", nil, caller_locations(1, 1).first)
    end
    $test_runner.add_assertion()
end
def assert_array_not_include?(array, value)
    if array.include?(value)
        FAIL("Expected array to not include \"#{value}\"", nil, caller_locations(1, 1).first)
    end
    $test_runner.add_assertion()
end
def assert_is_one_of(value, *values)
    unless values.include?(value)
        FAIL("Expected value to be one of #{values.join(", ")}", nil, caller_locations(1, 1).first)
    end
    $test_runner.add_assertion()
end
def assert_not_nil(value)
    if value.nil?
        FAIL("Expected value to not be nil", nil, caller_locations(1, 1).first)
    end
    $test_runner.add_assertion()
end
def assert_nil(value)
    unless value.nil?
        FAIL("Expected value to be nil", nil, caller_locations(1, 1).first)
    end
    $test_runner.add_assertion()
end


class DynCfgHttpClient
    def self.protocol(cfg)
        return cfg[:ssl] ? 'https://' : 'http://'
    end
    def self.url_base(host)
        return "#{protocol(host)}#{host[:host]}:#{host[:port]}"
    end
    def self.get_url_cfg_base(host, child = nil)
        url = url_base(host)
        url += "/host/#{child[:mguid]}" if child
        url += "/api/v2/config"
        return url
    end
    def self.get_url_cfg_plugin(host, plugin, child = nil)
        return get_url_cfg_base(host, child) + '/' + plugin
    end
    def self.get_url_cfg_module(host, plugin, mod, child = nil)
        return get_url_cfg_plugin(host, plugin, child) + '/' + mod
    end
    def self.get_url_cfg_job(host, plugin, mod, job_id, child = nil)
        return get_url_cfg_module(host, plugin, mod, child) + "/#{job_id}"
    end
    def self.get_plugin_list(host, child = nil)
        begin
            return HTTParty.get(get_url_cfg_base(host, child), verify: false, format: :plain)
        rescue => e
            FAIL(e.message, e)
        end
    end
    def self.get_plugin_config(host, plugin, child = nil)
        begin
            return HTTParty.get(get_url_cfg_plugin(host, plugin, child), verify: false)
        rescue => e
            FAIL(e.message, e)
        end
    end
    def self.set_plugin_config(host, plugin, cfg, child = nil)
        begin
            return HTTParty.put(get_url_cfg_plugin(host, plugin, child), verify: false, body: cfg)
        rescue => e
            FAIL(e.message, e)
        end
    end
    def self.get_plugin_module_list(host, plugin, child = nil)
        begin
            return HTTParty.get(get_url_cfg_plugin(host, plugin, child) + "/modules", verify: false, format: :plain)
        rescue => e
            FAIL(e.message, e)
        end
    end
    def self.get_job_list(host, plugin, mod, child = nil)
        begin
            return HTTParty.get(get_url_cfg_module(host, plugin, mod, child) + "/jobs", verify: false, format: :plain)
        rescue => e
            FAIL(e.message, e)
        end
    end
    def self.create_job(host, plugin, mod, job_id, job_cfg, child = nil)
        begin
            return HTTParty.post(get_url_cfg_job(host, plugin, mod, job_id, child), verify: false, body: job_cfg)
        rescue => e
            FAIL(e.message, e)
        end
    end
    def self.delete_job(host, plugin, mod, job_id, child = nil)
        begin
            return HTTParty.delete(get_url_cfg_job(host, plugin, mod, job_id, child), verify: false)
        rescue => e
            FAIL(e.message, e)
        end
    end
    def self.get_job_config(host, plugin, mod, job_id, child = nil)
        begin
            return HTTParty.get(get_url_cfg_job(host, plugin, mod, job_id, child), verify: false, format: :plain)
        rescue => e
            FAIL(e.message, e)
        end
    end
    def self.set_job_config(host, plugin, mod, job_id, job_cfg, child = nil)
        begin
            return HTTParty.put(get_url_cfg_job(host, plugin, mod, job_id, child), verify: false, body: job_cfg)
        rescue => e
            FAIL(e.message, e)
        end
    end
end

require_relative 'sub_tests/test_parent_child.rb'

$test_runner.FINALIZE()
puts $pastel.green.bold("All tests passed!")
puts ("Total #{$test_runner.stats[:assertions]} assertions, #{$test_runner.stats[:tests]} tests in #{$test_runner.stats[:suites]} suites")
exit 0