summaryrefslogtreecommitdiff
path: root/spec/unit/resource/execute_spec.rb
blob: c99e87b35136bb125e011a21e0dc82122eee826c (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
267
268
269
#
# Author:: Adam Jacob (<adam@chef.io>)
# Author:: Tyler Cloke (<tyler@chef.io>)
# Copyright:: Copyright 2008-2017, 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::Execute do
  let(:resource_instance_name) { "some command" }
  let(:execute_resource) { Chef::Resource::Execute.new(resource_instance_name) }
  it_behaves_like "an execute resource"

  it "default guard interpreter is :execute interpreter" do
    expect(execute_resource.guard_interpreter).to be(:execute)
  end

  it "defaults to not being a guard interpreter" do
    expect(execute_resource.is_guard_interpreter).to eq(false)
  end

  describe "#qualify_user" do
    let(:password) { "password" }
    let(:domain) { nil }

    context "when username is passed as user@domain" do
      let(:username) { "user@domain" }

      it "correctly parses the user and domain" do
        identity = execute_resource.qualify_user(username, password, domain)
        expect(identity[:domain]).to eq("domain")
        expect(identity[:user]).to eq("user")
      end
    end

    context "when username is passed as domain\\user" do
      let(:username) { "domain\\user" }

      it "correctly parses the user and domain" do
        identity = execute_resource.qualify_user(username, password, domain)
        expect(identity[:domain]).to eq("domain")
        expect(identity[:user]).to eq("user")
      end
    end
  end

  shared_examples_for "it received valid credentials" do
    describe "the validation method" do
      it "does not raise an error" do
        expect { execute_resource.validate_identity_platform(username, password, domain) }.not_to raise_error
      end
    end

    describe "the name qualification method" do
      it "correctly translates the user and domain" do
        identity = nil
        expect { identity = execute_resource.qualify_user(username, password, domain) }.not_to raise_error
        expect(identity[:domain]).to eq(domain)
        expect(identity[:user]).to eq(username)
      end
    end
  end

  shared_examples_for "it received invalid credentials" do
    describe "the validation method" do
      it "raises an error" do
        expect { execute_resource.validate_identity_platform(username, password, domain, elevated) }.to raise_error(ArgumentError)
      end
    end
  end

  shared_examples_for "it received invalid username and domain" do
    describe "the validation method" do
      it "raises an error" do
        expect { execute_resource.qualify_user(username, password, domain) }.to raise_error(ArgumentError)
      end
    end
  end

  shared_examples_for "it received credentials that are not valid on the platform" do
    describe "the validation method" do
      it "raises an error" do
        expect { execute_resource.validate_identity_platform(username, password, domain) }.to raise_error(Chef::Exceptions::UnsupportedPlatform)
      end
    end
  end

  shared_examples_for "a consumer of the Execute resource" do
    context "when running on Windows" do
      before do
        allow(execute_resource).to receive(:node).and_return({ :platform_family => "windows" })
      end

      context "when no user, domain, or password is specified" do
        let(:username) { nil }
        let(:domain) { nil }
        let(:password) { nil }
        it_behaves_like "it received valid credentials"
      end

      context "when a valid username is specified" do
        let(:username) { "starchild" }
        let(:elevated) { false }
        context "when a valid domain is specified" do
          let(:domain) { "mothership" }

          context "when the password is not specified" do
            let(:password) { nil }
            it_behaves_like "it received invalid credentials"
          end

          context "when the password is specified" do
            let(:password) { "we.funk!" }
            it_behaves_like "it received valid credentials"
          end
        end

        context "when the domain is not specified" do
          let(:domain) { nil }
          let(:elevated) { false }

          context "when the password is not specified" do
            let(:password) { nil }
            it_behaves_like "it received invalid credentials"
          end

          context "when the password is specified" do
            let(:password) { "we.funk!" }
            it_behaves_like "it received valid credentials"
          end
        end

        context "when username is not specified" do
          let(:username) { nil }

          context "when domain is specified" do
            let(:domain) { "mothership" }
            let(:password) { nil }
            it_behaves_like "it received invalid username and domain"
          end

          context "when password is specified" do
            let(:domain) { nil }
            let(:password) { "we.funk!" }
            it_behaves_like "it received invalid username and domain"
          end
        end
      end

      context "when invalid username is specified" do
        let(:username) { "user@domain@domain" }
        let(:domain) { nil }
        let(:password) { "we.funk!" }
        it_behaves_like "it received invalid username and domain"
      end

      context "when the domain is provided in both username and domain" do
        let(:domain) { "some_domain" }
        let(:password) { "we.funk!" }

        context "when username is in the form domain\\user" do
          let(:username) { "mothership\\starchild" }
          it_behaves_like "it received invalid username and domain"
        end

        context "when username is in the form user@domain" do
          let(:username) { "starchild@mothership" }
          it_behaves_like "it received invalid username and domain"
        end
      end

      context "when elevated is passed" do
        let(:elevated) { true }

        context "when username and password are not passed" do
          let(:username) { nil }
          let(:domain) { nil }
          let(:password) { nil }
          it_behaves_like "it received invalid credentials"
        end

        context "when username and password are passed" do
          let(:username) { "user" }
          let(:domain) { nil }
          let(:password) { "we.funk!" }
          it_behaves_like "it received valid credentials"
        end
      end
    end

    context "when not running on Windows" do
      before do
        allow(execute_resource).to receive(:node).and_return({ :platform_family => "ubuntu" })
      end

      context "when no user, domain, or password is specified" do
        let(:username) { nil }
        let(:domain) { nil }
        let(:password) { nil }
        it_behaves_like "it received valid credentials"
      end

      context "when the user is specified and the domain and password are not" do
        let(:username) { "starchild" }
        let(:domain) { nil }
        let(:password) { nil }
        it_behaves_like "it received valid credentials"

        context "when the password is specified and the domain is not" do
          let(:password) { "we.funk!" }
          let(:domain) { nil }
          it_behaves_like "it received credentials that are not valid on the platform"
        end

        context "when the domain is specified and the password is not" do
          let(:domain) { "mothership" }
          let(:password) { nil }
          it_behaves_like "it received credentials that are not valid on the platform"
        end

        context "when the domain and password are specified" do
          let(:domain) { "mothership" }
          let(:password) { "we.funk!" }
          it_behaves_like "it received credentials that are not valid on the platform"
        end
      end

      context "when the user is not specified" do
        let(:username) { nil }
        context "when the domain is specified" do
          let(:domain) { "mothership" }
          context "when the password is specified" do
            let(:password) { "we.funk!" }
            it_behaves_like "it received credentials that are not valid on the platform"
          end

          context "when password is not specified" do
            let(:password) { nil }
            it_behaves_like "it received credentials that are not valid on the platform"
          end
        end

        context "when the domain is not specified" do
          let(:domain) { nil }
          context "when the password is specified" do
            let(:password) { "we.funk!" }
            it_behaves_like "it received credentials that are not valid on the platform"
          end
        end
      end
    end
  end

  it_behaves_like "a consumer of the Execute resource"

end