github.com/zppinho/prow@v0.0.0-20240510014325-1738badeb017/cmd/deck/badge_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 main
    18  
    19  import (
    20  	"fmt"
    21  	"reflect"
    22  	"testing"
    23  
    24  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    25  	prowapi "sigs.k8s.io/prow/pkg/apis/prowjobs/v1"
    26  )
    27  
    28  func TestPickLatest(t *testing.T) {
    29  	earliest := metav1.Time{}
    30  	earlier := metav1.Now()
    31  	jobs := []prowapi.ProwJob{
    32  		// We're using Cluster as a simple way to distinguish runs
    33  		{Status: prowapi.ProwJobStatus{StartTime: earliest}, Spec: prowapi.ProwJobSpec{Job: "glob-1", Cluster: "1"}},
    34  		{Status: prowapi.ProwJobStatus{StartTime: earlier}, Spec: prowapi.ProwJobSpec{Job: "glob-1", Cluster: "2"}},
    35  		{Status: prowapi.ProwJobStatus{StartTime: earlier}, Spec: prowapi.ProwJobSpec{Job: "glob-2", Cluster: "1"}},
    36  		{Status: prowapi.ProwJobStatus{StartTime: earliest}, Spec: prowapi.ProwJobSpec{Job: "job-a", Cluster: "1"}},
    37  		{Status: prowapi.ProwJobStatus{StartTime: earlier}, Spec: prowapi.ProwJobSpec{Job: "job-a", Cluster: "2"}},
    38  		{Status: prowapi.ProwJobStatus{StartTime: earlier}, Spec: prowapi.ProwJobSpec{Job: "job-ab", Cluster: "1"}},
    39  	}
    40  	expected := []prowapi.ProwJob{
    41  		{Status: prowapi.ProwJobStatus{StartTime: earlier}, Spec: prowapi.ProwJobSpec{Job: "glob-1", Cluster: "2"}},
    42  		{Status: prowapi.ProwJobStatus{StartTime: earlier}, Spec: prowapi.ProwJobSpec{Job: "glob-2", Cluster: "1"}},
    43  		{Status: prowapi.ProwJobStatus{StartTime: earlier}, Spec: prowapi.ProwJobSpec{Job: "job-a", Cluster: "2"}},
    44  	}
    45  	result := pickLatestJobs(jobs, "glob-*,job-a")
    46  	if !reflect.DeepEqual(result, expected) {
    47  		fmt.Println("expected:")
    48  		for _, job := range expected {
    49  			fmt.Printf("  job: %s cluster: %s, timestamp %s", job.Spec.Job, job.Spec.Cluster, earlier)
    50  		}
    51  		fmt.Println("got:")
    52  		for _, job := range result {
    53  			fmt.Printf("  job: %s cluster: %s, timestamp %s", job.Spec.Job, job.Spec.Cluster, earlier)
    54  		}
    55  	}
    56  }
    57  
    58  func TestRenderBadge(t *testing.T) {
    59  	for _, tc := range []struct {
    60  		jobStates      []string
    61  		expectedColor  string
    62  		expectedStatus string
    63  	}{
    64  		{nil, "darkgrey", "no results"},
    65  		{[]string{"success"}, "brightgreen", "passing"},
    66  		{[]string{"success", "failure"}, "red", "failing 2"},
    67  		{[]string{"success", "failure", "failure", "failure", "failure"}, "red", "failing 2, 3, 4, ..."},
    68  	} {
    69  		jobs := []prowapi.ProwJob{}
    70  		for i, state := range tc.jobStates {
    71  			jobs = append(jobs, prowapi.ProwJob{
    72  				Spec:   prowapi.ProwJobSpec{Job: fmt.Sprintf("%d", i+1)},
    73  				Status: prowapi.ProwJobStatus{State: prowapi.ProwJobState(state)},
    74  			})
    75  		}
    76  		status, color, _ := renderBadge(jobs)
    77  		if color != tc.expectedColor {
    78  			t.Errorf("unexpected color for %v: got %s instead of %s", tc.jobStates, color, tc.expectedColor)
    79  		}
    80  		if status != tc.expectedStatus {
    81  			t.Errorf("unexpected status for %v: got %s instead of %s", tc.jobStates, status, tc.expectedStatus)
    82  		}
    83  	}
    84  }