Skip to content

Commit

Permalink
Don't use indented json, since it's big.
Browse files Browse the repository at this point in the history
  • Loading branch information
ejholmes committed May 7, 2016
1 parent 6a0cee1 commit fd8eb92
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 22 deletions.
9 changes: 8 additions & 1 deletion scheduler/cloudformation/cloudformation.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ import (
// The identifier of the ECS Service resource in CloudFormation.
const ecsServiceType = "AWS::ECS::Service"

// CloudFormation limits
//
// See http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/cloudformation-limits.html
const (
MaxTemplateSize = 460800 // bytes
)

// DefaultStackNameTemplate is the default text/template for generating a
// CloudFormation stack name for an app.
var DefaultStackNameTemplate = template.Must(template.New("stack_name").Parse("{{.Name}}"))
Expand Down Expand Up @@ -84,7 +91,7 @@ type serviceMetadata struct {
type Scheduler struct {
// Template is a text/template that will be executed using the
// twelvefactor.Manifest as data. This template should return a valid
// CloudFormation JSON manifest.
// CloudFormation JSON template.
Template Template

// The ECS cluster to run tasks in.
Expand Down
9 changes: 1 addition & 8 deletions scheduler/cloudformation/template.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cloudformation

import (
"bytes"
"encoding/json"
"errors"
"fmt"
Expand Down Expand Up @@ -102,13 +101,7 @@ func (t *EmpireTemplate) Execute(w io.Writer, data interface{}) error {
return err
}

raw, err := json.MarshalIndent(v, "", " ")
if err != nil {
return err
}

_, err = io.Copy(w, bytes.NewReader(raw))
return err
return json.NewEncoder(w).Encode(v)
}

// Build builds a Go representation of a CloudFormation template for the app.
Expand Down
60 changes: 47 additions & 13 deletions scheduler/cloudformation/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,19 +76,7 @@ func TestEmpireTemplate(t *testing.T) {
}

for _, tt := range tests {
tmpl := &EmpireTemplate{
Cluster: "cluster",
ServiceRole: "ecsServiceRole",
InternalSecurityGroupID: "sg-e7387381",
ExternalSecurityGroupID: "sg-1938737f",
InternalSubnetIDs: []string{"subnet-bb01c4cd", "subnet-c85f4091"},
ExternalSubnetIDs: []string{"subnet-ca96f4cd", "subnet-a13b909c"},
CustomResourcesTopic: "sns topic arn",
HostedZone: &route53.HostedZone{
Id: aws.String("Z3DG6IL3SJCGPX"),
Name: aws.String("empire"),
},
}
tmpl := newTemplate()
buf := new(bytes.Buffer)

filename := fmt.Sprintf("templates/%s", tt.file)
Expand All @@ -102,3 +90,49 @@ func TestEmpireTemplate(t *testing.T) {
ioutil.WriteFile(filename, buf.Bytes(), 0660)
}
}

func TestEmpireTemplate_Large(t *testing.T) {
labels := make(map[string]string)
env := make(map[string]string)
for i := 0; i < 100; i++ {
env[fmt.Sprintf("ENV_VAR_%d", i)] = fmt.Sprintf("value%d", i)
}
app := &scheduler.App{
ID: "",
Name: "bigappwithlotsofprocesses",
}

for i := 0; i < 60; i++ {
app.Processes = append(app.Processes, &scheduler.Process{
Type: fmt.Sprintf("%d", i),
Command: []string{"./bin/web"},
Env: env,
Labels: labels,
})
}

tmpl := newTemplate()
buf := new(bytes.Buffer)

err := tmpl.Execute(buf, app)
assert.NoError(t, err)
assert.Condition(t, func() bool {
return buf.Len() < MaxTemplateSize
}, fmt.Sprintf("template must be smaller than %d, was %d", MaxTemplateSize, buf.Len()))
}

func newTemplate() *EmpireTemplate {
return &EmpireTemplate{
Cluster: "cluster",
ServiceRole: "ecsServiceRole",
InternalSecurityGroupID: "sg-e7387381",
ExternalSecurityGroupID: "sg-1938737f",
InternalSubnetIDs: []string{"subnet-bb01c4cd", "subnet-c85f4091"},
ExternalSubnetIDs: []string{"subnet-ca96f4cd", "subnet-a13b909c"},
CustomResourcesTopic: "sns topic arn",
HostedZone: &route53.HostedZone{
Id: aws.String("Z3DG6IL3SJCGPX"),
Name: aws.String("empire"),
},
}
}

0 comments on commit fd8eb92

Please sign in to comment.