本文整理匯總了Golang中k8s/io/contrib/mungegithub/github.TestObject函數的典型用法代碼示例。如果您正苦於以下問題:Golang TestObject函數的具體用法?Golang TestObject怎麽用?Golang TestObject使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了TestObject函數的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: TestValidateLGTMAfterPush
func TestValidateLGTMAfterPush(t *testing.T) {
tests := []struct {
issueEvents []github.IssueEvent
commits []github.RepositoryCommit
shouldPass bool
}{
{
issueEvents: NewLGTMEvents(), // Label >= time.Unix(10)
commits: Commits(), // Modified at time.Unix(7), 8, and 9
shouldPass: true,
},
{
issueEvents: OldLGTMEvents(), // Label <= time.Unix(8)
commits: Commits(), // Modified at time.Unix(7), 8, and 9
shouldPass: false,
},
{
issueEvents: OverlappingLGTMEvents(), // Labeled at 8, 9, and 10
commits: Commits(), // Modified at time.Unix(7), 8, and 9
shouldPass: true,
},
}
for testNum, test := range tests {
config := &github_util.Config{}
client, server, _ := github_test.InitServer(t, nil, nil, test.issueEvents, test.commits, nil)
config.Org = "o"
config.Project = "r"
config.SetClient(client)
obj := github_util.TestObject(config, BareIssue(), nil, nil, nil)
if _, err := obj.GetCommits(); err != nil {
t.Errorf("Unexpected error getting filled commits: %v", err)
}
if _, err := obj.GetEvents(); err != nil {
t.Errorf("Unexpected error getting events commits: %v", err)
}
lastModifiedTime := obj.LastModifiedTime()
lgtmTime := obj.LabelTime("lgtm")
if lastModifiedTime == nil || lgtmTime == nil {
t.Errorf("unexpected lastModifiedTime or lgtmTime == nil")
}
ok := !lastModifiedTime.After(*lgtmTime)
if ok != test.shouldPass {
t.Errorf("%d: expected: %v, saw: %v", testNum, test.shouldPass, ok)
}
server.Close()
}
}
示例2: TestMunge
//.........這裏部分代碼省略.........
}
type comment struct {
Body string `json:"body"`
}
c := new(comment)
json.NewDecoder(r.Body).Decode(c)
msg := c.Body
if strings.HasPrefix(msg, "@k8s-bot test this") {
go fakeRunGithubE2ESuccess(test.ciStatus, test.e2ePass, test.unitPass)
}
w.WriteHeader(http.StatusOK)
data, err := json.Marshal(github.IssueComment{})
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
w.Write(data)
})
path = fmt.Sprintf("/repos/o/r/pulls/%d/merge", issueNum)
mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
if r.Method != "PUT" {
t.Errorf("Unexpected method: %s", r.Method)
}
w.WriteHeader(http.StatusOK)
data, err := json.Marshal(github.PullRequestMergeResult{})
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
w.Write(data)
test.pr.Merged = boolPtr(true)
})
path = fmt.Sprintf("/repos/o/r/statuses/%s", *test.pr.Head.SHA)
mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
t.Errorf("Unexpected method: %s", r.Method)
}
decoder := json.NewDecoder(r.Body)
var status github.RepoStatus
err := decoder.Decode(&status)
if err != nil {
t.Errorf("Unable to decode status: %v", err)
}
stateSet = *status.State
data, err := json.Marshal(status)
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
w.WriteHeader(http.StatusOK)
w.Write(data)
test.pr.Merged = boolPtr(true)
})
sq := SubmitQueue{}
sq.RequiredStatusContexts = []string{jenkinsUnitContext}
sq.E2EStatusContext = jenkinsE2EContext
sq.UnitStatusContext = jenkinsUnitContext
sq.JenkinsHost = server.URL
sq.JenkinsJobs = []string{"foo"}
sq.WhitelistOverride = "ok-to-merge"
sq.Initialize(config)
sq.EachLoop()
sq.userWhitelist.Insert(whitelistUser)
obj := github_util.TestObject(config, test.issue, test.pr, test.commits, test.events)
sq.Munge(obj)
done := make(chan bool, 1)
go func(done chan bool) {
for {
if sq.prStatus[issueNumStr].Reason == test.reason {
done <- true
return
}
found := false
for _, status := range sq.statusHistory {
if status.Number == issueNum && status.Reason == test.reason {
found = true
break
}
}
if found {
done <- true
return
}
time.Sleep(1 * time.Millisecond)
}
}(done)
select {
case <-done:
case <-time.After(10 * time.Second):
t.Errorf("%d:%s timed out waiting expected reason=%q but got prStatus:%q history:%v", testNum, test.name, test.reason, sq.prStatus[issueNumStr].Reason, sq.statusHistory)
}
close(done)
server.Close()
if test.state != "" && test.state != stateSet {
t.Errorf("%d:%s state set to %q but expected %q", testNum, test.name, stateSet, test.state)
}
}
}
示例3: TestSubmitQueue
//.........這裏部分代碼省略.........
return
}
t.Errorf("Unexpected method: %s", r.Method)
})
path = fmt.Sprintf("/repos/o/r/pulls/%d/merge", issueNum)
mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
if r.Method != "PUT" {
t.Errorf("Unexpected method: %s", r.Method)
}
w.WriteHeader(http.StatusOK)
data, err := json.Marshal(github.PullRequestMergeResult{})
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
w.Write(data)
test.pr.Merged = boolPtr(true)
wasMerged = true
})
path = fmt.Sprintf("/repos/o/r/statuses/%s", *test.pr.Head.SHA)
mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
t.Errorf("Unexpected method: %s", r.Method)
}
decoder := json.NewDecoder(r.Body)
var status github.RepoStatus
err := decoder.Decode(&status)
if err != nil {
t.Errorf("Unable to decode status: %v", err)
}
stateSet = *status.State
data, err := json.Marshal(status)
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
w.WriteHeader(http.StatusOK)
w.Write(data)
})
sq := getTestSQ(true, config, server)
sq.setEmergencyMergeStop(test.emergencyMergeStop)
obj := github_util.TestObject(config, test.issue, test.pr, test.commits, test.events)
if test.imBaseSHA != "" && test.imHeadSHA != "" {
sq.interruptedObj = &submitQueueInterruptedObject{obj, test.imHeadSHA, test.imBaseSHA}
}
sq.Munge(obj)
done := make(chan bool, 1)
go func(done chan bool) {
for {
defer func() {
if r := recover(); r != nil {
t.Errorf("%d:%q panic'd likely writing to 'done' channel", testNum, test.name)
}
}()
reason := func() string {
sq.Mutex.Lock()
defer sq.Mutex.Unlock()
return sq.prStatus[issueNumStr].Reason
}
if reason() == test.reason {
done <- true
return
}
found := false
for _, status := range sq.statusHistory {
if status.Reason == test.reason {
found = true
break
}
}
if found {
done <- true
return
}
time.Sleep(1 * time.Millisecond)
}
}(done)
select {
case <-done:
case <-time.After(10 * time.Second):
t.Errorf("%d:%q timed out waiting expected reason=%q but got prStatus:%q history:%v", testNum, test.name, test.reason, sq.prStatus[issueNumStr].Reason, sq.statusHistory)
}
close(done)
server.Close()
if test.state != "" && test.state != stateSet {
t.Errorf("%d:%q state set to %q but expected %q", testNum, test.name, stateSet, test.state)
}
if test.isMerged != wasMerged {
t.Errorf("%d:%q PR merged = %v but wanted %v", testNum, test.name, wasMerged, test.isMerged)
}
if e, a := test.retestsAvoided, int(sq.retestsAvoided); e != a {
t.Errorf("%d:%q expected %v tests avoided but got %v", testNum, test.name, e, a)
}
}
}
示例4: TestMunge
//.........這裏部分代碼省略.........
config.Org = "o"
config.Project = "r"
config.SetClient(client)
// Don't wait so long for it to go pending or back
d := 250 * time.Millisecond
config.PendingWaitTime = &d
numJenkinsCalls := 0
// Respond with success to jenkins requests.
mux.HandleFunc("/job/foo/lastCompletedBuild/api/json", func(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
t.Errorf("Unexpected method: %s", r.Method)
}
w.WriteHeader(http.StatusOK)
data, err := json.Marshal(test.jenkinsJob)
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
w.Write(data)
// There is no good spot for this, but this gets called
// before we queue the PR. So mark the PR as "merged".
// When the sq initializes, it will check the Jenkins status,
// so we don't want to modify the PR there. Instead we need
// to wait until the second time we check Jenkins, which happens
// we did the IsMerged() check.
numJenkinsCalls = numJenkinsCalls + 1
if numJenkinsCalls == 2 && test.mergeAfterQueued {
test.pr.Merged = boolPtr(true)
test.pr.Mergeable = nil
}
})
mux.HandleFunc("/repos/o/r/issues/1/comments", func(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
t.Errorf("Unexpected method: %s", r.Method)
}
type comment struct {
Body string `json:"body"`
}
c := new(comment)
json.NewDecoder(r.Body).Decode(c)
msg := c.Body
if strings.HasPrefix(msg, "@k8s-bot test this") {
go fakeRunGithubE2ESuccess(test.ciStatus, test.shouldPass)
}
w.WriteHeader(http.StatusOK)
data, err := json.Marshal(github.IssueComment{})
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
w.Write(data)
})
mux.HandleFunc("/repos/o/r/pulls/1/merge", func(w http.ResponseWriter, r *http.Request) {
if r.Method != "PUT" {
t.Errorf("Unexpected method: %s", r.Method)
}
w.WriteHeader(http.StatusOK)
data, err := json.Marshal(github.PullRequestMergeResult{})
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
w.Write(data)
test.pr.Merged = boolPtr(true)
})
sq := SubmitQueue{}
sq.RequiredStatusContexts = []string{claContext}
sq.DontRequireE2ELabel = "e2e-not-required"
sq.E2EStatusContext = gceE2EContext
sq.JenkinsHost = server.URL
sq.JenkinsJobs = []string{"foo"}
sq.WhitelistOverride = "ok-to-merge"
sq.Initialize(config)
sq.EachLoop()
sq.userWhitelist.Insert(whitelistUser)
obj := github_util.TestObject(config, test.issue, test.pr, test.commits, test.events)
sq.Munge(obj)
done := make(chan bool, 1)
go func(done chan bool) {
for {
reason := sq.prStatus["1"].Reason
for _, r := range test.reasons {
if r == reason {
done <- true
return
}
}
time.Sleep(1 * time.Millisecond)
}
}(done)
select {
case <-done:
case <-time.After(10 * time.Second):
t.Fatalf("test:%d timed out waiting expected reason=%v but got %q", testNum, test.reasons, sq.prStatus["1"].Reason)
}
server.Close()
}
}