Validate that all roles have hosts

This commit is contained in:
Jeremy Daer
2023-03-22 18:56:57 -07:00
parent 1887a6518e
commit 035e4afff7
3 changed files with 57 additions and 9 deletions

View File

@@ -80,7 +80,7 @@ class Mrsk::Configuration
end end
def primary_web_host def primary_web_host
role(:web).hosts.first role(:web).primary_host
end end
def traefik_hosts def traefik_hosts
@@ -196,6 +196,12 @@ class Mrsk::Configuration
raise ArgumentError, "You must specify a password for the registry in config/deploy.yml (or set the ENV variable if that's used)" raise ArgumentError, "You must specify a password for the registry in config/deploy.yml (or set the ENV variable if that's used)"
end end
roles.each do |role|
if role.hosts.empty?
raise ArgumentError, "No servers specified for the #{role.name} role"
end
end
true true
end end

View File

@@ -7,6 +7,10 @@ class Mrsk::Configuration::Role
@name, @config = name.inquiry, config @name, @config = name.inquiry, config
end end
def primary_host
hosts.first
end
def hosts def hosts
@hosts ||= extract_hosts_from_config @hosts ||= extract_hosts_from_config
end end
@@ -55,7 +59,7 @@ class Mrsk::Configuration::Role
config.servers config.servers
else else
servers = config.servers[name] servers = config.servers[name]
servers.is_a?(Array) ? servers : servers["hosts"] servers.is_a?(Array) ? servers : Array(servers["hosts"])
end end
end end

View File

@@ -26,14 +26,19 @@ class ConfigurationTest < ActiveSupport::TestCase
ENV.delete("VERSION") ENV.delete("VERSION")
end end
test "ensure valid keys" do %i[ service image registry ].each do |key|
test "#{key} config required" do
assert_raise(ArgumentError) do assert_raise(ArgumentError) do
Mrsk::Configuration.new(@deploy.tap { _1.delete(:service) }) Mrsk::Configuration.new @deploy.tap { _1.delete key }
Mrsk::Configuration.new(@deploy.tap { _1.delete(:image) }) end
Mrsk::Configuration.new(@deploy.tap { _1.delete(:registry) }) end
end
Mrsk::Configuration.new(@deploy.tap { _1[:registry].delete("username") }) %w[ username password ].each do |key|
Mrsk::Configuration.new(@deploy.tap { _1[:registry].delete("password") }) test "registry #{key} required" do
assert_raise(ArgumentError) do
Mrsk::Configuration.new @deploy.tap { _1[:registry].delete key }
end
end end
end end
@@ -148,6 +153,39 @@ class ConfigurationTest < ActiveSupport::TestCase
test "valid config" do test "valid config" do
assert @config.valid? assert @config.valid?
assert @config_with_roles.valid?
end
test "hosts required for all roles" do
# Empty server list for implied web role
assert_raises(ArgumentError) do
Mrsk::Configuration.new @deploy.merge(servers: [])
end
# Empty server list
assert_raises(ArgumentError) do
Mrsk::Configuration.new @deploy.merge(servers: { "web" => [] })
end
# Missing hosts key
assert_raises(ArgumentError) do
Mrsk::Configuration.new @deploy.merge(servers: { "web" => {} })
end
# Empty hosts list
assert_raises(ArgumentError) do
Mrsk::Configuration.new @deploy.merge(servers: { "web" => { "hosts" => [] } })
end
# Nil hosts
assert_raises(ArgumentError) do
Mrsk::Configuration.new @deploy.merge(servers: { "web" => { "hosts" => nil } })
end
# One role with hosts, one without
assert_raises(ArgumentError) do
Mrsk::Configuration.new @deploy.merge(servers: { "web" => %w[ web ], "workers" => { "hosts" => %w[ ] } })
end
end end
test "ssh options" do test "ssh options" do