forked from jthomas/copyenv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
copyenv_test.go
94 lines (77 loc) · 3.63 KB
/
copyenv_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package main_test
import (
"errors"
. "copyenv"
"code.cloudfoundry.org/cli/plugin/models"
"code.cloudfoundry.org/cli/plugin/pluginfakes"
io_helpers "code.cloudfoundry.org/cli/util/testhelpers/io"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("Cloud Foundry Copyenv Command", func() {
Describe(".Run", func() {
var fakeCliConnection *pluginfakes.FakeCliConnection
var callCopyEnvCommandPlugin *CopyEnv
BeforeEach(func() {
fakeCliConnection = &pluginfakes.FakeCliConnection{}
callCopyEnvCommandPlugin = &CopyEnv{}
})
It("Extract Application Name From Command Line Args", func() {
name, err := callCopyEnvCommandPlugin.ExtractAppName([]string{"copyenv"})
Ω(err).Should(MatchError("missing application name"))
name, err = callCopyEnvCommandPlugin.ExtractAppName([]string{"copyenv", "APP_NAME"})
Ω(err).ShouldNot(HaveOccurred())
Ω(name).Should(Equal("APP_NAME"))
})
It("Retrieve Application Environment Variables From Name", func() {
fakeCliConnection.CliCommandWithoutTerminalOutputReturns([]string{"SOME", "OUTPUT", "COMMAND"}, nil)
fakeCliConnection.GetAppReturns(plugin_models.GetAppModel{Guid: "testing"}, nil)
output, err := callCopyEnvCommandPlugin.RetrieveAppNameEnv(fakeCliConnection, "APP_NAME")
Ω(err).ShouldNot(HaveOccurred())
Ω(fakeCliConnection.CliCommandWithoutTerminalOutputCallCount()).Should(Equal(1))
Ω(fakeCliConnection.CliCommandWithoutTerminalOutputArgsForCall(0)).Should(Equal([]string{"curl", "/v2/apps/testing/env"}))
Ω(output).Should(Equal([]string{"SOME", "OUTPUT", "COMMAND"}))
})
It("Return Service Credentials From Appplication Environment", func() {
_, err := callCopyEnvCommandPlugin.ExtractCredentialsJSON("system_env_json", "VCAP_SERVICES", []string{""})
Ω(err).Should(MatchError("missing service credentials for application"))
service_creds := []string{"{\"system_env_json\": {\"VCAP_SERVICES\":{\"service\": [ { \"credentials\": {} } ]}}}"}
b, err := callCopyEnvCommandPlugin.ExtractCredentialsJSON("system_env_json", "VCAP_SERVICES", service_creds)
Ω(err).ShouldNot(HaveOccurred())
Ω(string(b[:])).Should(Equal("{\"service\":[{\"credentials\":{}}]}"))
})
It("Print Service Credentials As Shell Variable", func() {
output := io_helpers.CaptureOutput(func() {
callCopyEnvCommandPlugin.ExportCredsAsShellVar("VCAP_SERVICES", "testing")
})
Ω(output[0]).Should(Equal("export VCAP_SERVICES='testing';"))
})
It("Return Error When App Name Is Missing", func() {
fakeCliConnection.CliCommandWithoutTerminalOutputReturns([]string{}, errors.New(""))
output, err := callCopyEnvCommandPlugin.RetrieveAppNameEnv(fakeCliConnection, "missing_app")
Ω(output).Should(Equal([]string{}))
Ω(err).ShouldNot(Equal(nil))
})
It("Silently uninstalls", func() {
callCopyEnvCommandPlugin.Run(fakeCliConnection, []string{"CLI-MESSAGE-UNINSTALL"})
Ω(fakeCliConnection.CliCommandWithoutTerminalOutputCallCount()).Should(Equal(0))
})
Context("when called with --all", func() {
It("Extracts VCAP_APPLICATION and VCAP_SERVICE", func() {
services := "{\"VCAP_SERVICES\":[\"services\"]}"
application := "{\"VCAP_APPLICATION\":[\"application\"]}"
fakeCliConnection.CliCommandWithoutTerminalOutputReturns([]string{
services, application, "OTHER"}, nil)
output := io_helpers.CaptureOutput(func() {
callCopyEnvCommandPlugin.Run(fakeCliConnection, []string{"copyenv", "APP_NAME", "--all"})
})
Ω(output).Should(ContainElement(
"export VCAP_APPLICATION='[\"application\"]';",
))
Ω(output).Should(ContainElement(
"export VCAP_SERVICES='[\"services\"]';",
))
})
})
})
})