summaryrefslogtreecommitdiff
path: root/spec/unit/resource/rhsm_register_spec.rb
blob: 7ce76ad90807df8d0acdf025712ca0b3709128b9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#
# Copyright:: Copyright (c) Chef Software Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

require "spec_helper"

describe Chef::Resource::RhsmRegister do

  let(:resource) { Chef::Resource::RhsmRegister.new("foo") }
  let(:provider) { resource.provider_for_action(:register) }

  it "has a resource name of :rhsm_register" do
    expect(resource.resource_name).to eql(:rhsm_register)
  end

  it "sets the default action as :register" do
    expect(resource.action).to eql([:register])
  end

  it "supports :register, :unregister actions" do
    expect { resource.action :register }.not_to raise_error
    expect { resource.action :unregister }.not_to raise_error
  end

  it "coerces activation_key to an array" do
    resource.activation_key "foo"
    expect(resource.activation_key).to eql(["foo"])
  end

  describe "#katello_cert_rpm_installed?" do
    context "when the output contains katello-ca-consumer" do
      let(:with_katello) { double("shell_out", stdout: <<~RPM) }
        libevent-2.0.21-4.el7.x86_64
        gettext-libs-0.19.8.1-3.el7.x86_64
        yum-metadata-parser-1.1.4-10.el7.x86_64
        pyliblzma-0.5.3-11.el7.x86_64
        python-IPy-0.75-6.el7.noarch
        grubby-8.28-26.el7.x86_64
        fipscheck-lib-1.4.1-6.el7.x86_64
        centos-logos-70.0.6-3.el7.centos.noarch
        nss-tools-3.44.0-7.el7_7.x86_64
        katello-ca-consumer-somehostname-1.0-1.el7.x86_64
        rpm-4.11.3-43.el7.x86_64
        gpgme-1.3.2-5.el7.x86_64
        libnfsidmap-0.25-19.el7.x86_64
      RPM

      it "returns true" do
        allow(provider).to receive(:shell_out).and_return(with_katello)
        expect(provider.katello_cert_rpm_installed?).to eq(true)
      end
    end

    context "when the output does not contain katello-ca-consumer" do
      let(:without_katello) { double("shell_out", stdout: "") }

      it "returns false" do
        allow(provider).to receive(:shell_out).and_return(without_katello)
        expect(provider.katello_cert_rpm_installed?).to eq(false)
      end
    end
  end

  describe "#register_command" do
    before do
      allow(provider).to receive(:activation_key).and_return([])
      allow(provider).to receive(:auto_attach)
    end

    context "when activation keys exist" do
      before do
        allow(resource).to receive(:activation_key).and_return(%w{key1 key2})
      end

      context "when no org exists" do
        it "raises an exception" do
          allow(resource).to receive(:organization).and_return(nil)
          expect { provider.register_command }.to raise_error(RuntimeError)
        end
      end

      context "when an org exists" do
        it "returns a command containing the keys and org" do
          allow(resource).to receive(:organization).and_return("myorg")

          expect(provider.register_command).to match("--activationkey=key1 --activationkey=key2 --org=myorg")
        end
      end

      context "when a system_name is provided" do
        it "returns a command containing the system name" do
          allow(resource).to receive(:organization).and_return("myorg")
          allow(resource).to receive(:system_name).and_return("myname")
          expect(provider.register_command).to match("--name=myname")
        end
      end

      context "when a system_name is not provided" do
        it "returns a command containing the system name" do
          allow(resource).to receive(:organization).and_return("myorg")
          allow(resource).to receive(:system_name).and_return(nil)
          expect(provider.register_command).not_to match("--name")
        end
      end

      context "when auto_attach is true" do
        it "does not return a command with --auto-attach since it is not supported with activation keys" do
          allow(resource).to receive(:organization).and_return("myorg")
          allow(resource).to receive(:auto_attach).and_return(true)

          expect(provider.register_command).not_to match("--auto-attach")
        end
      end
    end

    context "when username and password exist" do
      before do
        allow(resource).to receive(:username).and_return("myuser")
        allow(resource).to receive(:password).and_return("mypass")
        allow(resource).to receive(:environment)
        allow(resource).to receive(:using_satellite_host?)
        allow(resource).to receive(:activation_key).and_return([])
      end

      context "when auto_attach is true" do
        it "returns a command containing --auto-attach" do
          allow(resource).to receive(:auto_attach).and_return(true)

          expect(provider.register_command).to match("--auto-attach")
        end
      end

      context "when auto_attach is false" do
        it "returns a command that does not contain --auto-attach" do
          allow(resource).to receive(:auto_attach).and_return(false)

          expect(provider.register_command).not_to match("--auto-attach")
        end
      end

      context "when a system_name is provided" do
        it "returns a command containing the system name" do
          allow(resource).to receive(:system_name).and_return("myname")
          expect(provider.register_command).to match("--name=myname")
        end
      end

      context "when a system_name is not provided" do
        it "returns a command containing the system name" do
          allow(resource).to receive(:system_name).and_return(nil)
          expect(provider.register_command).not_to match("--name")
        end
      end

      context "when auto_attach is nil" do
        it "returns a command that does not contain --auto-attach" do
          allow(resource).to receive(:auto_attach).and_return(nil)

          expect(provider.register_command).not_to match("--auto-attach")
        end
      end

      context "when environment does not exist" do
        context "when registering to a satellite server" do
          it "raises an exception" do
            allow(provider).to receive(:using_satellite_host?).and_return(true)
            allow(resource).to receive(:environment).and_return(nil)
            expect { provider.register_command }.to raise_error(RuntimeError)
          end
        end

        context "when registering to RHSM proper" do
          before do
            allow(provider).to receive(:using_satellite_host?).and_return(false)
            allow(resource).to receive(:environment).and_return(nil)
          end

          it "does not raise an exception" do
            expect { provider.register_command }.not_to raise_error
          end

          it "returns a command containing the username and password and no environment" do
            allow(resource).to receive(:environment).and_return("myenv")
            expect(provider.register_command).to match("--username=myuser --password=mypass")
            expect(provider.register_command).not_to match("--environment")
          end
        end
      end

      context "when an environment exists" do
        it "returns a command containing the username, password, and environment" do
          allow(provider).to receive(:using_satellite_host?).and_return(true)
          allow(resource).to receive(:environment).and_return("myenv")
          expect(provider.register_command).to match("--username=myuser --password=mypass --environment=myenv")
        end
      end
    end

    context "when no activation keys, username, or password exist" do
      it "raises an exception" do
        allow(resource).to receive(:activation_key).and_return([])
        allow(resource).to receive(:username).and_return(nil)
        allow(resource).to receive(:password).and_return(nil)

        expect { provider.register_command }.to raise_error(RuntimeError)
      end
    end
  end

  describe "#ca_consumer_package_source" do
    let(:satellite_host) { "sat-host" }

    before do
      resource.satellite_host = satellite_host
    end

    context "when https_for_ca_consumer is true" do
      before { resource.https_for_ca_consumer true }

      it "returns url with https" do
        expect(provider.ca_consumer_package_source).to eq("https://#{satellite_host}/pub/katello-ca-consumer-latest.noarch.rpm")
      end
    end

    context "when https_for_ca_consumer is false" do
      before { resource.https_for_ca_consumer false }

      it "returns url with http" do
        expect(provider.ca_consumer_package_source).to eq("http://#{satellite_host}/pub/katello-ca-consumer-latest.noarch.rpm")
      end
    end
  end

  describe "#registered_with_rhsm?" do
    context "when the status is Unknown" do
      let(:unknown_status) { double("shell_out", stdout: "Overall Status: Unknown") }

      it "returns false" do
        allow(provider).to receive(:shell_out).and_return(unknown_status)
        expect(provider.registered_with_rhsm?).to eq(false)
      end
    end

    context "when the status is anything else" do
      let(:known_status) { double("shell_out", stdout: "Overall Status: Insufficient") }

      it "returns true" do
        allow(provider).to receive(:shell_out).and_return(known_status)
        expect(provider.registered_with_rhsm?).to eq(true)
      end
    end
  end
end