-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathevents_test.go
More file actions
107 lines (90 loc) · 2.47 KB
/
events_test.go
File metadata and controls
107 lines (90 loc) · 2.47 KB
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
package events
import (
"fmt"
"net/http"
"net/http/httptest"
"reflect"
"testing"
drycc "github.com/drycc/controller-sdk-go"
"github.com/drycc/controller-sdk-go/api"
)
const podEventsFixture string = `
{
"count": 1,
"next": null,
"previous": null,
"results": [
{
"reason": "Scheduled",
"message": "Successfully assigned example-go/example-go-web-6b44dbd6c8-h89cg to node1",
"created": "2024-07-03T16:28:00"
}
]
}`
const ptypeEventsFixture string = `
{
"count": 1,
"next": null,
"previous": null,
"results": [
{
"reason": "ScalingReplicaSet",
"message": "Scaled up replica set example-go-web-6b44dbd6c8 to 2 from 1",
"created": "2024-07-03T16:28:00"
}
]
}`
type fakeHTTPServer struct{}
func (fakeHTTPServer) ServeHTTP(res http.ResponseWriter, req *http.Request) {
res.Header().Add("DRYCC_API_VERSION", drycc.APIVersion)
if req.URL.Path == "/v2/apps/example-go/events/" && req.Method == "GET" && req.URL.RawQuery == "ptype=example-go-web" {
res.Write([]byte(ptypeEventsFixture))
return
}
if req.URL.Path == "/v2/apps/example-go/events/" && req.Method == "GET" && req.URL.RawQuery == "pod_name=example-go-web-6b44dbd6c8-h89cg" {
res.Write([]byte(podEventsFixture))
return
}
fmt.Printf("Unrecognized URL %s\n", req.URL)
res.WriteHeader(http.StatusNotFound)
res.Write(nil)
}
func TestEvents(t *testing.T) {
t.Parallel()
created := "2024-07-03T16:28:00"
podExpected := api.AppEvents{
{
Reason: "Scheduled",
Message: "Successfully assigned example-go/example-go-web-6b44dbd6c8-h89cg to node1",
Created: created,
},
}
ptypeExpected := api.AppEvents{
{
Reason: "ScalingReplicaSet",
Message: "Scaled up replica set example-go-web-6b44dbd6c8 to 2 from 1",
Created: created,
},
}
handler := fakeHTTPServer{}
server := httptest.NewServer(handler)
defer server.Close()
drycc, err := drycc.New(false, server.URL, "abc")
if err != nil {
t.Fatal(err)
}
actual, _, err := ListPodEvents(drycc, "example-go", "example-go-web-6b44dbd6c8-h89cg", 100)
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(podExpected, actual) {
t.Error(fmt.Errorf("Expected %v, Got %v", podExpected, actual))
}
actual, _, err = ListPtypeEvents(drycc, "example-go", "web", 100)
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(ptypeExpected, actual) {
t.Error(fmt.Errorf("Expected %v, Got %v", ptypeExpected, actual))
}
}