moose-inventory 0.1.6 → 0.1.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/moose_inventory/cli/group.rb +2 -2
- data/lib/moose_inventory/cli/group_addchild.rb +77 -6
- data/lib/moose_inventory/cli/group_addhost.rb +2 -1
- data/lib/moose_inventory/cli/group_get.rb +7 -0
- data/lib/moose_inventory/cli/group_list.rb +9 -1
- data/lib/moose_inventory/cli/group_rmchild.rb +67 -5
- data/lib/moose_inventory/db/db.rb +4 -2
- data/lib/moose_inventory/db/models.rb +2 -1
- data/lib/moose_inventory/version.rb +1 -1
- data/spec/lib/moose_inventory/cli/group_addchild_spec.rb +181 -0
- data/spec/lib/moose_inventory/cli/group_rmchild_spec.rb +177 -0
- metadata +5 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c371e17bbc7c89eb31612771e665eb281d7f0c4c
|
4
|
+
data.tar.gz: 169b4a227e853a706db3ae6a2831b9ad6adbe546
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a30eec0b29edcd53ac7e8df397e6adb48c2877b9f867ffdc5f9113abe04dbcaebe8f8d5cbe64e260c95e234ba645695942a70dafaa0c87dbf8229ca1a4def965
|
7
|
+
data.tar.gz: cc8adf6c82178b9d662dfed5399cb0a42121dee21d948aed1e8c14e9ce83443561a9e60d427f595d57f377b346f98ced9299bfd0c1b6fd8e11cca1639444f83b
|
@@ -11,8 +11,8 @@ module Moose
|
|
11
11
|
require_relative 'group_get'
|
12
12
|
require_relative 'group_list'
|
13
13
|
require_relative 'group_rm'
|
14
|
-
|
15
|
-
|
14
|
+
require_relative 'group_addchild'
|
15
|
+
require_relative 'group_rmchild'
|
16
16
|
require_relative 'group_addhost'
|
17
17
|
require_relative 'group_rmhost'
|
18
18
|
require_relative 'group_addvar'
|
@@ -8,14 +8,85 @@ module Moose
|
|
8
8
|
# Implemention of the "group addchild" methods of the CLI
|
9
9
|
class Group < Thor # rubocop:disable ClassLength
|
10
10
|
#==========================
|
11
|
-
desc 'addchild [
|
12
|
-
'Associate
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
11
|
+
desc 'addchild PARENTGROUP CHILDGROUP_1 [CHILDGROUP_2 ... ]',
|
12
|
+
'Associate one or more child-groups CHILDGROUP_n with PARENTGROUP'
|
13
|
+
def addchild(*argv)
|
14
|
+
|
15
|
+
# Sanity check
|
16
|
+
if args.length < 2
|
17
|
+
abort("ERROR: Wrong number of arguments, #{args.length} "\
|
18
|
+
"for 2 or more.")
|
19
|
+
end
|
20
|
+
|
21
|
+
# Arguments
|
22
|
+
pname = args[0].downcase
|
23
|
+
cnames = args.slice(1, args.length - 1).uniq.map(&:downcase)
|
24
|
+
|
25
|
+
# Sanity
|
26
|
+
if pname == 'ungrouped' || cnames.include?('ungrouped')
|
27
|
+
abort("ERROR: Cannot manually manipulate the automatic group 'ungrouped'.")
|
28
|
+
end
|
29
|
+
|
30
|
+
# Convenience
|
31
|
+
db = Moose::Inventory::DB
|
32
|
+
fmt = Moose::Inventory::Cli::Formatter
|
33
|
+
|
34
|
+
# Transaction
|
35
|
+
warn_count = 0
|
36
|
+
begin
|
37
|
+
db.transaction do # Transaction start
|
38
|
+
puts "Associate parent group '#{pname}' with child group(s) '#{cnames.join(',')}':"
|
39
|
+
# Get the target group
|
40
|
+
fmt.puts 2, "- retrieve group '#{pname}'..."
|
41
|
+
pgroup = db.models[:group].find(name: pname)
|
42
|
+
if pgroup.nil?
|
43
|
+
abort("ERROR: The group '#{pname}' does not exist.")
|
44
|
+
end
|
45
|
+
fmt.puts 4, "- OK"
|
46
|
+
|
47
|
+
# Associate parent group with the child groups
|
48
|
+
|
49
|
+
groups_ds = pgroup.children_dataset
|
50
|
+
cnames.each do |cname|
|
51
|
+
fmt.puts 2, "- add association {group:#{pname} <-> group:#{cname}}..."
|
52
|
+
|
53
|
+
# Check against existing associations
|
54
|
+
unless groups_ds[name: cname].nil?
|
55
|
+
warn_count += 1
|
56
|
+
fmt.warn "Association {group:#{pname} <-> group:#{cname}}}"\
|
57
|
+
" already exists, skipping.\n"
|
58
|
+
fmt.puts 4, '- already exists, skipping.'
|
59
|
+
fmt.puts 4, '- OK'
|
60
|
+
next
|
61
|
+
end
|
62
|
+
|
63
|
+
# Add new association
|
64
|
+
cgroup = db.models[:group].find(name: cname)
|
65
|
+
if cgroup.nil?
|
66
|
+
warn_count += 1
|
67
|
+
fmt.warn "Group '#{cname}' does not exist and will be created.\n"
|
68
|
+
fmt.puts 4, '- child group does not exist, creating now...'
|
69
|
+
cgroup = db.models[:group].create(name: cname)
|
70
|
+
fmt.puts 6, '- OK'
|
71
|
+
end
|
72
|
+
pgroup.add_child(cgroup)
|
73
|
+
fmt.puts 4, '- OK'
|
74
|
+
end
|
75
|
+
fmt.puts 2, '- all OK'
|
76
|
+
end # Transaction end
|
77
|
+
rescue db.exceptions[:moose] => e
|
78
|
+
abort("ERROR: #{e}")
|
79
|
+
end
|
80
|
+
if warn_count == 0
|
81
|
+
puts 'Succeeded.'
|
82
|
+
else
|
83
|
+
puts 'Succeeded, with warnings.'
|
84
|
+
end
|
85
|
+
|
17
86
|
end
|
18
87
|
end
|
19
88
|
end
|
20
89
|
end
|
21
90
|
end
|
91
|
+
|
92
|
+
|
@@ -13,7 +13,8 @@ module Moose
|
|
13
13
|
def addhost(*args) # rubocop:disable Metrics/AbcSize
|
14
14
|
# Sanity
|
15
15
|
if args.length < 2
|
16
|
-
abort("ERROR: Wrong number of arguments, #{args.length}
|
16
|
+
abort("ERROR: Wrong number of arguments, #{args.length} "\
|
17
|
+
"for 2 or more.")
|
17
18
|
end
|
18
19
|
|
19
20
|
# Arguments
|
@@ -29,6 +29,8 @@ module Moose
|
|
29
29
|
unless group.nil?
|
30
30
|
hosts = group.hosts_dataset.map(:name)
|
31
31
|
|
32
|
+
children = group.children_dataset.map(:name)
|
33
|
+
|
32
34
|
groupvars = {}
|
33
35
|
group.groupvars_dataset.each do |gv|
|
34
36
|
groupvars[gv[:name].to_sym] = gv[:value]
|
@@ -38,6 +40,11 @@ module Moose
|
|
38
40
|
unless hosts.length == 0
|
39
41
|
results[group[:name].to_sym][:hosts] = hosts
|
40
42
|
end
|
43
|
+
|
44
|
+
unless children.length == 0
|
45
|
+
results[group[:name].to_sym][:children] = children
|
46
|
+
end
|
47
|
+
|
41
48
|
unless groupvars.length == 0
|
42
49
|
results[group[:name].to_sym][:groupvars] = groupvars
|
43
50
|
end
|
@@ -23,16 +23,24 @@ module Moose
|
|
23
23
|
|
24
24
|
# Hide the automatic ungrouped group, if it's empty
|
25
25
|
next if group[:name] == 'ungrouped' && hosts.length == 0
|
26
|
-
|
26
|
+
|
27
|
+
children = group.children_dataset.map(:name)
|
28
|
+
|
27
29
|
groupvars = {}
|
28
30
|
group.groupvars_dataset.each do |gv|
|
29
31
|
groupvars[gv[:name].to_sym] = gv[:value]
|
30
32
|
end
|
31
33
|
|
34
|
+
|
32
35
|
results[group[:name].to_sym] = {}
|
33
36
|
unless hosts.length == 0
|
34
37
|
results[group[:name].to_sym][:hosts] = hosts
|
35
38
|
end
|
39
|
+
|
40
|
+
unless children.length == 0
|
41
|
+
results[group[:name].to_sym][:children] = children
|
42
|
+
end
|
43
|
+
|
36
44
|
unless groupvars.length == 0
|
37
45
|
if confopts[:ansible] == true
|
38
46
|
results[group[:name].to_sym][:vars] = groupvars
|
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'thor'
|
2
|
+
|
2
3
|
require_relative './formatter.rb'
|
3
4
|
|
4
5
|
module Moose
|
@@ -8,11 +9,72 @@ module Moose
|
|
8
9
|
# Implemention of the "group rmchild" methods of the CLI
|
9
10
|
class Group < Thor # rubocop:disable ClassLength
|
10
11
|
#==========================
|
11
|
-
desc 'rmchild
|
12
|
-
|
13
|
-
def rmchild
|
14
|
-
|
15
|
-
|
12
|
+
desc 'rmchild PARENTGROUP CHILDGROUP_1 [CHILDGROUP_2 ... ]',
|
13
|
+
'Dissociate one or more child-groups CHILDGROUP_n from PARENTGROUP'
|
14
|
+
def rmchild(*argv)
|
15
|
+
|
16
|
+
# Sanity check
|
17
|
+
if args.length < 2
|
18
|
+
abort("ERROR: Wrong number of arguments, #{args.length} "\
|
19
|
+
"for 2 or more.")
|
20
|
+
end
|
21
|
+
|
22
|
+
# Arguments
|
23
|
+
pname = args[0].downcase
|
24
|
+
cnames = args.slice(1, args.length - 1).uniq.map(&:downcase)
|
25
|
+
|
26
|
+
# Sanity
|
27
|
+
if pname == 'ungrouped' || cnames.include?('ungrouped')
|
28
|
+
abort("ERROR: Cannot manually manipulate the automatic group 'ungrouped'.")
|
29
|
+
end
|
30
|
+
|
31
|
+
# Convenience
|
32
|
+
db = Moose::Inventory::DB
|
33
|
+
fmt = Moose::Inventory::Cli::Formatter
|
34
|
+
|
35
|
+
# Transaction
|
36
|
+
warn_count = 0
|
37
|
+
begin
|
38
|
+
db.transaction do # Transaction start
|
39
|
+
puts "Dissociate parent group '#{pname}' from child group(s) '#{cnames.join(',')}':"
|
40
|
+
# Get the target group
|
41
|
+
fmt.puts 2, "- retrieve group '#{pname}'..."
|
42
|
+
pgroup = db.models[:group].find(name: pname)
|
43
|
+
if pgroup.nil?
|
44
|
+
abort("ERROR: The group '#{pname}' does not exist.")
|
45
|
+
end
|
46
|
+
fmt.puts 4, "- OK"
|
47
|
+
|
48
|
+
# Dissociate parent group from the child groups
|
49
|
+
groups_ds = pgroup.children_dataset
|
50
|
+
cnames.each do |cname|
|
51
|
+
fmt.puts 2, "- remove association {group:#{pname} <-> group:#{cname}}..."
|
52
|
+
|
53
|
+
# Check against existing associations
|
54
|
+
if groups_ds[name: cname].nil?
|
55
|
+
warn_count += 1
|
56
|
+
fmt.warn "Association {group:#{pname} <-> group:#{cname}}"\
|
57
|
+
" does not exist, skipping.\n"
|
58
|
+
fmt.puts 4, "- doesn't exist, skipping."
|
59
|
+
fmt.puts 4, '- OK'
|
60
|
+
next
|
61
|
+
end
|
62
|
+
|
63
|
+
# remove association
|
64
|
+
cgroup = db.models[:group].find(name: cname)
|
65
|
+
pgroup.remove_child(cgroup)
|
66
|
+
fmt.puts 4, '- OK'
|
67
|
+
end
|
68
|
+
fmt.puts 2, '- all OK'
|
69
|
+
end # Transaction end
|
70
|
+
rescue db.exceptions[:moose] => e
|
71
|
+
abort("ERROR: #{e}")
|
72
|
+
end
|
73
|
+
if warn_count == 0
|
74
|
+
puts 'Succeeded.'
|
75
|
+
else
|
76
|
+
puts 'Succeeded, with warnings.'
|
77
|
+
end
|
16
78
|
end
|
17
79
|
end
|
18
80
|
end
|
@@ -34,9 +34,9 @@ module Moose
|
|
34
34
|
# long-running code. Personally, I don't like this pooling regime -
|
35
35
|
# perhaps I'm not understanding how it's supposed to be used?
|
36
36
|
#
|
37
|
-
#
|
37
|
+
# QUESTION: can the models be refreshed, to make then again valid? What if
|
38
38
|
# we "load" instead of "require" the models?
|
39
|
-
#
|
39
|
+
# ANSWER: Nope, still borks even if we use a load.
|
40
40
|
#
|
41
41
|
# @db = nil # <- fails for unit tests
|
42
42
|
return unless @db.nil? # <- works for unit tests
|
@@ -113,6 +113,7 @@ module Moose
|
|
113
113
|
Group.all.each do |g|
|
114
114
|
g.remove_all_hosts
|
115
115
|
g.remove_all_groupvars
|
116
|
+
g.remove_all_children
|
116
117
|
g.destroy
|
117
118
|
end
|
118
119
|
|
@@ -153,6 +154,7 @@ module Moose
|
|
153
154
|
unless @db.table_exists? :groups
|
154
155
|
@db.create_table(:groups) do
|
155
156
|
primary_key :id
|
157
|
+
foreign_key :parent_id
|
156
158
|
column :name, :text, unique: true
|
157
159
|
end
|
158
160
|
end
|
@@ -11,7 +11,8 @@ module Moose
|
|
11
11
|
##
|
12
12
|
# Model for the groups table
|
13
13
|
class Group < Sequel::Model
|
14
|
-
|
14
|
+
many_to_one :parent, :class=>self
|
15
|
+
one_to_many :children, :key=>:parent_id, :class=>self
|
15
16
|
many_to_many :hosts
|
16
17
|
one_to_many :groupvars
|
17
18
|
end
|
@@ -0,0 +1,181 @@
|
|
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::Group 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
|
+
@console = Moose::Inventory::Cli::Formatter
|
22
|
+
|
23
|
+
@config = Moose::Inventory::Config
|
24
|
+
@config.init(@mockargs)
|
25
|
+
|
26
|
+
@db = Moose::Inventory::DB
|
27
|
+
@db.init if @db.db.nil?
|
28
|
+
|
29
|
+
@group = Moose::Inventory::Cli::Group
|
30
|
+
@host = Moose::Inventory::Cli::Host
|
31
|
+
@app = Moose::Inventory::Cli::Application
|
32
|
+
end
|
33
|
+
|
34
|
+
before(:each) do
|
35
|
+
@db.reset
|
36
|
+
end
|
37
|
+
|
38
|
+
#=======================
|
39
|
+
describe 'addchild' do
|
40
|
+
#------------------------
|
41
|
+
it 'Group.addchild() should be responsive' do
|
42
|
+
result = @group.instance_methods(false).include?(:addchild)
|
43
|
+
expect(result).to eq(true)
|
44
|
+
end
|
45
|
+
|
46
|
+
#------------------------
|
47
|
+
it '<missing args> ... should abort with an error' do
|
48
|
+
actual = runner do
|
49
|
+
@app.start(%w(group addchild))
|
50
|
+
end
|
51
|
+
|
52
|
+
#@console.out(actual, 'y')
|
53
|
+
|
54
|
+
# Check output
|
55
|
+
desired = { aborted: true}
|
56
|
+
desired[:STDERR] = "ERROR: Wrong number of arguments, 0 for 2 or more.\n"
|
57
|
+
expected(actual, desired)
|
58
|
+
end
|
59
|
+
|
60
|
+
#------------------------
|
61
|
+
it 'ungrouped ... should abort with an error' do
|
62
|
+
parent_name = "ungrouped"
|
63
|
+
child_name = "fake"
|
64
|
+
|
65
|
+
actual = runner do
|
66
|
+
@app.start(%W(group addchild #{parent_name} #{child_name}))
|
67
|
+
end
|
68
|
+
|
69
|
+
#@console.out(actual, 'y')
|
70
|
+
|
71
|
+
# Check output
|
72
|
+
desired = { aborted: true}
|
73
|
+
desired[:STDERR] = "ERROR: Cannot manually manipulate the automatic group 'ungrouped'.\n"
|
74
|
+
expected(actual, desired)
|
75
|
+
|
76
|
+
############################
|
77
|
+
# Should work the other way round too, when the child in the ungrouped item
|
78
|
+
parent_name = "fake"
|
79
|
+
child_name = "ungrouped"
|
80
|
+
|
81
|
+
actual = runner do
|
82
|
+
@app.start(%W(group addchild #{parent_name} #{child_name}))
|
83
|
+
end
|
84
|
+
|
85
|
+
#@console.out(actual, 'y')
|
86
|
+
|
87
|
+
# Check output
|
88
|
+
desired = { aborted: true}
|
89
|
+
desired[:STDERR] = "ERROR: Cannot manually manipulate the automatic group 'ungrouped'.\n"
|
90
|
+
expected(actual, desired)
|
91
|
+
|
92
|
+
end
|
93
|
+
|
94
|
+
#------------------------
|
95
|
+
it 'GROUP CHILDGROUP ... should abort if GROUP does not exist' do
|
96
|
+
# TODO: Why don't we just create GROUP? Likewise for all similar functions?
|
97
|
+
|
98
|
+
pname = 'parent_group'
|
99
|
+
cname = 'child group'
|
100
|
+
|
101
|
+
actual = runner do
|
102
|
+
@app.start(%W(group addchild #{pname} #{cname}))
|
103
|
+
end
|
104
|
+
|
105
|
+
#@console.out(actual, 'y')
|
106
|
+
# Check output
|
107
|
+
desired = { aborted: true}
|
108
|
+
desired[:STDOUT] =
|
109
|
+
"Associate parent group '#{pname}' with child group(s) '#{cname}':\n"\
|
110
|
+
" - retrieve group '#{pname}'...\n"
|
111
|
+
desired[:STDERR] =
|
112
|
+
"ERROR: The group '#{pname}' does not exist.\n"\
|
113
|
+
"An error occurred during a transaction, any changes have been rolled back.\n"
|
114
|
+
expected(actual, desired)
|
115
|
+
end
|
116
|
+
|
117
|
+
#------------------------
|
118
|
+
it 'GROUP CHILDGROUP... should associate GROUP with an existing CHILDGROUP' do
|
119
|
+
pname = 'parent_group'
|
120
|
+
cname = 'child_group'
|
121
|
+
|
122
|
+
runner { @app.start(%W(group add #{pname} #{cname})) }
|
123
|
+
|
124
|
+
actual = runner { @app.start(%W(group addchild #{pname} #{cname} )) }
|
125
|
+
|
126
|
+
#@console.out(actual, 'y')
|
127
|
+
|
128
|
+
desired = { aborted: false}
|
129
|
+
desired[:STDOUT] =
|
130
|
+
"Associate parent group '#{pname}' with child group(s) '#{cname}':\n"\
|
131
|
+
" - retrieve group '#{pname}'...\n"\
|
132
|
+
" - OK\n"\
|
133
|
+
" - add association {group:#{pname} <-> group:#{cname}}...\n"\
|
134
|
+
" - OK\n"\
|
135
|
+
" - all OK\n"\
|
136
|
+
"Succeeded.\n"
|
137
|
+
expected(actual, desired)
|
138
|
+
|
139
|
+
# We should have the correct group associations
|
140
|
+
pgroup = @db.models[:group].find(name: pname)
|
141
|
+
cgroups = pgroup.children_dataset
|
142
|
+
expect(cgroups.count).to eq(1)
|
143
|
+
expect(cgroups[name: cname]).not_to be_nil
|
144
|
+
end
|
145
|
+
|
146
|
+
#------------------------
|
147
|
+
it 'GROUP CHILDGROUP... should associate GROUP with a CHILDGROUP '\
|
148
|
+
'creating it if necessary' do
|
149
|
+
#
|
150
|
+
pname = 'parent_group'
|
151
|
+
cname = 'child_group'
|
152
|
+
|
153
|
+
runner { @app.start(%W(group add #{pname})) } # <- don't pre-create the child
|
154
|
+
|
155
|
+
actual = runner { @app.start(%W(group addchild #{pname} #{cname} )) }
|
156
|
+
|
157
|
+
#@console.out(actual, 'y')
|
158
|
+
|
159
|
+
desired = { aborted: false}
|
160
|
+
desired[:STDOUT] =
|
161
|
+
"Associate parent group '#{pname}' with child group(s) '#{cname}':\n"\
|
162
|
+
" - retrieve group '#{pname}'...\n"\
|
163
|
+
" - OK\n"\
|
164
|
+
" - add association {group:#{pname} <-> group:#{cname}}...\n"\
|
165
|
+
" - child group does not exist, creating now...\n"\
|
166
|
+
" - OK\n"\
|
167
|
+
" - OK\n"\
|
168
|
+
" - all OK\n"\
|
169
|
+
"Succeeded, with warnings.\n"
|
170
|
+
desired[:STDERR] = "WARNING: Group '#{cname}' does not exist and will be created.\n"
|
171
|
+
expected(actual, desired)
|
172
|
+
|
173
|
+
# We should have the correct group associations
|
174
|
+
pgroup = @db.models[:group].find(name: pname)
|
175
|
+
cgroups = pgroup.children_dataset
|
176
|
+
expect(cgroups.count).to eq(1)
|
177
|
+
expect(cgroups[name: cname]).not_to be_nil
|
178
|
+
end
|
179
|
+
|
180
|
+
end
|
181
|
+
end
|
@@ -0,0 +1,177 @@
|
|
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::Group 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
|
+
@console = Moose::Inventory::Cli::Formatter
|
22
|
+
|
23
|
+
@config = Moose::Inventory::Config
|
24
|
+
@config.init(@mockargs)
|
25
|
+
|
26
|
+
@db = Moose::Inventory::DB
|
27
|
+
@db.init if @db.db.nil?
|
28
|
+
|
29
|
+
@group = Moose::Inventory::Cli::Group
|
30
|
+
@host = Moose::Inventory::Cli::Host
|
31
|
+
@app = Moose::Inventory::Cli::Application
|
32
|
+
end
|
33
|
+
|
34
|
+
before(:each) do
|
35
|
+
@db.reset
|
36
|
+
end
|
37
|
+
|
38
|
+
#=======================
|
39
|
+
describe 'rmchild' do
|
40
|
+
#------------------------
|
41
|
+
it 'Group.rmchild() should be responsive' do
|
42
|
+
result = @group.instance_methods(false).include?(:rmchild)
|
43
|
+
expect(result).to eq(true)
|
44
|
+
end
|
45
|
+
|
46
|
+
#------------------------
|
47
|
+
it '<missing args> ... should abort with an error' do
|
48
|
+
actual = runner do
|
49
|
+
@app.start(%w(group addchild))
|
50
|
+
end
|
51
|
+
|
52
|
+
#@console.out(actual, 'y')
|
53
|
+
|
54
|
+
# Check output
|
55
|
+
desired = { aborted: true}
|
56
|
+
desired[:STDERR] = "ERROR: Wrong number of arguments, 0 for 2 or more.\n"
|
57
|
+
expected(actual, desired)
|
58
|
+
end
|
59
|
+
|
60
|
+
#------------------------
|
61
|
+
it 'ungrouped ... should abort with an error' do
|
62
|
+
parent_name = "ungrouped"
|
63
|
+
child_name = "fake"
|
64
|
+
|
65
|
+
actual = runner do
|
66
|
+
@app.start(%W(group addchild #{parent_name} #{child_name}))
|
67
|
+
end
|
68
|
+
|
69
|
+
#@console.out(actual, 'y')
|
70
|
+
|
71
|
+
# Check output
|
72
|
+
desired = { aborted: true}
|
73
|
+
desired[:STDERR] = "ERROR: Cannot manually manipulate the automatic group 'ungrouped'.\n"
|
74
|
+
expected(actual, desired)
|
75
|
+
|
76
|
+
############################
|
77
|
+
# Should work the other way round too, when the child in the ungrouped item
|
78
|
+
parent_name = "fake"
|
79
|
+
child_name = "ungrouped"
|
80
|
+
|
81
|
+
actual = runner do
|
82
|
+
@app.start(%W(group rmchild #{parent_name} #{child_name}))
|
83
|
+
end
|
84
|
+
|
85
|
+
#@console.out(actual, 'y')
|
86
|
+
|
87
|
+
# Check output
|
88
|
+
desired = { aborted: true}
|
89
|
+
desired[:STDERR] = "ERROR: Cannot manually manipulate the automatic group 'ungrouped'.\n"
|
90
|
+
expected(actual, desired)
|
91
|
+
|
92
|
+
end
|
93
|
+
|
94
|
+
#------------------------
|
95
|
+
it 'GROUP CHILDGROUP ... should abort if GROUP does not exist' do
|
96
|
+
|
97
|
+
pname = 'parent_group'
|
98
|
+
cname = 'child group'
|
99
|
+
|
100
|
+
actual = runner do
|
101
|
+
@app.start(%W(group rmchild #{pname} #{cname}))
|
102
|
+
end
|
103
|
+
|
104
|
+
#@console.out(actual, 'y')
|
105
|
+
# Check output
|
106
|
+
desired = { aborted: true}
|
107
|
+
desired[:STDOUT] =
|
108
|
+
"Dissociate parent group '#{pname}' from child group(s) '#{cname}':\n"\
|
109
|
+
" - retrieve group '#{pname}'...\n"
|
110
|
+
desired[:STDERR] =
|
111
|
+
"ERROR: The group '#{pname}' does not exist.\n"\
|
112
|
+
"An error occurred during a transaction, any changes have been rolled back.\n"
|
113
|
+
expected(actual, desired)
|
114
|
+
end
|
115
|
+
|
116
|
+
#------------------------
|
117
|
+
it 'GROUP CHILDGROUP ... should succeed with warnings if CHILDGROUP is not associated' do
|
118
|
+
|
119
|
+
pname = 'parent_group'
|
120
|
+
cname = 'child group'
|
121
|
+
|
122
|
+
runner { @app.start(%W(group add #{pname} #{cname})) }
|
123
|
+
|
124
|
+
actual = runner do
|
125
|
+
@app.start(%W(group rmchild #{pname} #{cname}))
|
126
|
+
end
|
127
|
+
|
128
|
+
#@console.out(actual, 'y')
|
129
|
+
|
130
|
+
# Check output
|
131
|
+
desired = {}
|
132
|
+
desired[:STDOUT] =
|
133
|
+
"Dissociate parent group '#{pname}' from child group(s) '#{cname}':\n"\
|
134
|
+
" - retrieve group '#{pname}'...\n"\
|
135
|
+
" - OK\n"\
|
136
|
+
" - remove association {group:#{pname} <-> group:#{cname}}...\n"\
|
137
|
+
" - doesn't exist, skipping.\n"\
|
138
|
+
" - OK\n"\
|
139
|
+
" - all OK\n"\
|
140
|
+
"Succeeded, with warnings.\n"
|
141
|
+
|
142
|
+
desired[:STDERR] =
|
143
|
+
"WARNING: Association {group:#{pname} <-> group:#{cname}} does not exist, skipping.\n"
|
144
|
+
|
145
|
+
expected(actual, desired)
|
146
|
+
end
|
147
|
+
|
148
|
+
#------------------------
|
149
|
+
it 'GROUP CHILDGROUP ... should succeed without warnings if CHILDGROUP is associated' do
|
150
|
+
|
151
|
+
pname = 'parent_group'
|
152
|
+
cname = 'child group'
|
153
|
+
|
154
|
+
runner { @app.start(%W(group add #{pname} #{cname})) }
|
155
|
+
runner { @app.start(%W(group addchild #{pname} #{cname})) }
|
156
|
+
|
157
|
+
actual = runner do
|
158
|
+
@app.start(%W(group rmchild #{pname} #{cname}))
|
159
|
+
end
|
160
|
+
|
161
|
+
#@console.out(actual, 'y')
|
162
|
+
|
163
|
+
# Check output
|
164
|
+
desired = {}
|
165
|
+
desired[:STDOUT] =
|
166
|
+
"Dissociate parent group '#{pname}' from child group(s) '#{cname}':\n"\
|
167
|
+
" - retrieve group '#{pname}'...\n"\
|
168
|
+
" - OK\n"\
|
169
|
+
" - remove association {group:#{pname} <-> group:#{cname}}...\n"\
|
170
|
+
" - OK\n"\
|
171
|
+
" - all OK\n"\
|
172
|
+
"Succeeded.\n"
|
173
|
+
|
174
|
+
expected(actual, desired)
|
175
|
+
end
|
176
|
+
end
|
177
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: moose-inventory
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Russell Davies
|
@@ -309,12 +309,14 @@ files:
|
|
309
309
|
- spec/lib/moose_inventory/cli/cli_spec.rb
|
310
310
|
- spec/lib/moose_inventory/cli/formatter_spec.rb
|
311
311
|
- spec/lib/moose_inventory/cli/group_add_spec.rb
|
312
|
+
- spec/lib/moose_inventory/cli/group_addchild_spec.rb
|
312
313
|
- spec/lib/moose_inventory/cli/group_addhost_spec.rb
|
313
314
|
- spec/lib/moose_inventory/cli/group_addvar_spec.rb
|
314
315
|
- spec/lib/moose_inventory/cli/group_get_spec.rb
|
315
316
|
- spec/lib/moose_inventory/cli/group_list_spec.rb
|
316
317
|
- spec/lib/moose_inventory/cli/group_listvar_spec.rb
|
317
318
|
- spec/lib/moose_inventory/cli/group_rm_spec.rb
|
319
|
+
- spec/lib/moose_inventory/cli/group_rmchild_spec.rb
|
318
320
|
- spec/lib/moose_inventory/cli/group_rmhost_spec.rb
|
319
321
|
- spec/lib/moose_inventory/cli/group_rmvar_spec.rb
|
320
322
|
- spec/lib/moose_inventory/cli/group_spec.rb
|
@@ -363,12 +365,14 @@ test_files:
|
|
363
365
|
- spec/lib/moose_inventory/cli/cli_spec.rb
|
364
366
|
- spec/lib/moose_inventory/cli/formatter_spec.rb
|
365
367
|
- spec/lib/moose_inventory/cli/group_add_spec.rb
|
368
|
+
- spec/lib/moose_inventory/cli/group_addchild_spec.rb
|
366
369
|
- spec/lib/moose_inventory/cli/group_addhost_spec.rb
|
367
370
|
- spec/lib/moose_inventory/cli/group_addvar_spec.rb
|
368
371
|
- spec/lib/moose_inventory/cli/group_get_spec.rb
|
369
372
|
- spec/lib/moose_inventory/cli/group_list_spec.rb
|
370
373
|
- spec/lib/moose_inventory/cli/group_listvar_spec.rb
|
371
374
|
- spec/lib/moose_inventory/cli/group_rm_spec.rb
|
375
|
+
- spec/lib/moose_inventory/cli/group_rmchild_spec.rb
|
372
376
|
- spec/lib/moose_inventory/cli/group_rmhost_spec.rb
|
373
377
|
- spec/lib/moose_inventory/cli/group_rmvar_spec.rb
|
374
378
|
- spec/lib/moose_inventory/cli/group_spec.rb
|