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