Skip to content

Commit

Permalink
Remove os.Setenv/os.Getenv from Platformoptions
Browse files Browse the repository at this point in the history
[#152091072]
  • Loading branch information
dgodd committed Oct 20, 2017
1 parent f0c7a1c commit f4b846d
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 32 deletions.
2 changes: 1 addition & 1 deletion builder/main.go
Expand Up @@ -25,7 +25,7 @@ func main() {
usage()
}

if platformOptions, err := platformoptions.Get(); err != nil {
if platformOptions, err := platformoptions.Get(os.Getenv("VCAP_PLATFORM_OPTIONS")); err != nil {
fmt.Fprintf(os.Stderr, "Invalid platform options: %v", err)
os.Exit(3)
} else if platformOptions != nil && platformOptions.CredhubURI != "" {
Expand Down
2 changes: 1 addition & 1 deletion launcher/main.go
Expand Up @@ -82,7 +82,7 @@ func main() {
os.Exit(1)
}

if platformOptions, err := platformoptions.Get(); err != nil {
if platformOptions, err := platformoptions.Get(os.Getenv("VCAP_PLATFORM_OPTIONS")); err != nil {
fmt.Fprintf(os.Stderr, "Invalid platform options: %v", err)
os.Exit(3)
} else if platformOptions != nil && platformOptions.CredhubURI != "" {
Expand Down
4 changes: 1 addition & 3 deletions platformoptions/platformoptions.go
Expand Up @@ -2,7 +2,6 @@ package platformoptions

import (
"encoding/json"
"os"
)

type PlatformOptions struct {
Expand All @@ -11,8 +10,7 @@ type PlatformOptions struct {

var cachedPlatformOptions *PlatformOptions

func Get() (*PlatformOptions, error) {
jsonPlatformOptions := os.Getenv("VCAP_PLATFORM_OPTIONS")
func Get(jsonPlatformOptions string) (*PlatformOptions, error) {
if jsonPlatformOptions != "" {
platformOptions := PlatformOptions{}
err := json.Unmarshal([]byte(jsonPlatformOptions), &platformOptions)
Expand Down
33 changes: 6 additions & 27 deletions platformoptions/platformoptions_test.go
@@ -1,8 +1,6 @@
package platformoptions_test

import (
"os"

"code.cloudfoundry.org/buildpackapplifecycle/platformoptions"

. "github.com/onsi/ginkgo"
Expand All @@ -16,32 +14,13 @@ var _ = Describe("Platformoptions", func() {
vcapPlatformOptions string
)

BeforeEach(func() {
vcapPlatformOptions = os.Getenv("VCAP_PLATFORM_OPTIONS")
})

JustBeforeEach(func() {
platformOptions, err = platformoptions.Get()
})

AfterEach(func() {
os.Setenv("VCAP_PLATFORM_OPTIONS", vcapPlatformOptions)
})

Context("when VCAP_PLATFORM_OPTIONS is not set", func() {
BeforeEach(func() {
os.Unsetenv("VCAP_PLATFORM_OPTIONS")
})

It("returns nil PlatformOptions without error", func() {
Expect(platformOptions).To(BeNil())
Expect(err).ToNot(HaveOccurred())
})
platformOptions, err = platformoptions.Get(vcapPlatformOptions)
})

Context("when VCAP_PLATFORM_OPTIONS is an empty string", func() {
BeforeEach(func() {
os.Setenv("VCAP_PLATFORM_OPTIONS", "")
vcapPlatformOptions = ""
})

It("returns nil PlatformOptions without error", func() {
Expand All @@ -52,7 +31,7 @@ var _ = Describe("Platformoptions", func() {

Context("when VCAP_PLATFORM_OPTIONS is an empty JSON object", func() {
BeforeEach(func() {
os.Setenv("VCAP_PLATFORM_OPTIONS", "{}")
vcapPlatformOptions = "{}"
})

It("returns an unset PlatformOptions", func() {
Expand All @@ -64,7 +43,7 @@ var _ = Describe("Platformoptions", func() {

Context("when VCAP_PLATFORM_OPTIONS is an invalid JSON object", func() {
BeforeEach(func() {
os.Setenv("VCAP_PLATFORM_OPTIONS", `{"credhub-uri":"missing quote and brace`)
vcapPlatformOptions = `{"credhub-uri":"missing quote and brace`
})

It("returns a nil PlatformOptions with an error", func() {
Expand All @@ -75,7 +54,7 @@ var _ = Describe("Platformoptions", func() {

Context("when VCAP_PLATFORM_OPTIONS is a valid JSON object", func() {
BeforeEach(func() {
os.Setenv("VCAP_PLATFORM_OPTIONS", `{"credhub-uri":"valid_json"}`)
vcapPlatformOptions = `{"credhub-uri":"valid_json"}`
})

It("returns populated PlatformOptions", func() {
Expand All @@ -84,7 +63,7 @@ var _ = Describe("Platformoptions", func() {
})

It("returns the same populated PlatformOptions on subsequent invocations", func() {
platformOptions, err = platformoptions.Get()
platformOptions, err = platformoptions.Get(vcapPlatformOptions)
Expect(err).ToNot(HaveOccurred())
Expect(platformOptions).NotTo(BeNil())
Expect(platformOptions.CredhubURI).To(Equal("valid_json"))
Expand Down

0 comments on commit f4b846d

Please sign in to comment.