Add tests

This commit is contained in:
Donal McBreen
2024-08-14 09:49:36 +01:00
parent 0c6a593554
commit d5ecca0fd4
3 changed files with 44 additions and 31 deletions

View File

@@ -1,21 +1,21 @@
class Kamal::Cli::Secrets < Kamal::Cli::Base class Kamal::Cli::Secrets < Kamal::Cli::Base
desc "login", "Login to a secrets vault" desc "login", "Login to a secrets vault"
option :adapter, type: :string, aliases: "-a", required: true, desc: "Which vault adapter to use" option :adapter, type: :string, aliases: "-a", required: true, desc: "Which vault adapter to use"
option :adapter_options, type: :hash, aliases: "-O", required: true, desc: "Options to pass to the vault adapter" option :adapter_options, type: :hash, aliases: "-O", required: false, desc: "Options to pass to the vault adapter"
def login def login
puts adapter(options).login(**adapter_options(options)) puts adapter(options).login(**adapter_options(options))
end end
desc "fetch", "Fetch a secret from a vault" desc "fetch", "Fetch a secret from a vault"
option :adapter, type: :string, aliases: "-a", required: true, desc: "Which vault adapter to use" option :adapter, type: :string, aliases: "-a", required: true, desc: "Which vault adapter to use"
option :adapter_options, type: :hash, aliases: "-O", required: true, desc: "Options to pass to the vault adapter" option :adapter_options, type: :hash, aliases: "-O", required: false, desc: "Options to pass to the vault adapter"
def fetch(name) def fetch(name)
puts adapter(options).fetch(name, **adapter_options(options)) puts adapter(options).fetch(name, **adapter_options(options))
end end
desc "fetch_all", "Fetch multiple secrets from a vault" desc "fetch_all", "Fetch multiple secrets from a vault"
option :adapter, type: :string, aliases: "-a", required: true, desc: "Which vault adapter to use" option :adapter, type: :string, aliases: "-a", required: true, desc: "Which vault adapter to use"
option :adapter_options, type: :hash, aliases: "-O", required: true, desc: "Options to pass to the vault adapter" option :adapter_options, type: :hash, aliases: "-O", required: false, desc: "Options to pass to the vault adapter"
def fetch_all(*names) def fetch_all(*names)
puts JSON.dump(adapter(options).fetch_all(*names, **adapter_options(options))).shellescape puts JSON.dump(adapter(options).fetch_all(*names, **adapter_options(options))).shellescape
end end
@@ -31,6 +31,6 @@ class Kamal::Cli::Secrets < Kamal::Cli::Base
end end
def adapter_options(options) def adapter_options(options)
options[:adapter_options].transform_keys(&:to_sym) options.fetch(:adapter_options, {}).transform_keys(&:to_sym)
end end
end end

View File

@@ -2,45 +2,41 @@ require_relative "cli_test_case"
class CliSecretsTest < CliTestCase class CliSecretsTest < CliTestCase
test "login" do test "login" do
run_command("login").tap do |output| assert_equal "LOGIN_TOKEN", run_command("login", "--adapter", "test")
assert_match /docker login -u \[REDACTED\] -p \[REDACTED\] as .*@localhost/, output end
assert_match /docker login -u \[REDACTED\] -p \[REDACTED\] on 1.1.1.\d/, output
test "login failed" do
assert_raises("Boom!") do
run_command("login", "--adapter", "test", "--adapter-options", "boom:true")
end end
end end
test "fetch" do test "fetch" do
run_command("login", "-L").tap do |output| assert_equal "oof", run_command("fetch", "foo", "--adapter", "test")
assert_no_match /docker login -u \[REDACTED\] -p \[REDACTED\] as .*@localhost/, output end
assert_match /docker login -u \[REDACTED\] -p \[REDACTED\] on 1.1.1.\d/, output
test "fetch failed" do
assert_raises("Boom!") do
run_command("fetch", "foo", "--adapter", "test", "--adapter-options", "boom:true")
end end
end end
test "fetch_all" do test "fetch_all" do
run_command("login", "-R").tap do |output| assert_equal \
assert_match /docker login -u \[REDACTED\] -p \[REDACTED\] as .*@localhost/, output "\\{\\\"foo\\\":\\\"oof\\\",\\\"bar\\\":\\\"rab\\\",\\\"baz\\\":\\\"zab\\\"\\}",
assert_no_match /docker login -u \[REDACTED\] -p \[REDACTED\] on 1.1.1.\d/, output run_command("fetch_all", "foo", "bar", "baz", "--adapter", "test")
end
test "fetch_all failed" do
assert_raises("Boom!") do
assert_equal \
"\\{\\\"foo\\\":\\\"oof\\\",\\\"bar\\\":\\\"rab\\\",\\\"baz\\\":\\\"zab\\\"\\}",
run_command("fetch_all", "foo", "bar", "baz", "--adapter", "test", "--adapter-options", "boom:true")
end end
end end
test "extract" do test "extract" do
run_command("logout").tap do |output| assert_equal "oof", run_command("extract", "foo", "{\"foo\":\"oof\", \"bar\":\"rab\", \"baz\":\"zab\"}")
assert_match /docker logout as .*@localhost/, output
assert_match /docker logout on 1.1.1.\d/, output
end
end
test "logout skip local" do
run_command("logout", "-L").tap do |output|
assert_no_match /docker logout as .*@localhost/, output
assert_match /docker logout on 1.1.1.\d/, output
end
end
test "logout skip remote" do
run_command("logout", "-R").tap do |output|
assert_match /docker logout as .*@localhost/, output
assert_no_match /docker logout on 1.1.1.\d/, output
end
end end
private private

View File

@@ -63,3 +63,20 @@ class ActiveSupport::TestCase
FileUtils.rm_rf(@secrets_tmpdir) FileUtils.rm_rf(@secrets_tmpdir)
end end
end end
class Kamal::Secrets::Adapters::Test
def login(boom: false)
raise "Boom!" if boom
"LOGIN_TOKEN"
end
def fetch(name, boom: false)
raise "Boom!" if boom
name.reverse
end
def fetch_all(*names, boom: false)
raise "Boom!" if boom
names.to_h { |name| [ name, name.reverse ] }
end
end