Skip to content

Commit a89a3ed

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

File tree

1 file changed

+32
-44
lines changed

1 file changed

+32
-44
lines changed

spec/unit/util/puppetdb_validator_spec.rb

Lines changed: 32 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -8,51 +8,36 @@
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'
33-
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'
28+
allow(http).to receive(:get) do |uri, _opts|
29+
if uri.hostname == 'mypuppetdb.com' && uri.port == 8080 && uri.scheme == 'https'
30+
raise(Puppet::HTTP::HTTPError, 'Connection refused')
31+
elsif uri.hostname == 'mypuppetdb.com' && uri.port == 8080 && uri.scheme == 'http'
32+
Puppet::HTTP::ResponseNetHTTP.new(url, nethttpok)
33+
elsif uri.hostname == 'mypuppetdb.com' && uri.port == 8081 && uri.scheme == 'https'
34+
Puppet::HTTP::ResponseNetHTTP.new(url, nethttpok)
35+
elsif uri.hostname == 'wrongserver.com' && uri.port == 8081 && uri.scheme == 'https'
36+
raise Puppet::HTTP::ResponseError, Puppet::HTTP::ResponseNetHTTP.new(url, notfound)
37+
elsif uri.hostname == 'non-existing.com' && uri.scheme == 'https'
38+
raise Puppet::HTTP::HTTPError, 'Unknown host'
39+
end
40+
end
5641
end
5742
end
5843

@@ -70,23 +55,26 @@
7055
puppetdb_server = 'mypuppetdb.com'
7156
puppetdb_port = 8080
7257
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")
58+
allow(Puppet).to receive(:notice).with("Unable to connect to puppetdb server (https://#{puppetdb_server}:#{puppetdb_port}): Connection refused")
7459
expect(validator.attempt_connection).to be false
60+
expect(Puppet).to have_received(:notice).with("Unable to connect to puppetdb server (https://#{puppetdb_server}:#{puppetdb_port}): Connection refused")
7561
end
7662

7763
it 'returns false and issues an appropriate notice if connection succeeds but puppetdb is not available' do
7864
puppetdb_server = 'wrongserver.com'
7965
puppetdb_port = 8081
8066
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")
67+
allow(Puppet).to receive(:notice).with("Unable to connect to puppetdb server (https://#{puppetdb_server}:#{puppetdb_port}): [404] Not found")
8268
expect(validator.attempt_connection).to be false
69+
expect(Puppet).to have_received(:notice).with("Unable to connect to puppetdb server (https://#{puppetdb_server}:#{puppetdb_port}): [404] Not found")
8370
end
8471

8572
it 'returns false and issues an appropriate notice if host:port is unreachable or does not exist' do
8673
puppetdb_server = 'non-existing.com'
8774
puppetdb_port = nil
8875
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")
76+
allow(Puppet).to receive(:notice).with("Unable to connect to puppetdb server (https://#{puppetdb_server}:#{puppetdb_port}): Unknown host")
9077
expect(validator.attempt_connection).to be false
78+
expect(Puppet).to have_received(:notice).with("Unable to connect to puppetdb server (https://#{puppetdb_server}:#{puppetdb_port}): Unknown host")
9179
end
9280
end

0 commit comments

Comments
 (0)