Skip to content

Commit

Permalink
Validate volumes provided in the service configuration file
Browse files Browse the repository at this point in the history
  • Loading branch information
sarahloui committed Sep 1, 2023
1 parent 342ca4f commit 5671d30
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
7 changes: 7 additions & 0 deletions lib/cove/configuration/contracts/service_contract.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ class ServiceContract < Dry::Validation::Contract
required(:target).filled(:integer)
end
end
optional(:mounts).value(:array).each do
hash do
required(:type).value(eql?: "volume")
required(:source).filled(:string)
required(:target).filled(:string)
end
end
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/cove/configuration/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def validate!

def formatted_roles
config["roles"].map do |role_name, role_config|
{name: role_name, container_count: role_config["container_count"], ingress: role_config["ingress"]}.compact
{name: role_name, container_count: role_config["container_count"], ingress: role_config["ingress"], mounts: role_config["mounts"]}.compact
end
end
end
Expand Down
36 changes: 36 additions & 0 deletions spec/cove/configuration/contracts/service_contract_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -126,5 +126,41 @@
expect(result.success?).to eq(false)
end
end

context "mounts" do
it "succeeds when mounts are included" do
contract = Cove::Configuration::Contracts::ServiceContract.new
result = contract.call(service_name: "a", image: "b", roles: [{name: "web", mounts: [{type: "volume", source: "my-awesome-volume", target: "/data"}]}])
expect(result.success?).to eq(true)
end

it "fails when type is invalid" do
contract = Cove::Configuration::Contracts::ServiceContract.new
result = contract.call(service_name: "a", image: "b", roles: [{name: "web", mounts: [{type: "foo", source: "my-awesome-volume", target: "/data"}]}])
expect(result.errors[:roles][0][:mounts][0][:type]).to be_present
expect(result.success?).to eq(false)
end

it "fails when type is not included" do
contract = Cove::Configuration::Contracts::ServiceContract.new
result = contract.call(service_name: "a", image: "b", roles: [{name: "web", mounts: [{source: "my-awesome-volume", target: "/data"}]}])
expect(result.errors[:roles][0][:mounts][0][:type]).to be_present
expect(result.success?).to eq(false)
end

it "fails when source is not included" do
contract = Cove::Configuration::Contracts::ServiceContract.new
result = contract.call(service_name: "a", image: "b", roles: [{name: "web", mounts: [{type: "volume", target: "/data"}]}])
expect(result.errors[:roles][0][:mounts][0][:source]).to be_present
expect(result.success?).to eq(false)
end

it "fails when target is not included" do
contract = Cove::Configuration::Contracts::ServiceContract.new
result = contract.call(service_name: "a", image: "b", roles: [{name: "web", mounts: [{type: "volume", source: "my-awesome-volume"}]}])
expect(result.errors[:roles][0][:mounts][0][:target]).to be_present
expect(result.success?).to eq(false)
end
end
end
end

0 comments on commit 5671d30

Please sign in to comment.