This repository has been archived by the owner on Apr 17, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
e2e.go
175 lines (150 loc) · 4.5 KB
/
e2e.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
/*
Copyright 2015 The Kubernetes Authors All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"encoding/json"
"fmt"
"net/http"
"sync"
"time"
github_util "k8s.io/contrib/github"
"k8s.io/contrib/submit-queue/jenkins"
"github.com/golang/glog"
github_api "github.com/google/go-github/github"
)
// PRInfo explicitly collects the fields we wish to encode via JSON.
type PRInfo struct {
Number *int `json:"number"`
URL *string `json:"html_url"`
}
func prInfo(pr *github_api.PullRequest) *PRInfo {
var out PRInfo
if pr != nil {
out.Number = pr.Number
out.URL = pr.HTMLURL
}
return &out
}
type ExternalState struct {
// exported so that the json marshaller will print them
CurrentPR *PRInfo
Message []string
Err error
BuildStatus map[string]string
Whitelist []string
}
type e2eTester struct {
sync.Mutex
state *ExternalState
Config *SubmitQueueConfig
}
func (e *e2eTester) msg(msg string, args ...interface{}) {
e.Lock()
defer e.Unlock()
if len(e.state.Message) > 50 {
e.state.Message = e.state.Message[1:]
}
expanded := fmt.Sprintf(msg, args...)
e.state.Message = append(e.state.Message, fmt.Sprintf("%v: %v", time.Now().UTC(), expanded))
glog.V(2).Info(expanded)
}
func (e *e2eTester) error(err error) {
e.Lock()
defer e.Unlock()
e.state.Err = err
}
func (e *e2eTester) locked(f func()) {
e.Lock()
defer e.Unlock()
f()
}
func (e *e2eTester) setBuildStatus(build, status string) {
e.locked(func() { e.state.BuildStatus[build] = status })
}
func (e *e2eTester) checkBuilds() (allStable bool) {
// Test if the build is stable in Jenkins
jenkinsClient := &jenkins.JenkinsClient{Host: e.Config.JenkinsHost}
allStable = true
for _, build := range e.Config.JenkinsJobs {
e.msg("Checking build stability for %s", build)
stable, err := jenkinsClient.IsBuildStable(build)
if err != nil {
e.msg("Error checking build %v: %v", build, err)
e.setBuildStatus(build, "Error checking: "+err.Error())
allStable = false
continue
}
if stable {
e.setBuildStatus(build, "Stable")
} else {
e.setBuildStatus(build, "Not Stable")
}
}
return allStable
}
func (e *e2eTester) waitForStableBuilds() {
for !e.checkBuilds() {
e.msg("Not all builds stable. Checking again in 30s")
time.Sleep(30 * time.Second)
}
}
// This is called on a potentially mergeable PR
func (e *e2eTester) runE2ETests(pr *github_api.PullRequest, issue *github_api.Issue) error {
e.locked(func() { e.state.CurrentPR = prInfo(pr) })
defer e.locked(func() { e.state.CurrentPR = nil })
e.msg("Considering PR %d", *pr.Number)
e.waitForStableBuilds()
// if there is a 'e2e-not-required' label, just merge it.
if len(e.Config.DontRequireE2ELabel) > 0 && github_util.HasLabel(issue.Labels, e.Config.DontRequireE2ELabel) {
e.msg("Merging %d since %s is set", *pr.Number, e.Config.DontRequireE2ELabel)
return e.Config.MergePR(*pr.Number, "submit-queue")
}
body := "@k8s-bot test this [submit-queue is verifying that this PR is safe to merge]"
if err := e.Config.WriteComment(*pr.Number, body); err != nil {
e.error(err)
return err
}
// Wait for the build to start
_ = e.Config.WaitForPending(pr)
_ = e.Config.WaitForNotPending(pr)
// Wait for the status to go back to 'success'
if ok := e.Config.IsStatusSuccess(pr, []string{}); !ok {
e.msg("Status after build is not 'success', skipping PR %d", *pr.Number)
return nil
}
return e.Config.MergePR(*pr.Number, "submit-queue")
}
func (e *e2eTester) ServeHTTP(res http.ResponseWriter, req *http.Request) {
var (
data []byte
err error
)
e.locked(func() {
if e.state != nil {
data, err = json.MarshalIndent(e.state, "", "\t")
} else {
data = []byte("{}")
}
})
if err != nil {
glog.Errorf("Failed to encode status: %#v %v", e.state, err)
res.Header().Set("Content-type", "text/plain")
res.WriteHeader(http.StatusInternalServerError)
res.Write([]byte(err.Error()))
res.Write([]byte(fmt.Sprintf("%#v", e.state)))
} else {
res.Header().Set("Content-type", "application/json")
res.WriteHeader(http.StatusOK)
res.Write(data)
}
}