-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathfluentd_test.rb
185 lines (162 loc) · 4.92 KB
/
fluentd_test.rb
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
require "test_helper"
class FluentdTest < ActiveSupport::TestCase
module PathPermission
def self.included(base)
base.class_eval do
setup do
setup_target
end
sub_test_case "file exists" do
setup do
FileUtils.touch(@path)
end
teardown do
FileUtils.rm_f(@path)
end
test "writable" do
FileUtils.chmod(0600, @path)
@fluentd.check_permission(@column)
assert do
@fluentd.errors.blank?
end
end
test "not writable" do
FileUtils.chmod(0400, @path)
@fluentd.check_permission(@column)
assert do
@fluentd.errors.present?
end
assert_equal(I18n.t('activerecord.errors.messages.lack_write_permission'), @fluentd.errors[@column].first)
end
test "not readable" do
FileUtils.chmod(0200, @path)
@fluentd.check_permission(@column)
assert do
@fluentd.errors.present?
end
assert_equal(I18n.t('activerecord.errors.messages.lack_read_permission'), @fluentd.errors[@column].first)
end
test "is directory" do
@fluentd.__send__("#{@column}=", Rails.root + "tmp")
@fluentd.check_permission(@column)
assert do
@fluentd.errors.present?
end
assert_equal(I18n.t('activerecord.errors.messages.is_a_directory'), @fluentd.errors[@column].first)
end
end
sub_test_case "file not exists" do
setup do
@dir = File.dirname(@path)
FileUtils.rm_f(@path)
end
teardown do
FileUtils.chmod_R(0755, @dir)
end
test "writable" do
FileUtils.chmod(0700, @dir)
@fluentd.check_permission(@column)
assert do
@fluentd.errors.blank?
end
end
test "not writable" do
FileUtils.chmod(0500, @dir)
@fluentd.check_permission(@column)
assert do
@fluentd.errors.present?
end
assert_equal(I18n.t('activerecord.errors.messages.lack_write_permission'), @fluentd.errors[@column].first)
end
end
end
end
end
setup do
@fluentd = FactoryBot.build(:fluentd)
end
teardown do
File.unlink(Fluentd.json_path) if File.exist?(Fluentd.json_path)
end
sub_test_case "#valid?" do
setup do
%w(pid_file log_file config_file).each do |column|
FileUtils.mkdir_p(File.dirname(@fluentd.__send__(column)))
FileUtils.touch(@fluentd.__send__(column))
end
end
data("fluentd" => ["fluentd_gem", true],
"not declared in Fluentd.variants" => ["foobar", false])
test "variant" do |(variant, result)|
@fluentd.variant = variant
assert_equal(result, @fluentd.valid?)
end
sub_test_case "pid_file" do
def setup_target
@column = :pid_file
@path = @fluentd.pid_file
end
include PathPermission
end
sub_test_case "log_file" do
def setup_target
@column = :log_file
@path = @fluentd.log_file
end
include PathPermission
end
sub_test_case "config_file" do
def setup_target
@column = :config_file
@path = @fluentd.config_file
end
include PathPermission
end
end
data("fluentd_gem" => { variant: "fluentd_gem", fluentd_gem?: true },
"td-agent" => { variant: "td-agent", fluentd_gem?: false })
test "variant" do |data|
@fluentd.variant = data[:variant]
assert_equal(data[:fluentd_gem?], @fluentd.fluentd_gem?)
@fluentd.load_settings_from_agent_default
expected = {
pid_file: @fluentd.agent.class.default_options[:pid_file],
log_file: @fluentd.agent.class.default_options[:log_file],
config_file: @fluentd.agent.class.default_options[:config_file]
}
actual = {
pid_file: @fluentd.pid_file,
log_file: @fluentd.log_file,
config_file: @fluentd.config_file,
}
assert_equal(expected, actual)
end
data("fluentd_gem" => ["fluentd_gem", Fluentd::Agent::FluentdGem],
"td-agent" => ["td-agent", Fluentd::Agent::TdAgent])
test "#agent" do |(variant, klass)|
@fluentd.variant = variant
assert do
@fluentd.agent.instance_of?(klass)
end
end
sub_test_case "#ensure_default_config_file" do
setup do
@config_file = Rails.root + "tmp/test.conf"
@fluentd.config_file = @config_file
end
test "doesn't exist" do
File.unlink(@config_file) if File.exist?(@config_file)
@fluentd.save
assert do
File.exist?(@fluentd.config_file)
end
end
test "already exist" do
FileUtils.touch(@config_file)
@fluentd.save
assert do
File.exist?(@fluentd.config_file)
end
end
end
end