moose-inventory 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (74) hide show
  1. checksums.yaml +7 -0
  2. data/.coveralls.yml +0 -0
  3. data/.gitignore +17 -0
  4. data/.rubocop.yml +793 -0
  5. data/Gemfile +3 -0
  6. data/Guardfile +38 -0
  7. data/LICENSE.txt +22 -0
  8. data/README.md +31 -0
  9. data/README.md.orig +35 -0
  10. data/Rakefile +1 -0
  11. data/bin/moose_inventory +10 -0
  12. data/config/dotfiles/coveralls.yml +0 -0
  13. data/config/dotfiles/gitignore +17 -0
  14. data/config/dotfiles/rubocop.yml +793 -0
  15. data/lib/moose/inventory/cli/application.rb +30 -0
  16. data/lib/moose/inventory/cli/formatter.rb +92 -0
  17. data/lib/moose/inventory/cli/group.rb +23 -0
  18. data/lib/moose/inventory/cli/group_add.rb +98 -0
  19. data/lib/moose/inventory/cli/group_addchild.rb +21 -0
  20. data/lib/moose/inventory/cli/group_addhost.rb +97 -0
  21. data/lib/moose/inventory/cli/group_addvar.rb +72 -0
  22. data/lib/moose/inventory/cli/group_get.rb +52 -0
  23. data/lib/moose/inventory/cli/group_list.rb +41 -0
  24. data/lib/moose/inventory/cli/group_rm.rb +77 -0
  25. data/lib/moose/inventory/cli/group_rmchild.rb +20 -0
  26. data/lib/moose/inventory/cli/group_rmhost.rb +89 -0
  27. data/lib/moose/inventory/cli/group_rmvar.rb +65 -0
  28. data/lib/moose/inventory/cli/host.rb +24 -0
  29. data/lib/moose/inventory/cli/host_add.rb +93 -0
  30. data/lib/moose/inventory/cli/host_addgroup.rb +88 -0
  31. data/lib/moose/inventory/cli/host_addvar.rb +76 -0
  32. data/lib/moose/inventory/cli/host_get.rb +59 -0
  33. data/lib/moose/inventory/cli/host_list.rb +40 -0
  34. data/lib/moose/inventory/cli/host_rm.rb +62 -0
  35. data/lib/moose/inventory/cli/host_rmgroup.rb +80 -0
  36. data/lib/moose/inventory/cli/host_rmvar.rb +69 -0
  37. data/lib/moose/inventory/config/config.rb +169 -0
  38. data/lib/moose/inventory/db/db.rb +249 -0
  39. data/lib/moose/inventory/db/exceptions.rb +14 -0
  40. data/lib/moose/inventory/db/models.rb +32 -0
  41. data/lib/moose/inventory/moose_inventory_cli.rb +25 -0
  42. data/lib/moose/inventory/version.rb +7 -0
  43. data/moose-inventory.gemspec +45 -0
  44. data/scripts/guard_quality.sh +3 -0
  45. data/scripts/guard_test.sh +2 -0
  46. data/scripts/reports.sh +4 -0
  47. data/spec/config/config.yml +12 -0
  48. data/spec/lib/moose/inventory/cli/application_spec.rb +15 -0
  49. data/spec/lib/moose/inventory/cli/cli_spec.rb +26 -0
  50. data/spec/lib/moose/inventory/cli/formatter_spec.rb +63 -0
  51. data/spec/lib/moose/inventory/cli/group_add_spec.rb +398 -0
  52. data/spec/lib/moose/inventory/cli/group_addhost_spec.rb +251 -0
  53. data/spec/lib/moose/inventory/cli/group_addvar_spec.rb +235 -0
  54. data/spec/lib/moose/inventory/cli/group_get_spec.rb +107 -0
  55. data/spec/lib/moose/inventory/cli/group_list_spec.rb +79 -0
  56. data/spec/lib/moose/inventory/cli/group_rm_spec.rb +191 -0
  57. data/spec/lib/moose/inventory/cli/group_rmhost_spec.rb +215 -0
  58. data/spec/lib/moose/inventory/cli/group_rmvar_spec.rb +202 -0
  59. data/spec/lib/moose/inventory/cli/group_spec.rb +15 -0
  60. data/spec/lib/moose/inventory/cli/host_add_spec.rb +330 -0
  61. data/spec/lib/moose/inventory/cli/host_addgroup_spec.rb +248 -0
  62. data/spec/lib/moose/inventory/cli/host_addvar_spec.rb +233 -0
  63. data/spec/lib/moose/inventory/cli/host_get_spec.rb +106 -0
  64. data/spec/lib/moose/inventory/cli/host_list_spec.rb +83 -0
  65. data/spec/lib/moose/inventory/cli/host_rm_spec.rb +132 -0
  66. data/spec/lib/moose/inventory/cli/host_rmgroup_spec.rb +245 -0
  67. data/spec/lib/moose/inventory/cli/host_rmvar_spec.rb +206 -0
  68. data/spec/lib/moose/inventory/cli/host_spec.rb +12 -0
  69. data/spec/lib/moose/inventory/config/config_spec.rb +80 -0
  70. data/spec/lib/moose/inventory/db/db_spec.rb +184 -0
  71. data/spec/lib/moose/inventory/db/models_spec.rb +150 -0
  72. data/spec/shared/shared_config_setup.rb +21 -0
  73. data/spec/spec_helper.rb +110 -0
  74. metadata +386 -0
@@ -0,0 +1,206 @@
1
+ require 'spec_helper'
2
+
3
+ # TODO: the usual respond_to? method doesn't seem to work on Thor objects.
4
+ # Why not? For now, we'll check against instance_methods.
5
+
6
+ RSpec.describe Moose::Inventory::Cli::Host do
7
+ before(:all) do
8
+ # Set up the configuration object
9
+ @mockarg_parts = {
10
+ config: File.join(spec_root, 'config/config.yml'),
11
+ format: 'yaml',
12
+ env: 'test'
13
+ }
14
+
15
+ @mockargs = []
16
+ @mockarg_parts.each do |key, val|
17
+ @mockargs << "--#{key}"
18
+ @mockargs << val
19
+ end
20
+
21
+ @config = Moose::Inventory::Config
22
+ @config.init(@mockargs)
23
+
24
+ @db = Moose::Inventory::DB
25
+ @db.init if @db.db.nil?
26
+
27
+ @console = Moose::Inventory::Cli::Formatter
28
+ @host = Moose::Inventory::Cli::Host
29
+ @app = Moose::Inventory::Cli::Application
30
+ end
31
+
32
+ before(:each) do
33
+ @db.reset
34
+ end
35
+
36
+ describe 'rmvar' do
37
+ it 'should be responsive' do
38
+ result = @host.instance_methods(false).include?(:rmvar)
39
+ expect(result).to eq(true)
40
+ end
41
+
42
+ #-----------------
43
+ it '<missing args> ... should abort with an error' do
44
+ actual = runner do
45
+ @app.start(%w(host rmvar)) # <- no group given
46
+ end
47
+
48
+ # Check output
49
+ desired = { aborted: true}
50
+ desired[:STDERR] = "ERROR: Wrong number of arguments, 0 for 2 or more.\n"
51
+ expected(actual, desired)
52
+ end
53
+
54
+ #------------------------
55
+ it 'HOST key=value ... should abort if the host does not exist' do
56
+ host_name = "not-a-host"
57
+ var_name = "foo=bar"
58
+ actual = runner do
59
+ @app.start(%W(host rmvar #{host_name} #{var_name}))
60
+ end
61
+
62
+ # Check output
63
+ desired = { aborted: true}
64
+ desired[:STDOUT] =
65
+ "Remove variable(s) '#{var_name}' from host '#{host_name}':\n"\
66
+ " - retrieve host '#{host_name}'...\n"
67
+ desired[:STDERR] =
68
+ "An error occurred during a transaction, any changes have been rolled back.\n"\
69
+ "ERROR: The host '#{host_name}' does not exist.\n"
70
+ expected(actual, desired)
71
+ end
72
+
73
+ #------------------------
74
+ it 'HOST <malformed> ... should abort with an error' do
75
+ # 1. Should add the var to the db
76
+ # 2. Should associate the host with the var
77
+
78
+ host_name = 'test1'
79
+ @db.models[:host].create(name: host_name)
80
+
81
+ var = {name: 'foo', value: "bar"}
82
+ cases = %W(
83
+ =bar
84
+ foo=bar=
85
+ =foo=bar
86
+ foo=bar=extra
87
+ )
88
+
89
+ cases.each do |args|
90
+ actual = runner do
91
+ @app.start(%W(host rmvar #{host_name} #{args} ))
92
+ end
93
+ #@console.out(actual,'p')
94
+
95
+ desired = { aborted: true}
96
+ desired[:STDOUT] =
97
+ "Remove variable(s) '#{args}' from host '#{host_name}':\n"\
98
+ " - retrieve host '#{host_name}'...\n"\
99
+ " - OK\n"\
100
+ " - remove variable '#{args}'...\n"
101
+ desired[:STDERR] =
102
+ "An error occurred during a transaction, any changes have been rolled back.\n"\
103
+ "ERROR: Incorrect format in {#{args}}. Expected 'key' or 'key=value'.\n"
104
+
105
+ expected(actual, desired)
106
+ end
107
+ end
108
+
109
+ #------------------------
110
+ it 'host rmvar HOST <valid args> ... should remove the host variable' do
111
+ # 1. Should add the var to the db
112
+ # 2. Should associate the host with the var
113
+
114
+ host_name = 'test1'
115
+
116
+ var = {name: 'foo', value: "bar"}
117
+ cases = %W(
118
+ #{var[:name]}
119
+ #{var[:name]}=
120
+ #{var[:name]}=#{var[:value]}
121
+ )
122
+ cases.each do |example|
123
+ # reset the db
124
+ @db.reset
125
+
126
+ # Add an initial host and hostvar
127
+ @db.models[:host].create(name: host_name)
128
+ runner do
129
+ @app.start(%W(host addvar #{host_name} #{var[:name]}=#{var[:value]} ))
130
+ end
131
+
132
+ # Try to remove the hostvar using the case example valid args
133
+ actual = runner do
134
+ @app.start(%W(host rmvar #{host_name} #{example}))
135
+ end
136
+ #@console.out(actual,'p')
137
+
138
+ # Check the output
139
+ desired = { aborted: false}
140
+ desired[:STDOUT] =
141
+ "Remove variable(s) '#{example}' from host '#{host_name}':\n"\
142
+ " - retrieve host '#{host_name}'...\n"\
143
+ " - OK\n"\
144
+ " - remove variable '#{example}'...\n"\
145
+ " - OK\n"\
146
+ " - all OK\n"\
147
+ "Succeeded.\n"
148
+
149
+ #@console.out(desired,'p')
150
+ expected(actual, desired)
151
+
152
+ # Check the db
153
+ host = @db.models[:host].find(name: host_name)
154
+ hostvars = host.hostvars_dataset
155
+ expect(hostvars.count).to eq(0)
156
+
157
+ hostvars = @db.models[:hostvar].all
158
+ expect(hostvars.count).to eq(0)
159
+ end
160
+ end
161
+
162
+ #------------------------
163
+ it 'HOST key1=value1 key2=value2 ... should remove multiple key/value pairs' do
164
+ host_name = 'test1'
165
+ varsarray = [
166
+ {name: 'var1', value: "val1"},
167
+ {name: 'var2', value: "val2"}
168
+ ]
169
+
170
+ vars = []
171
+ varsarray.each do |var|
172
+ vars << "#{var[:name]}=#{var[:value]}"
173
+ end
174
+
175
+ @db.models[:host].create(name: host_name)
176
+ actual = runner do
177
+ @app.start(%W(host addvar #{host_name}) + vars )
178
+ end
179
+
180
+ actual = runner do
181
+ @app.start(%W(host rmvar #{host_name}) + vars )
182
+ end
183
+ #@console.out(actual,'p')
184
+
185
+ desired = { aborted: false}
186
+ desired[:STDOUT] =
187
+ "Remove variable(s) '#{vars.join(',')}' from host '#{host_name}':\n"\
188
+ " - retrieve host '#{host_name}'...\n"\
189
+ " - OK\n"
190
+ vars.each do |var|
191
+ desired[:STDOUT] = desired[:STDOUT] +
192
+ " - remove variable '#{var}'...\n"\
193
+ " - OK\n"
194
+ end
195
+ desired[:STDOUT] = desired[:STDOUT] +
196
+ " - all OK\n"\
197
+ "Succeeded.\n"
198
+ expected(actual, desired)
199
+
200
+ # We should have the correct hostvar associations
201
+ host = @db.models[:host].find(name: host_name)
202
+ hostvars = host.hostvars_dataset
203
+ expect(hostvars.count).to eq(0)
204
+ end
205
+ end
206
+ end
@@ -0,0 +1,12 @@
1
+ require 'spec_helper'
2
+
3
+ require_relative 'host_add_spec'
4
+ require_relative 'host_get_spec'
5
+ require_relative 'host_list_spec'
6
+ require_relative 'host_rm_spec'
7
+
8
+ require_relative 'host_addgroup_spec'
9
+ require_relative 'host_rmgroup_spec'
10
+
11
+ require_relative 'host_addvar_spec'
12
+ require_relative 'host_rmvar_spec'
@@ -0,0 +1,80 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe 'Moose::Inventory::Config' do
4
+ before(:all) do
5
+ # Set up the configuration object
6
+ @mockarg_parts = {
7
+ config: File.join(spec_root, 'config/config.yml'),
8
+ format: 'yaml',
9
+ env: 'test'
10
+ }
11
+
12
+ @mockargs = []
13
+ @mockarg_parts.each do |key, val|
14
+ @mockargs << "--#{key}"
15
+ @mockargs << val
16
+ end
17
+
18
+ @config = Moose::Inventory::Config
19
+ end
20
+
21
+ # .init()
22
+ describe '.init()' do
23
+ it 'should be responsive' do
24
+ result = @config.respond_to?(:init)
25
+ expect(result).to eq(true)
26
+ end
27
+ end
28
+
29
+ # ._configopts
30
+ describe '._configopts' do
31
+ it 'should be responsive' do
32
+ result = @config.respond_to?(:_confopts)
33
+ expect(result).to eq(true)
34
+ end
35
+
36
+ it 'should not be nil' do
37
+ @config.init(@mockargs)
38
+ expect(@config._confopts).not_to be_nil
39
+ end
40
+
41
+ it 'should default "--format" to json' do
42
+ @config.init([])
43
+ expect(@config._confopts[:format]).to eq('json')
44
+ end
45
+
46
+ it 'should pick up "--format" from its argument list' do
47
+ @config.init(@mockargs)
48
+ expect(@config._confopts[:format]).to eq('yaml')
49
+ end
50
+
51
+ it 'should default "--env" to ""' do
52
+ @config.init([])
53
+ expect(@config._confopts[:env]).to eq('')
54
+ end
55
+
56
+ it 'should pick up "--env" from its argument list' do
57
+ tmpargs = ['--env', 'rspectest', '--config', @mockarg_parts[:config]]
58
+ @config.init(tmpargs)
59
+ expect(@config._confopts[:env]).to eq('rspectest')
60
+ end
61
+ end
62
+
63
+ # ._settings
64
+ describe '._settings' do
65
+ it 'should be responsive' do
66
+ result = @config.respond_to?(:_settings)
67
+ expect(result).to eq(true)
68
+ end
69
+
70
+ it 'should not be nil' do
71
+ @config.init(@mockargs)
72
+ expect(@config._confopts).not_to be_nil
73
+ end
74
+
75
+ it 'should pick up "config.db" from the configuration file' do
76
+ @config.init(@mockargs)
77
+ expect(@config._settings[:config][:db]).not_to be_nil
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,184 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe 'Moose::Inventory::DB' do
4
+ #=============================
5
+ # Initialization
6
+ #
7
+
8
+ before(:all) do
9
+ # Set up the configuration object
10
+ @mockarg_parts = {
11
+ config: File.join(spec_root, 'config/config.yml'),
12
+ format: 'yaml',
13
+ env: 'test'
14
+ }
15
+
16
+ @mockargs = []
17
+ @mockarg_parts.each do |key, val|
18
+ @mockargs << "--#{key}"
19
+ @mockargs << val
20
+ end
21
+
22
+ @config = Moose::Inventory::Config
23
+ @config.init(@mockargs)
24
+
25
+ @db = Moose::Inventory::DB
26
+ end
27
+
28
+ #=============================
29
+ # Tests
30
+ #
31
+
32
+ describe '.init()' do
33
+ it 'should be responsive' do
34
+ expect(@db.respond_to?(:init)).to eq(true)
35
+ end
36
+
37
+ it 'shouldn\'t throw an error' do
38
+ failed = false
39
+ # rubocop:disable Lint/RescueException
40
+ begin
41
+ @db.init
42
+ rescue Exception => e
43
+ p e
44
+ failed = true
45
+ end
46
+ # rubocop:enable Lint/RescueException
47
+
48
+ expect(failed).to eq(false)
49
+ end
50
+ end
51
+
52
+ describe '.db' do
53
+ it 'should be responsive' do
54
+ expect(@db.respond_to?(:db)).to eq(true)
55
+ end
56
+
57
+ it 'should not be nil' do
58
+ expect(@db.db).not_to be_nil
59
+ end
60
+ end
61
+
62
+ describe '.models' do
63
+ it 'should be responsive' do
64
+ expect(@db.respond_to?(:models)).to eq(true)
65
+ end
66
+
67
+ it 'should not be nil' do
68
+ expect(@db.models).not_to be_nil
69
+ end
70
+ end
71
+
72
+ describe '.exceptions' do
73
+ it 'should be responsive' do
74
+ expect(@db.respond_to?(:exceptions)).to eq(true)
75
+ end
76
+
77
+ it 'should not be nil' do
78
+ expect(@db.exceptions).not_to be_nil
79
+ end
80
+ end
81
+
82
+ describe 'tables' do
83
+ it 'should have a hosts table' do
84
+ schema = @db.db.schema(:hosts)
85
+ expect(schema).not_to be_nil
86
+ end
87
+
88
+ it 'should have a hostsvars table' do
89
+ schema = @db.db.schema(:hostvars)
90
+ expect(schema).not_to be_nil
91
+ end
92
+ it 'should have a groups table' do
93
+ schema = @db.db.schema(:groups)
94
+ expect(schema).not_to be_nil
95
+ end
96
+
97
+ it 'should have a groupvars table' do
98
+ schema = @db.db.schema(:groupvars)
99
+ expect(schema).not_to be_nil
100
+ end
101
+
102
+ it 'should have a groups_hosts table' do
103
+ schema = @db.db.schema(:groups_hosts)
104
+ expect(schema).not_to be_nil
105
+ end
106
+ end
107
+
108
+ describe '.reset()' do
109
+ it 'should be responsive' do
110
+ result = @db.respond_to?(:reset)
111
+ expect(result).to eq(true)
112
+ end
113
+
114
+ it 'should purge the database of contents' do
115
+ # Put at least one host and one group into the database
116
+ @db.models[:host].create(name: 'reset-host')
117
+ @db.models[:group].create(name: 'reset-group')
118
+
119
+ # Reset the DB
120
+ @db.reset
121
+
122
+ #
123
+ hosts = @db.models[:host].all
124
+ expect(hosts.count).to eq(0)
125
+
126
+ group = @db.models[:group].all
127
+ expect(group.count).to eq(0)
128
+ end
129
+ end
130
+
131
+ describe '.transaction()' do
132
+ before(:each) do
133
+ @db.reset
134
+ end
135
+
136
+ it 'should be responsive' do
137
+ result = @db.respond_to?(:transaction)
138
+ expect(result).to eq(true)
139
+ end
140
+
141
+ it 'should perform transactions' do
142
+ hosts = @db.models[:host].all
143
+ count = { initial: hosts.count, items: 0 }
144
+
145
+ @db.transaction do
146
+ # @db.transaction('should perform transactions') do
147
+ (1..3).each do |_n|
148
+ count[:items] = count[:items] + 1
149
+ @db.models[:host].create(name: "transaction-#{count[:items]}")
150
+ end
151
+ end
152
+
153
+ hosts = @db.models[:host].all
154
+ count[:final] = hosts.count
155
+
156
+ # puts "\n#{ JSON.pretty_generate(@db.debug) }"
157
+ # puts "#{ JSON.pretty_generate(count) }"
158
+
159
+ expect(count[:final]).to eq(count[:initial] + count[:items])
160
+ end
161
+
162
+ it 'should roll back failed transactions' do
163
+ hosts = @db.models[:host].all
164
+ count = { initial: hosts.count, items: 0 }
165
+
166
+ @db.transaction do
167
+ # @db.transaction('should roll back failed transactions') do
168
+ (1..3).each do |_n|
169
+ count[:items] = count[:items] + 1
170
+ @db.models[:host].create(name: "rollback-#{count[:items]}")
171
+ end
172
+ fail Sequel::Rollback, 'Test error' #
173
+ end
174
+
175
+ hosts = @db.models[:host].all
176
+ count[:final] = hosts.count
177
+
178
+ # puts "\n#{ JSON.pretty_generate(@db.debug) }"
179
+ # puts "#{ JSON.pretty_generate(count) }"
180
+
181
+ expect(count[:final]).to eq(count[:initial])
182
+ end
183
+ end
184
+ end