Skip to content

Commit d4003e9

Browse files
committed
Modernize/Fix CI
1 parent efd8764 commit d4003e9

File tree

1 file changed

+26
-43
lines changed

1 file changed

+26
-43
lines changed

spec/unit/util/puppetdb_validator_spec.rb

Lines changed: 26 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -8,51 +8,34 @@
88

99
url = '/pdb/meta/v1/version'
1010
if Puppet::PUPPETVERSION.to_f < 7
11-
conn_ok = stub
12-
conn_ok.stubs(:get).with(url, 'Accept' => 'application/json').returns(nethttpok)
13-
conn_ok.stubs(:read_timeout=).with(2)
14-
conn_ok.stubs(:open_timeout=).with(2)
11+
conn_ok = double
12+
allow(conn_ok).to receive(:get).with(url, 'Accept' => 'application/json').and_return(nethttpok)
13+
allow(conn_ok).to receive(:read_timeout=).with(2)
14+
allow(conn_ok).to receive(:open_timeout=).with(2)
1515

16-
conn_not_found = stub
17-
conn_not_found.stubs(:get).with('/pdb/meta/v1/version', 'Accept' => 'application/json').returns(notfound)
16+
conn_not_found = double
17+
allow(conn_not_found).to receive(:get).with('/pdb/meta/v1/version', 'Accept' => 'application/json').and_return(notfound)
1818

19-
Puppet::Network::HttpPool.stubs(:http_instance).raises('Unknown host')
20-
Puppet::Network::HttpPool.stubs(:http_instance).with('mypuppetdb.com', 8080, true).raises('Connection refused')
21-
Puppet::Network::HttpPool.stubs(:http_instance).with('mypuppetdb.com', 8080, false).returns(conn_ok)
22-
Puppet::Network::HttpPool.stubs(:http_instance).with('mypuppetdb.com', 8081, true).returns(conn_ok)
23-
Puppet::Network::HttpPool.stubs(:http_instance).with('wrongserver.com', 8081, true).returns(conn_not_found)
19+
allow(Puppet::Network::HttpPool).to receive(:http_instance).and_raise('Unknown host')
20+
allow(Puppet::Network::HttpPool).to receive(:http_instance).with('mypuppetdb.com', 8080, true).and_raise('Connection refused')
21+
allow(Puppet::Network::HttpPool).to receive(:http_instance).with('mypuppetdb.com', 8080, false).and_return(conn_ok)
22+
allow(Puppet::Network::HttpPool).to receive(:http_instance).with('mypuppetdb.com', 8081, true).and_return(conn_ok)
23+
allow(Puppet::Network::HttpPool).to receive(:http_instance).with('wrongserver.com', 8081, true).and_return(conn_not_found)
2424
else
25-
http = stub
26-
Puppet::HTTP::Client.stubs(:new).returns(http)
25+
http = double
26+
allow(Puppet::HTTP::Client).to receive(:new).and_return(http)
2727

28-
http.stubs(:get).with { |uri, _opts|
29-
uri.hostname == 'mypuppetdb.com' &&
30-
uri.port == 8080 &&
31-
uri.scheme == 'https'
32-
}.raises Puppet::HTTP::HTTPError, 'Connection refused'
28+
allow(http).to receive(:get) do |uri, _opts|
29+
raise(Puppet::HTTP::HTTPError, 'Connection refused') if uri.hostname == 'mypuppetdb.com' && uri.port == 8080 && uri.scheme == 'https'
30+
raise Puppet::HTTP::ResponseError, Puppet::HTTP::ResponseNetHTTP.new(url, notfound) if uri.hostname == 'wrongserver.com' && uri.port == 8081 && uri.scheme == 'https'
31+
raise Puppet::HTTP::HTTPError, 'Unknown host' if uri.hostname == 'non-existing.com' && uri.scheme == 'https'
3332

34-
http.stubs(:get).with { |uri, _opts|
35-
uri.hostname == 'mypuppetdb.com' &&
36-
uri.port == 8080 &&
37-
uri.scheme == 'http'
38-
}.returns(Puppet::HTTP::ResponseNetHTTP.new(url, nethttpok))
39-
40-
http.stubs(:get).with { |uri, _opts|
41-
uri.hostname == 'mypuppetdb.com' &&
42-
uri.port == 8081 &&
43-
uri.scheme == 'https'
44-
}.returns(Puppet::HTTP::ResponseNetHTTP.new(url, nethttpok))
45-
46-
http.stubs(:get).with { |uri, _opts|
47-
uri.hostname == 'wrongserver.com' &&
48-
uri.port == 8081 &&
49-
uri.scheme == 'https'
50-
}.raises Puppet::HTTP::ResponseError, Puppet::HTTP::ResponseNetHTTP.new(url, notfound)
51-
52-
http.stubs(:get).with { |uri, _opts|
53-
uri.hostname == 'non-existing.com' &&
54-
uri.scheme == 'https'
55-
}.raises Puppet::HTTP::HTTPError, 'Unknown host'
33+
if uri.hostname == 'mypuppetdb.com' && uri.port == 8080 && uri.scheme == 'http'
34+
Puppet::HTTP::ResponseNetHTTP.new(url, nethttpok)
35+
elsif uri.hostname == 'mypuppetdb.com' && uri.port == 8081 && uri.scheme == 'https'
36+
Puppet::HTTP::ResponseNetHTTP.new(url, nethttpok)
37+
end
38+
end
5639
end
5740
end
5841

@@ -70,23 +53,23 @@
7053
puppetdb_server = 'mypuppetdb.com'
7154
puppetdb_port = 8080
7255
validator = Puppet::Util::PuppetdbValidator.new(puppetdb_server, puppetdb_port)
73-
Puppet.expects(:notice).with("Unable to connect to puppetdb server (https://#{puppetdb_server}:#{puppetdb_port}): Connection refused")
56+
expect(Puppet).to receive(:notice).with("Unable to connect to puppetdb server (https://#{puppetdb_server}:#{puppetdb_port}): Connection refused")
7457
expect(validator.attempt_connection).to be false
7558
end
7659

7760
it 'returns false and issues an appropriate notice if connection succeeds but puppetdb is not available' do
7861
puppetdb_server = 'wrongserver.com'
7962
puppetdb_port = 8081
8063
validator = Puppet::Util::PuppetdbValidator.new(puppetdb_server, puppetdb_port)
81-
Puppet.expects(:notice).with("Unable to connect to puppetdb server (https://#{puppetdb_server}:#{puppetdb_port}): [404] Not found")
64+
expect(Puppet).to receive(:notice).with("Unable to connect to puppetdb server (https://#{puppetdb_server}:#{puppetdb_port}): [404] Not found")
8265
expect(validator.attempt_connection).to be false
8366
end
8467

8568
it 'returns false and issues an appropriate notice if host:port is unreachable or does not exist' do
8669
puppetdb_server = 'non-existing.com'
8770
puppetdb_port = nil
8871
validator = Puppet::Util::PuppetdbValidator.new(puppetdb_server, puppetdb_port)
89-
Puppet.expects(:notice).with("Unable to connect to puppetdb server (https://#{puppetdb_server}:#{puppetdb_port}): Unknown host")
72+
expect(Puppet).to receive(:notice).with("Unable to connect to puppetdb server (https://#{puppetdb_server}:#{puppetdb_port}): Unknown host")
9073
expect(validator.attempt_connection).to be false
9174
end
9275
end

0 commit comments

Comments
 (0)