Skip to content

Commit

Permalink
Merge pull request #6 from ckaznocha/feature/template
Browse files Browse the repository at this point in the history
Feature/template
  • Loading branch information
Jeff DeCola committed Sep 20, 2016
2 parents f325cb7 + 40a8a15 commit 3458479
Show file tree
Hide file tree
Showing 31 changed files with 6,795 additions and 11 deletions.
3 changes: 3 additions & 0 deletions cmd/marathon-resource/fixtures/app.json
@@ -0,0 +1,3 @@
{
"foo": "bar"
}
3 changes: 3 additions & 0 deletions cmd/marathon-resource/fixtures/app_template.json
@@ -0,0 +1,3 @@
{
"foo": "{{ foo }}"
}
3 changes: 3 additions & 0 deletions cmd/marathon-resource/fixtures/app_template_bad.json
@@ -0,0 +1,3 @@
{
"foo": "{{%^}}"
}
16 changes: 13 additions & 3 deletions cmd/marathon-resource/main.go
Expand Up @@ -16,8 +16,8 @@ const (

type (
params struct {
AppJSON string `json:"app_json"`
Replacements map[string]string `json:"replacements"`
AppJSON string `json:"app_json"`
Replacements []metadata `json:"replacements"`
}
source struct {
AppID string `json:"app_id"`
Expand All @@ -31,12 +31,22 @@ type (
Source source `json:"source"`
Version version `json:"version"`
}
checkOut []version
metadata struct {
Name string `json:"name"`
Value string `json:"value"`
}
ioOut struct {
Version version `json:"version"`
Metadata []metadata `json:"metadata"`
}
)

var logger = logrus.New()

func main() {
var (
input inputJSON
logger = logrus.New()
decoder = json.NewDecoder(os.Stdin)
/*encoder*/ _ = json.NewEncoder(os.Stdout)
)
Expand Down
26 changes: 20 additions & 6 deletions cmd/marathon-resource/main_test.go
Expand Up @@ -7,17 +7,22 @@ import (
)

func Test_main(t *testing.T) {
logger = nil
type args struct {
osArgs []string
stdin string
}
tests := []struct {
name string
args args
name string
args args
wantPanic bool
}{
{"Check", args{[]string{"", "check"}, "{}"}},
{"In", args{[]string{"", "in"}, "{}"}},
{"Out", args{[]string{"", "out"}, "{}"}},
{"Check", args{[]string{"", "check"}, "{}"}, false},
{"In", args{[]string{"", "in"}, "{}"}, false},
{"Out", args{[]string{"", "out"}, "{}"}, false},
{"Bad json", args{[]string{"", "out"}, `{]`}, true},
{"Wrong number of args", args{[]string{""}, "{}"}, true},
{"Bad URI", args{[]string{"", "out"}, `{"source":{"uri":"http://192.168.0.%31/"}}`}, true},
}
for _, tt := range tests {
var stdin *os.File
Expand All @@ -26,6 +31,15 @@ func Test_main(t *testing.T) {
os.Stdin, stdin, _ = os.Pipe()
fmt.Fprint(stdin, tt.args.stdin)

main()
assertPanic(t, main, tt.wantPanic)
}
}

func assertPanic(t *testing.T, f func(), wantPanic bool) {
defer func() {
if (recover() != nil) != wantPanic {
t.Errorf("Expected panic to be %t but was %t", wantPanic, !wantPanic)
}
}()
f()
}
2 changes: 0 additions & 2 deletions cmd/marathon-resource/marathon.go
Expand Up @@ -124,10 +124,8 @@ func (m *marathon) CheckDeployment(deploymentID string) (bool, error) {
http.StatusOK,
&deployments,
)
fmt.Println(err)

for _, v := range deployments {
fmt.Println(v.ID)
if v.ID == deploymentID {
return true, nil
}
Expand Down
39 changes: 39 additions & 0 deletions cmd/marathon-resource/marathon_test.go
Expand Up @@ -31,6 +31,13 @@ func Test_marathon_LatestVersions(t *testing.T) {
},
nil,
)
mockClient.EXPECT().Do(gomock.Any()).Times(1).Return(
&http.Response{
StatusCode: http.StatusConflict,
Body: ioutil.NopCloser(strings.NewReader(`{"versions":["2015-02-11T09:31:50.021Z","2014-03-01T23:42:20.938Z"]}`)),
},
nil,
)
type fields struct {
client doer
url *url.URL
Expand All @@ -47,6 +54,7 @@ func Test_marathon_LatestVersions(t *testing.T) {
wantErr bool
}{
{"Works", fields{mockClient, u}, args{"foo", "2015-02-11T09:31:50.021Z"}, []string{"2015-02-11T09:31:50.021Z"}, false},
{"Errors", fields{mockClient, u}, args{"foo", "2015-02-11T09:31:50.021Z"}, nil, true},
}
for _, tt := range tests {
m := &marathon{
Expand Down Expand Up @@ -85,6 +93,13 @@ func Test_marathon_handleReq(t *testing.T) {
},
nil,
)
mockClient.EXPECT().Do(gomock.Any()).Times(1).Return(
&http.Response{
StatusCode: http.StatusOK,
Body: ioutil.NopCloser(strings.NewReader(`{]`)),
},
nil,
)
mockClient.EXPECT().Do(gomock.Any()).Times(1).Return(
nil,
errors.New("Something went wrong"),
Expand Down Expand Up @@ -142,6 +157,30 @@ func Test_marathon_handleReq(t *testing.T) {
},
true,
},
{
"Error",
fields{mockClient, u},
args{
"😂",
"/",
nil,
http.StatusOK,
&map[string]string{},
},
true,
},
{
"Error",
fields{mockClient, u},
args{
http.MethodGet,
"/",
nil,
http.StatusOK,
&[]string{},
},
true,
},
}
for _, tt := range tests {
m := &marathon{
Expand Down
32 changes: 32 additions & 0 deletions cmd/marathon-resource/resource.go
@@ -0,0 +1,32 @@
package main

import (
"bytes"
"io"
"path/filepath"

"github.com/aymerick/raymond"
)

func parsePayload(p params, path string) (io.Reader, error) {
var (
replacments = map[string]string{}
buf = bytes.NewBuffer([]byte{})
)
for _, v := range p.Replacements {
replacments[v.Name] = v.Value
}
tmpl, err := raymond.ParseFile(filepath.Join(path, p.AppJSON))
if err != nil {
return nil, err
}
app, err := tmpl.Exec(replacments)
if err != nil {
return nil, err
}

if _, err = buf.WriteString(app); err != nil {
return nil, err
}
return buf, nil
}
37 changes: 37 additions & 0 deletions cmd/marathon-resource/resource_test.go
@@ -0,0 +1,37 @@
package main

import (
"io/ioutil"
"reflect"
"testing"
)

func Test_parsePayload(t *testing.T) {
type args struct {
p params
path string
}
tests := []struct {
name string
args args
want []byte
wantErr bool
}{
{"Reads file with no replacements", args{params{AppJSON: "app.json"}, "fixtures"}, []byte{123, 10, 32, 32, 32, 32, 34, 102, 111, 111, 34, 58, 32, 34, 98, 97, 114, 34, 10, 125, 10}, false},
{"Reads file with replacements", args{params{AppJSON: "app_template.json", Replacements: []metadata{{"foo", "bar"}}}, "fixtures"}, []byte{123, 10, 32, 32, 32, 32, 34, 102, 111, 111, 34, 58, 32, 34, 98, 97, 114, 34, 10, 125, 10}, false},
{"Reads file with bad tmpl", args{params{AppJSON: "app_template_bad.json", Replacements: []metadata{{"foo", "bar"}}}, "fixtures"}, nil, true},
}
for _, tt := range tests {
got, err := parsePayload(tt.args.p, tt.args.path)
if (err != nil) != tt.wantErr {
t.Errorf("%q. parsePayload() error = %v, wantErr %v", tt.name, err, tt.wantErr)
continue
}
if !tt.wantErr {
p, _ := ioutil.ReadAll(got)
if !reflect.DeepEqual(p, tt.want) {
t.Errorf("%q. parsePayload() = %v, want %v", tt.name, string(p), string(tt.want))
}
}
}
}
46 changes: 46 additions & 0 deletions vendor/github.com/aymerick/raymond/BENCHMARKS.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 33 additions & 0 deletions vendor/github.com/aymerick/raymond/CHANGELOG.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions vendor/github.com/aymerick/raymond/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 3458479

Please sign in to comment.