chef-sugar 1.0.0.beta.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ require 'rspec'
2
+ require 'chefspec'
3
+ require 'chef/sugar'
4
+
5
+ require_relative 'support/shared_examples'
@@ -0,0 +1,20 @@
1
+ module RSpec
2
+ shared_examples 'a chef sugar' do
3
+ it 'acts as a singleton' do
4
+ described_class.module_eval("def foo; 'result'; end")
5
+ klass = Class.new.tap { |k| k.send(:include, described_class) }
6
+ expect(described_class.foo).to eq(klass.new.foo)
7
+ end
8
+
9
+ described_class.instance_methods.each do |name|
10
+ it "defines a `#{name}` DSL method" do
11
+ expect(Chef::Sugar::DSL.instance_methods).to include(name)
12
+ end
13
+
14
+ it 'has n-1 arity from the parent method' do
15
+ method = Chef::Sugar::DSL.instance_method(name)
16
+ expect(method.arity).to eq(described_class.method(name).arity - 1)
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,29 @@
1
+ require 'spec_helper'
2
+
3
+ describe Chef::Sugar::Architecture do
4
+ it_behaves_like 'a chef sugar'
5
+
6
+ describe '#_64_bit?' do
7
+ it 'returns true when the system is 64 bit' do
8
+ node = { 'kernel' => { 'machine' => 'x86_64' } }
9
+ expect(described_class._64_bit?(node)).to be_true
10
+ end
11
+
12
+ it 'returns false when the system is not 64 bit' do
13
+ node = { 'kernel' => { 'machine' => 'i386' } }
14
+ expect(described_class._64_bit?(node)).to be_false
15
+ end
16
+ end
17
+
18
+ describe '#_32_bit?' do
19
+ it 'returns true when the system is 32 bit' do
20
+ node = { 'kernel' => { 'machine' => 'i386' } }
21
+ expect(described_class._32_bit?(node)).to be_true
22
+ end
23
+
24
+ it 'returns false when the system is not 32 bit' do
25
+ node = { 'kernel' => { 'machine' => 'x86_64' } }
26
+ expect(described_class._32_bit?(node)).to be_false
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,113 @@
1
+ require 'spec_helper'
2
+
3
+ describe Chef::Sugar::Cloud do
4
+ it_behaves_like 'a chef sugar'
5
+
6
+ describe '#cloud?' do
7
+ it 'is true when the node is on cloud' do
8
+ node = { 'cloud' => nil }
9
+ expect(described_class.cloud?(node)).to be_true
10
+ end
11
+
12
+ it 'is false when the node is not on cloud' do
13
+ node = {}
14
+ expect(described_class.cloud?(node)).to be_false
15
+ end
16
+ end
17
+
18
+ describe '#ec2?' do
19
+ it 'is true when the node is on ec2' do
20
+ node = { 'ec2' => nil }
21
+ expect(described_class.ec2?(node)).to be_true
22
+ end
23
+
24
+ it 'is false when the node is not on ec2' do
25
+ node = {}
26
+ expect(described_class.ec2?(node)).to be_false
27
+ end
28
+ end
29
+
30
+ describe '#gce?' do
31
+ it 'is true when the node is on gce' do
32
+ node = { 'gce' => nil }
33
+ expect(described_class.gce?(node)).to be_true
34
+ end
35
+
36
+ it 'is false when the node is not on gce' do
37
+ node = {}
38
+ expect(described_class.gce?(node)).to be_false
39
+ end
40
+ end
41
+
42
+ describe '#rackspace?' do
43
+ it 'is true when the node is on rackspace' do
44
+ node = { 'rackspace' => nil }
45
+ expect(described_class.rackspace?(node)).to be_true
46
+ end
47
+
48
+ it 'is false when the node is not on rackspace' do
49
+ node = {}
50
+ expect(described_class.rackspace?(node)).to be_false
51
+ end
52
+ end
53
+
54
+ describe '#eucalyptus?' do
55
+ it 'is true when the node is on eucalyptus' do
56
+ node = { 'eucalyptus' => nil }
57
+ expect(described_class.eucalyptus?(node)).to be_true
58
+ end
59
+
60
+ it 'is false when the node is not on eucalyptus' do
61
+ node = {}
62
+ expect(described_class.eucalyptus?(node)).to be_false
63
+ end
64
+ end
65
+
66
+ describe '#euca?' do
67
+ it 'is true when the node is on eucalyptus' do
68
+ node = { 'eucalyptus' => nil }
69
+ expect(described_class.euca?(node)).to be_true
70
+ end
71
+
72
+ it 'is false when the node is not on eucalyptus' do
73
+ node = {}
74
+ expect(described_class.euca?(node)).to be_false
75
+ end
76
+ end
77
+
78
+ describe '#linode?' do
79
+ it 'is true when the node is on linode' do
80
+ node = { 'linode' => nil }
81
+ expect(described_class.linode?(node)).to be_true
82
+ end
83
+
84
+ it 'is false when the node is not on linode' do
85
+ node = {}
86
+ expect(described_class.linode?(node)).to be_false
87
+ end
88
+ end
89
+
90
+ describe '#openstack?' do
91
+ it 'is true when the node is on openstack' do
92
+ node = { 'openstack' => nil }
93
+ expect(described_class.openstack?(node)).to be_true
94
+ end
95
+
96
+ it 'is false when the node is not on openstack' do
97
+ node = {}
98
+ expect(described_class.openstack?(node)).to be_false
99
+ end
100
+ end
101
+
102
+ describe '#azure?' do
103
+ it 'is true when the node is on azure' do
104
+ node = { 'azure' => nil }
105
+ expect(described_class.azure?(node)).to be_true
106
+ end
107
+
108
+ it 'is false when the node is not on azure' do
109
+ node = {}
110
+ expect(described_class.azure?(node)).to be_false
111
+ end
112
+ end
113
+ end
@@ -0,0 +1,53 @@
1
+ require 'spec_helper'
2
+
3
+ describe Chef::Sugar::IP do
4
+ it_behaves_like 'a chef sugar'
5
+
6
+ let(:node) { { 'ipaddress' => '1.2.3.4' } }
7
+ let(:other) { { 'ipaddress' => '5.6.7.8' } }
8
+
9
+ context 'when not on a cloud' do
10
+ it 'returns the default IP address' do
11
+ expect(described_class.best_ip_for(node, other)).to eq(other['ipaddress'])
12
+ end
13
+ end
14
+
15
+ context 'when the target is on the cloud' do
16
+ before do
17
+ other['cloud'] = {}
18
+ other['cloud']['provider'] = 'ec2'
19
+ other['cloud']['local_ipv4'] = '9.10.11.12'
20
+ other['cloud']['public_ipv4'] = '13.14.15.16'
21
+
22
+ node['cloud'] = nil
23
+ end
24
+
25
+ context 'when the current node is not on the cloud' do
26
+ it 'uses the public ipv4' do
27
+ expect(described_class.best_ip_for(node, other)).to eq('13.14.15.16')
28
+ end
29
+ end
30
+
31
+ context 'when the current node is on a different cloud' do
32
+ before do
33
+ node['cloud'] = {}
34
+ node['cloud']['provider'] = 'rackspace'
35
+ end
36
+
37
+ it 'uses the public ipv4' do
38
+ expect(described_class.best_ip_for(node, other)).to eq('13.14.15.16')
39
+ end
40
+ end
41
+
42
+ context 'when the current node is on the same cloud' do
43
+ before do
44
+ node['cloud'] = {}
45
+ node['cloud']['provider'] = 'ec2'
46
+ end
47
+
48
+ it 'uses the local ipv4' do
49
+ expect(described_class.best_ip_for(node, other)).to eq('9.10.11.12')
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,30 @@
1
+ require 'spec_helper'
2
+
3
+ describe Chef::Node do
4
+ describe '#in?' do
5
+ it 'returns true when the node is in the environment' do
6
+ subject.stub(:chef_environment).and_return('production')
7
+ expect(subject.in?('production')).to be_true
8
+ expect(subject.in?(/production$/)).to be_true
9
+ end
10
+
11
+ it 'returns false when the node is not in the environment' do
12
+ subject.stub(:chef_environment).and_return('staging')
13
+ expect(subject.in?('production')).to be_false
14
+ expect(subject.in?(/production$/)).to be_false
15
+ end
16
+ end
17
+
18
+ describe '#includes_recipe?' do
19
+ it 'returns true when the recipe exists' do
20
+ subject.stub(:run_list).and_return(['recipe[magic::recipe]'])
21
+ expect(subject.includes_recipe?('recipe[magic::recipe]')).to be_true
22
+ end
23
+
24
+ it 'returns false when the recipe does
25
+ not exist' do
26
+ subject.stub(:run_list).and_return([])
27
+ expect(subject.includes_recipe?('recipe[magic::recipe]')).to be_false
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,137 @@
1
+ require 'spec_helper'
2
+
3
+ describe Chef::Sugar::PlatformFamily do
4
+ it_behaves_like 'a chef sugar'
5
+
6
+ describe '#arch_linux?' do
7
+ it 'returns true when the platform_family is arch linux' do
8
+ node = { 'platform_family' => 'arch' }
9
+ expect(described_class.arch_linux?(node)).to be_true
10
+ end
11
+
12
+ it 'returns false when the platform_family is not arch linux' do
13
+ node = { 'platform_family' => 'windows' }
14
+ expect(described_class.arch_linux?(node)).to be_false
15
+ end
16
+ end
17
+
18
+ describe '#debian?' do
19
+ it 'returns true when the platform_family is debian' do
20
+ node = { 'platform_family' => 'debian' }
21
+ expect(described_class.debian?(node)).to be_true
22
+ end
23
+
24
+ it 'returns false when the platform_family is not debian' do
25
+ node = { 'platform_family' => 'windows' }
26
+ expect(described_class.debian?(node)).to be_false
27
+ end
28
+ end
29
+
30
+ describe '#fedora?' do
31
+ it 'returns true when the platform_family is fedora' do
32
+ node = { 'platform_family' => 'fedora' }
33
+ expect(described_class.fedora?(node)).to be_true
34
+ end
35
+
36
+ it 'returns false when the platform_family is not fedora' do
37
+ node = { 'platform_family' => 'windows' }
38
+ expect(described_class.fedora?(node)).to be_false
39
+ end
40
+ end
41
+
42
+ describe '#gentoo?' do
43
+ it 'returns true when the platform_family is gentoo' do
44
+ node = { 'platform_family' => 'gentoo' }
45
+ expect(described_class.gentoo?(node)).to be_true
46
+ end
47
+
48
+ it 'returns false when the platform_family is not gentoo' do
49
+ node = { 'platform_family' => 'windows' }
50
+ expect(described_class.gentoo?(node)).to be_false
51
+ end
52
+ end
53
+
54
+ describe '#mac_os_x?' do
55
+ it 'returns true when the platform_family is mac_os_x' do
56
+ node = { 'platform_family' => 'mac_os_x' }
57
+ expect(described_class.mac_os_x?(node)).to be_true
58
+ end
59
+
60
+ it 'returns false when the platform_family is not mac_os_x' do
61
+ node = { 'platform_family' => 'windows' }
62
+ expect(described_class.mac_os_x?(node)).to be_false
63
+ end
64
+ end
65
+
66
+ describe '#openbsd?' do
67
+ it 'returns true when the platform_family is openbsd' do
68
+ node = { 'platform_family' => 'openbsd' }
69
+ expect(described_class.openbsd?(node)).to be_true
70
+ end
71
+
72
+ it 'returns false when the platform_family is not openbsd' do
73
+ node = { 'platform_family' => 'windows' }
74
+ expect(described_class.openbsd?(node)).to be_false
75
+ end
76
+ end
77
+
78
+ describe '#rhel?' do
79
+ it 'returns true when the platform_family is rhel' do
80
+ node = { 'platform_family' => 'rhel' }
81
+ expect(described_class.rhel?(node)).to be_true
82
+ end
83
+
84
+ it 'returns false when the platform_family is not rhel' do
85
+ node = { 'platform_family' => 'windows' }
86
+ expect(described_class.rhel?(node)).to be_false
87
+ end
88
+ end
89
+
90
+ describe '#slackware?' do
91
+ it 'returns true when the platform_family is slackware' do
92
+ node = { 'platform_family' => 'slackware' }
93
+ expect(described_class.slackware?(node)).to be_true
94
+ end
95
+
96
+ it 'returns false when the platform_family is not slackware' do
97
+ node = { 'platform_family' => 'windows' }
98
+ expect(described_class.slackware?(node)).to be_false
99
+ end
100
+ end
101
+
102
+ describe '#suse?' do
103
+ it 'returns true when the platform_family is suse' do
104
+ node = { 'platform_family' => 'suse' }
105
+ expect(described_class.suse?(node)).to be_true
106
+ end
107
+
108
+ it 'returns false when the platform_family is not suse' do
109
+ node = { 'platform_family' => 'windows' }
110
+ expect(described_class.suse?(node)).to be_false
111
+ end
112
+ end
113
+
114
+ describe '#windows?' do
115
+ it 'returns true when the platform_family is windows' do
116
+ node = { 'platform_family' => 'windows' }
117
+ expect(described_class.windows?(node)).to be_true
118
+ end
119
+
120
+ it 'returns false when the platform_family is not windows' do
121
+ node = { 'platform_family' => 'debian' }
122
+ expect(described_class.windows?(node)).to be_false
123
+ end
124
+ end
125
+
126
+ describe '#linux?' do
127
+ it 'returns true when the platform_family is linux-based' do
128
+ node = { 'platform_family' => 'debian' }
129
+ expect(described_class.debian?(node)).to be_true
130
+ end
131
+
132
+ it 'returns false when the platform_family is not linux-based' do
133
+ node = { 'platform_family' => 'windows' }
134
+ expect(described_class.debian?(node)).to be_false
135
+ end
136
+ end
137
+ end
@@ -0,0 +1,201 @@
1
+ require 'spec_helper'
2
+
3
+ describe Chef::Sugar::Platform do
4
+ it_behaves_like 'a chef sugar'
5
+
6
+ describe '#linux_mint?' do
7
+ it 'returns true when the platform is linux mint' do
8
+ node = { 'platform' => 'linuxmint' }
9
+ expect(described_class.linux_mint?(node)).to be_true
10
+ end
11
+
12
+ it 'returns false when the platform is not linux mint' do
13
+ node = { 'platform' => 'windows' }
14
+ expect(described_class.linux_mint?(node)).to be_false
15
+ end
16
+ end
17
+
18
+ describe '#ubuntu?' do
19
+ it 'returns true when the platform is ubuntu' do
20
+ node = { 'platform' => 'ubuntu' }
21
+ expect(described_class.ubuntu?(node)).to be_true
22
+ end
23
+
24
+ it 'returns false when the platform is not ubuntu' do
25
+ node = { 'platform' => 'windows' }
26
+ expect(described_class.ubuntu?(node)).to be_false
27
+ end
28
+ end
29
+
30
+ describe '#amazon_linux?' do
31
+ it 'returns true when the platform is amazon linux' do
32
+ node = { 'platform' => 'amazon' }
33
+ expect(described_class.amazon_linux?(node)).to be_true
34
+ end
35
+
36
+ it 'returns false when the platform is not amazon linux' do
37
+ node = { 'platform' => 'windows' }
38
+ expect(described_class.amazon_linux?(node)).to be_false
39
+ end
40
+ end
41
+
42
+ describe '#centos?' do
43
+ it 'returns true when the platform is centos' do
44
+ node = { 'platform' => 'centos' }
45
+ expect(described_class.centos?(node)).to be_true
46
+ end
47
+
48
+ it 'returns false when the platform is not centos' do
49
+ node = { 'platform' => 'windows' }
50
+ expect(described_class.centos?(node)).to be_false
51
+ end
52
+ end
53
+
54
+ describe '#oracle_linux?' do
55
+ it 'returns true when the platform is oracle linux' do
56
+ node = { 'platform' => 'oracle' }
57
+ expect(described_class.oracle_linux?(node)).to be_true
58
+ end
59
+
60
+ it 'returns false when the platform is not oracle linux' do
61
+ node = { 'platform' => 'windows' }
62
+ expect(described_class.oracle_linux?(node)).to be_false
63
+ end
64
+ end
65
+
66
+ describe '#scientific_linux?' do
67
+ it 'returns true when the platform is scientific linux' do
68
+ node = { 'platform' => 'scientific' }
69
+ expect(described_class.scientific_linux?(node)).to be_true
70
+ end
71
+
72
+ it 'returns false when the platform is not scientific linux' do
73
+ node = { 'platform' => 'windows' }
74
+ expect(described_class.scientific_linux?(node)).to be_false
75
+ end
76
+ end
77
+
78
+ describe '#redhat_enterprise_linux?' do
79
+ it 'returns true when the platform is redhat enterprise linux' do
80
+ node = { 'platform' => 'enterprise' }
81
+ expect(described_class.redhat_enterprise_linux?(node)).to be_true
82
+ end
83
+
84
+ it 'returns false when the platform is not redhat enterprise linux' do
85
+ node = { 'platform' => 'windows' }
86
+ expect(described_class.redhat_enterprise_linux?(node)).to be_false
87
+ end
88
+ end
89
+
90
+ context 'dynamic matchers' do
91
+ describe '#ubuntu_after_lucid?' do
92
+ it 'returns true when the version is later than 10.04' do
93
+ node = { 'platform' => 'ubuntu', 'platform_version' => '10.10' }
94
+ expect(described_class.ubuntu_after_lucid?(node)).to be_true
95
+ end
96
+
97
+ it 'returns false when the version is 10.04' do
98
+ node = { 'platform' => 'ubuntu', 'platform_version' => '10.04' }
99
+ expect(described_class.ubuntu_after_lucid?(node)).to be_false
100
+ end
101
+
102
+ it 'returns false when the version is a patch release higher than 10.04' do
103
+ node = { 'platform' => 'ubuntu', 'platform_version' => '10.04.4' }
104
+ expect(described_class.ubuntu_after_lucid?(node)).to be_false
105
+ end
106
+
107
+ it 'returns false when the version is less than 10.04' do
108
+ node = { 'platform' => 'ubuntu', 'platform_version' => '9.10' }
109
+ expect(described_class.ubuntu_after_lucid?(node)).to be_false
110
+ end
111
+ end
112
+
113
+ describe '#ubuntu_after_or_at_lucid?' do
114
+ it 'returns true when the version is later than 10.04' do
115
+ node = { 'platform' => 'ubuntu', 'platform_version' => '10.10' }
116
+ expect(described_class.ubuntu_after_or_at_lucid?(node)).to be_true
117
+ end
118
+
119
+ it 'returns true when the version is 10.04' do
120
+ node = { 'platform' => 'ubuntu', 'platform_version' => '10.04' }
121
+ expect(described_class.ubuntu_after_or_at_lucid?(node)).to be_true
122
+ end
123
+
124
+ it 'returns true when the version is a patch release higher than 10.04' do
125
+ node = { 'platform' => 'ubuntu', 'platform_version' => '10.04.4' }
126
+ expect(described_class.ubuntu_after_or_at_lucid?(node)).to be_true
127
+ end
128
+
129
+ it 'returns false when the version is less than 10.04' do
130
+ node = { 'platform' => 'ubuntu', 'platform_version' => '9.10' }
131
+ expect(described_class.ubuntu_after_or_at_lucid?(node)).to be_false
132
+ end
133
+ end
134
+
135
+ describe '#ubuntu_lucid?' do
136
+ it 'returns false when the version is later than 10.04' do
137
+ node = { 'platform' => 'ubuntu', 'platform_version' => '10.10' }
138
+ expect(described_class.ubuntu_lucid?(node)).to be_false
139
+ end
140
+
141
+ it 'returns true when the version is 10.04' do
142
+ node = { 'platform' => 'ubuntu', 'platform_version' => '10.04' }
143
+ expect(described_class.ubuntu_lucid?(node)).to be_true
144
+ end
145
+
146
+ it 'returns true when the version is a patch release higher than 10.04' do
147
+ node = { 'platform' => 'ubuntu', 'platform_version' => '10.04.4' }
148
+ expect(described_class.ubuntu_lucid?(node)).to be_true
149
+ end
150
+
151
+ it 'returns false when the version is less than 10.04' do
152
+ node = { 'platform' => 'ubuntu', 'platform_version' => '9.10' }
153
+ expect(described_class.ubuntu_lucid?(node)).to be_false
154
+ end
155
+ end
156
+
157
+ describe '#ubuntu_before_lucid?' do
158
+ it 'returns false when the version is later than 10.04' do
159
+ node = { 'platform' => 'ubuntu', 'platform_version' => '10.10' }
160
+ expect(described_class.ubuntu_before_lucid?(node)).to be_false
161
+ end
162
+
163
+ it 'returns false when the version is 10.04' do
164
+ node = { 'platform' => 'ubuntu', 'platform_version' => '10.04' }
165
+ expect(described_class.ubuntu_before_lucid?(node)).to be_false
166
+ end
167
+
168
+ it 'returns false when the version is a patch release higher than 10.04' do
169
+ node = { 'platform' => 'ubuntu', 'platform_version' => '10.04.4' }
170
+ expect(described_class.ubuntu_before_lucid?(node)).to be_false
171
+ end
172
+
173
+ it 'returns true when the version is less than 10.04' do
174
+ node = { 'platform' => 'ubuntu', 'platform_version' => '9.10' }
175
+ expect(described_class.ubuntu_before_lucid?(node)).to be_true
176
+ end
177
+ end
178
+
179
+ describe '#ubuntu_before_or_at_lucid?' do
180
+ it 'returns false when the version is later than 10.04' do
181
+ node = { 'platform' => 'ubuntu', 'platform_version' => '10.10' }
182
+ expect(described_class.ubuntu_before_or_at_lucid?(node)).to be_false
183
+ end
184
+
185
+ it 'returns true when the version is 10.04' do
186
+ node = { 'platform' => 'ubuntu', 'platform_version' => '10.04' }
187
+ expect(described_class.ubuntu_before_or_at_lucid?(node)).to be_true
188
+ end
189
+
190
+ it 'returns true when the version is a patch release higher than 10.04' do
191
+ node = { 'platform' => 'ubuntu', 'platform_version' => '10.04.4' }
192
+ expect(described_class.ubuntu_before_or_at_lucid?(node)).to be_true
193
+ end
194
+
195
+ it 'returns true when the version is less than 10.04' do
196
+ node = { 'platform' => 'ubuntu', 'platform_version' => '9.10' }
197
+ expect(described_class.ubuntu_before_or_at_lucid?(node)).to be_true
198
+ end
199
+ end
200
+ end
201
+ end