-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathservices_test.go
77 lines (66 loc) · 1.86 KB
/
services_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
package main
import (
"encoding/json"
"github.com/go-playground/assert/v2"
"gopkg.in/yaml.v2"
"io/ioutil"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
var NotFound = "NOT_FOUND"
var BadRequest = "BadRequest"
func TestGetSts(t *testing.T) {
expect := readExpectedSts()
bytes, _ := json.Marshal(expect)
ts := buildTS(&bytes)
defer ts.Close()
svc := buildMockSvc(ts.URL)
actual := svc.getSts(expect.Metadata.Namespace, expect.Metadata.Name)
assert.NotEqual(t, nil, actual)
assert.Equal(t, expect.Spec.Replicas, actual.Spec.Replicas)
}
func TestGetStsNotFound(t *testing.T) {
expect := readExpectedSts()
bytes, _ := json.Marshal(expect)
ts := buildTS(&bytes)
defer ts.Close()
svc := buildMockSvc(ts.URL)
actual := svc.getSts(expect.Metadata.Namespace, NotFound)
assert.Equal(t, nil, actual)
}
func TestFilterSts(t *testing.T) {
ownerNonSts := OwnerReference{Kind: "Deployment"}
resultNonSts := filterSts(append([]OwnerReference{}, ownerNonSts))
assert.Equal(t, nil, resultNonSts)
ownerSts := OwnerReference{Kind: "StatefulSet"}
resultSts := filterSts(append([]OwnerReference{}, ownerSts))
assert.Equal(t, ownerSts, resultSts)
}
func readExpectedSts() (expect Statefulset) {
expect = Statefulset{}
yamlFile, _ := ioutil.ReadFile("test/sts.yaml")
_ = yaml.Unmarshal(yamlFile, &expect)
return
}
func buildTS(body *[]byte) (ts *httptest.Server) {
ts = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if strings.Contains(r.URL.String(), NotFound) {
w.WriteHeader(http.StatusNotFound)
w.Write(*body)
} else if strings.Contains(r.URL.String(), BadRequest) {
w.WriteHeader(http.StatusBadRequest)
w.Write(*body)
} else {
w.WriteHeader(http.StatusOK)
w.Write(*body)
}
}))
return
}
func buildMockSvc(url string) (svc K8sService) {
config := initTestConfig(url)
svc = newK8sService(config)
return
}