rouster 0.42 → 0.53

Sign up to get free protection for your applications and to get access to all the features.
data/lib/rouster/tests.rb CHANGED
@@ -67,12 +67,11 @@ class Rouster
67
67
  # * <dir> - path to directory to act on, full path or relative to ~vagrant/
68
68
  # * [wildcard] - glob of directories to match, defaults to '*'
69
69
  # * [recursive] - boolean controlling whether or not to look in directories recursively, defaults to false
70
- def dirs(dir, wildcard='*', recursive=false)
70
+ def dirs(dir, wildcard='*', insensitive=true, recursive=false)
71
71
  # TODO use a numerical, not boolean value for 'recursive' -- and rename to 'depth' ?
72
- # TODO should we be running -iname ?
73
72
  raise InternalError.new(sprintf('invalid dir specified[%s]', dir)) unless self.is_dir?(dir)
74
73
 
75
- raw = self.run(sprintf("find %s %s -type d -name '%s'", dir, recursive ? '' : '-maxdepth 1', wildcard))
74
+ raw = self.run(sprintf("find %s %s -type d %s '%s'", dir, recursive ? '' : '-maxdepth 1', insensitive ? '-iname' : '-name', wildcard))
76
75
  res = Array.new
77
76
 
78
77
  raw.split("\n").each do |line|
@@ -145,11 +144,11 @@ class Rouster
145
144
  # * <dir> - directory to look in, full path or relative to ~vagrant/
146
145
  # * [wildcard] - glob of files to match, defaults to '*'
147
146
  # * [recursive] - boolean controlling whether or not to look in directories recursively, defaults to false
148
- def files(dir, wildcard='*', recursive=false)
147
+ def files(dir, wildcard='*', insensitive=true, recursive=false)
149
148
  # TODO use a numerical, not boolean value for 'recursive'
150
149
  raise InternalError.new(sprintf('invalid dir specified[%s]', dir)) unless self.is_dir?(dir)
151
150
 
152
- raw = self.run(sprintf("find %s %s -type f -name '%s'", dir, recursive ? '' : '-maxdepth 1', wildcard))
151
+ raw = self.run(sprintf("find %s %s -type f %s '%s'", dir, recursive ? '' : '-maxdepth 1', insensitive ? '-iname' : '-name', wildcard))
153
152
  res = Array.new
154
153
 
155
154
  raw.split("\n").each do |line|
@@ -167,7 +166,13 @@ class Rouster
167
166
  # parameters
168
167
  # * <dir> - path of directory to validate
169
168
  def is_dir?(dir)
170
- res = self.dir(dir)
169
+ res = nil
170
+ begin
171
+ res = self.dir(dir)
172
+ rescue => e
173
+ return false
174
+ end
175
+
171
176
  res.class.eql?(Hash) ? res[:directory?] : false
172
177
  end
173
178
 
@@ -221,7 +226,14 @@ class Rouster
221
226
  # parameters
222
227
  # * <file> - path of filename to validate
223
228
  def is_file?(file)
224
- res = self.file(file)
229
+ res = nil
230
+
231
+ begin
232
+ res = self.file(file)
233
+ rescue => e
234
+ return false
235
+ end
236
+
225
237
  res.class.eql?(Hash) ? res[:file?] : false
226
238
  end
227
239
 
@@ -368,7 +380,7 @@ class Rouster
368
380
  os = self.os_type()
369
381
 
370
382
  case os
371
- when :redhat, :osx, :ubuntu
383
+ when :redhat, :osx, :ubuntu, :debian
372
384
  res = self.run(sprintf('ps ax | grep -c %s', name))
373
385
  else
374
386
  raise InternalError.new(sprintf('currently unable to determine running process list on OS[%s]', os))
@@ -552,8 +564,8 @@ class Rouster
552
564
  value += 4
553
565
  when 'w'
554
566
  value += 2
555
- when 'x', 't'
556
- # is 't' really right here? copying Salesforce::Vagrant
567
+ when 'x', 't', 's'
568
+ # is 't' / 's' really right here? copying Salesforce::Vagrant
557
569
  value += 1
558
570
  when '-'
559
571
  # noop
@@ -581,4 +593,4 @@ class Rouster
581
593
  res
582
594
  end
583
595
 
584
- end
596
+ end
data/path_helper.rb CHANGED
@@ -1,17 +1,16 @@
1
1
  # heavily influenced by https://github.com/puppetlabs/hiera/blob/master/spec/spec_helper.rb
2
2
 
3
3
  # this gets us Rouster, still need to figure out how to find vagrant
4
- $LOAD_PATH << File.join([File.dirname(__FILE__), "lib"])
4
+ $LOAD_PATH << File.join([File.dirname(__FILE__), 'lib'])
5
5
 
6
6
  require 'rubygems'
7
7
 
8
- ## this is really optional, so don't die if we don't have it
8
+ # debugging help
9
9
  begin
10
- require 'ruby-debug'
10
+ require 'debugger'
11
11
  rescue LoadError
12
12
  end
13
13
 
14
- # debugging help
15
14
  class Object
16
15
  def my_methods
17
16
  # Cookbook implementation
data/rouster.gemspec CHANGED
@@ -23,7 +23,8 @@ Gem::Specification.new do |s|
23
23
  s.add_dependency 'net-ssh'
24
24
  s.add_dependency 'rake'
25
25
 
26
- s.add_development_dependency 'test-unit'
26
+ s.add_development_dependency 'test-unit', '~> 2.0'
27
+ s.add_development_dependency 'reek'
27
28
 
28
29
  s.files = `git ls-files`.split("\n")
29
30
  end
@@ -8,7 +8,7 @@ class TestDeltasGetCrontab < Test::Unit::TestCase
8
8
 
9
9
  def setup
10
10
  assert_nothing_raised do
11
- @app = Rouster.new(:name => 'app')
11
+ @app = Rouster.new(:name => 'app', :cache_timeout => 20)
12
12
  end
13
13
 
14
14
  @app.up()
@@ -92,6 +92,29 @@ class TestDeltasGetCrontab < Test::Unit::TestCase
92
92
 
93
93
  end
94
94
 
95
+ def test_happy_path_cache_invalidated
96
+
97
+ res1, res2 = nil, nil
98
+
99
+ assert_nothing_raised do
100
+ res1 = @app.get_crontab('root', true)
101
+ end
102
+
103
+ first_cache_time = @app.cache[:crontab]
104
+
105
+ sleep (@app.cache_timeout + 1)
106
+
107
+ assert_nothing_raised do
108
+ res2 = @app.get_crontab('root', true)
109
+ end
110
+
111
+ second_cache_time = @app.cache[:crontab]
112
+
113
+ assert_equal(res1, res2)
114
+ assert_not_equal(first_cache_time, second_cache_time)
115
+
116
+ end
117
+
95
118
  def teardown
96
119
  @app = nil
97
120
  end
@@ -8,7 +8,7 @@ class TestDeltasGetGroups < Test::Unit::TestCase
8
8
 
9
9
  def setup
10
10
  assert_nothing_raised do
11
- @app = Rouster.new(:name => 'app')
11
+ @app = Rouster.new(:name => 'app', :cache_timeout => 10)
12
12
  end
13
13
 
14
14
  @app.up()
@@ -39,7 +39,79 @@ class TestDeltasGetGroups < Test::Unit::TestCase
39
39
 
40
40
  end
41
41
 
42
- # TODO add some non-caching tests
42
+ def test_without_caching
43
+ old_groups, new_groups = nil, nil
44
+
45
+ assert_nothing_raised do
46
+ old_groups = @app.get_groups(false)
47
+ end
48
+
49
+ assert_nil(@app.deltas[:groups])
50
+
51
+ new_group = sprintf('rouster-%s', Time.now.to_i)
52
+
53
+ ## create a group here
54
+ if @app.os_type.eql?(:redhat)
55
+ @app.run(sprintf('groupadd %s', new_group))
56
+ else
57
+ skip('only doing group creation on RHEL hosts')
58
+ end
59
+
60
+ assert_nothing_raised do
61
+ new_groups = @app.get_groups(false)
62
+ end
63
+
64
+ assert_nil(@app.deltas[:groups])
65
+ assert_not_nil(new_groups[new_group])
66
+ assert_not_equal(old_groups, new_groups)
67
+
68
+ end
69
+
70
+ def test_deep_inspection
71
+ deep, shallow = nil, nil
72
+
73
+ assert_nothing_raised do
74
+ deep = @app.get_groups(false, true)
75
+ shallow = @app.get_groups(false, false)
76
+ end
77
+
78
+ assert_not_equal(deep, shallow)
79
+
80
+ ## this is not really the best test
81
+ deep_none, shallow_none = 0, 0
82
+
83
+ deep.each_key do |group|
84
+ deep_none += 1 if deep[group][:users][0].eql?('NONE')
85
+ end
86
+
87
+ shallow.each_key do |group|
88
+ shallow_none += 1 if shallow[group][:users][0].eql?('NONE')
89
+ end
90
+
91
+ assert(shallow_none > deep_none)
92
+ end
93
+
94
+ def test_happy_path_cache_invalidation
95
+ res1, res2 = nil, nil
96
+
97
+ assert_nothing_raised do
98
+ res1 = @app.get_groups(true, false)
99
+ end
100
+
101
+ first_cache_time = @app.cache[:groups]
102
+
103
+ sleep (@app.cache_timeout + 1)
104
+
105
+ assert_nothing_raised do
106
+ res2 = @app.get_groups(true, false)
107
+ end
108
+
109
+ second_cache_time = @app.cache[:groups]
110
+
111
+ assert_equal(res1, res2)
112
+ assert_not_equal(first_cache_time, second_cache_time)
113
+
114
+ end
43
115
 
44
116
  def teardown
45
117
  @app = nil
@@ -4,11 +4,13 @@ require 'rouster'
4
4
  require 'rouster/deltas'
5
5
  require 'test/unit'
6
6
 
7
+ # TODO need to figure out how to add package strings on our own for better testing (i.e. sfdc-razorpolicy-rhel-6.2-batch-1.0-17.noarch)
8
+
7
9
  class TestDeltasGetPackages < Test::Unit::TestCase
8
10
 
9
11
  def setup
10
12
  assert_nothing_raised do
11
- @app = Rouster.new(:name => 'app')
13
+ @app = Rouster.new(:name => 'app', :cache_timeout => 10)
12
14
  end
13
15
 
14
16
  @app.up()
@@ -28,10 +30,7 @@ class TestDeltasGetPackages < Test::Unit::TestCase
28
30
 
29
31
  res.each_key do |k|
30
32
  assert_not_nil(res[k])
31
-
32
- # this is not the best validation, but is not the worst either
33
- assert_match(/^\d+\./, res[k]) # start with a number
34
- assert_match(/\.(x86|i686|x86_64|noarch)$/, res[k]) # end with an arch type
33
+ assert_match(/^\d+/, res[k]) # start with a number
35
34
  end
36
35
 
37
36
  end
@@ -49,6 +48,45 @@ class TestDeltasGetPackages < Test::Unit::TestCase
49
48
  assert_equal(false, @app.deltas.has_key?(:packages))
50
49
  end
51
50
 
51
+ def test_without_deep_inspection
52
+ res = nil
53
+
54
+ assert_nothing_raised do
55
+ res = @app.get_packages(true, false)
56
+ end
57
+
58
+ res.each_key do |k|
59
+ assert_not_nil(res[k])
60
+
61
+ #assert_match(/\d*\..*/, res[k]) # testing the regular expression used in deltas.rb itself
62
+ assert_match(/\?/, res[k])
63
+ end
64
+
65
+ end
66
+
67
+ def test_happy_path_cache_invalidation
68
+ res1, res2 = nil, nil
69
+
70
+ assert_nothing_raised do
71
+ res1 = @app.get_packages(true, false)
72
+ end
73
+
74
+ first_cache_time = @app.cache[:packages]
75
+
76
+ sleep (@app.cache_timeout + 1)
77
+
78
+ assert_nothing_raised do
79
+ res2 = @app.get_packages(true, false)
80
+ end
81
+
82
+ second_cache_time = @app.cache[:packages]
83
+
84
+ assert_equal(res1, res2)
85
+ assert_not_equal(first_cache_time, second_cache_time)
86
+ assert(second_cache_time > first_cache_time)
87
+
88
+ end
89
+
52
90
  def teardown
53
91
  @app = nil
54
92
  end
@@ -8,7 +8,7 @@ class TestDeltasGetPorts < Test::Unit::TestCase
8
8
 
9
9
  def setup
10
10
  assert_nothing_raised do
11
- @app = Rouster.new(:name => 'app')
11
+ @app = Rouster.new(:name => 'app', :cache_timeout => 10)
12
12
  end
13
13
 
14
14
  @app.up()
@@ -95,6 +95,7 @@ class TestDeltasGetPorts < Test::Unit::TestCase
95
95
  }
96
96
 
97
97
  @app.deltas[:ports] = stock
98
+ @app.cache[:ports] = Time.now.to_i # since we're faking the contents, we also need to fake other artifacts that would have been generated
98
99
 
99
100
  assert_equal(true, @app.is_port_open?(1234, 'tcp', true))
100
101
  assert_equal(true, @app.is_port_active?(22, 'tcp', true))
@@ -107,11 +108,35 @@ class TestDeltasGetPorts < Test::Unit::TestCase
107
108
  assert_equal(false, @app.is_port_active?(1234, 'tcp', true))
108
109
 
109
110
  # caching/argument default validation -- can't currently do this, don't know what ports will be open on others systems
111
+ # TODO but can fix this by running some ncatish commands
110
112
  #assert_equal(true, @app.is_port_active?(22))
111
113
  #assert_equal(true, @app.is_port_open?(1234))
112
114
 
113
115
  end
114
116
 
117
+ def test_happy_path_cache_invalidation
118
+ res1, res2 = nil, nil
119
+
120
+ assert_nothing_raised do
121
+ res1 = @app.get_ports(true)
122
+ end
123
+
124
+ first_cache_time = @app.cache[:ports]
125
+
126
+ sleep (@app.cache_timeout + 1)
127
+
128
+ assert_nothing_raised do
129
+ res2 = @app.get_ports(true)
130
+ end
131
+
132
+ second_cache_time = @app.cache[:ports]
133
+
134
+ assert_equal(res1, res2)
135
+ assert_not_equal(first_cache_time, second_cache_time)
136
+ assert(second_cache_time > first_cache_time)
137
+
138
+ end
139
+
115
140
  def teardown
116
141
  @app = nil
117
142
  end
@@ -8,13 +8,12 @@ class TestDeltasGetServices < Test::Unit::TestCase
8
8
 
9
9
  def setup
10
10
  assert_nothing_raised do
11
- @app = Rouster.new(:name => 'app')
11
+ @app = Rouster.new(:name => 'app', :cache_timeout => 10)
12
12
  end
13
13
 
14
14
  @app.up()
15
15
 
16
- # eventually this should be standardized (and symbolized?)
17
- @allowed_states = %w(exists operational running stopped)
16
+ @allowed_states = %w(exists installed operational running stopped unsure)
18
17
  end
19
18
 
20
19
  def test_happy_path
@@ -29,11 +28,45 @@ class TestDeltasGetServices < Test::Unit::TestCase
29
28
 
30
29
  res.each_key do |k|
31
30
  assert_not_nil(res[k])
31
+ assert(@allowed_states.member?(res[k]))
32
32
  end
33
33
 
34
34
  end
35
35
 
36
- # TODO add some caching tests
36
+ def test_happy_path_caching
37
+
38
+ assert_nil(@app.deltas[:services])
39
+
40
+ assert_nothing_raised do
41
+ @app.get_services(true)
42
+ end
43
+
44
+ assert_equal(Hash, @app.deltas[:services].class)
45
+
46
+ end
47
+
48
+ def test_happy_path_cache_invalidation
49
+ res1, res2 = nil, nil
50
+
51
+ assert_nothing_raised do
52
+ res1 = @app.get_services(true)
53
+ end
54
+
55
+ first_cache_time = @app.cache[:services]
56
+
57
+ sleep (@app.cache_timeout + 1)
58
+
59
+ assert_nothing_raised do
60
+ res2 = @app.get_services(true)
61
+ end
62
+
63
+ second_cache_time = @app.cache[:services]
64
+
65
+ assert_equal(res1, res2)
66
+ assert_not_equal(first_cache_time, second_cache_time)
67
+ assert(second_cache_time > first_cache_time)
68
+
69
+ end
37
70
 
38
71
  def teardown
39
72
  @app = nil
@@ -8,7 +8,7 @@ class TestDeltasGetUsers < Test::Unit::TestCase
8
8
 
9
9
  def setup
10
10
  assert_nothing_raised do
11
- @app = Rouster.new(:name => 'app')
11
+ @app = Rouster.new(:name => 'app', :cache_timeout => 10)
12
12
  end
13
13
 
14
14
  @app.up()
@@ -36,7 +36,40 @@ class TestDeltasGetUsers < Test::Unit::TestCase
36
36
 
37
37
  end
38
38
 
39
- # TODO add some caching tests
39
+ def test_happy_path_caching
40
+
41
+ assert_nil(@app.deltas[:users])
42
+
43
+ assert_nothing_raised do
44
+ @app.get_users(true)
45
+ end
46
+
47
+ assert_equal(Hash, @app.deltas[:users].class)
48
+
49
+ end
50
+
51
+ def test_happy_path_cache_invalidation
52
+ res1, res2 = nil, nil
53
+
54
+ assert_nothing_raised do
55
+ res1 = @app.get_users(true)
56
+ end
57
+
58
+ first_cache_time = @app.cache[:users]
59
+
60
+ sleep (@app.cache_timeout + 1)
61
+
62
+ assert_nothing_raised do
63
+ res2 = @app.get_users(true)
64
+ end
65
+
66
+ second_cache_time = @app.cache[:users]
67
+
68
+ assert_equal(res1, res2)
69
+ assert_not_equal(first_cache_time, second_cache_time)
70
+ assert(second_cache_time > first_cache_time)
71
+
72
+ end
40
73
 
41
74
  def teardown
42
75
  @app = nil