Skip to content

Commit

Permalink
Fix/task image subs (#13)
Browse files Browse the repository at this point in the history
* fix: fnsub not working for repo

* fix: escape : char

* fix: escape : char (2)

* fix: need to use FnJoin because if tag_param is there then it will use Ref

* fix: spec files
  • Loading branch information
raykrishardi authored May 14, 2024
1 parent e8e8ca3 commit 2cd8b4a
Show file tree
Hide file tree
Showing 9 changed files with 425 additions and 386 deletions.
4 changes: 2 additions & 2 deletions ecs-task.cfndsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@

name = task.has_key?('name') ? task['name'] : task_name

image_repo = task.has_key?('repo') ? FnSub("#{task['repo']}/") : ''
image_repo = task.has_key?('repo') ? "#{task['repo']}" : ''
image_name = task.has_key?('image') ? task['image'] : task_name
image_tag = task.has_key?('tag') ? "#{task['tag']}" : 'latest'
image_tag = task.has_key?('tag_param') ? Ref("#{task['tag_param']}") : image_tag

# create main definition
task_def = {
Name: name,
Image: FnJoin('',[ image_repo, image_name, ":", image_tag ]),
Image: FnJoin('', [FnSub("#{image_repo}/#{image_name}"), ":", image_tag]),
LogConfiguration: {
LogDriver: 'awslogs',
Options: {
Expand Down
31 changes: 20 additions & 11 deletions spec/default_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'yaml'

describe 'compiled component' do
describe 'compiled component ecs-task' do

context 'cftest' do
it 'compiles test' do
Expand All @@ -9,18 +9,27 @@
end

let(:template) { YAML.load_file("#{File.dirname(__FILE__)}/../out/tests/default/ecs-task.compiled.yaml") }

context "Resource" do

context 'Resources' do
it 'has No Task ' do
expect(template["Resources"]['Task']).to eq(nil)
end

context "LogGroup" do
let(:resource) { template["Resources"]["LogGroup"] }

it 'has a Log Group' do
expect(template["Resources"]['LogGroup']).to eq({
"Type"=>"AWS::Logs::LogGroup",
"Properties"=>{"LogGroupName"=>{"Ref"=>"AWS::StackName"}, "RetentionInDays"=>7}
})
it "is of type AWS::Logs::LogGroup" do
expect(resource["Type"]).to eq("AWS::Logs::LogGroup")
end

it "to have property LogGroupName" do
expect(resource["Properties"]["LogGroupName"]).to eq({"Ref"=>"AWS::StackName"})
end

it "to have property RetentionInDays" do
expect(resource["Properties"]["RetentionInDays"]).to eq(7)
end

end

end

end
end
56 changes: 56 additions & 0 deletions spec/depends_on_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
require 'yaml'

describe 'compiled component ecs-task' do

context 'cftest' do
it 'compiles test' do
expect(system("cfhighlander cftest #{@validate} --tests tests/depends_on.test.yaml")).to be_truthy
end
end

let(:template) { YAML.load_file("#{File.dirname(__FILE__)}/../out/tests/depends_on/ecs-task.compiled.yaml") }

context "Resource" do


context "LogGroup" do
let(:resource) { template["Resources"]["LogGroup"] }

it "is of type AWS::Logs::LogGroup" do
expect(resource["Type"]).to eq("AWS::Logs::LogGroup")
end

it "to have property LogGroupName" do
expect(resource["Properties"]["LogGroupName"]).to eq({"Ref"=>"AWS::StackName"})
end

it "to have property RetentionInDays" do
expect(resource["Properties"]["RetentionInDays"]).to eq(7)
end

end

context "Task" do
let(:resource) { template["Resources"]["Task"] }

it "is of type AWS::ECS::TaskDefinition" do
expect(resource["Type"]).to eq("AWS::ECS::TaskDefinition")
end

it "to have property ContainerDefinitions" do
expect(resource["Properties"]["ContainerDefinitions"]).to eq([{"Name"=>"nginx", "Image"=>{"Fn::Join"=>["", [{"Fn::Sub"=>"nginx/nginx"}, ":", "latest"]]}, "LogConfiguration"=>{"LogDriver"=>"awslogs", "Options"=>{"awslogs-group"=>{"Ref"=>"LogGroup"}, "awslogs-region"=>{"Ref"=>"AWS::Region"}, "awslogs-stream-prefix"=>"nginx"}}, "DependsOn"=>[{"ContainerName"=>"db", "Condition"=>"START"}]}, {"Name"=>"db", "Image"=>{"Fn::Join"=>["", [{"Fn::Sub"=>"/postgres"}, ":", "latest"]]}, "LogConfiguration"=>{"LogDriver"=>"awslogs", "Options"=>{"awslogs-group"=>{"Ref"=>"LogGroup"}, "awslogs-region"=>{"Ref"=>"AWS::Region"}, "awslogs-stream-prefix"=>"db"}}, "Essential"=>false, "PortMappings"=>[{"ContainerPort"=>5432}]}])
end

it "to have property RequiresCompatibilities" do
expect(resource["Properties"]["RequiresCompatibilities"]).to eq(["EC2"])
end

it "to have property Tags" do
expect(resource["Properties"]["Tags"]).to eq([{"Key"=>"Name", "Value"=>"ecs-task"}, {"Key"=>"Environment", "Value"=>{"Ref"=>"EnvironmentName"}}, {"Key"=>"EnvironmentType", "Value"=>{"Ref"=>"EnvironmentType"}}])
end

end

end

end
78 changes: 38 additions & 40 deletions spec/ec2_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'yaml'

describe 'compiled component' do
describe 'compiled component ecs-task' do

context 'cftest' do
it 'compiles test' do
Expand All @@ -9,50 +9,48 @@
end

let(:template) { YAML.load_file("#{File.dirname(__FILE__)}/../out/tests/ec2/ecs-task.compiled.yaml") }

context "Resource" do

context 'Resource Task' do
let(:properties) { template["Resources"]["Task"]["Properties"] }

it 'has property RequiresCompatibilities ' do
expect(properties["RequiresCompatibilities"]).to eq(['EC2'])
end

it 'has property NetworkMode ' do
expect(properties["NetworkMode"]).to eq(nil)
end

it 'has property CPU ' do
expect(properties["Cpu"]).to eq(nil)
end
context "LogGroup" do
let(:resource) { template["Resources"]["LogGroup"] }

it 'has property Memory ' do
expect(properties["Memory"]).to eq(nil)
end

it 'has property One container definition ' do
expect(properties["ContainerDefinitions"].count).to eq(1)
expect(properties["ContainerDefinitions"]).to eq([{
"Image"=>{"Fn::Join"=>["", [{"Fn::Sub"=>"myrepo/"}, "backend", ":", {"Ref"=>"SchemaTag"}]]},
"LogConfiguration"=>
{
"LogDriver"=>"awslogs",
"Options"=> {
"awslogs-group"=>{"Ref"=>"LogGroup"},
"awslogs-region"=>{"Ref"=>"AWS::Region"},
"awslogs-stream-prefix"=>"schema"
}
},
"Name"=>"schema"
}])
it "is of type AWS::Logs::LogGroup" do
expect(resource["Type"]).to eq("AWS::Logs::LogGroup")
end

it "to have property LogGroupName" do
expect(resource["Properties"]["LogGroupName"]).to eq({"Ref"=>"AWS::StackName"})
end

it "to have property RetentionInDays" do
expect(resource["Properties"]["RetentionInDays"]).to eq(7)
end

end

context "Task" do
let(:resource) { template["Resources"]["Task"] }

it 'has property Tags' do
expect(properties["Tags"]).to eq([
{"Key"=>"Name", "Value"=>"ecs-task"},
{"Key"=>"Environment", "Value"=>{"Ref"=>"EnvironmentName"}},
{"Key"=>"EnvironmentType", "Value"=>{"Ref"=>"EnvironmentType"}},
{"Key"=>"CostCenter", "Value"=>"TeamA"}
])
it "is of type AWS::ECS::TaskDefinition" do
expect(resource["Type"]).to eq("AWS::ECS::TaskDefinition")
end

it "to have property ContainerDefinitions" do
expect(resource["Properties"]["ContainerDefinitions"]).to eq([{"Name"=>"schema", "Image"=>{"Fn::Join"=>["", [{"Fn::Sub"=>"myrepo/backend"}, ":", {"Ref"=>"SchemaTag"}]]}, "LogConfiguration"=>{"LogDriver"=>"awslogs", "Options"=>{"awslogs-group"=>{"Ref"=>"LogGroup"}, "awslogs-region"=>{"Ref"=>"AWS::Region"}, "awslogs-stream-prefix"=>"schema"}}}])
end

it "to have property RequiresCompatibilities" do
expect(resource["Properties"]["RequiresCompatibilities"]).to eq(["EC2"])
end

it "to have property Tags" do
expect(resource["Properties"]["Tags"]).to eq([{"Key"=>"Name", "Value"=>"ecs-task"}, {"Key"=>"Environment", "Value"=>{"Ref"=>"EnvironmentName"}}, {"Key"=>"EnvironmentType", "Value"=>{"Ref"=>"EnvironmentType"}}, {"Key"=>"CostCenter", "Value"=>"TeamA"}])
end

end

end

end
118 changes: 118 additions & 0 deletions spec/ecs-exec_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
require 'yaml'

describe 'compiled component ecs-task' do

context 'cftest' do
it 'compiles test' do
expect(system("cfhighlander cftest #{@validate} --tests tests/ecs-exec.test.yaml")).to be_truthy
end
end

let(:template) { YAML.load_file("#{File.dirname(__FILE__)}/../out/tests/ecs-exec/ecs-task.compiled.yaml") }

context "Resource" do


context "LogGroup" do
let(:resource) { template["Resources"]["LogGroup"] }

it "is of type AWS::Logs::LogGroup" do
expect(resource["Type"]).to eq("AWS::Logs::LogGroup")
end

it "to have property LogGroupName" do
expect(resource["Properties"]["LogGroupName"]).to eq({"Ref"=>"AWS::StackName"})
end

it "to have property RetentionInDays" do
expect(resource["Properties"]["RetentionInDays"]).to eq(7)
end

end

context "TaskRole" do
let(:resource) { template["Resources"]["TaskRole"] }

it "is of type AWS::IAM::Role" do
expect(resource["Type"]).to eq("AWS::IAM::Role")
end

it "to have property AssumeRolePolicyDocument" do
expect(resource["Properties"]["AssumeRolePolicyDocument"]).to eq({"Version"=>"2012-10-17", "Statement"=>[{"Effect"=>"Allow", "Principal"=>{"Service"=>"ecs-tasks.amazonaws.com"}, "Action"=>"sts:AssumeRole"}, {"Effect"=>"Allow", "Principal"=>{"Service"=>"ssm.amazonaws.com"}, "Action"=>"sts:AssumeRole"}]})
end

it "to have property Path" do
expect(resource["Properties"]["Path"]).to eq("/")
end

it "to have property Policies" do
expect(resource["Properties"]["Policies"]).to eq([{"PolicyName"=>"ssm-session-manager", "PolicyDocument"=>{"Version"=>"2012-10-17", "Statement"=>[{"Sid"=>"ssmsessionmanager", "Action"=>["ssmmessages:CreateControlChannel", "ssmmessages:CreateDataChannel", "ssmmessages:OpenControlChannel", "ssmmessages:OpenDataChannel"], "Resource"=>["*"], "Effect"=>"Allow"}]}}])
end

end

context "ExecutionRole" do
let(:resource) { template["Resources"]["ExecutionRole"] }

it "is of type AWS::IAM::Role" do
expect(resource["Type"]).to eq("AWS::IAM::Role")
end

it "to have property AssumeRolePolicyDocument" do
expect(resource["Properties"]["AssumeRolePolicyDocument"]).to eq({"Version"=>"2012-10-17", "Statement"=>[{"Effect"=>"Allow", "Principal"=>{"Service"=>"ecs-tasks.amazonaws.com"}, "Action"=>"sts:AssumeRole"}, {"Effect"=>"Allow", "Principal"=>{"Service"=>"ssm.amazonaws.com"}, "Action"=>"sts:AssumeRole"}]})
end

it "to have property Path" do
expect(resource["Properties"]["Path"]).to eq("/")
end

it "to have property ManagedPolicyArns" do
expect(resource["Properties"]["ManagedPolicyArns"]).to eq(["arn:aws:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy"])
end

end

context "Task" do
let(:resource) { template["Resources"]["Task"] }

it "is of type AWS::ECS::TaskDefinition" do
expect(resource["Type"]).to eq("AWS::ECS::TaskDefinition")
end

it "to have property ContainerDefinitions" do
expect(resource["Properties"]["ContainerDefinitions"]).to eq([{"Name"=>"schema", "Image"=>{"Fn::Join"=>["", [{"Fn::Sub"=>"myrepo/backend"}, ":", {"Ref"=>"SchemaTag"}]]}, "LogConfiguration"=>{"LogDriver"=>"awslogs", "Options"=>{"awslogs-group"=>{"Ref"=>"LogGroup"}, "awslogs-region"=>{"Ref"=>"AWS::Region"}, "awslogs-stream-prefix"=>"schema"}}}])
end

it "to have property RequiresCompatibilities" do
expect(resource["Properties"]["RequiresCompatibilities"]).to eq(["FARGATE"])
end

it "to have property Cpu" do
expect(resource["Properties"]["Cpu"]).to eq(256)
end

it "to have property Memory" do
expect(resource["Properties"]["Memory"]).to eq(512)
end

it "to have property NetworkMode" do
expect(resource["Properties"]["NetworkMode"]).to eq("awsvpc")
end

it "to have property TaskRoleArn" do
expect(resource["Properties"]["TaskRoleArn"]).to eq({"Ref"=>"TaskRole"})
end

it "to have property ExecutionRoleArn" do
expect(resource["Properties"]["ExecutionRoleArn"]).to eq({"Ref"=>"ExecutionRole"})
end

it "to have property Tags" do
expect(resource["Properties"]["Tags"]).to eq([{"Key"=>"Name", "Value"=>"ecs-task"}, {"Key"=>"Environment", "Value"=>{"Ref"=>"EnvironmentName"}}, {"Key"=>"EnvironmentType", "Value"=>{"Ref"=>"EnvironmentType"}}])
end

end

end

end
Loading

0 comments on commit 2cd8b4a

Please sign in to comment.