-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.go
193 lines (168 loc) · 4.87 KB
/
tests.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package main
import (
"bytes"
"crypto/tls"
"encoding/json"
"fmt"
"html/template"
"io/ioutil"
"k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"log"
"net/http"
"strings"
"time"
)
var psDefaultTransport = http.DefaultTransport.(*http.Transport)
// Create new Transport that ignores self-signed SSL
var httpClientWithSelfSignedTLS = &http.Transport{
Proxy: psDefaultTransport.Proxy,
DialContext: psDefaultTransport.DialContext,
MaxIdleConns: psDefaultTransport.MaxIdleConns,
IdleConnTimeout: psDefaultTransport.IdleConnTimeout,
ExpectContinueTimeout: psDefaultTransport.ExpectContinueTimeout,
TLSHandshakeTimeout: psDefaultTransport.TLSHandshakeTimeout,
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
var psClient = &http.Client{Transport: httpClientWithSelfSignedTLS}
type TestTemplateVars struct {
IndexTemplateVars
Nodes []v1.Node
}
// Process the /tests path
func TestsHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
return
}
session, err := filestore.Get(r, "prp-session")
if err != nil {
log.Printf("Error getting the session: %s", err.Error())
}
if session.IsNew || session.Values["userid"] == nil {
http.Redirect(w, r, "/", http.StatusFound)
return
}
var src = r.URL.Query().Get("src")
var dst = r.URL.Query().Get("dst")
var test = r.URL.Query().Get("test")
if test != "" {
if res, err := RunTest(src, dst, test); err == nil {
if jsonRes, err := json.MarshalIndent(res, "", " "); err == nil {
w.Write(jsonRes)
} else {
http.Error(w, "Failed to retrieve results: "+err.Error(), http.StatusInternalServerError)
}
} else {
http.Error(w, "Failed to retrieve results: "+err.Error(), http.StatusInternalServerError)
}
} else {
t, err := template.New("layout.tmpl").ParseFiles("templates/layout.tmpl", "templates/tests.tmpl")
if err != nil {
w.Write([]byte(err.Error()))
} else {
nodesList, _ := clientset.Core().Nodes().List(metav1.ListOptions{})
nsVars := TestTemplateVars{Nodes: nodesList.Items, IndexTemplateVars: buildIndexTemplateVars(session, w, r)}
err = t.ExecuteTemplate(w, "layout.tmpl", nsVars)
if err != nil {
w.Write([]byte(err.Error()))
}
}
}
}
func RunTest(snode string, dnode string, testType string) (map[string]interface{}, error) {
pschedUrl := fmt.Sprintf("https://%s:8443/pscheduler/tasks", snode)
if resp, err := psClient.Get(pschedUrl); err == nil && resp.StatusCode != 200 {
pschedUrl = fmt.Sprintf("https://%s/pscheduler/tasks", snode)
} else {
snode += ":8443"
}
task := Task{
Schema: 1,
Test: Test{
Type: testType,
Spec: TestSpec{
Schema: 1,
SourceNode: snode,
Dest: dnode,
// Duration: "PT3M",
},
},
Schedule: Schedule{},
}
if testType == "trace" {
task.Tools = []string{"tracepath"}
}
var result Result
if buf, err := json.Marshal(&task); err == nil {
if resp, err := psClient.Post(pschedUrl, "application/json", bytes.NewBuffer(buf)); err == nil {
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
resUrl := fmt.Sprintf("%s/runs/first?wait=10", body)
resUrl = strings.Replace(resUrl, "\"", "", -1)
resUrl = strings.Replace(resUrl, "\n", "", -1)
err = nil
state := "pending"
for err == nil && isExecuting(state) {
// fmt.Printf("Querying url: %s\n", resUrl)
if resp, err := psClient.Get(resUrl); err == nil {
body, _ := ioutil.ReadAll(resp.Body)
if merr := json.Unmarshal(body, &result); merr == nil {
state = result.State
} else {
state = "error"
fmt.Printf("Error getting the state: %s\n", merr)
}
} else {
fmt.Printf("Error submitting the test: %s\n", err)
}
if isExecuting(state) {
time.Sleep(5 * time.Second)
}
}
if result.Errors != "" {
return result.ResultMerged, fmt.Errorf(result.Errors)
} else if result.State == "finished" {
return result.ResultMerged, nil
} else {
return result.ResultMerged, fmt.Errorf("Got state: %s", result.State)
}
} else {
return result.ResultMerged, err
}
} else {
return result.ResultMerged, err
}
}
func isExecuting(state string) bool {
switch state {
case "pending",
"on-deck",
"running",
"cleanup":
return true
}
return false
}
type Result struct {
Errors string `json:"errors"`
State string `json:"state"`
ResultMerged map[string]interface{} `json:"result-merged"`
}
type Task struct {
Schema uint16 `json:"schema"`
Test Test `json:"test"`
Schedule Schedule `json:"schedule"`
Tools []string `json:"tools,omitempty"`
}
type Test struct {
Type string `json:"type"`
Spec TestSpec `json:"spec"`
}
type TestSpec struct {
Schema uint16 `json:"schema"`
SourceNode string `json:"source-node"`
Dest string `json:"dest"`
Duration string `json:"duration,omitempty"`
}
type Schedule struct {
}