github.com/abayer/test-infra@v0.0.5/mungegithub/mungers/matchers/pinger_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 matchers
    18  
    19  import (
    20  	"testing"
    21  	"time"
    22  
    23  	"github.com/google/go-github/github"
    24  )
    25  
    26  func timeAgo(d time.Duration) *time.Time {
    27  	t := time.Now().Add(-d)
    28  	return &t
    29  }
    30  
    31  func makeComment(body, author string, beforeNow time.Duration) *github.IssueComment {
    32  	return &github.IssueComment{
    33  		Body:      &body,
    34  		User:      &github.User{Login: &author},
    35  		UpdatedAt: timeAgo(beforeNow),
    36  	}
    37  }
    38  
    39  func TestMaxReachNotReachedNoStart(t *testing.T) {
    40  	comments := []*github.IssueComment{
    41  		makeComment("[SOMETHING] Something", "k8s-merge-robot", 10*time.Hour),
    42  		makeComment("[NOTIF] Notification", "k8s-merge-robot", 10*time.Hour),
    43  	}
    44  
    45  	pinger := NewPinger("NOTIF", "k8s-merge-robot").SetMaxCount(2)
    46  
    47  	if pinger.IsMaxReached(comments, nil) {
    48  		t.Error("Should not have reached the maximum")
    49  	}
    50  }
    51  
    52  func TestMaxReachNotReachedWithStart(t *testing.T) {
    53  	comments := []*github.IssueComment{
    54  		makeComment("[NOTIF] Notification", "k8s-merge-robot", 14*time.Hour),
    55  		makeComment("[NOTIF] Notification", "k8s-merge-robot", 12*time.Hour),
    56  		makeComment("[SOMETHING] Something", "k8s-merge-robot", 10*time.Hour),
    57  		makeComment("[NOTIF] Notification", "k8s-merge-robot", 10*time.Hour),
    58  	}
    59  
    60  	pinger := NewPinger("NOTIF", "k8s-merge-robot").SetMaxCount(2)
    61  
    62  	if pinger.IsMaxReached(comments, timeAgo(11*time.Hour)) {
    63  		t.Error("Should not have reached the maximum")
    64  	}
    65  }
    66  
    67  func TestMaxReachNoLimit(t *testing.T) {
    68  	comments := []*github.IssueComment{
    69  		makeComment("[NOTIF] Notification", "k8s-merge-robot", 14*time.Hour),
    70  		makeComment("[NOTIF] Notification", "k8s-merge-robot", 12*time.Hour),
    71  		makeComment("[SOMETHING] Something", "k8s-merge-robot", 10*time.Hour),
    72  		makeComment("[NOTIF] Notification", "k8s-merge-robot", 10*time.Hour),
    73  	}
    74  
    75  	pinger := NewPinger("NOTIF", "k8s-merge-robot")
    76  
    77  	if pinger.IsMaxReached(comments, nil) {
    78  		t.Error("Should not have reached the non-existing maximum")
    79  	}
    80  }
    81  
    82  func TestNotification(t *testing.T) {
    83  	comments := []*github.IssueComment{
    84  		makeComment("[SOMETHING] Something", "k8s-merge-robot", 10*time.Hour),
    85  	}
    86  
    87  	notif := NewPinger("NOTIF", "k8s-merge-robot").SetDescription("Description").PingNotification(comments, "who", nil)
    88  	if notif == nil {
    89  		t.Error("PingNotification should have created a notif")
    90  	}
    91  	expectedNotif := Notification{Name: "NOTIF", Arguments: "who", Context: "Description"}
    92  	if *notif != expectedNotif {
    93  		t.Error(*notif, "doesn't match expectedNotif:", expectedNotif)
    94  	}
    95  }
    96  
    97  func TestNotificationNilTimePeriod(t *testing.T) {
    98  	comments := []*github.IssueComment{
    99  		makeComment("[NOTIF] Notification", "k8s-merge-robot", 14*time.Hour),
   100  		makeComment("[NOTIF] Notification", "k8s-merge-robot", 13*time.Hour),
   101  		makeComment("[NOTIF] Notification", "k8s-merge-robot", 12*time.Hour),
   102  		makeComment("[NOTIF] Notification", "k8s-merge-robot", 11*time.Hour),
   103  		makeComment("[SOMETHING] Something", "k8s-merge-robot", 10*time.Hour),
   104  	}
   105  
   106  	notif := NewPinger("NOTIF", "k8s-merge-robot").PingNotification(comments, "who", nil)
   107  	if notif == nil {
   108  		t.Error("PingNotification should have created a notif")
   109  	}
   110  }
   111  
   112  func TestNotificationTimePeriodNotReached(t *testing.T) {
   113  	comments := []*github.IssueComment{
   114  		makeComment("[NOTIF] Notification", "k8s-merge-robot", 5*time.Hour),
   115  		makeComment("[NOTIF] Notification", "k8s-merge-robot", 3*time.Hour),
   116  		makeComment("[NOTIF] Notification", "k8s-merge-robot", 1*time.Hour),
   117  	}
   118  
   119  	notif := NewPinger("NOTIF", "k8s-merge-robot").SetTimePeriod(2*time.Hour).PingNotification(comments, "who", nil)
   120  	if notif != nil {
   121  		t.Error("PingNotification shouldn't have created a notif")
   122  	}
   123  }
   124  
   125  func TestNotificationTimePeriodReached(t *testing.T) {
   126  	comments := []*github.IssueComment{
   127  		makeComment("[NOTIF] Notification", "k8s-merge-robot", 4*time.Hour),
   128  		makeComment("[NOTIF] Notification", "k8s-merge-robot", 3*time.Hour),
   129  		makeComment("[NOTIF] Notification", "k8s-merge-robot", 2*time.Hour),
   130  	}
   131  
   132  	notif := NewPinger("NOTIF", "k8s-merge-robot").SetTimePeriod(time.Hour).PingNotification(comments, "who", nil)
   133  	if notif == nil {
   134  		t.Error("PingNotification should have created a notif")
   135  	}
   136  }
   137  
   138  func TestNotificationStartDate(t *testing.T) {
   139  	comments := []*github.IssueComment{}
   140  	notif := NewPinger("NOTIF", "k8s-merge-robot").SetTimePeriod(10*time.Hour).PingNotification(comments, "who", timeAgo(2*time.Hour))
   141  	if notif != nil {
   142  		t.Error("PingNotification shouldn't have created a notif")
   143  	}
   144  }