Skip to content

Commit

Permalink
refactor(test): upgrade to ginkgo v2 and fixed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenfonseca committed Feb 25, 2022
1 parent bdd5860 commit c00bf6b
Show file tree
Hide file tree
Showing 17 changed files with 214 additions and 163 deletions.
2 changes: 1 addition & 1 deletion cloudformation/cloudformation_suite_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package cloudformation_test

import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

"testing"
Expand Down
9 changes: 9 additions & 0 deletions cloudformation/convert_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ func String(v string) *string {
return &v
}

// Strings returns a pointer to a slice of string pointers.
func Strings(v... string) *[]string {
slice := make([]string, len(v))
for i := 0; i < len(v); i++ {
slice[i] = v[i]
}
return &slice
}

// StringValue returns the value of the string pointer passed in or
// "" if the pointer is nil.
func StringValue(v *string) string {
Expand Down
2 changes: 1 addition & 1 deletion cloudformation/intrinsics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/awslabs/goformation/v6/cloudformation"
"github.com/awslabs/goformation/v6/intrinsics"

. "github.com/onsi/ginkgo"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

Expand Down
2 changes: 1 addition & 1 deletion cloudformation/policies_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/awslabs/goformation/v6/cloudformation/autoscaling"
"github.com/awslabs/goformation/v6/cloudformation/policies"

. "github.com/onsi/ginkgo"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

Expand Down
88 changes: 65 additions & 23 deletions cloudformation/serverless/function_properties.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package serverless

import (
"bytes"
"encoding/json"
"io"
"sort"

"github.com/awslabs/goformation/v6/cloudformation/utils"
Expand Down Expand Up @@ -77,6 +79,7 @@ func (r Function_Properties) value() interface{} {
sort.Sort(utils.ByJSONLength(ret)) // Heuristic to select best attribute
if len(ret) > 0 {
return ret[0]
// log.Fatalf("FunctionProperties matched more than one property. This is a bug. %+v", r)
}

return nil
Expand All @@ -100,29 +103,68 @@ func (r *Function_Properties) UnmarshalJSON(b []byte) error {
case map[string]interface{}:
val = val // This ensures val is used to stop an error

json.Unmarshal(b, &r.S3Event)

json.Unmarshal(b, &r.SNSEvent)

json.Unmarshal(b, &r.SQSEvent)

json.Unmarshal(b, &r.KinesisEvent)

json.Unmarshal(b, &r.DynamoDBEvent)

json.Unmarshal(b, &r.ApiEvent)

json.Unmarshal(b, &r.ScheduleEvent)

json.Unmarshal(b, &r.CloudWatchEventEvent)

json.Unmarshal(b, &r.CloudWatchLogsEvent)

json.Unmarshal(b, &r.IoTRuleEvent)

json.Unmarshal(b, &r.AlexaSkillEvent)

json.Unmarshal(b, &r.EventBridgeRuleEvent)
reader := bytes.NewReader(b)
decoder := json.NewDecoder(reader)
decoder.DisallowUnknownFields()

if err := decoder.Decode(&r.S3Event); err != nil {
r.S3Event = nil
}

reader.Seek(0, io.SeekStart)
if err := decoder.Decode(&r.SNSEvent); err != nil {
r.SNSEvent = nil
}

reader.Seek(0, io.SeekStart)
if err := decoder.Decode(&r.SQSEvent); err != nil {
r.SQSEvent = nil
}

reader.Seek(0, io.SeekStart)
if err := decoder.Decode(&r.KinesisEvent); err != nil {
r.KinesisEvent = nil
}

reader.Seek(0, io.SeekStart)
if err := decoder.Decode(&r.DynamoDBEvent); err != nil {
r.DynamoDBEvent = nil
}

reader.Seek(0, io.SeekStart)
if err := decoder.Decode(&r.ApiEvent); err != nil {
r.ApiEvent = nil
}

reader.Seek(0, io.SeekStart)
if err := decoder.Decode(&r.ScheduleEvent); err != nil {
r.ScheduleEvent = nil
}

reader.Seek(0, io.SeekStart)
if err := decoder.Decode(&r.CloudWatchEventEvent); err != nil {
r.CloudWatchEventEvent = nil
}

reader.Seek(0, io.SeekStart)
if err := decoder.Decode(&r.CloudWatchLogsEvent); err != nil {
r.CloudWatchLogsEvent = nil
}

reader.Seek(0, io.SeekStart)
if err := decoder.Decode(&r.IoTRuleEvent); err != nil {
r.IoTRuleEvent = nil
}

reader.Seek(0, io.SeekStart)
if err := decoder.Decode(&r.AlexaSkillEvent); err != nil {
r.AlexaSkillEvent = nil
}

reader.Seek(0, io.SeekStart)
if err := decoder.Decode(&r.EventBridgeRuleEvent); err != nil {
r.EventBridgeRuleEvent = nil
}

case []interface{}:

Expand Down
4 changes: 2 additions & 2 deletions example/yaml-to-go/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func main() {
for name, topic := range topics {

// E.g. Found a AWS::SNS::Topic with Logical ID ExampleTopic and TopicName 'example'
log.Printf("Found a %s with Logical ID %s and TopicName %s\n", topic.AWSCloudFormationType(), name, topic.TopicName)
log.Printf("Found a %s with Logical ID %s and TopicName %s\n", topic.AWSCloudFormationType(), name, *topic.TopicName)

}

Expand All @@ -32,6 +32,6 @@ func main() {
}

// E.g. Found a AWS::Serverless::Function named GetHelloWorld (runtime: nodejs6.10)
log.Printf("Found a %s with Logical ID %s and TopicName %s\n", topic.AWSCloudFormationType(), search, topic.TopicName)
log.Printf("Found a %s with Logical ID %s and TopicName %s\n", topic.AWSCloudFormationType(), search, *topic.TopicName)

}
2 changes: 1 addition & 1 deletion generate/generate_suite_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package main_test

import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

"testing"
Expand Down
20 changes: 10 additions & 10 deletions generate/globals_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/awslabs/goformation/v6/cloudformation"
"github.com/awslabs/goformation/v6/cloudformation/global"
"github.com/awslabs/goformation/v6/cloudformation/serverless"
. "github.com/onsi/ginkgo"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

Expand All @@ -21,7 +21,7 @@ var _ = Describe("SAM Globals", func() {

codeuri := "s3://bucket/key"
resource := &global.Function{
Runtime: "nodejs6.10",
Runtime: cloudformation.String("nodejs6.10"),
CodeUri: &serverless.Function_CodeUri{
String: &codeuri,
},
Expand All @@ -40,12 +40,12 @@ var _ = Describe("SAM Globals", func() {
Context("with a custom type used for a polymorphic property", func() {

resource := &global.Function{
Runtime: "nodejs6.10",
Runtime: cloudformation.String("nodejs6.10"),
CodeUri: &serverless.Function_CodeUri{
S3Location: &serverless.Function_S3Location{
Bucket: "test-bucket",
Key: "test-key",
Version: 123,
Version: cloudformation.Int(123),
},
},
}
Expand All @@ -67,9 +67,9 @@ var _ = Describe("SAM Globals", func() {

template := cloudformation.NewTemplate()
template.Globals["Function"] = &global.Function{
Runtime: "nodejs12.x",
Timeout: 180,
Handler: "index.handler",
Runtime: cloudformation.String("nodejs12.x"),
Timeout: cloudformation.Int(180),
Handler: cloudformation.String("index.handler"),
Environment: &serverless.Function_FunctionEnvironment{
Variables: map[string]string{
"TABLE_NAME": "data-table",
Expand Down Expand Up @@ -115,9 +115,9 @@ var _ = Describe("SAM Globals", func() {
It("should have a global Function, with properties set", func() {

expected := &global.Function{
Runtime: "nodejs12.x",
Timeout: 180,
Handler: "index.handler",
Runtime: cloudformation.String("nodejs12.x"),
Timeout: cloudformation.Int(180),
Handler: cloudformation.String("index.handler"),
Environment: &serverless.Function_FunctionEnvironment{
Variables: map[string]string{
"TABLE_NAME": "data-table",
Expand Down
17 changes: 9 additions & 8 deletions generate/property_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package main_test
import (
"encoding/json"

"github.com/awslabs/goformation/v6/cloudformation"
"github.com/awslabs/goformation/v6/cloudformation/serverless"
. "github.com/onsi/ginkgo"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

Expand All @@ -17,7 +18,7 @@ var _ = Describe("Goformation Code Generator", func() {
property := &serverless.Function_S3Location{
Bucket: "test-bucket",
Key: "test-key",
Version: 123,
Version: cloudformation.Int(123),
}
expected := []byte(`{"Bucket":"test-bucket","Key":"test-key","Version":123}`)

Expand All @@ -34,7 +35,7 @@ var _ = Describe("Goformation Code Generator", func() {
expected := &serverless.Function_S3Location{
Bucket: "test-bucket",
Key: "test-key",
Version: 123,
Version: cloudformation.Int(123),
}

result := &serverless.Function_S3Location{}
Expand Down Expand Up @@ -85,11 +86,11 @@ var _ = Describe("Goformation Code Generator", func() {
})

Context("properly Marshals best value", func() {
expected := []byte(`{"BatchSize":10,"Stream":"arn"}`)
expected := []byte(`{"BatchSize":10,"StartingPosition":"LATEST","Stream":"stream"}`)

result := &serverless.Function_Properties{
SQSEvent: &serverless.Function_SQSEvent{BatchSize: 10},
KinesisEvent: &serverless.Function_KinesisEvent{BatchSize: 10, Stream: "arn"},
SQSEvent: &serverless.Function_SQSEvent{BatchSize: cloudformation.Int(10)},
KinesisEvent: &serverless.Function_KinesisEvent{BatchSize: cloudformation.Int(10), StartingPosition: "LATEST", Stream: "stream"},
}

output, err := result.MarshalJSON()
Expand Down Expand Up @@ -148,7 +149,7 @@ var _ = Describe("Goformation Code Generator", func() {
S3Location: &serverless.Function_S3Location{
Bucket: "test-bucket",
Key: "test-key",
Version: 123,
Version: cloudformation.Int(123),
},
}

Expand All @@ -170,7 +171,7 @@ var _ = Describe("Goformation Code Generator", func() {
S3Location: &serverless.Function_S3Location{
Bucket: "test-bucket",
Key: "test-key",
Version: 123,
Version: cloudformation.Int(123),
},
}

Expand Down
24 changes: 13 additions & 11 deletions generate/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ package main_test
import (
"encoding/json"

"github.com/awslabs/goformation/v6/cloudformation"
"github.com/awslabs/goformation/v6/cloudformation/rds"

"github.com/awslabs/goformation/v6/cloudformation/ec2"
"github.com/awslabs/goformation/v6/cloudformation/s3"
"github.com/awslabs/goformation/v6/cloudformation/serverless"
. "github.com/onsi/ginkgo"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

Expand All @@ -22,7 +23,7 @@ var _ = Describe("Resource", func() {

codeuri := "s3://bucket/key"
resource := &serverless.Function{
Runtime: "nodejs6.10",
Runtime: cloudformation.String("nodejs6.10"),
CodeUri: &serverless.Function_CodeUri{
String: &codeuri,
},
Expand All @@ -41,12 +42,12 @@ var _ = Describe("Resource", func() {
Context("with a custom type used for a polymorphic property", func() {

resource := &serverless.Function{
Runtime: "nodejs6.10",
Runtime: cloudformation.String("nodejs6.10"),
CodeUri: &serverless.Function_CodeUri{
S3Location: &serverless.Function_S3Location{
Bucket: "test-bucket",
Key: "test-key",
Version: 123,
Version: cloudformation.Int(123),
},
},
}
Expand All @@ -72,7 +73,7 @@ var _ = Describe("Resource", func() {
Context("with a dependency on another resource", func() {

resource := &ec2.Instance{
ImageId: "ami-0123456789",
ImageId: cloudformation.String("ami-0123456789"),
}
resource.AWSCloudFormationDependsOn = []string{"MyDependency"}

Expand All @@ -89,7 +90,7 @@ var _ = Describe("Resource", func() {
Context("with a metadata attribute", func() {

resource := &s3.Bucket{
BucketName: "MyBucket",
BucketName: cloudformation.String("MyBucket"),
}
resource.AWSCloudFormationMetadata = map[string]interface{}{"Object1": "Location1", "Object2": "Location2"}

Expand All @@ -106,11 +107,12 @@ var _ = Describe("Resource", func() {
Context("with a condition attribute", func() {

resource := &rds.DBCluster{
DatabaseName: "MyDatabase",
DatabaseName: cloudformation.String("MyDatabase"),
Engine: "mysql",
}
resource.AWSCloudFormationCondition = "MyCondition"

expected := []byte(`{"Type":"AWS::RDS::DBCluster","Properties":{"DatabaseName":"MyDatabase"},"Condition":"MyCondition"}`)
expected := []byte(`{"Type":"AWS::RDS::DBCluster","Properties":{"DatabaseName":"MyDatabase","Engine":"mysql"},"Condition":"MyCondition"}`)

result, err := json.Marshal(resource)
It("should marshal to JSON successfully", func() {
Expand All @@ -128,7 +130,7 @@ var _ = Describe("Resource", func() {

property := []byte(`{"Type":"AWS::EC2::Instance","Properties":{"ImageId":"ami-0123456789"},"DependsOn":["MyDependency"]}`)
expected := &ec2.Instance{
ImageId: "ami-0123456789",
ImageId: cloudformation.String("ami-0123456789"),
}
expected.AWSCloudFormationDependsOn = []string{"MyDependency"}

Expand All @@ -145,7 +147,7 @@ var _ = Describe("Resource", func() {

property := []byte(`{"Type":"AWS::S3::Bucket","Properties":{"BucketName":"MyBucket"},"Metadata":{"Object1":"Location1","Object2":"Location2"}}`)
expected := &s3.Bucket{
BucketName: "MyBucket",
BucketName: cloudformation.String("MyBucket"),
}
expected.AWSCloudFormationMetadata = map[string]interface{}{"Object1": "Location1", "Object2": "Location2"}

Expand All @@ -162,7 +164,7 @@ var _ = Describe("Resource", func() {

property := []byte(`{"Type":"AWS::RDS::DBCluster","Properties":{"DatabaseName":"MyDatabase"},"Condition":"MyCondition"}`)
expected := &rds.DBCluster{
DatabaseName: "MyDatabase",
DatabaseName: cloudformation.String("MyDatabase"),
}
expected.AWSCloudFormationCondition = "MyCondition"

Expand Down
Loading

0 comments on commit c00bf6b

Please sign in to comment.