github.com/munnerz/test-infra@v0.0.0-20190108210205-ce3d181dc989/prow/pubsub/reporter/reporter_test.go (about) 1 /* 2 Copyright 2018 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 reporter 18 19 import ( 20 "reflect" 21 "testing" 22 23 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 24 "k8s.io/test-infra/prow/kube" 25 ) 26 27 const ( 28 testPubSubProjectName = "test-project" 29 testPubSubTopicName = "test-topic" 30 testPubSubRunID = "test-id" 31 ) 32 33 func TestGenerateMessageFromPJ(t *testing.T) { 34 var testcases = []struct { 35 name string 36 pj *kube.ProwJob 37 expectedMessage *ReportMessage 38 expectedError error 39 }{ 40 { 41 name: "Prowjob with all information should work with no error", 42 pj: &kube.ProwJob{ 43 ObjectMeta: metav1.ObjectMeta{ 44 Name: "test1", 45 Labels: map[string]string{ 46 PubSubProjectLabel: testPubSubProjectName, 47 PubSubTopicLabel: testPubSubTopicName, 48 PubSubRunIDLabel: testPubSubRunID, 49 }, 50 }, 51 Status: kube.ProwJobStatus{ 52 State: kube.SuccessState, 53 URL: "guber/test1", 54 }, 55 }, 56 expectedMessage: &ReportMessage{ 57 Project: testPubSubProjectName, 58 Topic: testPubSubTopicName, 59 RunID: testPubSubRunID, 60 Status: kube.SuccessState, 61 URL: "guber/test1", 62 }, 63 }, 64 { 65 name: "Prowjob has no pubsub runID label, should return a message with runid empty", 66 pj: &kube.ProwJob{ 67 ObjectMeta: metav1.ObjectMeta{ 68 Name: "test-no-runID", 69 Labels: map[string]string{ 70 PubSubProjectLabel: testPubSubProjectName, 71 PubSubTopicLabel: testPubSubTopicName, 72 }, 73 }, 74 Status: kube.ProwJobStatus{ 75 State: kube.SuccessState, 76 }, 77 }, 78 expectedMessage: &ReportMessage{ 79 Project: testPubSubProjectName, 80 Topic: testPubSubTopicName, 81 RunID: "", 82 Status: kube.SuccessState, 83 }, 84 }, 85 } 86 87 for _, tc := range testcases { 88 m := generateMessageFromPJ(tc.pj) 89 90 if !reflect.DeepEqual(m, tc.expectedMessage) { 91 t.Errorf("Unexpected result from test: %s.\nExpected: %v\nGot: %v", 92 tc.name, tc.expectedMessage, m) 93 } 94 } 95 } 96 97 func TestShouldReport(t *testing.T) { 98 var testcases = []struct { 99 name string 100 pj *kube.ProwJob 101 expectedResult bool 102 }{ 103 { 104 name: "Prowjob with all pubsub information should return", 105 pj: &kube.ProwJob{ 106 ObjectMeta: metav1.ObjectMeta{ 107 Name: "test1", 108 Labels: map[string]string{ 109 PubSubProjectLabel: testPubSubProjectName, 110 PubSubTopicLabel: testPubSubTopicName, 111 PubSubRunIDLabel: testPubSubRunID, 112 }, 113 }, 114 Status: kube.ProwJobStatus{ 115 State: kube.SuccessState, 116 }, 117 }, 118 expectedResult: true, 119 }, 120 { 121 name: "Prowjob has no pubsub project label, should not report", 122 pj: &kube.ProwJob{ 123 ObjectMeta: metav1.ObjectMeta{ 124 Name: "test-no-project", 125 Labels: map[string]string{ 126 PubSubTopicLabel: testPubSubTopicName, 127 PubSubRunIDLabel: testPubSubRunID, 128 }, 129 }, 130 Status: kube.ProwJobStatus{ 131 State: kube.SuccessState, 132 }, 133 }, 134 expectedResult: false, 135 }, 136 { 137 name: "Prowjob has no pubsub topic label, should not report", 138 pj: &kube.ProwJob{ 139 ObjectMeta: metav1.ObjectMeta{ 140 Name: "test-no-topic", 141 Labels: map[string]string{ 142 PubSubProjectLabel: testPubSubProjectName, 143 PubSubRunIDLabel: testPubSubRunID, 144 }, 145 }, 146 Status: kube.ProwJobStatus{ 147 State: kube.SuccessState, 148 }, 149 }, 150 expectedResult: false, 151 }, 152 } 153 154 c := NewReporter() 155 156 for _, tc := range testcases { 157 r := c.ShouldReport(tc.pj) 158 159 if r != tc.expectedResult { 160 t.Errorf("Unexpected result from test: %s.\nExpected: %v\nGot: %v", 161 tc.name, tc.expectedResult, r) 162 } 163 } 164 }