forked from nanobox-io/slurp
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
85 lines (67 loc) · 1.81 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
package main
import (
"bytes"
"crypto/tls"
"fmt"
"io/ioutil"
"net/http"
"os"
"strings"
"testing"
"time"
"github.com/jcelliott/lumber"
"github.com/mu-box/slurp/backend"
"github.com/mu-box/slurp/config"
)
func TestMain(m *testing.M) {
// clean test dir
os.RemoveAll("/tmp/slurpMain")
// manually configure
initialize()
args := strings.Split("-b /tmp/slurpMain/ -l fatal -k /tmp/slurp_rsa -s 127.0.0.1:1568 -a https://127.0.0.1:1564", " ")
slurp.SetArgs(args)
// start api
go slurp.Execute()
<-time.After(time.Second)
rtn := m.Run()
// clean test dir
os.RemoveAll("/tmp/slurpMain")
os.Exit(rtn)
}
func TestPing(t *testing.T) {
body, err := rest("GET", "/ping", "")
if err != nil {
t.Error(err)
}
if string(body) != "pong\n" {
t.Errorf("%q doesn't match expected out", body)
}
}
////////////////////////////////////////////////////////////////////////////////
// PRIVS
////////////////////////////////////////////////////////////////////////////////
// manually configure and start internals
func initialize() {
http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
config.Log = lumber.NewConsoleLogger(lumber.LvlInt(config.LogLevel))
config.ApiToken = ""
// check for hoarder
err := backend.Initialize()
if err != nil {
fmt.Printf("Backend init failed, skipping tests - %v\n", err)
os.Exit(0)
}
}
// hit api and return response body
func rest(method, route, data string) ([]byte, error) {
body := bytes.NewBuffer([]byte(data))
req, _ := http.NewRequest(method, fmt.Sprintf("%s%s", config.ApiAddress, route), body)
req.Header.Add("X-AUTH-TOKEN", "")
res, err := http.DefaultClient.Do(req)
if err != nil {
return nil, fmt.Errorf("Unable to %v %v - %v", method, route, err)
}
defer res.Body.Close()
b, _ := ioutil.ReadAll(res.Body)
return b, nil
}