-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
147 lines (126 loc) · 4.99 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
package main_test
import (
"fmt"
"net/http"
"net/http/httptest"
. "github.com/anynines/cf_service_jumper_cli_plugin"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("main", func() {
Describe("ArgsExtractServiceInstanceName", func() {
It("errors if less than 2 args", func() {
_, err := ArgsExtractServiceInstanceName([]string{"arg0"})
Expect(err).To(Equal(ErrMissingServiceInstanceArg))
})
It("works with 2 args", func() {
instanceName, err := ArgsExtractServiceInstanceName([]string{"arg0", "arg1"})
Expect(err).To(BeNil())
Expect(instanceName).To(Equal("arg1"))
})
})
Describe("ArgsExtractConnectionID", func() {
It("errors if less than 2 args", func() {
_, err := ArgsExtractConnectionID([]string{"arg0", "arg1"})
Expect(err).To(Equal(ErrMissingConnectionID))
})
It("works with 2 args", func() {
connectionId, err := ArgsExtractConnectionID([]string{"arg0", "arg1", "arg2"})
Expect(err).To(BeNil())
Expect(connectionId).To(Equal("arg2"))
})
})
Describe("FetchCfServiceJumperAPIEndpoint", func() {
It("returns service jumper endpoint", func() {
fakeEndpointServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
panic(fmt.Sprintf("fake endpoint server: verb GET expected", r.Method))
}
jsonStr := `{ "name": "Anynines", "custom": { "service_jumper_endpoint": "https://service-jumper.de.a9sservice.eu" } }`
fmt.Fprintln(w, jsonStr)
}))
sjEndpoint, err := FetchCfServiceJumperAPIEndpointFromInfo(fakeEndpointServer.URL, false)
Expect(err).To(BeNil())
Expect(sjEndpoint).To(Equal("https://service-jumper.de.a9sservice.eu"))
})
It("errors if json broken", func() {
fakeEndpointServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
jsonStr := `"name": "Anynines"`
fmt.Fprintln(w, jsonStr)
}))
_, err := FetchCfServiceJumperAPIEndpointFromInfo(fakeEndpointServer.URL, false)
Expect(err).ToNot(BeNil())
})
It("errors if cf service humper endpoint not present", func() {
fakeEndpointServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
jsonStr := `{ "name": "Anynines", "custom": { } }`
fmt.Fprintln(w, jsonStr)
}))
_, err := FetchCfServiceJumperAPIEndpointFromInfo(fakeEndpointServer.URL, false)
Expect(err).To(Equal(ErrCfServiceJumperEndpointNotPresent))
})
})
Describe("FetchCfServiceJumperAPIEndpointFromSharedDomain", func() {
It("returns service jumper endpoint", func() {
endpoint, err := FetchCfServiceJumperAPIEndpointFromSharedDomain("https://api.the.endpoint.com")
Expect(err).To(BeNil())
Expect(endpoint).To(Equal("https://a9s-service-jumper.the.endpoint.com"))
endpoint, err = FetchCfServiceJumperAPIEndpointFromSharedDomain("http://api.the.endpoint.com")
Expect(err).To(BeNil())
Expect(endpoint).To(Equal("http://a9s-service-jumper.the.endpoint.com"))
endpoint, err = FetchCfServiceJumperAPIEndpointFromSharedDomain("http://api.aws.ie.a9sapp.eu")
Expect(err).To(BeNil())
Expect(endpoint).To(Equal("http://a9s-service-jumper.aws.ie.a9sapp.eu"))
})
})
Describe("create forward", func() {
It("returns ForwardDataSet", func() {
fakeServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
panic(fmt.Sprintf("fake server: verb POST expected", r.Method))
}
jsonStr := `{ "public_uris": ["10.100.0.60:27017", "10.100.0.61:27017"], "credentials": { "credentials": { "username": "the_username" } }, "shared_secret": "luser:01234567890123456789", "id": 1234 }`
fmt.Fprintln(w, jsonStr)
}))
p := CfServiceJumperPlugin{
CfServiceJumperAPIEndpoint: fakeServer.URL,
}
forwardDataSet, err := p.CreateForward("serviceGuid")
Expect(err).To(BeNil())
expectedForwardDataSet := ForwardDataSet{
ID: 1234,
Hosts: []string{"10.100.0.60:27017", "10.100.0.61:27017"},
SharedSecret: "luser:01234567890123456789",
Credentials: ForwardCredentials{
Credentials: ForwardSbCredentials{
"username": "the_username",
},
},
}
Expect(forwardDataSet).To(Equal(expectedForwardDataSet))
})
})
Describe("ForwardDataSet", func() {
Describe("CredentialsMap", func() {
It("return a human readble string", func() {
fds := ForwardDataSet{
Credentials: ForwardCredentials{
Credentials: ForwardSbCredentials{
"uri": "theuri",
"username": "username",
"password": "password",
"default_database": "defaultdatabase",
"database": "database",
},
},
}
credentials := fds.CredentialsMap()
Expect(credentials["uri"]).To(Equal("theuri"))
Expect(credentials["username"]).To(Equal("username"))
Expect(credentials["password"]).To(Equal("password"))
Expect(credentials["default_database"]).To(Equal("defaultdatabase"))
Expect(credentials["database"]).To(Equal("database"))
})
})
})
})