github.com/abayer/test-infra@v0.0.5/mungegithub/mungers/stale-green-ci_test.go (about)

     1  /*
     2  Copyright 2015 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package mungers
    18  
    19  import (
    20  	"encoding/json"
    21  	"fmt"
    22  	"net/http"
    23  	"runtime"
    24  	"strings"
    25  	"testing"
    26  	"time"
    27  
    28  	github_util "k8s.io/test-infra/mungegithub/github"
    29  	github_test "k8s.io/test-infra/mungegithub/github/testing"
    30  	"k8s.io/test-infra/mungegithub/mungeopts"
    31  	"k8s.io/test-infra/mungegithub/options"
    32  
    33  	"github.com/golang/glog"
    34  	"github.com/google/go-github/github"
    35  )
    36  
    37  const (
    38  	jenkinsE2EContext    = "Jenkins GCE e2e"
    39  	jenkinsUnitContext   = "Jenkins unit/integration"
    40  	jenkinsVerifyContext = "Jenkins verification"
    41  	jenkinsNodeContext   = "Jenkins GCE Node e2e"
    42  )
    43  
    44  var (
    45  	_ = fmt.Printf
    46  	_ = glog.Errorf
    47  
    48  	requiredContexts = []string{
    49  		jenkinsUnitContext,
    50  		jenkinsE2EContext,
    51  		jenkinsNodeContext,
    52  		jenkinsVerifyContext,
    53  	}
    54  )
    55  
    56  func timePtr(t time.Time) *time.Time { return &t }
    57  
    58  func NowStatus() *github.CombinedStatus {
    59  	status := github_test.Status("mysha", requiredContexts, nil, nil, nil)
    60  	for i := range status.Statuses {
    61  		s := &status.Statuses[i]
    62  		s.CreatedAt = timePtr(time.Now())
    63  		s.UpdatedAt = timePtr(time.Now())
    64  	}
    65  	return status
    66  }
    67  
    68  func OldStatus() *github.CombinedStatus {
    69  	return github_test.Status("mysha", requiredContexts, nil, nil, nil)
    70  }
    71  
    72  func TestOldUnitTestMunge(t *testing.T) {
    73  	runtime.GOMAXPROCS(runtime.NumCPU())
    74  
    75  	// Avoid a 5 second delay
    76  	github_util.SetCombinedStatusLifetime(time.Millisecond)
    77  
    78  	tests := []struct {
    79  		name     string
    80  		tested   bool
    81  		ciStatus *github.CombinedStatus
    82  	}{
    83  		{
    84  			name:     "Test0",
    85  			tested:   true,
    86  			ciStatus: OldStatus(), // Ran at time.Unix(0,0)
    87  		},
    88  		{
    89  			name:     "Test1",
    90  			tested:   false,
    91  			ciStatus: NowStatus(), // Ran at time.Unix(0,0)
    92  		},
    93  	}
    94  	for testNum, test := range tests {
    95  		issueNum := testNum + 1
    96  		tested := false
    97  
    98  		issue := LGTMIssue()
    99  		issue.Number = intPtr(issueNum)
   100  		pr := ValidPR()
   101  		pr.Number = intPtr(issueNum)
   102  		client, server, mux := github_test.InitServer(t, issue, pr, nil, nil, test.ciStatus, nil, nil)
   103  
   104  		path := fmt.Sprintf("/repos/o/r/issues/%d/comments", issueNum)
   105  		mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
   106  			if r.Method != "POST" {
   107  				t.Errorf("Unexpected method: %s", r.Method)
   108  			}
   109  
   110  			type comment struct {
   111  				Body string `json:"body"`
   112  			}
   113  			c := new(comment)
   114  			json.NewDecoder(r.Body).Decode(c)
   115  			msg := c.Body
   116  			if strings.HasPrefix(msg, "/test all") {
   117  				tested = true
   118  				test.ciStatus.State = stringPtr("pending")
   119  				for id := range test.ciStatus.Statuses {
   120  					status := &test.ciStatus.Statuses[id]
   121  					if *status.Context == jenkinsE2EContext || *status.Context == jenkinsUnitContext {
   122  						status.State = stringPtr("pending")
   123  						break
   124  					}
   125  				}
   126  
   127  			}
   128  			w.WriteHeader(http.StatusOK)
   129  			data, err := json.Marshal(github.IssueComment{})
   130  			if err != nil {
   131  				t.Errorf("Unexpected error: %v", err)
   132  			}
   133  			w.Write(data)
   134  		})
   135  
   136  		config := &github_util.Config{
   137  			Org:          "o",
   138  			Project:      "r",
   139  			BaseWaitTime: time.Nanosecond, // Avoid a 30 second delay
   140  		}
   141  		config.SetClient(client)
   142  
   143  		s := StaleGreenCI{}
   144  		err := s.Initialize(config, nil)
   145  		if err != nil {
   146  			t.Fatalf("%v", err)
   147  		}
   148  
   149  		obj, err := config.GetObject(issueNum)
   150  		if err != nil {
   151  			t.Fatalf("%v", err)
   152  		}
   153  
   154  		s.opts = options.New()
   155  		mungeopts.RequiredContexts.Retest = requiredContexts
   156  		s.Munge(obj)
   157  
   158  		if tested != test.tested {
   159  			t.Errorf("%d:%s tested=%t but should be %t", testNum, test.name, tested, test.tested)
   160  		}
   161  		server.Close()
   162  	}
   163  }