Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
crewjam committed Nov 29, 2015
1 parent e6e69c6 commit d237b55
Show file tree
Hide file tree
Showing 19 changed files with 478 additions and 190 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
.scraper-cache
.scraper-cache
coverage.html
coverage.out
8 changes: 8 additions & 0 deletions bool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,12 @@ func (testSuite *BoolTest) TestBool(c *C) {
c.Assert(err, IsNil)
c.Assert(string(buf), Equals,
`{"B":true,"C":false,"D":{"Ref":"foo"},"E":true}`)

inputBuf = `{"A": "invalid"}`
err = json.Unmarshal([]byte(inputBuf), &v)
c.Assert(err, ErrorMatches, "json: cannot unmarshal string into Go value of type bool")

inputBuf = `{"A": {"Fn::Missing": "invalid"}}`
err = json.Unmarshal([]byte(inputBuf), &v)
c.Assert(err, ErrorMatches, "unknown function Fn::Missing")
}
2 changes: 1 addition & 1 deletion func.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ type UnknownFunctionError struct {
}

func (ufe UnknownFunctionError) Error() string {
return fmt.Sprintf("unkown function %s", ufe.Name)
return fmt.Sprintf("unknown function %s", ufe.Name)
}

// unmarshalFunc unmarshals data into a Func, or returns an error
Expand Down
55 changes: 55 additions & 0 deletions func_base64_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package cloudformation

import (
"encoding/json"

. "gopkg.in/check.v1"
)

type Base64FuncTest struct{}

var _ = Suite(&Base64FuncTest{})

func (testSuite *Base64FuncTest) TestRef(c *C) {
inputBuf := `{ "Fn::Base64" : { "Fn::Join" : ["", [
"#!/bin/bash -xe\n",
"yum update -y aws-cfn-bootstrap\n",
"# Install the files and packages from the metadata\n",
"/opt/aws/bin/cfn-init -v ",
" --stack ", { "Ref" : "AWS::StackName" },
" --resource LaunchConfig ",
" --region ", { "Ref" : "AWS::Region" }, "\n",
"# Signal the status from cfn-init\n",
"/opt/aws/bin/cfn-signal -e $? ",
" --stack ", { "Ref" : "AWS::StackName" },
" --resource WebServerGroup ",
" --region ", { "Ref" : "AWS::Region" }, "\n"
]]}}`
f, err := unmarshalFunc([]byte(inputBuf))
c.Assert(err, IsNil)
c.Assert(f.(StringFunc).String(), DeepEquals, Base64(*Join("",
*String("#!/bin/bash -xe\n"),
*String("yum update -y aws-cfn-bootstrap\n"),
*String("# Install the files and packages from the metadata\n"),
*String("/opt/aws/bin/cfn-init -v "),
*String(" --stack "), *Ref("AWS::StackName").String(),
*String(" --resource LaunchConfig "),
*String(" --region "), *Ref("AWS::Region").String(), *String("\n"),
*String("# Signal the status from cfn-init\n"),
*String("/opt/aws/bin/cfn-signal -e $? "),
*String(" --stack "), *Ref("AWS::StackName").String(),
*String(" --resource WebServerGroup "),
*String(" --region "), *Ref("AWS::Region").String(), *String("\n"),
).String()).String())

// tidy the JSON input
inputStruct := map[string]interface{}{}
_ = json.Unmarshal([]byte(inputBuf), &inputStruct)
expectedBuf, _ := json.Marshal(inputStruct)

buf, err := json.Marshal(f)
c.Assert(err, IsNil)
c.Assert(string(buf), Equals, string(expectedBuf))
}
49 changes: 49 additions & 0 deletions func_findinmap_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package cloudformation

import (
"encoding/json"

. "gopkg.in/check.v1"
)

type FindInMapFuncTest struct{}

var _ = Suite(&FindInMapFuncTest{})

func (testSuite *FindInMapFuncTest) TestBasics(c *C) {
inputBuf := `{ "Fn::FindInMap" : [ "AWSRegionArch2AMI", { "Ref" : "AWS::Region" },
{ "Fn::FindInMap" : [ "AWSInstanceType2Arch", { "Ref" : "InstanceType" }, "Arch" ] } ] }`
f, err := unmarshalFunc([]byte(inputBuf))
c.Assert(err, IsNil)
c.Assert(f.(StringFunc).String(), DeepEquals, FindInMap(
"AWSRegionArch2AMI", *Ref("AWS::Region").String(),
*FindInMap("AWSInstanceType2Arch", *Ref("InstanceType").String(),
*String("Arch")).String()).String())

// tidy the JSON input
inputStruct := map[string]interface{}{}
_ = json.Unmarshal([]byte(inputBuf), &inputStruct)
expectedBuf, _ := json.Marshal(inputStruct)

buf, err := json.Marshal(f)
c.Assert(err, IsNil)
c.Assert(string(buf), Equals, string(expectedBuf))
}

func (testSuite *FindInMapFuncTest) TestFailures(c *C) {
inputBuf := `{"Fn::FindInMap": 1}`
_, err := unmarshalFunc([]byte(inputBuf))
c.Assert(err, ErrorMatches, "cannot decode function")

inputBuf = `{"Fn::FindInMap": [1, "2", "3"]}`
_, err = unmarshalFunc([]byte(inputBuf))
c.Assert(err, ErrorMatches, "cannot decode function")

inputBuf = `{"Fn::FindInMap": ["1", 2, "3"]}`
_, err = unmarshalFunc([]byte(inputBuf))
c.Assert(err, ErrorMatches, "cannot decode function")

inputBuf = `{"Fn::FindInMap": ["1", "2", 3]}`
_, err = unmarshalFunc([]byte(inputBuf))
c.Assert(err, ErrorMatches, "cannot decode function")
}
9 changes: 6 additions & 3 deletions func_getatt.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,16 @@ func (f GetAttFunc) MarshalJSON() ([]byte, error) {
}

// UnmarshalJSON sets the object from the provided JSON representation
func (f *GetAttFunc) UnmarshalJSON(buf []byte) error {
func (f *GetAttFunc) UnmarshalJSON(data []byte) error {
v := struct {
FnGetAtt [2]string `json:"Fn::GetAtt"`
FnGetAtt []string `json:"Fn::GetAtt"`
}{}
if err := json.Unmarshal(buf, &v); err != nil {
if err := json.Unmarshal(data, &v); err != nil {
return err
}
if len(v.FnGetAtt) != 2 {
return &json.UnsupportedValueError{Str: string(data)}
}
f.Resource = v.FnGetAtt[0]
f.Name = v.FnGetAtt[1]
return nil
Expand Down
42 changes: 42 additions & 0 deletions func_getatt_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package cloudformation

import (
"encoding/json"

. "gopkg.in/check.v1"
)

type GetAttFuncTest struct{}

var _ = Suite(&GetAttFuncTest{})

func (testSuite *GetAttFuncTest) TestBasics(c *C) {
inputBuf := `{"Fn::GetAtt" : ["MySQLDatabase", "Endpoint.Address"]}`
f, err := unmarshalFunc([]byte(inputBuf))
c.Assert(err, IsNil)
c.Assert(f.(StringFunc).String(), DeepEquals,
GetAtt("MySQLDatabase", "Endpoint.Address").String())

// tidy the JSON input
inputStruct := map[string]interface{}{}
_ = json.Unmarshal([]byte(inputBuf), &inputStruct)
expectedBuf, _ := json.Marshal(inputStruct)

buf, err := json.Marshal(f)
c.Assert(err, IsNil)
c.Assert(string(buf), Equals, string(expectedBuf))
}

func (testSuite *GetAttFuncTest) TestFailures(c *C) {
inputBuf := `{"Fn::GetAtt": 1}`
_, err := unmarshalFunc([]byte(inputBuf))
c.Assert(err, ErrorMatches, "cannot decode function")

inputBuf = `{"Fn::GetAtt": ["1"]}`
_, err = unmarshalFunc([]byte(inputBuf))
c.Assert(err, ErrorMatches, "cannot decode function")

inputBuf = `{"Fn::GetAtt": ["1", "2", "3"]}`
_, err = unmarshalFunc([]byte(inputBuf))
c.Assert(err, ErrorMatches, "cannot decode function")
}
42 changes: 42 additions & 0 deletions func_getazs_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package cloudformation

import (
"encoding/json"

. "gopkg.in/check.v1"
)

type GetAZsFuncTest struct{}

var _ = Suite(&GetAZsFuncTest{})

func (testSuite *GetAZsFuncTest) TestBasics(c *C) {
inputBuf := `{"Fn::GetAZs" : {"Ref": "AWS::Region"}}`
f, err := unmarshalFunc([]byte(inputBuf))
c.Assert(err, IsNil)
c.Assert(f.(StringListFunc).StringList(), DeepEquals,
GetAZs(*Ref("AWS::Region").String()).StringList())

// tidy the JSON input
inputStruct := map[string]interface{}{}
_ = json.Unmarshal([]byte(inputBuf), &inputStruct)
expectedBuf, _ := json.Marshal(inputStruct)

buf, err := json.Marshal(f)
c.Assert(err, IsNil)
c.Assert(string(buf), Equals, string(expectedBuf))
}

func (testSuite *GetAZsFuncTest) TestFailures(c *C) {
inputBuf := `{"Fn::GetAZs": 1}`
_, err := unmarshalFunc([]byte(inputBuf))
c.Assert(err, ErrorMatches, "cannot decode function")

inputBuf = `{"Fn::GetAZs": ["1"]}`
_, err = unmarshalFunc([]byte(inputBuf))
c.Assert(err, ErrorMatches, "cannot decode function")

inputBuf = `{"Fn::GetAZs": ["1", "2", "3"]}`
_, err = unmarshalFunc([]byte(inputBuf))
c.Assert(err, ErrorMatches, "cannot decode function")
}
2 changes: 1 addition & 1 deletion func_if.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func (f IfFunc) String() *StringExpr {
// StringList returns a new StringListExpr representing the literal value v.
func (f IfFunc) StringList() *StringListExpr {
if !f.list {
panic("IfFunc is a scaler, but being treated as a list of strings.")
panic("IfFunc is a scalar, but being treated as a list of strings.")
}
return &StringListExpr{Func: f}
}
Expand Down
9 changes: 6 additions & 3 deletions func_join.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,16 @@ func (f JoinFunc) MarshalJSON() ([]byte, error) {
}

// UnmarshalJSON sets the object from the provided JSON representation
func (f *JoinFunc) UnmarshalJSON(buf []byte) error {
func (f *JoinFunc) UnmarshalJSON(data []byte) error {
v := struct {
FnJoin [2]json.RawMessage `json:"Fn::Join"`
FnJoin []json.RawMessage `json:"Fn::Join"`
}{}
if err := json.Unmarshal(buf, &v); err != nil {
if err := json.Unmarshal(data, &v); err != nil {
return err
}
if len(v.FnJoin) != 2 {
return &json.UnsupportedValueError{Str: string(data)}
}
if err := json.Unmarshal(v.FnJoin[0], &f.Separator); err != nil {
return err
}
Expand Down
25 changes: 24 additions & 1 deletion func_join_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,32 @@ func (testSuite *JoinFuncTest) TestRef(c *C) {
inputBuf := `{"Fn::Join":["x",["y",{"Ref":"foo"},"1"]]}`
f, err := unmarshalFunc([]byte(inputBuf))
c.Assert(err, IsNil)
c.Assert(f, DeepEquals, Join("x", *String("y"), *Ref("foo").String(), *String("1")))
c.Assert(f.(StringFunc).String(), DeepEquals,
Join("x", *String("y"), *Ref("foo").String(), *String("1")).String())

buf, err := json.Marshal(f)
c.Assert(err, IsNil)
c.Assert(string(buf), Equals, inputBuf)
}

func (testSuite *JoinFuncTest) TestFailures(c *C) {
inputBuf := `{"Fn::Join": 1}`
_, err := unmarshalFunc([]byte(inputBuf))
c.Assert(err, ErrorMatches, "cannot decode function")

inputBuf = `{"Fn::Join": ["1"]}`
_, err = unmarshalFunc([]byte(inputBuf))
c.Assert(err, ErrorMatches, "cannot decode function")

inputBuf = `{"Fn::Join": ["1", [1]]}`
_, err = unmarshalFunc([]byte(inputBuf))
c.Assert(err, ErrorMatches, "cannot decode function")

inputBuf = `{"Fn::Join": ["1", ["2"], "3"]}`
_, err = unmarshalFunc([]byte(inputBuf))
c.Assert(err, ErrorMatches, "cannot decode function")

inputBuf = `{"Fn::Join": [false, ["2"]]}`
_, err = unmarshalFunc([]byte(inputBuf))
c.Assert(err, ErrorMatches, "cannot decode function")
}
15 changes: 14 additions & 1 deletion func_ref.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,23 @@ func (r RefFunc) Bool() *BoolExpr {
return &BoolExpr{Func: r}
}

// Bool returns this reference as a StringExpr
// Integer returns this reference as a IntegerExpr
func (r RefFunc) Integer() *IntegerExpr {
return &IntegerExpr{Func: r}
}

// String returns this reference as a StringExpr
func (r RefFunc) String() *StringExpr {
return &StringExpr{Func: r}
}

// StringList returns this reference as a StringListExpr
func (r RefFunc) StringList() *StringListExpr {
return &StringListExpr{Func: r}
}

var _ Func = RefFunc{}
var _ BoolFunc = RefFunc{}
var _ IntegerFunc = RefFunc{}
var _ StringFunc = RefFunc{}
var _ StringListFunc = RefFunc{}
41 changes: 41 additions & 0 deletions func_ref_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package cloudformation

import (
"encoding/json"

. "gopkg.in/check.v1"
)

type RefFuncTest struct{}

var _ = Suite(&RefFuncTest{})

func (testSuite *RefFuncTest) TestRef(c *C) {
inputBuf := `{"Ref" : "AWS::Region"}`
f, err := unmarshalFunc([]byte(inputBuf))
c.Assert(err, IsNil)
c.Assert(f.(StringFunc).String(), DeepEquals, Ref("AWS::Region").String())

// tidy the JSON input
inputStruct := map[string]interface{}{}
_ = json.Unmarshal([]byte(inputBuf), &inputStruct)
expectedBuf, _ := json.Marshal(inputStruct)

buf, err := json.Marshal(f)
c.Assert(err, IsNil)
c.Assert(string(buf), Equals, string(expectedBuf))
}

func (testSuite *RefFuncTest) TestFailures(c *C) {
inputBuf := `{"Ref": {"Ref": "foo"}}`
_, err := unmarshalFunc([]byte(inputBuf))
c.Assert(err, ErrorMatches, "cannot decode function")

inputBuf = `{"Ref": ["1"]}`
_, err = unmarshalFunc([]byte(inputBuf))
c.Assert(err, ErrorMatches, "cannot decode function")

inputBuf = `{"Ref": true}`
_, err = unmarshalFunc([]byte(inputBuf))
c.Assert(err, ErrorMatches, "cannot decode function")
}
9 changes: 6 additions & 3 deletions func_select.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,16 @@ func (f SelectFunc) MarshalJSON() ([]byte, error) {
}

// UnmarshalJSON sets the object from the provided JSON representation
func (f *SelectFunc) UnmarshalJSON(buf []byte) error {
func (f *SelectFunc) UnmarshalJSON(data []byte) error {
v := struct {
FnSelect [2]json.RawMessage `json:"Fn::Select"`
FnSelect []json.RawMessage `json:"Fn::Select"`
}{}
if err := json.Unmarshal(buf, &v); err != nil {
if err := json.Unmarshal(data, &v); err != nil {
return err
}
if len(v.FnSelect) != 2 {
return &json.UnsupportedValueError{Str: string(data)}
}
if err := json.Unmarshal(v.FnSelect[0], &f.Selector); err != nil {
return err
}
Expand Down
Loading

0 comments on commit d237b55

Please sign in to comment.