server_settings 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,300 @@
1
+ require 'spec_helper'
2
+ require 'server_settings'
3
+
4
+ describe ServerSettings do
5
+ let (:config1) {
6
+ yaml_text = <<-EOF
7
+ role1:
8
+ port: 1000
9
+ hosts:
10
+ - 1.1.1.1
11
+ - 2.2.2.2
12
+ role2:
13
+ hosts:
14
+ - 3.3.3.3
15
+ EOF
16
+ }
17
+
18
+ let (:config2) {
19
+ yaml_text = <<-EOF
20
+ role2:
21
+ hosts:
22
+ - 4.4.4.4
23
+ EOF
24
+ }
25
+
26
+ after do
27
+ ServerSettings.destroy
28
+ end
29
+
30
+ describe "config_load" do
31
+ context "when config file exists" do
32
+ before do
33
+ filepath = "config.yml"
34
+ allow(IO).to receive(:read).with(filepath).and_return(config1)
35
+ allow(File).to receive(:mtime).with(filepath).and_return(Time.now)
36
+ end
37
+ it 'can load yaml file' do
38
+ ServerSettings.load_config("config.yml")
39
+ end
40
+
41
+ it 'raise error when found duplicate role' do
42
+ ServerSettings.load_from_yaml("role1: { hosts: [ 1.1.1.1 ] }")
43
+ expect do
44
+ ServerSettings.load_from_yaml("role1: { hosts: [ 2.2.2.2 ] }")
45
+ end.to raise_error(ServerSettings::DuplicateRole)
46
+ end
47
+
48
+ it 'raise error not array hosts' do
49
+ expect do
50
+ ServerSettings.load_from_yaml("role: { hosts: 1.1.1.1 }")
51
+ end.to raise_exception(ServerSettings::HostCollection::InvalidHosts)
52
+ end
53
+ end
54
+
55
+ context "when config file does not exist" do
56
+ it 'raise error' do
57
+ expect { ServerSettings.load_config "does_not_exist.yml" }.to raise_error
58
+ end
59
+ end
60
+ end
61
+
62
+ describe "load_config_dir" do
63
+ it 'can load yaml files from directory pattern' do
64
+ ServerSettings.load_config_dir("spec/test-yaml/*.yml")
65
+ expect( ServerSettings.roles.keys.sort ).to eq(["role1", "role2"])
66
+ end
67
+
68
+ context "when any yaml does not exist" do
69
+ it "not raise error, but not define any role" do
70
+ ServerSettings.load_config_dir("spec/does_not_exist/*.yml")
71
+ expect(ServerSettings.roles.keys.sort).to eq []
72
+ end
73
+ end
74
+
75
+ context "when argument is directory" do
76
+ it "raise error" do
77
+ expect { ServerSettings.load_config_dir("spec/test-yaml") }.to raise_error
78
+ end
79
+ context "when does not exist directory " do
80
+ it "not raise error, but not define any role" do
81
+ ServerSettings.load_config_dir("spec/does_not_exist")
82
+ expect(ServerSettings.roles.keys.sort).to eq []
83
+ end
84
+ end
85
+ end
86
+ end
87
+
88
+ describe "#reload" do
89
+ before do
90
+ ServerSettings.load_config_dir("spec/test-yaml/*.yml")
91
+ end
92
+
93
+ context 'when file has not changes' do
94
+ it 'not reload yaml files' do
95
+ expect(ServerSettings).to_not receive(:load_config)
96
+ ServerSettings.reload
97
+ end
98
+ end
99
+
100
+ context 'when file modified' do
101
+ it 'reload yaml files' do
102
+ allow(File).to receive(:mtime).with("spec/test-yaml/role1.yml").and_return(Time.now)
103
+ allow(File).to receive(:mtime).with("spec/test-yaml/role2.yml").and_return(Time.at(0))
104
+ expect(ServerSettings).to receive(:load_config).with("spec/test-yaml/role1.yml")
105
+ ServerSettings.reload
106
+ end
107
+ end
108
+ end
109
+
110
+ describe "role accessor" do
111
+ context "exist role" do
112
+ it 'return array of hosts corresponds to role' do
113
+ ServerSettings.load_from_yaml(config1)
114
+ expect(ServerSettings.role1.hosts.with_format("%host")).to eq(["1.1.1.1", "2.2.2.2"])
115
+ end
116
+ end
117
+
118
+ context "not exist role" do
119
+ it 'return nil' do
120
+ ServerSettings.load_from_yaml(config1)
121
+ expect(ServerSettings.not_found_role).to be_nil
122
+ end
123
+ end
124
+ end
125
+
126
+ describe "each role" do
127
+ it 'can iterate each server' do
128
+ ServerSettings.load_from_yaml(config1)
129
+ expect { |b| ServerSettings.each_role(&b) }.to yield_successive_args([String, Array],
130
+ [String, Array])
131
+ end
132
+ end
133
+
134
+ describe "with_format" do
135
+ it 'can format host string with configuration params' do
136
+ ServerSettings.load_from_yaml(config1)
137
+ expect(ServerSettings.role1.with_format("%host:%port")).to eq(["1.1.1.1:1000", "2.2.2.2:1000"])
138
+ end
139
+ end
140
+
141
+ describe "render erb yaml" do
142
+ it 'can render yaml file using erb' do
143
+ ip = "4.4.4.4"
144
+ ServerSettings.load_from_yaml_erb(<<-EOF, erb_binding: binding)
145
+ role:
146
+ hosts:
147
+ - <%= ip %>
148
+ EOF
149
+ expect(ServerSettings.role.hosts.first.host).to eq("4.4.4.4")
150
+ end
151
+ end
152
+
153
+ describe 'databases' do
154
+ let (:db_config) { <<EOS }
155
+ databases:
156
+ :adapter: mysql2
157
+ :encoding: utf8
158
+ :reconnect: true
159
+ :database: app
160
+ :pool: 10
161
+ :username: db_user1
162
+ :password: db_pass1
163
+ :master: 192.168.30.86
164
+ :backup: 192.168.30.85
165
+ db1:
166
+ :database: app_db1
167
+ :master: 192.168.30.86
168
+ :backup: 192.168.30.85
169
+ shards:
170
+ user_shard_1:
171
+ :database: app_user_shard1
172
+ :master: 192.168.30.86
173
+ :backup: 192.168.30.85
174
+ user_shard_2:
175
+ :database: app_user_shard2
176
+ :master: 192.168.30.86
177
+ :backup: 192.168.30.85
178
+ db2:
179
+ :database: app_db2
180
+ :master: 192.168.30.86
181
+ :backup: 192.168.30.85
182
+ :slaves: [ 192.168.30.87, 192.168.30.88 ]
183
+ group1:
184
+ db3:
185
+ :master: 192.168.30.86
186
+ :backup: 192.168.30.85
187
+ EOS
188
+
189
+ describe ServerSettings::Database do
190
+
191
+ context "db in group" do
192
+ it 'describe database with group' do
193
+ ServerSettings.load_from_yaml(db_config)
194
+ db = ServerSettings.databases.find("user_shard_1")
195
+ expect(db.group).to eq("shards")
196
+ expect(db.config(:master)).to eq({
197
+ :adapter => "mysql2",
198
+ :database => "app_user_shard1",
199
+ :encoding => "utf8",
200
+ :host => "192.168.30.86",
201
+ :password => "db_pass1",
202
+ :pool => 10,
203
+ :reconnect => true,
204
+ :username => "db_user1"})
205
+ expect(db.config(:backup)).to eq({
206
+ :adapter => "mysql2",
207
+ :database => "app_user_shard1",
208
+ :encoding => "utf8",
209
+ :host => "192.168.30.85",
210
+ :password => "db_pass1",
211
+ :pool => 10,
212
+ :reconnect => true,
213
+ :username => "db_user1"})
214
+ expect(db.has_slave?).to be_falsey
215
+
216
+ end
217
+ end
218
+
219
+ context "db not in group" do
220
+ it 'describe database instance without group' do
221
+ ServerSettings.load_from_yaml(db_config)
222
+ db = ServerSettings.databases.find("db1")
223
+ expect(db.group).to be_nil
224
+ expect(db.config(:master)).to eq({
225
+ :adapter => "mysql2",
226
+ :database => "app_db1",
227
+ :encoding => "utf8",
228
+ :host => "192.168.30.86",
229
+ :password => "db_pass1",
230
+ :pool => 10,
231
+ :reconnect => true,
232
+ :username => "db_user1"})
233
+ expect(db.config(:backup)).to eq({
234
+ :adapter => "mysql2",
235
+ :database => "app_db1",
236
+ :encoding => "utf8",
237
+ :host => "192.168.30.85",
238
+ :password => "db_pass1",
239
+ :pool => 10,
240
+ :reconnect => true,
241
+ :username => "db_user1"})
242
+ expect(db.has_slave?).to be_falsey
243
+ end
244
+ end
245
+
246
+ context 'db has slaves' do
247
+ it 'has slaves and return slave configurations' do
248
+ ServerSettings.load_from_yaml(db_config)
249
+ db = ServerSettings.databases.find("db2")
250
+ expect(db.has_slave?).to be_truthy
251
+ expect(db.config(:slaves)[0]).to eq({
252
+ :adapter => "mysql2",
253
+ :database => "app_db2",
254
+ :encoding => "utf8",
255
+ :host => "192.168.30.87",
256
+ :password => "db_pass1",
257
+ :pool => 10,
258
+ :reconnect => true,
259
+ :username => "db_user1"})
260
+
261
+ end
262
+ end
263
+
264
+ end
265
+
266
+ describe "hosts" do
267
+ it 'return array of db instance' do
268
+ ServerSettings.load_from_yaml(db_config)
269
+ expect(ServerSettings.databases.hosts).to include(instance_of(ServerSettings::Database))
270
+ end
271
+ end
272
+
273
+ describe "find" do
274
+ it 'return db instance ' do
275
+ ServerSettings.load_from_yaml(db_config)
276
+ db1 = ServerSettings.databases.find("db1")
277
+ expect(db1.master).to eq("192.168.30.86")
278
+ expect(db1.backup).to eq("192.168.30.85")
279
+ expect(db1.settings[:database]).to eq("app_db1")
280
+ expect(db1.settings[:username]).to eq("db_user1")
281
+ end
282
+
283
+ it 'return nil if not found' do
284
+ ServerSettings.load_from_yaml(db_config)
285
+ db = ServerSettings.databases.find("not-found")
286
+ expect(db).to be_nil
287
+ end
288
+ end
289
+
290
+
291
+ describe "each" do
292
+ it 'loop 6 times yield with db argument' do
293
+ ServerSettings.load_from_yaml(db_config)
294
+ args = 6.times.map {ServerSettings::Database}
295
+ expect { |b| ServerSettings.databases.each(&b)}.to yield_control.exactly(6).times
296
+ expect { |b| ServerSettings.databases.each(&b)}.to yield_successive_args(*args)
297
+ end
298
+ end
299
+ end
300
+ end
@@ -0,0 +1,91 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # The generated `.rspec` file contains `--require spec_helper` which will cause
4
+ # this file to always be loaded, without a need to explicitly require it in any
5
+ # files.
6
+ #
7
+ # Given that it is always loaded, you are encouraged to keep this file as
8
+ # light-weight as possible. Requiring heavyweight dependencies from this file
9
+ # will add to the boot time of your test suite on EVERY test run, even for an
10
+ # individual file that may not need all of that loaded. Instead, consider making
11
+ # a separate helper file that requires the additional dependencies and performs
12
+ # the additional setup, and require it from the spec files that actually need
13
+ # it.
14
+ #
15
+ # The `.rspec` file also contains a few flags that are not defaults but that
16
+ # users commonly want.
17
+ #
18
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
19
+ RSpec.configure do |config|
20
+ # rspec-expectations config goes here. You can use an alternate
21
+ # assertion/expectation library such as wrong or the stdlib/minitest
22
+ # assertions if you prefer.
23
+ config.expect_with :rspec do |expectations|
24
+ # This option will default to `true` in RSpec 4. It makes the `description`
25
+ # and `failure_message` of custom matchers include text for helper methods
26
+ # defined using `chain`, e.g.:
27
+ # be_bigger_than(2).and_smaller_than(4).description
28
+ # # => "be bigger than 2 and smaller than 4"
29
+ # ...rather than:
30
+ # # => "be bigger than 2"
31
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
32
+ end
33
+
34
+ # rspec-mocks config goes here. You can use an alternate test double
35
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
36
+ config.mock_with :rspec do |mocks|
37
+ # Prevents you from mocking or stubbing a method that does not exist on
38
+ # a real object. This is generally recommended, and will default to
39
+ # `true` in RSpec 4.
40
+ mocks.verify_partial_doubles = true
41
+ end
42
+
43
+ # The settings below are suggested to provide a good initial experience
44
+ # with RSpec, but feel free to customize to your heart's content.
45
+ =begin
46
+ # These two settings work together to allow you to limit a spec run
47
+ # to individual examples or groups you care about by tagging them with
48
+ # `:focus` metadata. When nothing is tagged with `:focus`, all examples
49
+ # get run.
50
+ config.filter_run :focus
51
+ config.run_all_when_everything_filtered = true
52
+
53
+ # Limits the available syntax to the non-monkey patched syntax that is
54
+ # recommended. For more details, see:
55
+ # - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
56
+ # - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
57
+ # - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
58
+ config.disable_monkey_patching!
59
+
60
+ # This setting enables warnings. It's recommended, but in some cases may
61
+ # be too noisy due to issues in dependencies.
62
+ config.warnings = true
63
+
64
+ # Many RSpec users commonly either run the entire suite or an individual
65
+ # file, and it's useful to allow more verbose output when running an
66
+ # individual spec file.
67
+ if config.files_to_run.one?
68
+ # Use the documentation formatter for detailed output,
69
+ # unless a formatter has already been configured
70
+ # (e.g. via a command-line flag).
71
+ config.default_formatter = 'doc'
72
+ end
73
+
74
+ # Print the 10 slowest examples and example groups at the
75
+ # end of the spec run, to help surface which specs are running
76
+ # particularly slow.
77
+ config.profile_examples = 10
78
+
79
+ # Run specs in random order to surface order dependencies. If you find an
80
+ # order dependency and want to debug it, you can fix the order by providing
81
+ # the seed, which is printed after each run.
82
+ # --seed 1234
83
+ config.order = :random
84
+
85
+ # Seed global randomization in this process using the `--seed` CLI option.
86
+ # Setting this allows you to use `--seed` to deterministically reproduce
87
+ # test failures related to randomization by passing the same `--seed` value
88
+ # as the one that triggered the failure.
89
+ Kernel.srand config.seed
90
+ =end
91
+ end
@@ -0,0 +1,3 @@
1
+ role1:
2
+ hosts:
3
+ - 1.1.1.1
@@ -0,0 +1,3 @@
1
+ role2:
2
+ hosts:
3
+ - 2.2.3.4
metadata ADDED
@@ -0,0 +1,133 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: server_settings
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Osamu MATSUMOTO
8
+ - Contributers
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2016-04-18 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: colorize
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ">="
19
+ - !ruby/object:Gem::Version
20
+ version: '0'
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ version: '0'
28
+ - !ruby/object:Gem::Dependency
29
+ name: bundler
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: '1.7'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: '1.7'
42
+ - !ruby/object:Gem::Dependency
43
+ name: rake
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - "~>"
47
+ - !ruby/object:Gem::Version
48
+ version: '10.0'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - "~>"
54
+ - !ruby/object:Gem::Version
55
+ version: '10.0'
56
+ - !ruby/object:Gem::Dependency
57
+ name: rspec
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ description: Server Configuration logic for any where
71
+ email:
72
+ - osamu.matsumoto@gmail.com
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - ".rspec"
79
+ - CHANGELOG.md
80
+ - Gemfile
81
+ - LICENSE.txt
82
+ - README.md
83
+ - Rakefile
84
+ - example/sample.rb
85
+ - lib/capistrano/server_settings.rb
86
+ - lib/server_settings.rb
87
+ - lib/server_settings/capistrano.rb
88
+ - lib/server_settings/database.rb
89
+ - lib/server_settings/database_config.rb
90
+ - lib/server_settings/host.rb
91
+ - lib/server_settings/host_collection.rb
92
+ - lib/server_settings/railtie.rb
93
+ - lib/server_settings/role.rb
94
+ - lib/server_settings/role_db.rb
95
+ - lib/server_settings/tasks.rb
96
+ - lib/server_settings/tasks/db.rake
97
+ - lib/server_settings/version.rb
98
+ - server_settings.gemspec
99
+ - spec/lib/server_settings/database_config_spec.rb
100
+ - spec/lib/servers_config_spec.rb
101
+ - spec/spec_helper.rb
102
+ - spec/test-yaml/role1.yml
103
+ - spec/test-yaml/role2.yml
104
+ homepage: https://github.com/monsterstrike/server_settings
105
+ licenses:
106
+ - MIT
107
+ metadata: {}
108
+ post_install_message:
109
+ rdoc_options: []
110
+ require_paths:
111
+ - lib
112
+ required_ruby_version: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ required_rubygems_version: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - ">="
120
+ - !ruby/object:Gem::Version
121
+ version: '0'
122
+ requirements: []
123
+ rubyforge_project:
124
+ rubygems_version: 2.6.3
125
+ signing_key:
126
+ specification_version: 4
127
+ summary: Server Configuration logic
128
+ test_files:
129
+ - spec/lib/server_settings/database_config_spec.rb
130
+ - spec/lib/servers_config_spec.rb
131
+ - spec/spec_helper.rb
132
+ - spec/test-yaml/role1.yml
133
+ - spec/test-yaml/role2.yml