本文整理汇总了Golang中k8s/io/contrib/mungegithub/github/testing.InitServer函数的典型用法代码示例。如果您正苦于以下问题:Golang InitServer函数的具体用法?Golang InitServer怎么用?Golang InitServer使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了InitServer函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestPRGetFixesList
func TestPRGetFixesList(t *testing.T) {
tests := []struct {
issue *github.Issue
body string
expected []int
}{
{
issue: github_test.Issue("", 1, []string{"label1"}, false),
body: `bla resolve
this pr closes #45545 and also fixes #679
bla, some more bla with close here and there.
some suggest that it resolved #5643`,
expected: []int{45545, 679, 5643},
},
{
issue: github_test.Issue("", 2, []string{"label1"}, false),
body: `bla resolve 345
some suggest that it also closes #892`,
expected: []int{892},
},
{
issue: github_test.Issue("", 3, []string{"label1"}, false),
body: `bla resolve
this pr closes and fixes nothing`,
expected: nil,
},
{
issue: github_test.Issue("", 4, []string{"label1"}, false),
body: `bla bla
this pr Fixes #23 and FIXES #45 but not fixxx #99`,
expected: []int{23, 45},
},
}
for testNum, test := range tests {
client, server, _ := github_test.InitServer(t, test.issue, nil, nil, nil, nil, nil, nil)
config := &Config{}
config.Org = "o"
config.Project = "r"
config.SetClient(client)
obj, err := config.GetObject(*test.issue.Number)
if err != nil {
t.Fatalf("%d: unable to get issue: %v", testNum, *test.issue.Number)
}
obj.Issue.Body = &test.body
fixes := obj.GetPRFixesList()
if len(test.expected) != len(fixes) {
t.Errorf("%d: len(fixes) not equal, expected: %v but got: %v", testNum, test.expected, fixes)
return
}
for i, n := range test.expected {
if n != fixes[i] {
t.Errorf("%d: expected fixes: %v but got fixes: %v", testNum, test.expected, fixes)
}
}
server.Close()
}
}
示例2: 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()
}
}
示例3: TestRemoveLabel
func TestRemoveLabel(t *testing.T) {
tests := []struct {
issue *github.Issue
remove string
expected []string
}{
{
issue: github_test.Issue("", 1, []string{"label1"}, false),
remove: "label1",
expected: []string{},
},
{
issue: github_test.Issue("", 1, []string{"label2", "label1"}, false),
remove: "label1",
expected: []string{"label2"},
},
{
issue: github_test.Issue("", 1, []string{"label2"}, false),
remove: "label1",
expected: []string{"label2"},
},
{
issue: github_test.Issue("", 1, []string{}, false),
remove: "label1",
expected: []string{},
},
}
for testNum, test := range tests {
client, server, mux := github_test.InitServer(t, test.issue, nil, nil, nil, nil, nil)
config := &Config{}
config.Org = "o"
config.Project = "r"
config.SetClient(client)
mux.HandleFunc(fmt.Sprintf("/repos/o/r/issues/1/labels/%s", test.remove), func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
})
obj, err := config.GetObject(*test.issue.Number)
if err != nil {
t.Fatalf("%d: unable to get issue: %v", testNum, *test.issue.Number)
}
obj.RemoveLabel(test.remove)
if len(test.expected) != len(obj.Issue.Labels) {
t.Errorf("%d: len(labels) not equal, expected labels: %v but got labels: %v", testNum, test.expected, obj.Issue.Labels)
return
}
for i, l := range test.expected {
if l != *obj.Issue.Labels[i].Name {
t.Errorf("%d: expected labels: %v but got labels: %v", testNum, test.expected, obj.Issue.Labels)
}
}
server.Close()
}
}
示例4: TestSubmitQueue
//.........这里部分代码省略.........
// // previous two runs.
// {
// name: "Test22",
// pr: ValidPR(),
// issue: LGTMIssue(),
// events: NewLGTMEvents(),
// commits: Commits(), // Modified at time.Unix(7), 8, and 9
// ciStatus: SuccessStatus(),
// lastBuildNumber: LastBuildNumber(),
// gcsResult: SuccessGCS(),
// weakResults: map[int]utils.FinishedFile{
// LastBuildNumber(): FailGCS(),
// LastBuildNumber() - 1: SuccessGCS(),
// LastBuildNumber() - 2: FailGCS(),
// },
// gcsJunit: map[string][]byte{
// "junit_01.xml": getJUnit(5, 0),
// "junit_02.xml": getJUnit(6, 0),
// "junit_03.xml": getJUnit(7, 0),
// },
// retest1Pass: true,
// retest2Pass: true,
// reason: e2eFailure,
// state: "success",
// },
}
for testNum := range tests {
test := &tests[testNum]
fmt.Printf("---------Starting test %v (%v)---------------------\n", testNum, test.name)
issueNum := testNum + 1
issueNumStr := strconv.Itoa(issueNum)
test.issue.Number = &issueNum
client, server, mux := github_test.InitServer(t, test.issue, test.pr, test.events, test.commits, test.ciStatus, test.masterCommit, nil)
config := &github_util.Config{}
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
stateSet := ""
wasMerged := false
numTestChecks := 0
path := "/bucket/logs/foo/latest-build.txt"
mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
t.Errorf("Unexpected method: %s", r.Method)
}
w.WriteHeader(http.StatusOK)
w.Write([]byte(strconv.Itoa(test.lastBuildNumber)))
// 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.
numTestChecks = numTestChecks + 1
if numTestChecks == 2 && test.mergeAfterQueued {
test.pr.Merged = boolPtr(true)
test.pr.Mergeable = nil
}
示例5: TestQueueOrder
//.........这里部分代码省略.........
},
{
name: "With a priority label",
issues: []*github.Issue{
github_test.Issue(someUserName, 2, []string{"priority/P1"}, true),
github_test.Issue(someUserName, 3, []string{"priority/P1"}, true),
github_test.Issue(someUserName, 4, []string{"priority/P0"}, true),
github_test.Issue(someUserName, 5, nil, true),
},
issueToEvents: labelEvents,
expected: []int{4, 3, 2, 5},
},
{
name: "With two priority labels",
issues: []*github.Issue{
github_test.Issue(someUserName, 2, []string{"priority/P1", "priority/P0"}, true),
github_test.Issue(someUserName, 3, []string{"priority/P1"}, true),
github_test.Issue(someUserName, 4, []string{"priority/P0"}, true),
github_test.Issue(someUserName, 5, nil, true),
},
issueToEvents: labelEvents,
expected: []int{4, 2, 3, 5},
},
{
name: "With unrelated labels",
issues: []*github.Issue{
github_test.Issue(someUserName, 2, []string{"priority/P1", "priority/P0"}, true),
github_test.Issue(someUserName, 3, []string{"priority/P1", "kind/design"}, true),
github_test.Issue(someUserName, 4, []string{"priority/P0"}, true),
github_test.Issue(someUserName, 5, []string{lgtmLabel, "kind/new-api"}, true),
},
issueToEvents: labelEvents,
expected: []int{4, 2, 3, 5},
},
{
name: "With invalid priority label",
issues: []*github.Issue{
github_test.Issue(someUserName, 2, []string{"priority/P1", "priority/P0"}, true),
github_test.Issue(someUserName, 3, []string{"priority/P1", "kind/design", "priority/high"}, true),
github_test.Issue(someUserName, 4, []string{"priority/P0", "priorty/bob"}, true),
github_test.Issue(someUserName, 5, nil, true),
},
issueToEvents: labelEvents,
expected: []int{4, 2, 3, 5},
},
{
name: "Unlabeled counts as P3",
issues: []*github.Issue{
github_test.Issue(someUserName, 2, nil, true),
github_test.Issue(someUserName, 3, []string{"priority/P3"}, true),
github_test.Issue(someUserName, 4, []string{"priority/P2"}, true),
github_test.Issue(someUserName, 5, nil, true),
},
issueToEvents: labelEvents,
expected: []int{4, 5, 3, 2},
},
{
name: "retestNotRequiredLabel counts as P-negative 1",
issues: []*github.Issue{
github_test.Issue(someUserName, 2, nil, true),
github_test.Issue(someUserName, 3, []string{"priority/P3"}, true),
github_test.Issue(someUserName, 4, []string{"priority/P0"}, true),
github_test.Issue(someUserName, 5, nil, true),
github_test.Issue(someUserName, 6, []string{"priority/P3", retestNotRequiredLabel}, true),
},
issueToEvents: labelEvents,
expected: []int{6, 4, 5, 3, 2},
},
}
for testNum, test := range tests {
config := &github_util.Config{}
client, server, mux := github_test.InitServer(t, nil, nil, github_test.MultiIssueEvents(test.issueToEvents), nil, nil, nil, nil)
config.Org = "o"
config.Project = "r"
config.SetClient(client)
sq := getTestSQ(false, config, server)
for i := range test.issues {
issue := test.issues[i]
github_test.ServeIssue(t, mux, issue)
issueNum := *issue.Number
obj, err := config.GetObject(issueNum)
if err != nil {
t.Fatalf("%d:%q unable to get issue: %v", testNum, test.name, err)
}
sq.githubE2EQueue[issueNum] = obj
}
actual := sq.orderedE2EQueue()
if len(actual) != len(test.expected) {
t.Fatalf("%d:%q len(actual):%v != len(expected):%v", testNum, test.name, actual, test.expected)
}
for i, a := range actual {
e := test.expected[i]
if a != e {
t.Errorf("%d:%q a[%d]:%d != e[%d]:%d", testNum, test.name, i, a, i, e)
}
}
server.Close()
}
}
示例6: TestAssignFixes
func TestAssignFixes(t *testing.T) {
runtime.GOMAXPROCS(runtime.NumCPU())
tests := []struct {
name string
assignee string
pr *github.PullRequest
prIssue *github.Issue
prBody string
fixesIssue *github.Issue
}{
{
name: "fixes an issue",
assignee: "dev45",
pr: github_test.PullRequest("dev45", false, true, true),
prIssue: github_test.Issue("fred", 7779, []string{}, true),
prBody: "does stuff and fixes #8889.",
fixesIssue: github_test.Issue("jill", 8889, []string{}, true),
},
}
for _, test := range tests {
test.prIssue.Body = &test.prBody
client, server, mux := github_test.InitServer(t, test.prIssue, test.pr, nil, nil, nil, nil)
path := fmt.Sprintf("/repos/o/r/issues/%d", *test.fixesIssue.Number)
mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
data, err := json.Marshal(test.fixesIssue)
if err != nil {
t.Errorf("%v", err)
}
if r.Method != "PATCH" && r.Method != "GET" {
t.Errorf("Unexpected method: expected: GET/PATCH got: %s", r.Method)
}
if r.Method == "PATCH" {
body, _ := ioutil.ReadAll(r.Body)
type IssuePatch struct {
Assignee string
}
var ip IssuePatch
err := json.Unmarshal(body, &ip)
if err != nil {
fmt.Println("error:", err)
}
if ip.Assignee != test.assignee {
t.Errorf("Patching the incorrect Assignee %v instead of %v", ip.Assignee, test.assignee)
}
}
w.WriteHeader(http.StatusOK)
w.Write(data)
})
config := &github_util.Config{}
config.Org = "o"
config.Project = "r"
config.SetClient(client)
c := AssignFixesMunger{}
err := c.Initialize(config, nil)
if err != nil {
t.Fatalf("%v", err)
}
err = c.EachLoop()
if err != nil {
t.Fatalf("%v", err)
}
obj, err := config.GetObject(*test.prIssue.Number)
if err != nil {
t.Fatalf("%v", err)
}
c.Munge(obj)
server.Close()
}
}
示例7: TestReleaseNoteLabel
//.........这里部分代码省略.........
issue: github_test.Issue(botName, 1, []string{}, true),
body: "Cherry pick of #2 on release-1.2.",
secondIssue: github_test.Issue(botName, 2, []string{releaseNote}, true),
mustNotHave: []string{releaseNoteLabelNeeded},
},
{
name: "do not touch LGTM on non-master when parent PR has releaseNote label",
branch: "release-1.2",
issue: github_test.Issue(botName, 1, []string{"lgtm"}, true),
body: "Cherry pick of #2 on release-1.2.",
secondIssue: github_test.Issue(botName, 2, []string{releaseNote}, true),
mustHave: []string{"lgtm"},
mustNotHave: []string{releaseNoteLabelNeeded},
},
{
name: "add needs label when parent PR does not have releaseNote label",
branch: "release-1.2",
issue: github_test.Issue(botName, 1, []string{}, true),
body: "Cherry pick of #2 on release-1.2.",
secondIssue: github_test.Issue(botName, 2, []string{releaseNoteNone}, true),
mustHave: []string{releaseNoteLabelNeeded},
},
{
name: "remove LGTM on non-master when parent PR has releaseNote label",
branch: "release-1.2",
issue: github_test.Issue(botName, 1, []string{"lgtm"}, true),
body: "Cherry pick of #2 on release-1.2.",
secondIssue: github_test.Issue(botName, 2, []string{releaseNoteNone}, true),
mustHave: []string{releaseNoteLabelNeeded},
mustNotHave: []string{"lgtm"},
},
}
for testNum, test := range tests {
pr := ValidPR()
if test.branch != "" {
pr.Base.Ref = &test.branch
}
test.issue.Body = &test.body
client, server, mux := github_test.InitServer(t, test.issue, pr, nil, nil, nil)
path := fmt.Sprintf("/repos/o/r/issue/%s/labels", *test.issue.Number)
mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
out := []github.Label{{}}
data, err := json.Marshal(out)
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
w.Write(data)
})
if test.secondIssue != nil {
path = fmt.Sprintf("/repos/o/r/issues/%d", *test.secondIssue.Number)
mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
data, err := json.Marshal(test.secondIssue)
if err != nil {
t.Errorf("%v", err)
}
if r.Method != "GET" {
t.Errorf("Unexpected method: expected: GET got: %s", r.Method)
}
w.WriteHeader(http.StatusOK)
w.Write(data)
})
}
config := &github_util.Config{}
config.Org = "o"
config.Project = "r"
config.SetClient(client)
r := ReleaseNoteLabel{}
err := r.Initialize(config, nil)
if err != nil {
t.Fatalf("%v", err)
}
err = r.EachLoop()
if err != nil {
t.Fatalf("%v", err)
}
obj, err := config.GetObject(*test.issue.Number)
if err != nil {
t.Fatalf("%v", err)
}
r.Munge(obj)
for _, l := range test.mustHave {
if !obj.HasLabel(l) {
t.Errorf("%s:%d: Did not find label %q, labels: %v", test.name, testNum, l, obj.Issue.Labels)
}
}
for _, l := range test.mustNotHave {
if obj.HasLabel(l) {
t.Errorf("%s:%d: Found label %q and should not have, labels: %v", test.name, testNum, l, obj.Issue.Labels)
}
}
server.Close()
}
}
示例8: TestCLAMunger
func TestCLAMunger(t *testing.T) {
runtime.GOMAXPROCS(runtime.NumCPU())
tests := []struct {
name string
issue *github.Issue
status *github.CombinedStatus
mustHave []string
mustNotHave []string
}{
{
name: "CLA status success should add cncf/cla:yes label and remove cncf/cla:no label",
issue: github_test.Issue("user1", 1, []string{cncfClaNoLabel}, true),
status: &github.CombinedStatus{
Statuses: []github.RepoStatus{
{
Context: stringPtr(claContext),
State: stringPtr(contextSuccess),
},
},
},
mustHave: []string{cncfClaYesLabel},
mustNotHave: []string{cncfClaNoLabel},
},
{
name: "CLA status failure should add cncf/cla:no label and remove cncf/cla:yes label",
issue: github_test.Issue("user1", 1, []string{cncfClaYesLabel}, true),
status: &github.CombinedStatus{
Statuses: []github.RepoStatus{
{
Context: stringPtr(claContext),
State: stringPtr(contextFailure),
},
},
},
mustHave: []string{cncfClaNoLabel},
mustNotHave: []string{cncfClaYesLabel},
},
{
name: "CLA status error should apply cncf/cla:no label.",
issue: github_test.Issue("user1", 1, []string{}, true),
status: &github.CombinedStatus{
Statuses: []github.RepoStatus{
{
Context: stringPtr(claContext),
State: stringPtr(contextError),
},
},
},
mustHave: []string{cncfClaNoLabel},
mustNotHave: []string{cncfClaYesLabel},
},
{
name: "CLA status pending should not apply labels.",
issue: github_test.Issue("user1", 1, []string{}, true),
status: &github.CombinedStatus{
Statuses: []github.RepoStatus{
{
Context: stringPtr(claContext),
State: stringPtr(contextPending),
},
},
},
mustHave: []string{},
mustNotHave: []string{cncfClaYesLabel, cncfClaNoLabel},
},
}
for testNum, test := range tests {
pr := ValidPR()
pr.Head = &github.PullRequestBranch{}
pr.Head.SHA = stringPtr("0")
client, server, mux := github_test.InitServer(t, test.issue, pr, nil, nil, nil, nil, nil)
setUpMockFunctions(mux, t, test.issue)
path := fmt.Sprintf("/repos/o/r/commits/%s/status", *pr.Head.SHA)
mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
out := test.status
data, err := json.Marshal(out)
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
w.Write(data)
})
config := &github_util.Config{}
config.Org = "o"
config.Project = "r"
config.SetClient(client)
cla := ClaMunger{
CLAStatusContext: claContext,
pinger: c.NewPinger("[fake-ping]").SetDescription(""),
}
obj, err := config.GetObject(*test.issue.Number)
if err != nil {
t.Fatalf("%v", err)
}
cla.Munge(obj)
//.........这里部分代码省略.........
示例9: TestPathLabelMunge
func TestPathLabelMunge(t *testing.T) {
runtime.GOMAXPROCS(runtime.NumCPU())
tests := []struct {
commits []github.RepositoryCommit
mustHave []string
mustNotHave []string
}{
{
commits: pathCommits("docs/proposals"),
mustHave: []string{"kind/design"},
mustNotHave: []string{"kind/api-change", "kind/new-api"},
},
{
commits: pathCommits("docs/my/proposals"),
mustHave: []string{},
mustNotHave: []string{"kind/design", "kind/api-change", "kind/new-api"},
},
{
commits: pathCommits("pkg/api/types.go"),
mustHave: []string{"kind/api-change"},
mustNotHave: []string{"kind/design", "kind/new-api"},
},
{
commits: pathCommits("pkg/api/v1/types.go"),
mustHave: []string{"kind/api-change"},
mustNotHave: []string{"kind/design", "kind/new-api"},
},
{
commits: pathCommits("pkg/api/v1/duh/types.go"),
mustHave: []string{},
mustNotHave: []string{"kind/design", "kind/api-change", "kind/new-api"},
},
{
commits: pathCommits("pkg/apis/experimental/register.go"),
mustHave: []string{"kind/new-api"},
mustNotHave: []string{"kind/api-change", "kind/design"},
},
{
commits: pathCommits("pkg/apis/experimental/v1beta1/register.go"),
mustHave: []string{"kind/new-api"},
mustNotHave: []string{"kind/api-change", "kind/design"},
},
{
commits: pathCommits("pkg/apis/experiments/v1beta1/duh/register.go"),
mustHave: []string{},
mustNotHave: []string{"kind/design", "kind/api-change", "kind/new-api"},
},
}
for testNum, test := range tests {
client, server, mux := github_test.InitServer(t, NoOKToMergeIssue(), ValidPR(), nil, test.commits, nil)
mux.HandleFunc("/repos/o/r/issues/1/labels", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
out := []github.Label{
{
// TODO figure out the label name from the request...
Name: stringPtr("label"),
},
}
data, err := json.Marshal(out)
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
w.Write(data)
})
config := &github_util.Config{}
config.Org = "o"
config.Project = "r"
config.SetClient(client)
p := PathLabelMunger{}
p.pathLabelFile = "../path-label.txt"
err := p.Initialize(config)
if err != nil {
t.Fatalf("%v", err)
}
obj, err := config.GetObject(1)
if err != nil {
t.Fatalf("%v", err)
}
p.Munge(obj)
for _, l := range test.mustHave {
if !obj.HasLabel(l) {
t.Errorf("%d: Did not find label %q, labels: %v", testNum, l, obj.Issue.Labels)
}
}
for _, l := range test.mustNotHave {
if obj.HasLabel(l) {
t.Errorf("%d: Found label %q and should not have, labels: %v", testNum, l, obj.Issue.Labels)
}
}
server.Close()
}
}
示例10: TestGetLastModified
func TestGetLastModified(t *testing.T) {
tests := []struct {
commits []github.RepositoryCommit
expectedTime *time.Time
}{
{
commits: github_test.Commits(1, 10),
expectedTime: timePtr(time.Unix(10, 0)),
},
{
// remember the order of github_test.Commits() is non-deterministic
commits: github_test.Commits(3, 10),
expectedTime: timePtr(time.Unix(12, 0)),
},
{
// so this is probably not quite the same test...
commits: github_test.Commits(3, 8),
expectedTime: timePtr(time.Unix(10, 0)),
},
{
// We can't represent the same time in 2 commits using github_test.Commits()
commits: []github.RepositoryCommit{
{
SHA: stringPtr("mysha1"),
Commit: &github.Commit{
SHA: stringPtr("mysha1"),
Committer: &github.CommitAuthor{
Date: timePtr(time.Unix(9, 0)),
},
},
},
{
SHA: stringPtr("mysha2"),
Commit: &github.Commit{
SHA: stringPtr("mysha2"),
Committer: &github.CommitAuthor{
Date: timePtr(time.Unix(10, 0)),
},
},
},
{
SHA: stringPtr("mysha3"),
Commit: &github.Commit{
SHA: stringPtr("mysha3"),
Committer: &github.CommitAuthor{
Date: timePtr(time.Unix(9, 0)),
},
},
},
},
expectedTime: timePtr(time.Unix(10, 0)),
},
}
for _, test := range tests {
client, server, _ := github_test.InitServer(t, nil, nil, nil, test.commits, nil, nil)
config := &Config{}
config.Org = "o"
config.Project = "r"
config.SetClient(client)
obj := &MungeObject{
config: config,
Issue: github_test.Issue("bob", 1, nil, true),
}
ts := obj.LastModifiedTime()
if !ts.Equal(*test.expectedTime) {
t.Errorf("expected: %v, saw: %v for: %v", test.expectedTime, ts, test)
}
server.Close()
}
}
示例11: TestForEachIssueDo
func TestForEachIssueDo(t *testing.T) {
issue1 := github_test.Issue("bob", 1, nil, true)
issue5 := github_test.Issue("bob", 5, nil, true)
issue6 := github_test.Issue("bob", 6, nil, true)
issue7 := github_test.Issue("bob", 7, nil, true)
issue20 := github_test.Issue("bob", 20, nil, true)
user := github.User{Login: stringPtr("bob")}
tests := []struct {
Issues [][]github.Issue
Pages []int
ValidIssues int
}{
{
Issues: [][]github.Issue{
{*issue5},
},
Pages: []int{0},
ValidIssues: 1,
},
{
Issues: [][]github.Issue{
{*issue5},
{*issue6},
{*issue7},
{
{
Number: intPtr(8),
// no User, invalid
},
},
},
Pages: []int{4, 4, 4, 0},
ValidIssues: 3,
},
{
Issues: [][]github.Issue{
// Invalid 1 < MinPRNumber
// Invalid 20 > MaxPRNumber
{*issue1, *issue20},
// two valid issues
{*issue5, *issue6},
{
{
// no Number, invalid
User: &user,
},
},
},
Pages: []int{3, 3, 0},
ValidIssues: 2,
},
}
for i, test := range tests {
client, server, mux := github_test.InitServer(t, nil, nil, nil, nil, nil, nil)
config := &Config{
client: client,
Org: "foo",
Project: "bar",
MinPRNumber: 5,
MaxPRNumber: 15,
}
count := 0
mux.HandleFunc("/repos/foo/bar/issues", func(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
t.Errorf("Unexpected method: %s", r.Method)
}
// this means page 0, return page 1
page := r.URL.Query().Get("page")
if page == "" {
t.Errorf("Should not get page 0, start with page 1")
}
if page != strconv.Itoa(count+1) {
t.Errorf("Unexpected page: %s", r.URL.Query().Get("page"))
}
if r.URL.Query().Get("sort") != "created" {
t.Errorf("Unexpected sort: %s", r.URL.Query().Get("sort"))
}
if r.URL.Query().Get("per_page") != "100" {
t.Errorf("Unexpected per_page: %s", r.URL.Query().Get("per_page"))
}
w.Header().Add("Link",
fmt.Sprintf("<https://api.github.com/?page=%d>; rel=\"last\"", test.Pages[count]))
w.WriteHeader(http.StatusOK)
data, err := json.Marshal(test.Issues[count])
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
w.Write(data)
count++
})
objects := []*MungeObject{}
handle := func(obj *MungeObject) error {
objects = append(objects, obj)
return nil
}
err := config.ForEachIssueDo(handle)
if err != nil {
//.........这里部分代码省略.........
示例12: TestPathLabelMunge
func TestPathLabelMunge(t *testing.T) {
runtime.GOMAXPROCS(runtime.NumCPU())
tests := []struct {
files []*github.CommitFile
events []*github.IssueEvent
mustHave []string
mustNotHave []string
}{
{
files: commitFiles([]string{"docs/proposals"}),
events: BotAddedDesign(),
mustHave: []string{"kind/design"},
mustNotHave: []string{"kind/api-change", "kind/new-api"},
},
{
files: commitFiles([]string{"docs/my/proposals"}),
events: BotAddedDesign(),
mustHave: []string{},
mustNotHave: []string{"kind/design", "kind/api-change", "kind/new-api"},
},
{
files: commitFiles([]string{"pkg/api/types.go"}),
events: BotAddedDesign(),
mustHave: []string{"kind/api-change"},
mustNotHave: []string{"kind/design", "kind/new-api"},
},
{
files: commitFiles([]string{"pkg/api/v1/types.go"}),
events: BotAddedDesign(),
mustHave: []string{"kind/api-change"},
mustNotHave: []string{"kind/design", "kind/new-api"},
},
{
files: commitFiles([]string{"pkg/api/v1/duh/types.go"}),
events: BotAddedDesign(),
mustHave: []string{},
mustNotHave: []string{"kind/design", "kind/api-change", "kind/new-api"},
},
{
files: commitFiles([]string{"pkg/apis/experimental/register.go"}),
events: BotAddedDesign(),
mustHave: []string{"kind/new-api"},
mustNotHave: []string{"kind/api-change", "kind/design"},
},
{
files: commitFiles([]string{"pkg/apis/experimental/v1beta1/register.go"}),
events: BotAddedDesign(),
mustHave: []string{"kind/new-api"},
mustNotHave: []string{"kind/api-change", "kind/design"},
},
{
files: commitFiles([]string{"pkg/apis/experiments/v1beta1/duh/register.go"}),
events: BotAddedDesign(),
mustHave: []string{},
mustNotHave: []string{"kind/design", "kind/api-change", "kind/new-api"},
},
{
files: commitFiles([]string{"README"}),
events: OtherAddedDesign(),
mustHave: []string{"kind/design"},
mustNotHave: []string{"kind/api-change", "kind/new-api"},
},
}
for testNum, test := range tests {
client, server, mux := github_test.InitServer(t, docsProposalIssue(), ValidPR(), test.events, nil, nil, nil, test.files)
mux.HandleFunc("/repos/o/r/issues/1/labels/kind/design", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte{})
})
mux.HandleFunc("/repos/o/r/issues/1/labels", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
out := []github.Label{{}}
data, err := json.Marshal(out)
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
w.Write(data)
})
config := &github_util.Config{}
config.Org = "o"
config.Project = "r"
config.SetClient(client)
p := PathLabelMunger{}
p.PathLabelFile = "../path-label.txt"
err := p.Initialize(config, nil)
if err != nil {
t.Fatalf("%v", err)
}
obj, err := config.GetObject(1)
if err != nil {
t.Fatalf("%v", err)
}
p.Munge(obj)
//.........这里部分代码省略.........
示例13: TestCherrypickAuthApprove
func TestCherrypickAuthApprove(t *testing.T) {
runtime.GOMAXPROCS(runtime.NumCPU())
tests := []struct {
name string
issue *github.Issue
issueBody string
prBranch string
parentIssue *github.Issue
milestone *github.Milestone
shouldHaveLabel string
shouldHaveMilestone string
shouldNotHaveLabel string
shouldNotHaveMile string
}{
{
name: "Add cpApproved and milestone",
issue: github_test.Issue(botName, 1, []string{}, true),
issueBody: "Cherry pick of #2 on release-1.2.",
prBranch: "release-1.2",
parentIssue: github_test.Issue(botName, 2, []string{cpApprovedLabel}, true),
milestone: &github.Milestone{Title: stringPtr("v1.2"), Number: intPtr(1)},
shouldHaveLabel: cpApprovedLabel,
shouldHaveMilestone: "v1.2",
},
{
name: "Add milestone",
issue: github_test.Issue(botName, 1, []string{cpApprovedLabel}, true),
issueBody: "Cherry pick of #2 on release-1.2.",
prBranch: "release-1.2",
parentIssue: github_test.Issue(botName, 2, []string{cpApprovedLabel}, true),
milestone: &github.Milestone{Title: stringPtr("v1.2"), Number: intPtr(1)},
shouldHaveLabel: cpApprovedLabel,
shouldHaveMilestone: "v1.2",
},
{
name: "Do not add because parent not have",
issue: github_test.Issue(botName, 1, []string{}, true),
issueBody: "Cherry pick of #2 on release-1.2.",
prBranch: "release-1.2",
parentIssue: github_test.Issue(botName, 2, []string{}, true),
milestone: &github.Milestone{Title: stringPtr("v1.2"), Number: intPtr(1)},
shouldNotHaveLabel: cpApprovedLabel,
shouldNotHaveMile: "v1.2",
},
{
name: "PR against wrong branch",
issue: github_test.Issue(botName, 1, []string{}, true),
issueBody: "Cherry pick of #2 on release-1.2.",
prBranch: "release-1.1",
parentIssue: github_test.Issue(botName, 2, []string{cpApprovedLabel}, true),
milestone: &github.Milestone{Title: stringPtr("v1.2"), Number: intPtr(1)},
shouldNotHaveLabel: cpApprovedLabel,
shouldNotHaveMile: "v1.2",
},
{
name: "Parent milestone against other branch",
issue: github_test.Issue(botName, 1, []string{}, true),
issueBody: "Cherry pick of #2 on release-1.2.",
prBranch: "release-1.2",
parentIssue: github_test.Issue(botName, 2, []string{cpApprovedLabel}, true),
milestone: &github.Milestone{Title: stringPtr("v1.1"), Number: intPtr(1)},
shouldNotHaveLabel: cpApprovedLabel,
shouldNotHaveMile: "v1.1",
},
{
name: "Parent has no milestone",
issue: github_test.Issue(botName, 1, []string{}, true),
issueBody: "Cherry pick of #2 on release-1.2.",
prBranch: "release-1.2",
parentIssue: github_test.Issue(botName, 2, []string{cpApprovedLabel}, true),
shouldNotHaveLabel: cpApprovedLabel,
shouldNotHaveMile: "v1.2",
},
}
for testNum, test := range tests {
test.issue.Body = &test.issueBody
pr := ValidPR()
pr.Base.Ref = &test.prBranch
client, server, mux := github_test.InitServer(t, test.issue, pr, nil, nil, nil, nil, nil)
path := fmt.Sprintf("/repos/o/r/issues/%d/labels", *test.issue.Number)
mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
out := []github.Label{{}}
data, err := json.Marshal(out)
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
w.Write(data)
})
path = "/repos/o/r/milestones"
mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
out := []github.Milestone{}
if test.milestone != nil {
out = append(out, *test.milestone)
}
//.........这里部分代码省略.........
示例14: TestQueueOrder
func TestQueueOrder(t *testing.T) {
tests := []struct {
name string
issues []github.Issue
expected []int
}{
{
name: "Just prNum",
issues: []github.Issue{
*github_test.Issue(whitelistUser, 2, nil, true),
*github_test.Issue(whitelistUser, 3, nil, true),
*github_test.Issue(whitelistUser, 4, nil, true),
*github_test.Issue(whitelistUser, 5, nil, true),
},
expected: []int{2, 3, 4, 5},
},
{
name: "With a priority label",
issues: []github.Issue{
*github_test.Issue(whitelistUser, 2, []string{"priority/P1"}, true),
*github_test.Issue(whitelistUser, 3, []string{"priority/P1"}, true),
*github_test.Issue(whitelistUser, 4, []string{"priority/P0"}, true),
*github_test.Issue(whitelistUser, 5, nil, true),
},
expected: []int{4, 2, 3, 5},
},
{
name: "With two priority labels",
issues: []github.Issue{
*github_test.Issue(whitelistUser, 2, []string{"priority/P1", "priority/P0"}, true),
*github_test.Issue(whitelistUser, 3, []string{"priority/P1"}, true),
*github_test.Issue(whitelistUser, 4, []string{"priority/P0"}, true),
*github_test.Issue(whitelistUser, 5, nil, true),
},
expected: []int{2, 4, 3, 5},
},
{
name: "With unrelated labels",
issues: []github.Issue{
*github_test.Issue(whitelistUser, 2, []string{"priority/P1", "priority/P0"}, true),
*github_test.Issue(whitelistUser, 3, []string{"priority/P1", "kind/design"}, true),
*github_test.Issue(whitelistUser, 4, []string{"priority/P0"}, true),
*github_test.Issue(whitelistUser, 5, []string{"LGTM", "kind/new-api"}, true),
},
expected: []int{2, 4, 3, 5},
},
{
name: "With invalid priority label",
issues: []github.Issue{
*github_test.Issue(whitelistUser, 2, []string{"priority/P1", "priority/P0"}, true),
*github_test.Issue(whitelistUser, 3, []string{"priority/P1", "kind/design", "priority/high"}, true),
*github_test.Issue(whitelistUser, 4, []string{"priority/P0", "priorty/bob"}, true),
*github_test.Issue(whitelistUser, 5, nil, true),
},
expected: []int{2, 4, 3, 5},
},
{
name: "Unlabeled counts as P3",
issues: []github.Issue{
*github_test.Issue(whitelistUser, 2, nil, true),
*github_test.Issue(whitelistUser, 3, []string{"priority/P3"}, true),
*github_test.Issue(whitelistUser, 4, []string{"priority/P2"}, true),
*github_test.Issue(whitelistUser, 5, nil, true),
},
expected: []int{4, 2, 3, 5},
},
{
name: "e2e-not-required counts as P-negative 1",
issues: []github.Issue{
*github_test.Issue(whitelistUser, 2, nil, true),
*github_test.Issue(whitelistUser, 3, []string{"priority/P3"}, true),
*github_test.Issue(whitelistUser, 4, []string{"priority/P2"}, true),
*github_test.Issue(whitelistUser, 5, nil, true),
*github_test.Issue(whitelistUser, 6, []string{"priority/P3", e2eNotRequiredLabel}, true),
},
expected: []int{6, 4, 2, 3, 5},
},
}
for testNum, test := range tests {
config := &github_util.Config{}
client, server, mux := github_test.InitServer(t, nil, nil, nil, nil, nil)
config.Org = "o"
config.Project = "r"
config.SetClient(client)
sq := getTestSQ(false, config, server)
for i := range test.issues {
issue := &test.issues[i]
github_test.ServeIssue(t, mux, issue)
issueNum := *issue.Number
obj, err := config.GetObject(issueNum)
if err != nil {
t.Fatalf("%d:%q unable to get issue: %v", testNum, test.name, err)
}
sq.githubE2EQueue[issueNum] = obj
}
actual := sq.orderedE2EQueue()
if len(actual) != len(test.expected) {
t.Fatalf("%d:%q len(actual):%v != len(expected):%v", testNum, test.name, actual, test.expected)
}
//.........这里部分代码省略.........
示例15: TestMunge
//.........这里部分代码省略.........
},
{
name: "Fail because E2E pass, but unit test fail",
pr: ValidPR(),
issue: NoOKToMergeIssue(),
events: NewLGTMEvents(),
commits: Commits(), // Modified at time.Unix(7), 8, and 9
ciStatus: SuccessStatus(),
jenkinsJob: SuccessJenkins(),
e2ePass: true,
unitPass: false,
reason: ghE2EFailed,
state: "pending",
},
{
name: "Fail because E2E fail, but unit test pass",
pr: ValidPR(),
issue: NoOKToMergeIssue(),
events: NewLGTMEvents(),
commits: Commits(), // Modified at time.Unix(7), 8, and 9
ciStatus: SuccessStatus(),
jenkinsJob: SuccessJenkins(),
e2ePass: false,
unitPass: true,
reason: ghE2EFailed,
state: "pending",
},
}
for testNum, test := range tests {
issueNum := testNum + 1
issueNumStr := strconv.Itoa(issueNum)
test.issue.Number = &issueNum
client, server, mux := github_test.InitServer(t, test.issue, test.pr, test.events, test.commits, test.ciStatus)
config := &github_util.Config{}
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
stateSet := ""
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 {