github.com/shashidharatd/test-infra@v0.0.0-20171006011030-71304e1ca560/mungegithub/mungers/comment-deleter-jenkins_test.go (about)

     1  /*
     2  Copyright 2016 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  	"testing"
    21  	"time"
    22  
    23  	githubapi "github.com/google/go-github/github"
    24  
    25  	github_test "k8s.io/test-infra/mungegithub/github/testing"
    26  )
    27  
    28  const (
    29  	passComment = `GCE e2e build/test **passed** for commit 436b966bcb6c19b792b7918e59e4f50195224065.
    30  * [Build Log](http://pr-test.k8s.io/23958/kubernetes-pull-build-test-e2e-gce/35810/build-log.txt)
    31  * [Test Artifacts](https://console.developers.google.com/storage/browser/kubernetes-jenkins/pr-logs/pull/23958/kubernetes-pull-build-test-e2e-gce/35810/artifacts/)
    32  * [Internal Jenkins Results](http://goto.google.com/prkubekins/job/kubernetes-pull-build-test-e2e-gce//35810)`
    33  	failComment = `GCE e2e build/test **failed** for commit 436b966bcb6c19b792b7918e59e4f50195224065.
    34  * [Build Log](http://pr-test.k8s.io/23958/kubernetes-pull-build-test-e2e-gce/35830/build-log.txt)
    35  * [Test Artifacts](https://console.developers.google.com/storage/browser/kubernetes-jenkins/pr-logs/pull/23958/kubernetes-pull-build-test-e2e-gce/35830/artifacts/)
    36  * [Internal Jenkins Results](http://goto.google.com/prkubekins/job/kubernetes-pull-build-test-e2e-gce//35830)
    37  
    38  Please reference the [list of currently known flakes](https://github.com/kubernetes/kubernetes/issues?q=is:issue+label:kind/flake+is:open) when examining this failure. If you request a re-test, you must reference the issue describing the flake.`
    39  	oldBuildLinkComment = `GCE e2e test build/test **passed** for commit c801f3fc6221e4b1a54fd08378f35009dd01f052.
    40  * [Build Log](https://storage.cloud.google.com/kubernetes-jenkins/pr-logs/pull/13006/kubernetes-pull-build-test-e2e-gce/26147/build-log.txt)
    41  * [Test Artifacts](https://console.developers.google.com/storage/browser/kubernetes-jenkins/pr-logs/pull/13006/kubernetes-pull-build-test-e2e-gce/26147/artifacts/)
    42  * [Internal Jenkins Results](http://goto.google.com/prkubekins/job/kubernetes-pull-build-test-e2e-gce//26147)`
    43  	updatedPassComment = `GCE e2e build/test **passed** for commit 4c92572bef90215de02be96436364ff06a7a5435.
    44  * [Test Results](https://k8s-gubernator.appspot.com/build/kubernetes-jenkins/pr-logs/pull/28636/kubernetes-pull-build-test-e2e-gce/48147)
    45  * [Build Log](http://pr-test.k8s.io/28636/kubernetes-pull-build-test-e2e-gce/48147/build-log.txt)
    46  * [Test Artifacts](https://console.developers.google.com/storage/browser/kubernetes-jenkins/pr-logs/pull/28636/kubernetes-pull-build-test-e2e-gce/48147/artifacts/)
    47  * [Internal Jenkins Results](http://goto.google.com/prkubekins/job/kubernetes-pull-build-test-e2e-gce//48147)`
    48  	//Updated fail comment's links are the same as updated pass's
    49  	updatedFailComment = `GCE e2e build/test **failed** for commit 4c92572bef90215de02be96436364ff06a7a5435.
    50  * [Test Results](https://k8s-gubernator.appspot.com/build/kubernetes-jenkins/pr-logs/pull/28636/kubernetes-pull-build-test-e2e-gce/48147)
    51  * [Build Log](http://pr-test.k8s.io/28636/kubernetes-pull-build-test-e2e-gce/48147/build-log.txt)
    52  * [Test Artifacts](https://console.developers.google.com/storage/browser/kubernetes-jenkins/pr-logs/pull/28636/kubernetes-pull-build-test-e2e-gce/48147/artifacts/)
    53  * [Internal Jenkins Results](http://goto.google.com/prkubekins/job/kubernetes-pull-build-test-e2e-gce//48147)`
    54  )
    55  
    56  func TestIsJenkinsTestComment(t *testing.T) {
    57  	tests := []struct {
    58  		name      string
    59  		value     string
    60  		isJenkins bool
    61  	}{
    62  		{
    63  			name:      "success comment",
    64  			value:     updatedPassComment,
    65  			isJenkins: true,
    66  		},
    67  		{
    68  			name:      "success comment",
    69  			value:     passComment,
    70  			isJenkins: true,
    71  		},
    72  		{
    73  			name:      "fail comment",
    74  			value:     updatedFailComment,
    75  			isJenkins: true,
    76  		},
    77  		{
    78  			name:      "fail comment",
    79  			value:     failComment,
    80  			isJenkins: true,
    81  		},
    82  		{
    83  			name:      "other comment",
    84  			value:     oldBuildLinkComment,
    85  			isJenkins: true,
    86  		},
    87  		{
    88  			name:      "Empty string",
    89  			isJenkins: false,
    90  		},
    91  		{
    92  			name:      "Random string",
    93  			value:     "Bob says do it another way, ok Brendan?!",
    94  			isJenkins: false,
    95  		},
    96  	}
    97  	for testNum, test := range tests {
    98  		output := isJenkinsTestComment(test.value)
    99  		if output != test.isJenkins {
   100  			t.Errorf("%d:%s: expected: %v, saw: %v for %s", testNum, test.name, test.isJenkins, output, test.value)
   101  		}
   102  	}
   103  }
   104  
   105  func comment(id int, body string) *githubapi.IssueComment {
   106  	return github_test.Comment(id, jenkinsBotName, time.Now(), passComment)
   107  }
   108  
   109  func TestJenkinsStaleIssueComments(t *testing.T) {
   110  	c := CommentDeleterJenkins{}
   111  
   112  	tests := []struct {
   113  		name     string
   114  		comments []*githubapi.IssueComment
   115  		expected []*githubapi.IssueComment
   116  	}{
   117  		{
   118  			name: "single pass",
   119  			comments: []*githubapi.IssueComment{
   120  				comment(1, passComment),
   121  			},
   122  		},
   123  		{
   124  			name: "double pass",
   125  			comments: []*githubapi.IssueComment{
   126  				comment(1, passComment),
   127  				comment(2, passComment),
   128  			},
   129  			expected: []*githubapi.IssueComment{
   130  				comment(1, passComment),
   131  			},
   132  		},
   133  		{
   134  			name: "pass fail pass",
   135  			comments: []*githubapi.IssueComment{
   136  				comment(1, passComment),
   137  				comment(2, failComment),
   138  				comment(3, passComment),
   139  			},
   140  			expected: []*githubapi.IssueComment{
   141  				comment(1, passComment),
   142  				comment(2, failComment),
   143  			},
   144  		},
   145  	}
   146  	for testNum, test := range tests {
   147  		out := c.StaleIssueComments(nil, test.comments)
   148  		if len(out) != len(test.expected) {
   149  			t.Errorf("%d:%s: len(expected):%d, len(out):%d", testNum, test.name, len(test.expected), len(out))
   150  		}
   151  		for _, cexpected := range test.expected {
   152  			found := false
   153  			for _, cout := range out {
   154  				if *cout.ID == *cexpected.ID {
   155  					found = true
   156  					break
   157  				}
   158  			}
   159  			if !found {
   160  				t.Errorf("%d:%s: missing %v from output", testNum, test.name, cexpected)
   161  			}
   162  		}
   163  	}
   164  }