Skip to content

Commit

Permalink
Fixed runtime container working directory that caused it to be an empty
Browse files Browse the repository at this point in the history
string if the CodeUri was set to something that wasn't a valid
directory.

Wrote a load of test cases for working directory related stuff to
prevent future regressions.

Fixes #22
  • Loading branch information
PaulMaddox committed Aug 18, 2017
1 parent 64de79a commit d4773f9
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 67 deletions.
4 changes: 2 additions & 2 deletions runtime.go
Expand Up @@ -458,7 +458,7 @@ func getEnvironmentVariables(function resources.AWSServerlessFunction, overrides
// Variables available in Lambda execution environment for all functions (AWS_* variables)
env := map[string]string{
"AWS_SAM_LOCAL": "true",
"AWS_REGION": creds["region"],
"AWS_REGION": creds["region"],
"AWS_DEFAULT_REGION": creds["region"],
"AWS_ACCESS_KEY_ID": creds["key"],
"AWS_SECRET_ACCESS_KEY": creds["secret"],
Expand Down Expand Up @@ -584,7 +584,7 @@ func getWorkingDir(basedir string, codeuri string, checkWorkingDirExist bool) (s
if _, err := os.Stat(dir); err != nil {
// It doesn't, so just use the directory of the SAM template
// which might have been passed as a relative directory
dir = basedir
dir = abs
}

}
Expand Down
198 changes: 133 additions & 65 deletions runtime_test.go
@@ -1,72 +1,140 @@
package main

import (
"os"
"github.com/awslabs/goformation"
"github.com/awslabs/goformation/resources"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"fmt"
"os"
"path/filepath"
"strconv"
"strings"

"github.com/awslabs/goformation"
"github.com/awslabs/goformation/resources"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

var _ = Describe("sam", func() {
Describe("runtime", func() {

var functions map[string]resources.Resource

Context("environment variables", func() {

BeforeEach(func() {
template, _, _ := goformation.Open("test/templates/sam-official-samples/iot_backend/template.yaml")
functions = template.GetResourcesByType("AWS::Serverless::Function")
})

It("return defaults with those defined in the template", func() {

for _, resource := range functions {
function := resource.(resources.AWSServerlessFunction)
variables := getEnvironmentVariables(function, map[string]string{})
Expect(variables).To(HaveLen(10))
Expect(variables).To(HaveKey("AWS_SAM_LOCAL"))
Expect(variables).To(HaveKey("AWS_REGION"))
Expect(variables).To(HaveKey("AWS_DEFAULT_REGION"))
Expect(variables).To(HaveKey("AWS_ACCESS_KEY_ID"))
Expect(variables).To(HaveKey("AWS_SECRET_ACCESS_KEY"))
Expect(variables).To(HaveKey("AWS_SESSION_TOKEN"))
Expect(variables).To(HaveKey("AWS_LAMBDA_FUNCTION_MEMORY_SIZE"))
Expect(variables).To(HaveKey("AWS_LAMBDA_FUNCTION_TIMEOUT"))
Expect(variables).To(HaveKey("AWS_LAMBDA_FUNCTION_HANDLER"))
Expect(variables).To(HaveKey("TABLE_NAME"))
}
})

It("overides template with environment variables", func() {
for _, resource := range functions {
function := resource.(resources.AWSServerlessFunction)
variables := getEnvironmentVariables(function, map[string]string{})
Expect(variables["TABLE_NAME"]).To(Equal(""))

os.Setenv("TABLE_NAME", "ENV_TABLE")
variables = getEnvironmentVariables(function, map[string]string{})
Expect(variables["TABLE_NAME"]).To(Equal("ENV_TABLE"))
os.Unsetenv("TABLE_NAME")
}
})

It("overrides template and environment with customer overrides", func() {
os.Setenv("TABLE_NAME", "ENV_TABLE")
overrides := map[string]string{
"TABLE_NAME": "OVERRIDE_TABLE",
}

for _, resource := range functions {
function := resource.(resources.AWSServerlessFunction)
variables := getEnvironmentVariables(function, overrides)
Expect(variables["TABLE_NAME"]).To(Equal("OVERRIDE_TABLE"))
}
os.Unsetenv("TABLE_NAME")
})

})

})

Describe("runtime", func() {

Context("working directory", func() {

cwd, err := os.Getwd()
It("should determin the current working directory", func() {
Expect(cwd).ToNot(BeNil())
Expect(err).To(BeNil())
})

baseDir := "/" + strings.Trim(os.TempDir(), "/")
baseDirNonExistent := "/yeHNpGawFr0SJ9RZae6mWJKrqvfjaNbJgOM3oQhAQQif6e1Dpbra"
baseDirEmpty := ""

codeUri := "./test"
codeUriUnknown := "s3://<bucket>/packaged.zip"
codeUriEmpty := ""
os.Mkdir(filepath.Join(baseDir, codeUri), 0755)

inputs := [][]string{
// basedir, CodeUri, checkWorkingDir, expected result
[]string{baseDir, codeUri, "true", filepath.Join(baseDir, codeUri)},
[]string{baseDirNonExistent, codeUri, "true", baseDirNonExistent},
[]string{baseDirEmpty, codeUri, "true", filepath.Join(cwd, codeUri)},
[]string{baseDir, codeUriUnknown, "true", baseDir},
[]string{baseDirNonExistent, codeUriUnknown, "true", baseDirNonExistent},
[]string{baseDirEmpty, codeUriUnknown, "true", cwd},
[]string{baseDir, codeUriEmpty, "true", baseDir},
[]string{baseDirNonExistent, codeUriEmpty, "true", baseDirNonExistent},
[]string{baseDirEmpty, codeUriEmpty, "true", cwd},

// getWorkingDir=false is used when SAM Local runs locally, but the Docker container
// is on a remote host. The base path / CodeUri might not resolve locally.
[]string{baseDir, codeUri, "false", filepath.Join(baseDir, codeUri)},
[]string{baseDirNonExistent, codeUri, "false", filepath.Join(baseDirNonExistent, codeUri)},
[]string{baseDirEmpty, codeUri, "false", filepath.Join(cwd, codeUri)},
[]string{baseDir, codeUriUnknown, "false", filepath.Join(baseDir, codeUriUnknown)},
[]string{baseDirNonExistent, codeUriUnknown, "false", filepath.Join(baseDirNonExistent, codeUriUnknown)},
[]string{baseDirEmpty, codeUriUnknown, "false", filepath.Join(cwd, codeUriUnknown)},
[]string{baseDir, codeUriEmpty, "false", baseDir},
[]string{baseDirNonExistent, codeUriEmpty, "false", baseDirNonExistent},
[]string{baseDirEmpty, codeUriEmpty, "false", cwd},
}

// func getWorkingDir(basedir string, codeuri string, checkWorkingDirExist bool) (string, error) {
for _, input := range inputs {

basedir := input[0]
codeuri := input[1]
check, _ := strconv.ParseBool(input[2])
expected := input[3]

context := fmt.Sprintf("with basedir=%s, CodeUri=%s and checkWorkingDirExists=%t", basedir, codeuri, check)
Context(context, func() {
It("should have the correct directory", func() {
dir, err := getWorkingDir(basedir, codeuri, check)
Expect(err).To(BeNil())
Expect(dir).To(Equal(expected))
})
})

}

})

Context("environment variables", func() {

var functions map[string]resources.Resource
BeforeEach(func() {
template, _, _ := goformation.Open("test/templates/sam-official-samples/iot_backend/template.yaml")
functions = template.GetResourcesByType("AWS::Serverless::Function")
})

It("return defaults with those defined in the template", func() {

for _, resource := range functions {
function := resource.(resources.AWSServerlessFunction)
variables := getEnvironmentVariables(function, map[string]string{})
Expect(variables).To(HaveLen(10))
Expect(variables).To(HaveKey("AWS_SAM_LOCAL"))
Expect(variables).To(HaveKey("AWS_REGION"))
Expect(variables).To(HaveKey("AWS_DEFAULT_REGION"))
Expect(variables).To(HaveKey("AWS_ACCESS_KEY_ID"))
Expect(variables).To(HaveKey("AWS_SECRET_ACCESS_KEY"))
Expect(variables).To(HaveKey("AWS_SESSION_TOKEN"))
Expect(variables).To(HaveKey("AWS_LAMBDA_FUNCTION_MEMORY_SIZE"))
Expect(variables).To(HaveKey("AWS_LAMBDA_FUNCTION_TIMEOUT"))
Expect(variables).To(HaveKey("AWS_LAMBDA_FUNCTION_HANDLER"))
Expect(variables).To(HaveKey("TABLE_NAME"))
}
})

It("overides template with environment variables", func() {
for _, resource := range functions {
function := resource.(resources.AWSServerlessFunction)
variables := getEnvironmentVariables(function, map[string]string{})
Expect(variables["TABLE_NAME"]).To(Equal(""))

os.Setenv("TABLE_NAME", "ENV_TABLE")
variables = getEnvironmentVariables(function, map[string]string{})
Expect(variables["TABLE_NAME"]).To(Equal("ENV_TABLE"))
os.Unsetenv("TABLE_NAME")
}
})

It("overrides template and environment with customer overrides", func() {
os.Setenv("TABLE_NAME", "ENV_TABLE")
overrides := map[string]string{
"TABLE_NAME": "OVERRIDE_TABLE",
}

for _, resource := range functions {
function := resource.(resources.AWSServerlessFunction)
variables := getEnvironmentVariables(function, overrides)
Expect(variables["TABLE_NAME"]).To(Equal("OVERRIDE_TABLE"))
}
os.Unsetenv("TABLE_NAME")
})

})

})
})

0 comments on commit d4773f9

Please sign in to comment.