-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
96 lines (84 loc) · 2.25 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
package main
import (
"encoding/json"
"fmt"
"github.com/ssdowd/couchbasebroker/config"
"github.com/ssdowd/couchbasebroker/utils"
"github.com/ssdowd/couchbasebroker/web_server"
"io/ioutil"
"net"
"net/http"
"testing"
)
var opt = Options{
ConfigPath: "assets/config.json",
Cloud: "Bosh",
CloudOptionsPath: "assets/bosh-config.json",
}
var myconfig *config.Config
func TestValidCloud(t *testing.T) {
err := checkCloudName(opt.Cloud)
if err != nil {
t.Errorf("Failed valid cloud name: %v", err)
}
err = checkCloudName(utils.AWS)
if err != nil {
t.Errorf("Failed valid cloud name: %v", err)
}
badName := "Bogus Cloud Name"
err = checkCloudName(badName)
if err == nil {
t.Errorf("Failed to detect invalid cloud name: %v", badName)
}
}
func TestLoadOptions(t *testing.T) {
t.Log("Testing load", opt.ConfigPath)
c, err := config.LoadConfig(opt.ConfigPath)
if err != nil {
t.Errorf("Failed load config: (%s): %v", opt.ConfigPath, err)
}
myconfig = c
t.Log("Loaded config", myconfig)
}
func TestCreateServer(t *testing.T) {
// test stuff here...
t.Log("Creating Server", opt.Cloud)
server, err := web_server.CreateServer(opt.Cloud, opt.CloudOptionsPath)
if err != nil {
t.Errorf("Failed to create server: %v", err)
}
go server.Start()
}
func TestServerListening(t *testing.T) {
// test stuff here...
t.Log("Testing Server")
_, err := net.Dial("tcp", "localhost:"+config.GetConfig().Port)
if err != nil {
t.Errorf("Failed to connect to server: %v", err)
}
}
func TestGetCatalog(t *testing.T) {
url := "http://localhost:" + config.GetConfig().Port + "/v2/catalog"
t.Log("Testing GET", url)
client := &http.Client{}
req, err := http.NewRequest("GET", url, nil)
req.SetBasicAuth(myconfig.RestUser, myconfig.RestPassword)
res, err := client.Do(req)
if err != nil {
t.Errorf("Failed to GET catalog: %v", err)
}
if res.StatusCode != http.StatusOK {
t.Errorf("Failed to GET catalog, status: %d", res.StatusCode)
}
jsonBytes, err := ioutil.ReadAll(res.Body)
res.Body.Close()
if err != nil {
t.Errorf("Failed reading catalog json: %v", err)
}
t.Log("Catalog:", fmt.Sprintf("%s", jsonBytes))
var f interface{}
err = json.Unmarshal(jsonBytes, &f)
if err != nil {
t.Errorf("Failed unmarshalling catalog json: %v", err)
}
}