github.com/munnerz/test-infra@v0.0.0-20190108210205-ce3d181dc989/boskos/metrics/metrics_test.go (about) 1 /* 2 Copyright 2017 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 "reflect" 21 "testing" 22 23 "k8s.io/test-infra/boskos/common" 24 ) 25 26 func TestFilterMetric(t *testing.T) { 27 28 testCases := []struct { 29 name string 30 states []string 31 src, dest map[string]int 32 }{ 33 { 34 name: "noOther", 35 states: []string{common.Dirty, common.Cleaning, common.Busy, common.Free}, 36 src: map[string]int{ 37 common.Dirty: 10, 38 common.Cleaning: 2, 39 common.Busy: 5, 40 common.Free: 3, 41 }, 42 dest: map[string]int{ 43 common.Dirty: 10, 44 common.Cleaning: 2, 45 common.Busy: 5, 46 common.Free: 3, 47 common.Other: 0, 48 }, 49 }, 50 { 51 name: "multipleOther", 52 states: []string{common.Dirty, common.Cleaning, common.Busy, common.Free}, 53 src: map[string]int{ 54 common.Dirty: 10, 55 common.Cleaning: 2, 56 common.Busy: 5, 57 common.Free: 3, 58 "test": 10, 59 "new": 14, 60 }, 61 dest: map[string]int{ 62 common.Dirty: 10, 63 common.Cleaning: 2, 64 common.Busy: 5, 65 common.Free: 3, 66 common.Other: 24, 67 }, 68 }, 69 { 70 name: "multipleOtherNoLeased", 71 states: []string{common.Dirty, common.Cleaning, common.Busy, common.Free, common.Leased}, 72 src: map[string]int{ 73 common.Dirty: 10, 74 common.Cleaning: 2, 75 common.Busy: 5, 76 common.Free: 3, 77 "test": 10, 78 "new": 14, 79 }, 80 dest: map[string]int{ 81 common.Dirty: 10, 82 common.Cleaning: 2, 83 common.Busy: 5, 84 common.Free: 3, 85 common.Leased: 0, 86 common.Other: 24, 87 }, 88 }, 89 { 90 name: "NoOtherLeased", 91 states: []string{common.Dirty, common.Cleaning, common.Busy, common.Free, common.Leased}, 92 src: map[string]int{ 93 common.Dirty: 10, 94 common.Cleaning: 2, 95 common.Busy: 5, 96 common.Free: 3, 97 common.Leased: 10, 98 }, 99 dest: map[string]int{ 100 common.Dirty: 10, 101 common.Cleaning: 2, 102 common.Busy: 5, 103 common.Free: 3, 104 common.Leased: 10, 105 common.Other: 0, 106 }, 107 }, 108 } 109 110 for _, tc := range testCases { 111 test := func(t *testing.T) { 112 states = tc.states 113 dest := filterMetrics(tc.src) 114 if !reflect.DeepEqual(dest, tc.dest) { 115 t.Errorf("dest: %v is different than expected %v", dest, tc.dest) 116 } 117 } 118 t.Run(tc.name, test) 119 } 120 }