chef-utils 0.0.1 → 15.5.9

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,139 @@
1
+ #
2
+ # Copyright:: Copyright 2018-2019, Chef Software Inc.
3
+ # License:: Apache License, Version 2.0
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+ #
17
+
18
+ require "spec_helper"
19
+
20
+ def arch_reports_true_for(*args)
21
+ args.each do |method|
22
+ it "reports true for #{method}" do
23
+ expect(described_class.send(method, node)).to be true
24
+ end
25
+ end
26
+ (ARCH_HELPERS - args).each do |method|
27
+ it "reports false for #{method}" do
28
+ expect(described_class.send(method, node)).to be false
29
+ end
30
+ end
31
+ end
32
+
33
+ RSpec.describe ChefUtils::DSL::Architecture do
34
+ let(:node) { { "kernel" => { "machine" => arch } } }
35
+
36
+ ( HELPER_MODULES - [ described_class ] ).each do |klass|
37
+ it "does not have methods that collide with #{klass}" do
38
+ expect((klass.methods - Module.methods) & ARCH_HELPERS).to be_empty
39
+ end
40
+ end
41
+
42
+ ARCH_HELPERS.each do |helper|
43
+ it "has the #{helper} in the ChefUtils module" do
44
+ expect(ChefUtils).to respond_to(helper)
45
+ end
46
+ end
47
+
48
+ context "on x86_64" do
49
+ let(:arch) { "x86_64" }
50
+
51
+ arch_reports_true_for(:intel?, :_64_bit?)
52
+ end
53
+
54
+ context "on amd64" do
55
+ let(:arch) { "amd64" }
56
+
57
+ arch_reports_true_for(:intel?, :_64_bit?)
58
+ end
59
+ context "on ppc64" do
60
+ let(:arch) { "ppc64" }
61
+
62
+ arch_reports_true_for(:ppc64?, :_64_bit?)
63
+ end
64
+ context "on ppc64le" do
65
+ let(:arch) { "ppc64le" }
66
+
67
+ arch_reports_true_for(:ppc64le?, :_64_bit?)
68
+ end
69
+ context "on s390x" do
70
+ let(:arch) { "s390x" }
71
+
72
+ arch_reports_true_for(:s390x?, :_64_bit?)
73
+ end
74
+ context "on ia64" do
75
+ let(:arch) { "ia64" }
76
+
77
+ arch_reports_true_for(:_64_bit?)
78
+ end
79
+ context "on sparc64" do
80
+ let(:arch) { "sparc64" }
81
+
82
+ arch_reports_true_for(:_64_bit?)
83
+ end
84
+ context "on aarch64" do
85
+ let(:arch) { "aarch64" }
86
+
87
+ arch_reports_true_for(:_64_bit?)
88
+ end
89
+ context "on arch64" do
90
+ let(:arch) { "arch64" }
91
+
92
+ arch_reports_true_for(:_64_bit?)
93
+ end
94
+ context "on arm64" do
95
+ let(:arch) { "arm64" }
96
+
97
+ arch_reports_true_for(:_64_bit?)
98
+ end
99
+ context "on sun4v" do
100
+ let(:arch) { "sun4v" }
101
+
102
+ arch_reports_true_for(:sparc?, :_64_bit?)
103
+ end
104
+ context "on sun4u" do
105
+ let(:arch) { "sun4u" }
106
+
107
+ arch_reports_true_for(:sparc?, :_64_bit?)
108
+ end
109
+ context "on i86pc" do
110
+ let(:arch) { "i86pc" }
111
+
112
+ arch_reports_true_for(:i386?, :intel?, :_32_bit?)
113
+ end
114
+ context "on i386" do
115
+ let(:arch) { "i386" }
116
+
117
+ arch_reports_true_for(:i386?, :intel?, :_32_bit?)
118
+ end
119
+ context "on i686" do
120
+ let(:arch) { "i686" }
121
+
122
+ arch_reports_true_for(:i386?, :intel?, :_32_bit?)
123
+ end
124
+ context "on powerpc" do
125
+ let(:arch) { "powerpc" }
126
+
127
+ arch_reports_true_for(:powerpc?, :_32_bit?)
128
+ end
129
+ context "on armhf" do
130
+ let(:arch) { "armhf" }
131
+
132
+ arch_reports_true_for(:armhf?, :_32_bit?)
133
+ end
134
+ context "on s390" do
135
+ let(:arch) { "s390" }
136
+
137
+ arch_reports_true_for(:s390?, :_32_bit?)
138
+ end
139
+ end
@@ -0,0 +1,33 @@
1
+ #
2
+ # Copyright:: Copyright 2018-2019, Chef Software Inc.
3
+ # License:: Apache License, Version 2.0
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+ #
17
+
18
+ require "spec_helper"
19
+
20
+ RSpec.describe ChefUtils do
21
+ class ThingWithDSL
22
+ extend ChefUtils
23
+ end
24
+
25
+ (OS_HELPERS + ARCH_HELPERS + PLATFORM_HELPERS + PLATFORM_FAMILY_HELPERS + INTROSPECTION_HELPERS).each do |helper|
26
+ it "has the #{helper} in the ChefUtils module" do
27
+ expect(ThingWithDSL).to respond_to(helper)
28
+ end
29
+ it "has the #{helper} class method in the ChefUtils module" do
30
+ expect(ChefUtils).to respond_to(helper)
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,168 @@
1
+ #
2
+ # Copyright:: Copyright 2018-2019, Chef Software Inc.
3
+ # License:: Apache License, Version 2.0
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+ #
17
+
18
+ require "spec_helper"
19
+
20
+ RSpec.describe ChefUtils::DSL::Introspection do
21
+ class IntrospectionTestClass
22
+ include ChefUtils::DSL::Introspection
23
+ attr_accessor :node
24
+ def initialize(node)
25
+ @node = node
26
+ end
27
+ end
28
+
29
+ let(:node) { double("node") }
30
+
31
+ let(:test_instance) { IntrospectionTestClass.new(node) }
32
+
33
+ context "#docker?" do
34
+ # FIXME: use a real VividMash for these tests insted of stubbing
35
+ it "is false by default" do
36
+ expect(node).to receive(:read).with("virtualization", "systems", "docker").and_return(nil)
37
+ expect(ChefUtils.docker?(node)).to be false
38
+ end
39
+ it "is true when ohai reports a docker guest" do
40
+ expect(node).to receive(:read).with("virtualization", "systems", "docker").and_return("guest")
41
+ expect(ChefUtils.docker?(node)).to be true
42
+ end
43
+ it "is false for any other value other than guest" do
44
+ expect(node).to receive(:read).with("virtualization", "systems", "docker").and_return("some nonsense")
45
+ expect(ChefUtils.docker?(node)).to be false
46
+ end
47
+ end
48
+
49
+ context "#systemd?" do
50
+ # FIXME: somehow test the train helpers
51
+ it "returns false if /proc/1/comm does not exist" do
52
+ expect(File).to receive(:exist?).with("/proc/1/comm").and_return(false)
53
+ expect(ChefUtils.systemd?(node)).to be false
54
+ end
55
+
56
+ it "returns false if /proc/1/comm is not systemd" do
57
+ expect(File).to receive(:exist?).with("/proc/1/comm").and_return(true)
58
+ expect(File).to receive(:open).with("/proc/1/comm").and_return(StringIO.new("upstart\n"))
59
+ expect(ChefUtils.systemd?(node)).to be false
60
+ end
61
+
62
+ it "returns true if /proc/1/comm is systemd" do
63
+ expect(File).to receive(:exist?).with("/proc/1/comm").and_return(true)
64
+ expect(File).to receive(:open).with("/proc/1/comm").and_return(StringIO.new("systemd\n"))
65
+ expect(ChefUtils.systemd?(node)).to be true
66
+ end
67
+ end
68
+
69
+ context "#kitchen?" do
70
+ before do
71
+ @saved = ENV["TEST_KITCHEN"]
72
+ end
73
+ after do
74
+ ENV["TEST_KITCHEN"] = @saved
75
+ end
76
+
77
+ it "return true if ENV['TEST_KITCHEN'] is not set" do
78
+ ENV.delete("TEST_KITCHEN")
79
+ expect(ChefUtils.kitchen?(node)).to be false
80
+ end
81
+
82
+ it "return true if ENV['TEST_KITCHEN'] is nil" do
83
+ ENV["TEST_KITCHEN"] = nil
84
+ expect(ChefUtils.kitchen?(node)).to be false
85
+ end
86
+
87
+ it "return true if ENV['TEST_KITCHEN'] is set" do
88
+ ENV["TEST_KITCHEN"] = "1"
89
+ expect(ChefUtils.kitchen?(node)).to be true
90
+ end
91
+ end
92
+
93
+ context "#ci?" do
94
+ before do
95
+ @saved = ENV["CI"]
96
+ end
97
+ after do
98
+ ENV["CI"] = @saved
99
+ end
100
+
101
+ it "return true if ENV['CI'] is not set" do
102
+ ENV.delete("CI")
103
+ expect(ChefUtils.ci?(node)).to be false
104
+ end
105
+
106
+ it "return true if ENV['CI'] is nil" do
107
+ ENV["CI"] = nil
108
+ expect(ChefUtils.ci?(node)).to be false
109
+ end
110
+
111
+ it "return true if ENV['CI'] is set" do
112
+ ENV["CI"] = "1"
113
+ expect(ChefUtils.ci?(node)).to be true
114
+ end
115
+ end
116
+
117
+ context "#has_systemd_service_unit?" do
118
+ # FIXME: test through train helpers
119
+
120
+ before do
121
+ %w{ /etc /usr/lib /lib /run }.each do |base|
122
+ allow(File).to receive(:exist?).with("#{base}/systemd/system/example.service").and_return(false)
123
+ allow(File).to receive(:exist?).with("#{base}/systemd/system/example@.service").and_return(false)
124
+ end
125
+ end
126
+
127
+ it "is false if no unit is present" do
128
+ expect(ChefUtils.has_systemd_service_unit?("example")).to be false
129
+ end
130
+
131
+ it "is false if no template is present" do
132
+ expect(ChefUtils.has_systemd_service_unit?("example@instance1")).to be false
133
+ end
134
+
135
+ %w{ /etc /usr/lib /lib /run }.each do |base|
136
+ it "finds a unit in #{base}" do
137
+ expect(File).to receive(:exist?).with("#{base}/systemd/system/example.service").and_return(true)
138
+ expect(ChefUtils.has_systemd_service_unit?("example")).to be true
139
+ end
140
+
141
+ it "finds a template in #{base}" do
142
+ expect(File).to receive(:exist?).with("#{base}/systemd/system/example@.service").and_return(true)
143
+ expect(ChefUtils.has_systemd_service_unit?("example@instance1")).to be true
144
+ end
145
+ end
146
+ end
147
+
148
+ context "#has_systemd_unit?" do
149
+ # FIXME: test through train helpers
150
+
151
+ before do
152
+ %w{ /etc /usr/lib /lib /run }.each do |base|
153
+ allow(File).to receive(:exist?).with("#{base}/systemd/system/example.mount").and_return(false)
154
+ end
155
+ end
156
+
157
+ it "is false if no unit is present" do
158
+ expect(ChefUtils.has_systemd_unit?("example.mount")).to be false
159
+ end
160
+
161
+ %w{ /etc /usr/lib /lib /run }.each do |base|
162
+ it "finds a unit in #{base}" do
163
+ expect(File).to receive(:exist?).with("#{base}/systemd/system/example.mount").and_return(true)
164
+ expect(ChefUtils.has_systemd_unit?("example.mount")).to be true
165
+ end
166
+ end
167
+ end
168
+ end
@@ -0,0 +1,174 @@
1
+ #
2
+ # Copyright:: Copyright 2018-2019, Chef Software Inc.
3
+ # License:: Apache License, Version 2.0
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+ #
17
+
18
+ require "spec_helper"
19
+ require "fauxhai"
20
+
21
+ def os_reports_true_for(*args)
22
+ args.each do |method|
23
+ it "reports true for #{method}" do
24
+ expect(described_class.send(method, node)).to be true
25
+ end
26
+ end
27
+ (OS_HELPERS - args).each do |method|
28
+ it "reports false for #{method}" do
29
+ expect(described_class.send(method, node)).to be false
30
+ end
31
+ end
32
+ end
33
+
34
+ RSpec.describe ChefUtils::DSL::OS do
35
+ let(:node) { Fauxhai.mock(options).data }
36
+
37
+ ( HELPER_MODULES - [ described_class ] ).each do |klass|
38
+ it "does not have methods that collide with #{klass}" do
39
+ expect((klass.methods - Module.methods) & OS_HELPERS).to be_empty
40
+ end
41
+ end
42
+
43
+ OS_HELPERS.each do |helper|
44
+ it "has the #{helper} in the ChefUtils module" do
45
+ expect(ChefUtils).to respond_to(helper)
46
+ end
47
+ end
48
+
49
+ context "on ubuntu" do
50
+ let(:options) { { platform: "ubuntu" } }
51
+
52
+ os_reports_true_for(:linux?)
53
+ end
54
+
55
+ context "on raspbian" do
56
+ let(:options) { { platform: "raspbian" } }
57
+
58
+ os_reports_true_for(:linux?)
59
+ end
60
+
61
+ context "on linuxmint" do
62
+ let(:options) { { platform: "linuxmint" } }
63
+
64
+ os_reports_true_for(:linux?)
65
+ end
66
+
67
+ context "on debian" do
68
+ let(:options) { { platform: "debian" } }
69
+
70
+ os_reports_true_for(:linux?)
71
+ end
72
+
73
+ context "on amazon" do
74
+ let(:options) { { platform: "amazon" } }
75
+
76
+ os_reports_true_for(:linux?)
77
+ end
78
+
79
+ context "on arch" do
80
+ let(:options) { { platform: "arch" } }
81
+
82
+ os_reports_true_for(:linux?)
83
+ end
84
+
85
+ context "on centos" do
86
+ let(:options) { { platform: "centos" } }
87
+
88
+ os_reports_true_for(:linux?)
89
+ end
90
+
91
+ context "on clearos" do
92
+ let(:options) { { platform: "clearos" } }
93
+
94
+ os_reports_true_for(:linux?)
95
+ end
96
+
97
+ context "on dragonfly4" do
98
+ let(:options) { { platform: "dragonfly4" } }
99
+
100
+ os_reports_true_for
101
+ end
102
+
103
+ context "on fedora" do
104
+ let(:options) { { platform: "fedora" } }
105
+
106
+ os_reports_true_for(:linux?)
107
+ end
108
+
109
+ context "on freebsd" do
110
+ let(:options) { { platform: "freebsd" } }
111
+
112
+ os_reports_true_for
113
+ end
114
+
115
+ context "on gentoo" do
116
+ let(:options) { { platform: "gentoo" } }
117
+
118
+ os_reports_true_for(:linux?)
119
+ end
120
+
121
+ context "on mac_os_x" do
122
+ let(:options) { { platform: "mac_os_x" } }
123
+
124
+ os_reports_true_for(:darwin?)
125
+ end
126
+
127
+ context "on openbsd" do
128
+ let(:options) { { platform: "openbsd" } }
129
+
130
+ os_reports_true_for
131
+ end
132
+
133
+ context "on opensuse" do
134
+ let(:options) { { platform: "opensuse" } }
135
+
136
+ os_reports_true_for(:linux?)
137
+ end
138
+
139
+ context "on oracle" do
140
+ let(:options) { { platform: "oracle" } }
141
+
142
+ os_reports_true_for(:linux?)
143
+ end
144
+
145
+ context "on redhat" do
146
+ let(:options) { { platform: "redhat" } }
147
+
148
+ os_reports_true_for(:linux?)
149
+ end
150
+
151
+ context "on smartos" do
152
+ let(:options) { { platform: "smartos" } }
153
+
154
+ os_reports_true_for
155
+ end
156
+
157
+ context "on solaris2" do
158
+ let(:options) { { platform: "solaris2" } }
159
+
160
+ os_reports_true_for
161
+ end
162
+
163
+ context "on suse" do
164
+ let(:options) { { platform: "suse" } }
165
+
166
+ os_reports_true_for(:linux?)
167
+ end
168
+
169
+ context "on windows" do
170
+ let(:options) { { platform: "windows" } }
171
+
172
+ os_reports_true_for
173
+ end
174
+ end