sigs.k8s.io/prow@v0.0.0-20240503223140-c5e374dc7eb1/pkg/kube/metrics_test.go (about)

     1  /*
     2  Copyright 2019 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 kube
    18  
    19  import (
    20  	"reflect"
    21  	"testing"
    22  
    23  	v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    24  	"k8s.io/apimachinery/pkg/util/diff"
    25  	prowapi "sigs.k8s.io/prow/pkg/apis/prowjobs/v1"
    26  )
    27  
    28  func TestGetJobLabelMap(t *testing.T) {
    29  	pjs := []prowapi.ProwJob{
    30  		{
    31  			Spec: prowapi.ProwJobSpec{
    32  				Job:  "test-job-1",
    33  				Type: prowapi.PresubmitJob,
    34  				Refs: &prowapi.Refs{
    35  					Org:     "org1",
    36  					Repo:    "repo1",
    37  					BaseRef: "master",
    38  				},
    39  			},
    40  			Status: prowapi.ProwJobStatus{
    41  				State: prowapi.PendingState,
    42  			},
    43  		},
    44  		{
    45  			Spec: prowapi.ProwJobSpec{
    46  				Job:  "test-job-1",
    47  				Type: prowapi.PresubmitJob,
    48  				Refs: &prowapi.Refs{
    49  					Org:     "org1",
    50  					Repo:    "repo1",
    51  					BaseRef: "master",
    52  				},
    53  			},
    54  			Status: prowapi.ProwJobStatus{
    55  				State: prowapi.PendingState,
    56  			},
    57  		},
    58  		{
    59  			Spec: prowapi.ProwJobSpec{
    60  				Job:  "test-job-2",
    61  				Type: prowapi.PresubmitJob,
    62  				Refs: &prowapi.Refs{
    63  					Org:     "org1",
    64  					Repo:    "repo1",
    65  					BaseRef: "master",
    66  				},
    67  			},
    68  			Status: prowapi.ProwJobStatus{
    69  				State: prowapi.PendingState,
    70  			},
    71  		},
    72  		{
    73  			Spec: prowapi.ProwJobSpec{
    74  				Job:  "test-job-2",
    75  				Type: prowapi.PresubmitJob,
    76  				Refs: &prowapi.Refs{
    77  					Org:     "org1",
    78  					Repo:    "repo1",
    79  					BaseRef: "release-4.1",
    80  				},
    81  			},
    82  			Status: prowapi.ProwJobStatus{
    83  				State: prowapi.PendingState,
    84  			},
    85  		},
    86  		{
    87  			Spec: prowapi.ProwJobSpec{
    88  				Job:  "test-job-3",
    89  				Type: prowapi.PresubmitJob,
    90  				Refs: nil,
    91  				ExtraRefs: []prowapi.Refs{
    92  					{
    93  						Org:     "org1",
    94  						Repo:    "repo1",
    95  						BaseRef: "release-4.2",
    96  					},
    97  				},
    98  			},
    99  			Status: prowapi.ProwJobStatus{
   100  				State: prowapi.FailureState,
   101  			},
   102  		},
   103  		{
   104  			ObjectMeta: v1.ObjectMeta{
   105  				Labels: map[string]string{
   106  					RetestLabel: "true",
   107  				},
   108  			},
   109  			Spec: prowapi.ProwJobSpec{
   110  				Job:  "test-job-4",
   111  				Type: prowapi.PresubmitJob,
   112  				Refs: nil,
   113  				ExtraRefs: []prowapi.Refs{
   114  					{
   115  						Org:     "org1",
   116  						Repo:    "repo1",
   117  						BaseRef: "release-4.2",
   118  					},
   119  				},
   120  			},
   121  			Status: prowapi.ProwJobStatus{
   122  				State: prowapi.FailureState,
   123  			},
   124  		},
   125  	}
   126  
   127  	jobLabelMap := getJobLabelMap(pjs)
   128  
   129  	expected := map[jobLabel]float64{
   130  		{jobName: "test-job-1", jobType: string(prowapi.PresubmitJob), org: "org1", repo: "repo1", baseRef: "master", state: string(prowapi.PendingState), retest: "false"}:      2,
   131  		{jobName: "test-job-2", jobType: string(prowapi.PresubmitJob), org: "org1", repo: "repo1", baseRef: "master", state: string(prowapi.PendingState), retest: "false"}:      1,
   132  		{jobName: "test-job-2", jobType: string(prowapi.PresubmitJob), org: "org1", repo: "repo1", baseRef: "release-4.1", state: string(prowapi.PendingState), retest: "false"}: 1,
   133  		{jobName: "test-job-3", jobType: string(prowapi.PresubmitJob), org: "org1", repo: "repo1", baseRef: "release-4.2", state: string(prowapi.FailureState), retest: "false"}: 1,
   134  		{jobName: "test-job-4", jobType: string(prowapi.PresubmitJob), org: "org1", repo: "repo1", baseRef: "release-4.2", state: string(prowapi.FailureState), retest: "true"}:  1,
   135  	}
   136  
   137  	if !reflect.DeepEqual(expected, jobLabelMap) {
   138  		t.Errorf("Unexpected mis-match: %s", diff.ObjectReflectDiff(expected, jobLabelMap))
   139  	}
   140  }