Configuration validation

Validate the Kamal configuration giving useful warning on errors.
Each section of the configuration has its own config class and a YAML
file containing documented example configuration.

You can run `kamal docs` to see the example configuration, and
`kamal docs <section>` to see the example configuration for a specific
section.

The validation matches the configuration to the example configuration
checking that there are no unknown keys and that the values are of
matching types.

Where there is more complex validation - e.g for envs and servers, we
have custom validators that implement those rules.

Additonally the configuration examples are used to generate the
configuration documentation in the kamal-site repo.

You generate them by running:

```
bundle exec bin/docs <kamal-site-checkout>
```
This commit is contained in:
Donal McBreen
2024-05-28 09:25:42 +01:00
parent 6e60ab918a
commit 4f317b8499
59 changed files with 1942 additions and 480 deletions

View File

@@ -7,41 +7,37 @@ class ConfigurationBuilderTest < ActiveSupport::TestCase
servers: [ "1.1.1.1" ]
}
@config = Kamal::Configuration.new(@deploy)
@deploy_with_builder_option = {
service: "app", image: "dhh/app", registry: { "username" => "dhh", "password" => "secret" },
servers: [ "1.1.1.1" ],
builder: {}
}
@config_with_builder_option = Kamal::Configuration.new(@deploy_with_builder_option)
end
test "multiarch?" do
assert_equal true, @config.builder.multiarch?
assert_equal true, config.builder.multiarch?
end
test "setting multiarch to false" do
@deploy_with_builder_option[:builder] = { "multiarch" => false }
assert_equal false, @config_with_builder_option.builder.multiarch?
assert_equal false, config_with_builder_option.builder.multiarch?
end
test "local?" do
assert_equal false, @config.builder.local?
assert_equal false, config.builder.local?
end
test "remote?" do
assert_equal false, @config.builder.remote?
assert_equal false, config.builder.remote?
end
test "remote_arch" do
assert_nil @config.builder.remote_arch
assert_nil config.builder.remote_arch
end
test "remote_host" do
assert_nil @config.builder.remote_host
assert_nil config.builder.remote_host
end
test "setting both local and remote configs" do
@@ -50,112 +46,121 @@ class ConfigurationBuilderTest < ActiveSupport::TestCase
"remote" => { "arch" => "amd64", "host" => "ssh://root@192.168.0.1" }
}
assert_equal true, @config_with_builder_option.builder.local?
assert_equal true, @config_with_builder_option.builder.remote?
assert_equal true, config_with_builder_option.builder.local?
assert_equal true, config_with_builder_option.builder.remote?
assert_equal "amd64", @config_with_builder_option.builder.remote_arch
assert_equal "ssh://root@192.168.0.1", @config_with_builder_option.builder.remote_host
assert_equal "amd64", config_with_builder_option.builder.remote_arch
assert_equal "ssh://root@192.168.0.1", config_with_builder_option.builder.remote_host
assert_equal "arm64", @config_with_builder_option.builder.local_arch
assert_equal "unix:///Users/<%= `whoami`.strip %>/.docker/run/docker.sock", @config_with_builder_option.builder.local_host
assert_equal "arm64", config_with_builder_option.builder.local_arch
assert_equal "unix:///Users/<%= `whoami`.strip %>/.docker/run/docker.sock", config_with_builder_option.builder.local_host
end
test "cached?" do
assert_equal false, @config.builder.cached?
assert_equal false, config.builder.cached?
end
test "invalid cache type specified" do
@deploy_with_builder_option[:builder] = { "cache" => { "type" => "invalid" } }
assert_raises(ArgumentError) do
@config_with_builder_option.builder
assert_raises(Kamal::ConfigurationError) do
config_with_builder_option.builder
end
end
test "cache_from" do
assert_nil @config.builder.cache_from
assert_nil config.builder.cache_from
end
test "cache_to" do
assert_nil @config.builder.cache_to
assert_nil config.builder.cache_to
end
test "setting gha cache" do
@deploy_with_builder_option[:builder] = { "cache" => { "type" => "gha", "options" => "mode=max" } }
assert_equal "type=gha", @config_with_builder_option.builder.cache_from
assert_equal "type=gha,mode=max", @config_with_builder_option.builder.cache_to
assert_equal "type=gha", config_with_builder_option.builder.cache_from
assert_equal "type=gha,mode=max", config_with_builder_option.builder.cache_to
end
test "setting registry cache" do
@deploy_with_builder_option[:builder] = { "cache" => { "type" => "registry", "options" => "mode=max,image-manifest=true,oci-mediatypes=true" } }
assert_equal "type=registry,ref=dhh/app-build-cache", @config_with_builder_option.builder.cache_from
assert_equal "type=registry,mode=max,image-manifest=true,oci-mediatypes=true,ref=dhh/app-build-cache", @config_with_builder_option.builder.cache_to
assert_equal "type=registry,ref=dhh/app-build-cache", config_with_builder_option.builder.cache_from
assert_equal "type=registry,mode=max,image-manifest=true,oci-mediatypes=true,ref=dhh/app-build-cache", config_with_builder_option.builder.cache_to
end
test "setting registry cache when using a custom registry" do
@config_with_builder_option.registry["server"] = "registry.example.com"
@deploy_with_builder_option[:registry]["server"] = "registry.example.com"
@deploy_with_builder_option[:builder] = { "cache" => { "type" => "registry", "options" => "mode=max,image-manifest=true,oci-mediatypes=true" } }
assert_equal "type=registry,ref=registry.example.com/dhh/app-build-cache", @config_with_builder_option.builder.cache_from
assert_equal "type=registry,mode=max,image-manifest=true,oci-mediatypes=true,ref=registry.example.com/dhh/app-build-cache", @config_with_builder_option.builder.cache_to
assert_equal "type=registry,ref=registry.example.com/dhh/app-build-cache", config_with_builder_option.builder.cache_from
assert_equal "type=registry,mode=max,image-manifest=true,oci-mediatypes=true,ref=registry.example.com/dhh/app-build-cache", config_with_builder_option.builder.cache_to
end
test "setting registry cache with image" do
@deploy_with_builder_option[:builder] = { "cache" => { "type" => "registry", "image" => "kamal", "options" => "mode=max" } }
assert_equal "type=registry,ref=kamal", @config_with_builder_option.builder.cache_from
assert_equal "type=registry,mode=max,ref=kamal", @config_with_builder_option.builder.cache_to
assert_equal "type=registry,ref=kamal", config_with_builder_option.builder.cache_from
assert_equal "type=registry,mode=max,ref=kamal", config_with_builder_option.builder.cache_to
end
test "args" do
assert_equal({}, @config.builder.args)
assert_equal({}, config.builder.args)
end
test "setting args" do
@deploy_with_builder_option[:builder] = { "args" => { "key" => "value" } }
assert_equal({ "key" => "value" }, @config_with_builder_option.builder.args)
assert_equal({ "key" => "value" }, config_with_builder_option.builder.args)
end
test "secrets" do
assert_equal [], @config.builder.secrets
assert_equal [], config.builder.secrets
end
test "setting secrets" do
@deploy_with_builder_option[:builder] = { "secrets" => [ "GITHUB_TOKEN" ] }
assert_equal [ "GITHUB_TOKEN" ], @config_with_builder_option.builder.secrets
assert_equal [ "GITHUB_TOKEN" ], config_with_builder_option.builder.secrets
end
test "dockerfile" do
assert_equal "Dockerfile", @config.builder.dockerfile
assert_equal "Dockerfile", config.builder.dockerfile
end
test "setting dockerfile" do
@deploy_with_builder_option[:builder] = { "dockerfile" => "Dockerfile.dev" }
assert_equal "Dockerfile.dev", @config_with_builder_option.builder.dockerfile
assert_equal "Dockerfile.dev", config_with_builder_option.builder.dockerfile
end
test "context" do
assert_equal ".", @config.builder.context
assert_equal ".", config.builder.context
end
test "setting context" do
@deploy_with_builder_option[:builder] = { "context" => ".." }
assert_equal "..", @config_with_builder_option.builder.context
assert_equal "..", config_with_builder_option.builder.context
end
test "ssh" do
assert_nil @config.builder.ssh
assert_nil config.builder.ssh
end
test "setting ssh params" do
@deploy_with_builder_option[:builder] = { "ssh" => "default=$SSH_AUTH_SOCK" }
assert_equal "default=$SSH_AUTH_SOCK", @config_with_builder_option.builder.ssh
assert_equal "default=$SSH_AUTH_SOCK", config_with_builder_option.builder.ssh
end
private
def config
Kamal::Configuration.new(@deploy)
end
def config_with_builder_option
Kamal::Configuration.new(@deploy_with_builder_option)
end
end