@@ -39,6 +39,7 @@ import (
3939 "sigs.k8s.io/prow/pkg/gcsupload"
4040 "sigs.k8s.io/prow/pkg/github"
4141 "sigs.k8s.io/prow/pkg/initupload"
42+ "sigs.k8s.io/prow/pkg/kube"
4243 "sigs.k8s.io/prow/pkg/pod-utils/wrapper"
4344 "sigs.k8s.io/prow/pkg/sidecar"
4445 "sigs.k8s.io/prow/pkg/testutil"
@@ -65,6 +66,132 @@ func cookiePathOnly(secret string) string {
6566 return vp
6667}
6768
69+ func TestLabelsAndAnnotationsForSpecAddsPullAuthorLabel (t * testing.T ) {
70+ tests := []struct {
71+ name string
72+ spec prowapi.ProwJobSpec
73+ extraLabel map [string ]string
74+ want string
75+ wantExists bool
76+ }{
77+ {
78+ name : "presubmit with pull author" ,
79+ spec : prowapi.ProwJobSpec {
80+ Job : "pull-test" ,
81+ Refs : & prowapi.Refs {
82+ Org : "pingcap" ,
83+ Repo : "tidb" ,
84+ BaseRef : "master" ,
85+ BaseSHA : "base-sha" ,
86+ Pulls : []prowapi.Pull {{
87+ Number : 123 ,
88+ Author : "pr-author" ,
89+ SHA : "pull-sha" ,
90+ }},
91+ },
92+ },
93+ want : "pr-author" ,
94+ wantExists : true ,
95+ },
96+ {
97+ name : "postsubmit with refs but no pulls" ,
98+ spec : prowapi.ProwJobSpec {
99+ Job : "postsubmit-test" ,
100+ Refs : & prowapi.Refs {
101+ Org : "pingcap" ,
102+ Repo : "tidb" ,
103+ BaseRef : "master" ,
104+ BaseSHA : "base-sha" ,
105+ },
106+ },
107+ },
108+ {
109+ name : "presubmit with empty pull author" ,
110+ spec : prowapi.ProwJobSpec {
111+ Job : "pull-test" ,
112+ Refs : & prowapi.Refs {
113+ Org : "pingcap" ,
114+ Repo : "tidb" ,
115+ Pulls : []prowapi.Pull {{
116+ Number : 123 ,
117+ }},
118+ },
119+ },
120+ },
121+ {
122+ name : "invalid author is removed by label validation" ,
123+ spec : prowapi.ProwJobSpec {
124+ Job : "pull-test" ,
125+ Refs : & prowapi.Refs {
126+ Org : "pingcap" ,
127+ Repo : "tidb" ,
128+ Pulls : []prowapi.Pull {{
129+ Number : 123 ,
130+ Author : "bad author" ,
131+ }},
132+ },
133+ },
134+ },
135+ {
136+ name : "extra labels can override author" ,
137+ spec : prowapi.ProwJobSpec {
138+ Job : "pull-test" ,
139+ Refs : & prowapi.Refs {
140+ Org : "pingcap" ,
141+ Repo : "tidb" ,
142+ Pulls : []prowapi.Pull {{
143+ Number : 123 ,
144+ Author : "pr-author" ,
145+ }},
146+ },
147+ },
148+ extraLabel : map [string ]string {
149+ kube .AuthorLabel : "custom-author" ,
150+ },
151+ want : "custom-author" ,
152+ wantExists : true ,
153+ },
154+ }
155+
156+ for _ , tc := range tests {
157+ t .Run (tc .name , func (t * testing.T ) {
158+ got , _ := LabelsAndAnnotationsForSpec (tc .spec , tc .extraLabel , nil )
159+ value , exists := got [kube .AuthorLabel ]
160+ if exists != tc .wantExists {
161+ t .Fatalf ("expected author label existence %t, got %t" , tc .wantExists , exists )
162+ }
163+ if value != tc .want {
164+ t .Fatalf ("expected author label %q, got %q" , tc .want , value )
165+ }
166+ })
167+ }
168+ }
169+
170+ func TestLabelsAndAnnotationsForJobAddsPullAuthorLabel (t * testing.T ) {
171+ got , _ := LabelsAndAnnotationsForJob (prowapi.ProwJob {
172+ ObjectMeta : metav1.ObjectMeta {
173+ Name : "pj" ,
174+ },
175+ Spec : prowapi.ProwJobSpec {
176+ Job : "pull-test" ,
177+ Type : prowapi .PresubmitJob ,
178+ Context : "pull-test" ,
179+ Refs : & prowapi.Refs {
180+ Org : "pingcap" ,
181+ Repo : "tidb" ,
182+ Pulls : []prowapi.Pull {{
183+ Number : 1 ,
184+ Author : "pr-author" ,
185+ }},
186+ },
187+ },
188+ })
189+
190+ if got [kube .AuthorLabel ] != "pr-author" {
191+ t .Fatalf ("expected author label %q, got %q" , "pr-author" , got [kube .AuthorLabel ])
192+ }
193+ }
194+
68195func TestCloneRefs (t * testing.T ) {
69196 truth := true
70197 logMount := coreapi.VolumeMount {
0 commit comments