sigs.k8s.io/prow@v0.0.0-20240503223140-c5e374dc7eb1/pkg/crier/reporters/pubsub/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 pubsub
    18  
    19  import (
    20  	"context"
    21  	"reflect"
    22  	"sync"
    23  	"testing"
    24  
    25  	"github.com/sirupsen/logrus"
    26  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    27  
    28  	prowapi "sigs.k8s.io/prow/pkg/apis/prowjobs/v1"
    29  	"sigs.k8s.io/prow/pkg/config"
    30  )
    31  
    32  const (
    33  	testPubSubProjectName = "test-project"
    34  	testPubSubTopicName   = "test-topic"
    35  	testPubSubRunID       = "test-id"
    36  )
    37  
    38  type fca struct {
    39  	sync.Mutex
    40  	c *config.Config
    41  }
    42  
    43  func (f *fca) Config() *config.Config {
    44  	f.Lock()
    45  	defer f.Unlock()
    46  	return f.c
    47  }
    48  
    49  func TestGenerateMessageFromPJ(t *testing.T) {
    50  	var testcases = []struct {
    51  		name            string
    52  		pj              *prowapi.ProwJob
    53  		jobURLPrefix    string
    54  		expectedMessage *ReportMessage
    55  		expectedError   error
    56  	}{
    57  		// tests with gubernator job URLs
    58  		{
    59  			name: "Prowjob with all information for presubmit jobs should work with no error",
    60  			pj: &prowapi.ProwJob{
    61  				ObjectMeta: metav1.ObjectMeta{
    62  					Name: "test1",
    63  					Labels: map[string]string{
    64  						PubSubProjectLabel: testPubSubProjectName,
    65  						PubSubTopicLabel:   testPubSubTopicName,
    66  						PubSubRunIDLabel:   testPubSubRunID,
    67  					},
    68  				},
    69  				Status: prowapi.ProwJobStatus{
    70  					State: prowapi.SuccessState,
    71  					URL:   "guber/test1",
    72  				},
    73  				Spec: prowapi.ProwJobSpec{
    74  					Type: prowapi.PresubmitJob,
    75  					Job:  "test1",
    76  					Refs: &prowapi.Refs{
    77  						Pulls: []prowapi.Pull{{Number: 123}},
    78  					},
    79  				},
    80  			},
    81  			jobURLPrefix: "guber/",
    82  			expectedMessage: &ReportMessage{
    83  				Project: testPubSubProjectName,
    84  				Topic:   testPubSubTopicName,
    85  				RunID:   testPubSubRunID,
    86  				Status:  prowapi.SuccessState,
    87  				URL:     "guber/test1",
    88  				GCSPath: "gs://test1",
    89  				Refs: []prowapi.Refs{
    90  					{
    91  						Pulls: []prowapi.Pull{{Number: 123}},
    92  					},
    93  				},
    94  				JobType: prowapi.PresubmitJob,
    95  				JobName: "test1",
    96  			},
    97  		},
    98  		{
    99  			name: "Prowjob with all information for periodic jobs should work with no error",
   100  			pj: &prowapi.ProwJob{
   101  				ObjectMeta: metav1.ObjectMeta{
   102  					Name: "test1",
   103  					Labels: map[string]string{
   104  						PubSubProjectLabel: testPubSubProjectName,
   105  						PubSubTopicLabel:   testPubSubTopicName,
   106  						PubSubRunIDLabel:   testPubSubRunID,
   107  					},
   108  				},
   109  				Status: prowapi.ProwJobStatus{
   110  					State: prowapi.SuccessState,
   111  					URL:   "guber/test1",
   112  				},
   113  				Spec: prowapi.ProwJobSpec{
   114  					Type: prowapi.PeriodicJob,
   115  					Job:  "test1",
   116  				},
   117  			},
   118  			jobURLPrefix: "guber/",
   119  			expectedMessage: &ReportMessage{
   120  				Project: testPubSubProjectName,
   121  				Topic:   testPubSubTopicName,
   122  				RunID:   testPubSubRunID,
   123  				Status:  prowapi.SuccessState,
   124  				URL:     "guber/test1",
   125  				GCSPath: "gs://test1",
   126  				JobType: prowapi.PeriodicJob,
   127  				JobName: "test1",
   128  			},
   129  		},
   130  		{
   131  			name: "Prowjob has no pubsub runID label, should return a message with runid empty",
   132  			pj: &prowapi.ProwJob{
   133  				ObjectMeta: metav1.ObjectMeta{
   134  					Name: "test-no-runID",
   135  					Labels: map[string]string{
   136  						PubSubProjectLabel: testPubSubProjectName,
   137  						PubSubTopicLabel:   testPubSubTopicName,
   138  					},
   139  				},
   140  				Status: prowapi.ProwJobStatus{
   141  					State: prowapi.SuccessState,
   142  				},
   143  			},
   144  			expectedMessage: &ReportMessage{
   145  				Project: testPubSubProjectName,
   146  				Topic:   testPubSubTopicName,
   147  				RunID:   "",
   148  				Status:  prowapi.SuccessState,
   149  			},
   150  		},
   151  		{
   152  			name: "Prowjob with all information annotations should work with no error",
   153  			pj: &prowapi.ProwJob{
   154  				ObjectMeta: metav1.ObjectMeta{
   155  					Name: "test1",
   156  					Annotations: map[string]string{
   157  						PubSubProjectLabel: testPubSubProjectName,
   158  						PubSubTopicLabel:   testPubSubTopicName,
   159  						PubSubRunIDLabel:   testPubSubRunID,
   160  					},
   161  				},
   162  				Status: prowapi.ProwJobStatus{
   163  					State: prowapi.SuccessState,
   164  					URL:   "guber/test1",
   165  				},
   166  			},
   167  			jobURLPrefix: "guber/",
   168  			expectedMessage: &ReportMessage{
   169  				Project: testPubSubProjectName,
   170  				Topic:   testPubSubTopicName,
   171  				RunID:   testPubSubRunID,
   172  				Status:  prowapi.SuccessState,
   173  				URL:     "guber/test1",
   174  				GCSPath: "gs://test1",
   175  			},
   176  		},
   177  		{
   178  			name: "Prowjob has no pubsub runID annotation, should return a message with runid empty",
   179  			pj: &prowapi.ProwJob{
   180  				ObjectMeta: metav1.ObjectMeta{
   181  					Name: "test-no-runID",
   182  					Annotations: map[string]string{
   183  						PubSubProjectLabel: testPubSubProjectName,
   184  						PubSubTopicLabel:   testPubSubTopicName,
   185  					},
   186  				},
   187  				Status: prowapi.ProwJobStatus{
   188  					State: prowapi.SuccessState,
   189  				},
   190  			},
   191  			expectedMessage: &ReportMessage{
   192  				Project: testPubSubProjectName,
   193  				Topic:   testPubSubTopicName,
   194  				RunID:   "",
   195  				Status:  prowapi.SuccessState,
   196  			},
   197  		},
   198  
   199  		// tests with regular job URLs
   200  		{
   201  			name: "Prowjob with all information for presubmit jobs should work with no error",
   202  			pj: &prowapi.ProwJob{
   203  				ObjectMeta: metav1.ObjectMeta{
   204  					Name: "test1",
   205  					Labels: map[string]string{
   206  						PubSubProjectLabel: testPubSubProjectName,
   207  						PubSubTopicLabel:   testPubSubTopicName,
   208  						PubSubRunIDLabel:   testPubSubRunID,
   209  					},
   210  				},
   211  				Status: prowapi.ProwJobStatus{
   212  					State: prowapi.SuccessState,
   213  					URL:   "https://prow.k8s.io/view/gcs/test1",
   214  				},
   215  				Spec: prowapi.ProwJobSpec{
   216  					Type: prowapi.PresubmitJob,
   217  					Job:  "test1",
   218  					Refs: &prowapi.Refs{
   219  						Pulls: []prowapi.Pull{{Number: 123}},
   220  					},
   221  				},
   222  			},
   223  			jobURLPrefix: "https://prow.k8s.io/view/gcs/",
   224  			expectedMessage: &ReportMessage{
   225  				Project: testPubSubProjectName,
   226  				Topic:   testPubSubTopicName,
   227  				RunID:   testPubSubRunID,
   228  				Status:  prowapi.SuccessState,
   229  				URL:     "https://prow.k8s.io/view/gcs/test1",
   230  				GCSPath: "gs://test1",
   231  				Refs: []prowapi.Refs{
   232  					{
   233  						Pulls: []prowapi.Pull{{Number: 123}},
   234  					},
   235  				},
   236  				JobType: prowapi.PresubmitJob,
   237  				JobName: "test1",
   238  			},
   239  		},
   240  		{
   241  			name: "Prowjob with all information for periodic jobs should work with no error",
   242  			pj: &prowapi.ProwJob{
   243  				ObjectMeta: metav1.ObjectMeta{
   244  					Name: "test1",
   245  					Labels: map[string]string{
   246  						PubSubProjectLabel: testPubSubProjectName,
   247  						PubSubTopicLabel:   testPubSubTopicName,
   248  						PubSubRunIDLabel:   testPubSubRunID,
   249  					},
   250  				},
   251  				Status: prowapi.ProwJobStatus{
   252  					State: prowapi.SuccessState,
   253  					URL:   "https://prow.k8s.io/view/gcs/test1",
   254  				},
   255  				Spec: prowapi.ProwJobSpec{
   256  					Type: prowapi.PeriodicJob,
   257  					Job:  "test1",
   258  				},
   259  			},
   260  			jobURLPrefix: "https://prow.k8s.io/view/gcs/",
   261  			expectedMessage: &ReportMessage{
   262  				Project: testPubSubProjectName,
   263  				Topic:   testPubSubTopicName,
   264  				RunID:   testPubSubRunID,
   265  				Status:  prowapi.SuccessState,
   266  				URL:     "https://prow.k8s.io/view/gcs/test1",
   267  				GCSPath: "gs://test1",
   268  				JobType: prowapi.PeriodicJob,
   269  				JobName: "test1",
   270  			},
   271  		},
   272  		{
   273  			name: "Prowjob with all information annotations should work with no error",
   274  			pj: &prowapi.ProwJob{
   275  				ObjectMeta: metav1.ObjectMeta{
   276  					Name: "test1",
   277  					Annotations: map[string]string{
   278  						PubSubProjectLabel: testPubSubProjectName,
   279  						PubSubTopicLabel:   testPubSubTopicName,
   280  						PubSubRunIDLabel:   testPubSubRunID,
   281  					},
   282  				},
   283  				Status: prowapi.ProwJobStatus{
   284  					State: prowapi.SuccessState,
   285  					URL:   "https://prow.k8s.io/view/gcs/test1",
   286  				},
   287  			},
   288  			jobURLPrefix: "https://prow.k8s.io/view/gcs/",
   289  			expectedMessage: &ReportMessage{
   290  				Project: testPubSubProjectName,
   291  				Topic:   testPubSubTopicName,
   292  				RunID:   testPubSubRunID,
   293  				Status:  prowapi.SuccessState,
   294  				URL:     "https://prow.k8s.io/view/gcs/test1",
   295  				GCSPath: "gs://test1",
   296  			},
   297  		},
   298  		{
   299  			name: "Status message",
   300  			pj: &prowapi.ProwJob{
   301  				ObjectMeta: metav1.ObjectMeta{
   302  					Name: "test1",
   303  					Annotations: map[string]string{
   304  						PubSubProjectLabel: testPubSubProjectName,
   305  						PubSubTopicLabel:   testPubSubTopicName,
   306  						PubSubRunIDLabel:   testPubSubRunID,
   307  					},
   308  				},
   309  				Status: prowapi.ProwJobStatus{
   310  					State:       prowapi.SuccessState,
   311  					URL:         "https://prow.k8s.io/view/gcs/test1",
   312  					Description: "this job went great",
   313  				},
   314  			},
   315  			jobURLPrefix: "https://prow.k8s.io/view/gcs/",
   316  			expectedMessage: &ReportMessage{
   317  				Project: testPubSubProjectName,
   318  				Topic:   testPubSubTopicName,
   319  				RunID:   testPubSubRunID,
   320  				Status:  prowapi.SuccessState,
   321  				URL:     "https://prow.k8s.io/view/gcs/test1",
   322  				GCSPath: "gs://test1",
   323  				Message: "this job went great",
   324  			},
   325  		},
   326  	}
   327  
   328  	for _, tc := range testcases {
   329  		fca := &fca{
   330  			c: &config.Config{
   331  				ProwConfig: config.ProwConfig{
   332  					Plank: config.Plank{
   333  						JobURLPrefixConfig: map[string]string{"*": tc.jobURLPrefix},
   334  					},
   335  				},
   336  			},
   337  		}
   338  
   339  		c := &Client{
   340  			config: fca.Config,
   341  		}
   342  
   343  		m := c.generateMessageFromPJ(tc.pj)
   344  
   345  		if !reflect.DeepEqual(m, tc.expectedMessage) {
   346  			t.Errorf("Unexpected result from test: %s.\nExpected: %v\nGot: %v",
   347  				tc.name, tc.expectedMessage, m)
   348  		}
   349  	}
   350  }
   351  
   352  func TestShouldReport(t *testing.T) {
   353  	var testcases = []struct {
   354  		name           string
   355  		pj             *prowapi.ProwJob
   356  		expectedResult bool
   357  	}{
   358  		{
   359  			name: "Prowjob with all pubsub information labels should return",
   360  			pj: &prowapi.ProwJob{
   361  				ObjectMeta: metav1.ObjectMeta{
   362  					Name: "test1",
   363  					Labels: map[string]string{
   364  						PubSubProjectLabel: testPubSubProjectName,
   365  						PubSubTopicLabel:   testPubSubTopicName,
   366  						PubSubRunIDLabel:   testPubSubRunID,
   367  					},
   368  				},
   369  				Status: prowapi.ProwJobStatus{
   370  					State: prowapi.SuccessState,
   371  				},
   372  			},
   373  			expectedResult: true,
   374  		},
   375  		{
   376  			name: "Prowjob has no pubsub project label, should not report",
   377  			pj: &prowapi.ProwJob{
   378  				ObjectMeta: metav1.ObjectMeta{
   379  					Name: "test-no-project",
   380  					Labels: map[string]string{
   381  						PubSubTopicLabel: testPubSubTopicName,
   382  						PubSubRunIDLabel: testPubSubRunID,
   383  					},
   384  				},
   385  				Status: prowapi.ProwJobStatus{
   386  					State: prowapi.SuccessState,
   387  				},
   388  			},
   389  			expectedResult: false,
   390  		},
   391  		{
   392  			name: "Prowjob has no pubsub topic label, should not report",
   393  			pj: &prowapi.ProwJob{
   394  				ObjectMeta: metav1.ObjectMeta{
   395  					Name: "test-no-topic",
   396  					Labels: map[string]string{
   397  						PubSubProjectLabel: testPubSubProjectName,
   398  						PubSubRunIDLabel:   testPubSubRunID,
   399  					},
   400  				},
   401  				Status: prowapi.ProwJobStatus{
   402  					State: prowapi.SuccessState,
   403  				},
   404  			},
   405  			expectedResult: false,
   406  		},
   407  		{
   408  			name: "Prowjob with all pubsub information annotations should return",
   409  			pj: &prowapi.ProwJob{
   410  				ObjectMeta: metav1.ObjectMeta{
   411  					Name: "test1",
   412  					Annotations: map[string]string{
   413  						PubSubProjectLabel: testPubSubProjectName,
   414  						PubSubTopicLabel:   testPubSubTopicName,
   415  						PubSubRunIDLabel:   testPubSubRunID,
   416  					},
   417  				},
   418  				Status: prowapi.ProwJobStatus{
   419  					State: prowapi.SuccessState,
   420  				},
   421  			},
   422  			expectedResult: true,
   423  		},
   424  		{
   425  			name: "Prowjob has no pubsub project annotation, should not report",
   426  			pj: &prowapi.ProwJob{
   427  				ObjectMeta: metav1.ObjectMeta{
   428  					Name: "test-no-project",
   429  					Annotations: map[string]string{
   430  						PubSubTopicLabel: testPubSubTopicName,
   431  						PubSubRunIDLabel: testPubSubRunID,
   432  					},
   433  				},
   434  				Status: prowapi.ProwJobStatus{
   435  					State: prowapi.SuccessState,
   436  				},
   437  			},
   438  			expectedResult: false,
   439  		},
   440  		{
   441  			name: "Prowjob has no pubsub topic annotation, should not report",
   442  			pj: &prowapi.ProwJob{
   443  				ObjectMeta: metav1.ObjectMeta{
   444  					Name: "test-no-topic",
   445  					Annotations: map[string]string{
   446  						PubSubProjectLabel: testPubSubProjectName,
   447  						PubSubRunIDLabel:   testPubSubRunID,
   448  					},
   449  				},
   450  				Status: prowapi.ProwJobStatus{
   451  					State: prowapi.SuccessState,
   452  				},
   453  			},
   454  			expectedResult: false,
   455  		},
   456  	}
   457  
   458  	var fakeConfigAgent fca
   459  	c := NewReporter(fakeConfigAgent.Config)
   460  
   461  	for _, tc := range testcases {
   462  		r := c.ShouldReport(context.Background(), logrus.NewEntry(logrus.StandardLogger()), tc.pj)
   463  
   464  		if r != tc.expectedResult {
   465  			t.Errorf("Unexpected result from test: %s.\nExpected: %v\nGot: %v",
   466  				tc.name, tc.expectedResult, r)
   467  		}
   468  	}
   469  }