Skip to content

Commit

Permalink
spec: use test_action helper for action tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan Koch committed Jun 22, 2023
1 parent c18ab34 commit 940784d
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 258 deletions.
16 changes: 4 additions & 12 deletions spec/hcloud/certificate_actions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
require 'spec_helper'

describe Hcloud::Certificate, doubles: :certificate do
include_context 'test doubles'
include_context 'action tests'

let :certificates do
Array.new(Faker::Number.within(range: 10..50)).map { new_certificate }
end
Expand All @@ -21,18 +24,7 @@

context '#retry' do
it 'works' do
stub_action(:certificates, certificate[:id], :retry) do |_req, _info|
{
action: build_action_resp(
:issue_certificate, :running,
resources: [{ id: 42, type: 'certificate' }]
)
}
end

action = certificate_obj.retry
expect(action).to be_a Hcloud::Action
expect(action.command).to eq('issue_certificate')
test_action(:retry, :issue_certificate)
end
end
end
77 changes: 21 additions & 56 deletions spec/hcloud/firewall_actions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
require 'spec_helper'

describe Hcloud::Firewall, doubles: :firewall do
include_context 'test doubles'
include_context 'action tests'

let :firewalls do
Array.new(Faker::Number.within(range: 20..150)).map { new_firewall }
end
Expand Down Expand Up @@ -49,25 +52,12 @@ def actions_resource_ids(actions)
{ 'server' => { 'id' => 42 }, 'type' => 'server' },
{ 'server' => { 'id' => 1 }, 'type' => 'server' }
]
stub_action(:firewalls, firewall[:id], :apply_to_resources) do |_req, _info|
{
actions: [
build_action_resp(
:apply_firewall, :running,
resources: [{ id: 42, type: 'server' }, { id: firewall[:id], type: 'firewall' }]
),
build_action_resp(
:apply_firewall, :running,
resources: [{ id: 1, type: 'server' }, { id: firewall[:id], type: 'firewall' }]
)
]
}
end

actions = firewall_obj.apply_to_resources(apply_to: apply_to)
expect(actions.length).to eq(2)
expect(actions[0]).to be_a(Hcloud::Action)
expect(actions_resource_ids(actions)).to include(1, 42, firewall[:id])
test_action(
:apply_to_resources,
:apply_firewall,
params: { apply_to: apply_to },
additional_resources: %i[server]
)
end
end

Expand Down Expand Up @@ -98,25 +88,12 @@ def actions_resource_ids(actions)
{ 'server' => { 'id' => 42 }, 'type' => 'server' },
{ 'server' => { 'id' => 1 }, 'type' => 'server' }
]
stub_action(:firewalls, firewall[:id], :remove_from_resources) do |_req, _info|
{
actions: [
build_action_resp(
:remove_firewall, :running,
resources: [{ id: 42, type: 'server' }, { id: firewall[:id], type: 'firewall' }]
),
build_action_resp(
:remove_firewall, :running,
resources: [{ id: 1, type: 'server' }, { id: firewall[:id], type: 'firewall' }]
)
]
}
end

actions = firewall_obj.remove_from_resources(remove_from: remove_from)
expect(actions.length).to eq(2)
expect(actions[0]).to be_a(Hcloud::Action)
expect(actions_resource_ids(actions)).to include(1, 42, firewall[:id])
test_action(
:remove_from_resources,
:remove_firewall,
params: { remove_from: remove_from },
additional_resources: %i[server]
)
end
end

Expand Down Expand Up @@ -146,24 +123,12 @@ def actions_resource_ids(actions)
rules = [
{ protocol: 'tcp', port: 80, direction: 'in', source_ips: '0.0.0.0/0' }
]
expectation = stub_action(:firewalls, firewall[:id], :set_rules) do |req, _info|
expect(req).to have_body_params(a_hash_including({ 'rules' => rules }.deep_stringify_keys))

{
actions: [
build_action_resp(
:set_firewall_rules, :running,
resources: [{ id: firewall[:id], type: 'firewall' }]
)
]
}
end

actions = firewall_obj.set_rules(rules: rules)
expect(actions.length).to eq(1)
expect(actions[0]).to be_a(Hcloud::Action)
expect(actions_resource_ids(actions)).to eq([firewall[:id]])
expect(expectation.times_called).to eq(1)
test_action(
:set_rules,
:set_firewall_rules,
params: { rules: rules },
additional_resources: %i[server]
)
end
end
end
71 changes: 15 additions & 56 deletions spec/hcloud/floating_ip_actions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
require 'spec_helper'

describe Hcloud::FloatingIP, doubles: :floating_ip do
include_context 'test doubles'
include_context 'action tests'

let :floating_ips do
Array.new(Faker::Number.within(range: 20..150)).map { new_floating_ip }
end
Expand All @@ -27,45 +30,22 @@
end

it 'works' do
expectation = stub_action(:floating_ips, floating_ip[:id], :assign) do |req, _info|
expect(req).to have_body_params(a_hash_including({ 'server' => 42 }))

{
action: build_action_resp(
:assign_floating_ip, :success,
resources: [
{ id: 42, type: 'server' },
{ id: floating_ip[:id], type: 'floating_ip' }
]
)
}
end

action = floating_ip_obj.assign(server: 42)
expect(expectation.times_called).to eq(1)
expect(action).to be_a(Hcloud::Action)
expect(action.resources.map { |res| res['id'] }).to eq([42, floating_ip[:id]])
test_action(
:assign,
:assign_floating_ip,
params: { server: 42 },
additional_resources: %i[server]
)
end
end

context '#unassign' do
it 'works' do
expectation = stub_action(:floating_ips, floating_ip[:id], :unassign) do |_req, _info|
{
action: build_action_resp(
:unassign_floating_ip, :success,
resources: [
{ id: 42, type: 'server' },
{ id: floating_ip[:id], type: 'floating_ip' }
]
)
}
end

action = floating_ip_obj.unassign
expect(expectation.times_called).to eq(1)
expect(action).to be_a(Hcloud::Action)
expect(action.resources.map { |res| res['id'] }).to eq([42, floating_ip[:id]])
test_action(
:unassign,
:unassign_floating_ip,
additional_resources: %i[server]
)
end
end

Expand All @@ -77,28 +57,7 @@
end

it 'works' do
expectation = stub_action(:floating_ips, floating_ip[:id], :change_dns_ptr) do |req, _info|
expect(req).to have_body_params(
a_hash_including(
{ 'ip' => '2001:db8::1', 'dns_ptr' => 'example.com' }
)
)

{
action: build_action_resp(
:change_dns_ptr, :success,
resources: [
{ id: floating_ip[:id], type: 'floating_ip' }
]
)
}
end

action = floating_ip_obj.change_dns_ptr(ip: '2001:db8::1', dns_ptr: 'example.com')
expect(expectation.times_called).to eq(1)
expect(action).to be_a(Hcloud::Action)
expect(action.command).to eq('change_dns_ptr')
expect(action.resources[0]['id']).to eq(floating_ip[:id])
test_action(:change_dns_ptr, params: { ip: '2001:db8::1', dns_ptr: 'example.com' })
end
end
end
77 changes: 9 additions & 68 deletions spec/hcloud/network_actions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
require 'spec_helper'

describe Hcloud::Network, doubles: :network do
include_context 'test doubles'
include_context 'action tests'

let :networks do
Array.new(Faker::Number.within(range: 20..150)).map { new_network }
end
Expand All @@ -19,8 +22,6 @@
client.networks[network[:id]]
end

# TODO: Also this spec could profit from a standardization mechanism for action tests like
# server and load balancer
context '#add_route' do
it 'handles missing destination' do
expect do
Expand All @@ -35,25 +36,7 @@
end

it 'works' do
expectation = stub_action(:networks, network[:id], :add_route) do |req, _info|
expect(req).to have_body_params(
a_hash_including(
{ 'destination' => '192.168.0.0/24', 'gateway' => '192.168.2.2' }
)
)

{
action: build_action_resp(
:add_route, :running,
resources: [{ id: network[:id], type: 'network' }]
)
}
end

action = network_obj.add_route(destination: '192.168.0.0/24', gateway: '192.168.2.2')
expect(expectation.times_called).to eq(1)
expect(action).to be_a(Hcloud::Action)
expect(action.resources[0]['id']).to eq(network[:id])
test_action(:add_route, params: { destination: '192.168.0.0/24', gateway: '192.168.2.2' })
end
end

Expand Down Expand Up @@ -111,27 +94,10 @@
end

it 'works' do
expectation = stub_action(:networks, network[:id], :add_subnet) do |req, _info|
expect(req).to have_body_params(
a_hash_including(
{ 'ip_range' => '10.0.0.0/24', 'network_zone' => 'eu-central', 'type' => 'cloud' }
)
)

{
action: build_action_resp(
:add_subnet, :running,
resources: [{ id: network[:id], type: 'network' }]
)
}
end

action = network_obj.add_subnet(
ip_range: '10.0.0.0/24', network_zone: 'eu-central', type: 'cloud'
test_action(
:add_subnet,
params: { ip_range: '10.0.0.0/24', network_zone: 'eu-central', type: 'cloud' }
)
expect(expectation.times_called).to eq(1)
expect(action).to be_a(Hcloud::Action)
expect(action.resources[0]['id']).to eq(network[:id])
end
end

Expand Down Expand Up @@ -169,38 +135,13 @@
end

it 'works' do
expectation = stub_action(:networks, network[:id], :change_ip_range) do |req, _info|
expect(req).to have_body_params(a_hash_including({ 'ip_range' => '10.0.0.0/24' }))

{
action: build_action_resp(
:change_ip_range, :running,
resources: [{ id: network[:id], type: 'network' }]
)
}
end

action = network_obj.change_ip_range(ip_range: '10.0.0.0/24')
expect(expectation.times_called).to eq(1)
expect(action).to be_a(Hcloud::Action)
expect(action.resources[0]['id']).to eq(network[:id])
test_action(:change_ip_range, params: { ip_range: '10.0.0.0/24' })
end
end

context '#change_protection' do
it 'works' do
expectation = stub_action(:networks, network[:id], :change_protection) do |_req, _info|
{
action: build_action_resp(
:change_protection, :running,
resources: [{ id: network[:id], type: 'network' }]
)
}
end

action = network_obj.change_protection
expect(expectation.times_called).to eq(1)
expect(action).to be_a(Hcloud::Action)
test_action(:change_protection, params: { delete: true })
end
end
end
Loading

0 comments on commit 940784d

Please sign in to comment.