forked from cloudfoundry-attic/ltc
/
main_test.go
150 lines (124 loc) · 4.28 KB
/
main_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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package main_test
import (
"fmt"
"io/ioutil"
"net"
"net/http"
"os"
"os/exec"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/gbytes"
"github.com/onsi/gomega/gexec"
"github.com/onsi/gomega/ghttp"
"github.com/cloudfoundry-incubator/ltc/config"
"github.com/cloudfoundry-incubator/ltc/config/config_helpers"
"github.com/cloudfoundry-incubator/ltc/config/persister"
"github.com/cloudfoundry-incubator/ltc/test_helpers"
"github.com/cloudfoundry-incubator/receptor"
)
var _ = Describe("LatticeCli Main", func() {
const latticeCliHomeVar = "LATTICE_CLI_HOME"
var (
outputBuffer *gbytes.Buffer
fakeServer *ghttp.Server
listenerAddr, configRoot string
listenerHost, listenerPort string
)
desiredResponse := []receptor.DesiredLRPResponse{
{
ProcessGuid: "some-guid",
Instances: 1,
},
}
actualResponse := []receptor.ActualLRPResponse{
{
ProcessGuid: "some-guid",
Index: 12,
State: receptor.ActualLRPStateRunning,
},
}
tasksResponse := []receptor.TaskResponse{
{TaskGuid: "task-guid"},
}
BeforeEach(func() {
outputBuffer = gbytes.NewBuffer()
fakeServer = ghttp.NewServer()
listenerAddr = fakeServer.HTTPTestServer.Listener.Addr().String()
fakeServer.RouteToHandler("GET", "/v1/desired_lrps",
ghttp.CombineHandlers(
ghttp.VerifyRequest("GET", "/v1/desired_lrps"),
ghttp.RespondWithJSONEncoded(http.StatusOK, desiredResponse),
),
)
fakeServer.RouteToHandler("GET", "/v1/actual_lrps",
ghttp.CombineHandlers(
ghttp.VerifyRequest("GET", "/v1/actual_lrps"),
ghttp.RespondWithJSONEncoded(http.StatusOK, actualResponse),
),
)
fakeServer.RouteToHandler("GET", "/v1/tasks",
ghttp.CombineHandlers(
ghttp.VerifyRequest("GET", "/v1/tasks"),
ghttp.RespondWithJSONEncoded(http.StatusOK, tasksResponse),
),
)
var err error
configRoot, err = ioutil.TempDir("", "config")
Expect(err).NotTo(HaveOccurred())
ltcConfig := config.New(persister.NewFilePersister(config_helpers.ConfigFileLocation(configRoot)))
listenerHost, listenerPort, err = net.SplitHostPort(listenerAddr)
Expect(err).NotTo(HaveOccurred())
ltcConfig.SetTarget(fmt.Sprintf("%s.xip.io:%s", listenerHost, listenerPort))
Expect(ltcConfig.Save()).To(Succeed())
os.Setenv(latticeCliHomeVar, configRoot)
})
AfterEach(func() {
fakeServer.Close()
Expect(os.RemoveAll(configRoot)).To(Succeed())
Expect(os.Unsetenv(latticeCliHomeVar)).To(Succeed())
})
ltcCommand := func(args ...string) *exec.Cmd {
command := exec.Command(ltcPath, args...)
cliHome := fmt.Sprintf("LATTICE_CLI_HOME=%s", configRoot)
command.Env = []string{cliHome}
return command
}
Context("when ltc is invoked with no args", func() {
It("should exit zero and display ltc help text", func() {
command := ltcCommand()
session, err := gexec.Start(command, GinkgoWriter, GinkgoWriter)
Expect(err).NotTo(HaveOccurred())
Eventually(session, 3).Should(gexec.Exit(0))
Eventually(session.Out).Should(gbytes.Say("ltc - Command line interface for Lattice."))
})
})
Describe("Exit Codes", func() {
It("should exit zero and run desired command", func() {
command := ltcCommand("target")
session, err := gexec.Start(command, GinkgoWriter, GinkgoWriter)
Expect(err).NotTo(HaveOccurred())
Eventually(session, 3).Should(gexec.Exit(0))
Expect(session.Out).To(gbytes.Say("Target:"))
Expect(session.Out).To(gbytes.Say(fmt.Sprintf("%s.xip.io:%s", listenerHost, listenerPort)))
})
Context("when an unknown command is invoked", func() {
It("should exit non-zero when an unknown command is invoked", func() {
command := ltcCommand("unknown-command")
session, err := gexec.Start(command, GinkgoWriter, GinkgoWriter)
Expect(err).NotTo(HaveOccurred())
Eventually(session, 3).Should(gexec.Exit(1))
Expect(session.Out).To(gbytes.Say("not a registered command"))
})
})
Context("when a known command is invoked with an invalid flag", func() {
It("should exit non-zero and print incorrect usage", func() {
command := ltcCommand("status", "--badFlag")
session, err := gexec.Start(command, GinkgoWriter, GinkgoWriter)
Expect(err).NotTo(HaveOccurred())
Eventually(session, 3).Should(gexec.Exit(1))
Expect(session.Out).To(test_helpers.SayIncorrectUsage())
})
})
})
})