github.com/yrj2011/jx-test-infra@v0.0.0-20190529031832-7a2065ee98eb/prow/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 "k8s.io/test-infra/prow/kube" 25 ) 26 27 func TestPickLatest(t *testing.T) { 28 jobs := []kube.ProwJob{ 29 // We're using Cluster as a simple way to distinguish runs 30 {Spec: kube.ProwJobSpec{Job: "glob-1", Cluster: "1"}}, 31 {Spec: kube.ProwJobSpec{Job: "glob-1", Cluster: "2"}}, 32 {Spec: kube.ProwJobSpec{Job: "glob-2", Cluster: "1"}}, 33 {Spec: kube.ProwJobSpec{Job: "job-a", Cluster: "1"}}, 34 {Spec: kube.ProwJobSpec{Job: "job-ab", Cluster: "1"}}, 35 } 36 expected := []kube.ProwJob{ 37 {Spec: kube.ProwJobSpec{Job: "glob-1", Cluster: "1"}}, 38 {Spec: kube.ProwJobSpec{Job: "glob-2", Cluster: "1"}}, 39 {Spec: kube.ProwJobSpec{Job: "job-a", Cluster: "1"}}, 40 } 41 result := pickLatestJobs(jobs, "glob-*,job-a") 42 if !reflect.DeepEqual(result, expected) { 43 fmt.Println("expected:") 44 for _, job := range expected { 45 fmt.Printf(" job: %s cluster: %s,", job.Spec.Job, job.Spec.Cluster) 46 } 47 fmt.Println("got:") 48 for _, job := range result { 49 fmt.Printf(" job: %s cluster: %s,", job.Spec.Job, job.Spec.Cluster) 50 } 51 } 52 } 53 54 func TestRenderBadge(t *testing.T) { 55 for _, tc := range []struct { 56 jobStates []string 57 expectedColor string 58 expectedStatus string 59 }{ 60 {nil, "darkgrey", "no results"}, 61 {[]string{"success"}, "brightgreen", "passing"}, 62 {[]string{"success", "failure"}, "red", "failing 2"}, 63 {[]string{"success", "failure", "failure", "failure", "failure"}, "red", "failing 2, 3, 4, ..."}, 64 } { 65 jobs := []kube.ProwJob{} 66 for i, state := range tc.jobStates { 67 jobs = append(jobs, kube.ProwJob{ 68 Spec: kube.ProwJobSpec{Job: fmt.Sprintf("%d", i+1)}, 69 Status: kube.ProwJobStatus{State: kube.ProwJobState(state)}, 70 }) 71 } 72 status, color, _ := renderBadge(jobs) 73 if color != tc.expectedColor { 74 t.Errorf("unexpected color for %v: got %s instead of %s", tc.jobStates, color, tc.expectedColor) 75 } 76 if status != tc.expectedStatus { 77 t.Errorf("unexpected status for %v: got %s instead of %s", tc.jobStates, status, tc.expectedStatus) 78 } 79 } 80 }