-
Notifications
You must be signed in to change notification settings - Fork 1
/
server_test.go
106 lines (100 loc) · 3.1 KB
/
server_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
package main
import (
"bytes"
"encoding/json"
"fmt"
"github.com/go-martini/martini"
"github.com/martini-contrib/binding"
"github.com/martini-contrib/render"
. "github.com/smartystreets/goconvey/convey"
"net/http"
"net/http/httptest"
"os"
"testing"
)
func Server() *martini.ClassicMartini {
m := martini.Classic()
m.Use(render.Renderer())
m.Get("/v1/es/plans", plans_handler)
m.Get("/v1/es/url/:domainname", status_handler)
m.Get("/v1/es/instance/:domainname/status", status_handler)
m.Post("/v1/es/tag", binding.Json(tagspec{}), tag_handler)
return m
}
func Init() *martini.ClassicMartini {
setenv()
err := createdb()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
m := Server()
return m
}
func TestBroker(t *testing.T) {
m := Init()
Convey("Given we want the plans\n", t, func() {
r, _ := http.NewRequest("GET", "/v1/es/plans", nil)
w := httptest.NewRecorder()
m.ServeHTTP(w, r)
So(w.Code, ShouldEqual, http.StatusOK)
fmt.Println(w)
decoder := json.NewDecoder(w.Body)
var response map[string]string
if err := decoder.Decode(&response); err != nil {
panic(err)
}
So(response["micro"], ShouldNotEqual, "")
Convey("To get the url\n", func() {
r, _ := http.NewRequest("GET", "/v1/es/url/merpderp", nil)
w := httptest.NewRecorder()
m.ServeHTTP(w, r)
So(w.Code, ShouldEqual, http.StatusOK)
fmt.Println(w)
decoder := json.NewDecoder(w.Body)
var response map[string]string
if err := decoder.Decode(&response); err != nil {
panic(err)
}
So(response["ES_URL"], ShouldEqual, "https://vpc-merpderp-266sk4si4qhodjybbn3gbkznpq.us-west-2.es.amazonaws.com")
So(response["KIBANA_URL"], ShouldEqual, "https://vpc-merpderp-266sk4si4qhodjybbn3gbkznpq.us-west-2.es.amazonaws.com/_plugin/kibana")
Convey("To get the status\n", func() {
r, _ := http.NewRequest("GET", "/v1/es/instance/merpderp/status", nil)
w := httptest.NewRecorder()
m.ServeHTTP(w, r)
So(w.Code, ShouldEqual, http.StatusOK)
fmt.Println(w)
decoder := json.NewDecoder(w.Body)
var response map[string]string
if err := decoder.Decode(&response); err != nil {
panic(err)
}
So(response["ES_URL"], ShouldEqual, "https://vpc-merpderp-266sk4si4qhodjybbn3gbkznpq.us-west-2.es.amazonaws.com")
So(response["KIBANA_URL"], ShouldEqual, "https://vpc-merpderp-266sk4si4qhodjybbn3gbkznpq.us-west-2.es.amazonaws.com/_plugin/kibana")
Convey("Tag and instance", func() {
b := new(bytes.Buffer)
var payload tagspec
payload.Resource = "merpderp"
payload.Name = "unittestname"
payload.Value = "unittestvalue"
if err := json.NewEncoder(b).Encode(payload); err != nil {
panic(err)
}
fmt.Println(b)
req, _ := http.NewRequest("POST", "/v1/es/tag", b)
resp := httptest.NewRecorder()
m.ServeHTTP(resp, req)
fmt.Println(resp)
So(resp.Code, ShouldEqual, http.StatusCreated)
decoder := json.NewDecoder(resp.Body)
var response map[string]string
if err := decoder.Decode(&response); err != nil {
panic(err)
}
fmt.Println(response)
So(response["response"], ShouldEqual, "tag added")
})
})
})
})
}