forked from jthomas/copyenv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
copyenv_test.go
82 lines (68 loc) · 3.09 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
package main_test
import (
. "."
"github.com/cloudfoundry/cli/plugin/fakes"
io_helpers "github.com/cloudfoundry/cli/testhelpers/io"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("Cloud Foundry Copyenv Command", func() {
Describe(".Run", func() {
var fakeCliConnection *fakes.FakeCliConnection
var callCopyEnvCommandPlugin *CopyEnv
BeforeEach(func() {
fakeCliConnection = &fakes.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)
output, err := callCopyEnvCommandPlugin.RetrieveAppNameEnv(fakeCliConnection, "APP_NAME")
Ω(err).ShouldNot(HaveOccurred())
Ω(fakeCliConnection.CliCommandWithoutTerminalOutputCallCount()).Should(Equal(1))
Ω(fakeCliConnection.CliCommandWithoutTerminalOutputArgsForCall(0)).Should(Equal([]string{"env", "APP_NAME"}))
Ω(output).Should(Equal([]string{"SOME", "OUTPUT", "COMMAND"}))
})
It("Return Service Credentials From Appplication Environment", func() {
_, err := callCopyEnvCommandPlugin.ExtractCredentialsJSON("VCAP_SERVICES", []string{""})
Ω(err).Should(MatchError("missing service credentials for application"))
service_creds := []string{"{\"VCAP_SERVICES\":{\"service\": [ { \"credentials\": {} } ]}}"}
b, err := callCopyEnvCommandPlugin.ExtractCredentialsJSON("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("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\"]';",
))
})
})
})
})