github.com/jenkins-x/test-infra@v0.0.7/kubetest/kubernetes_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 "testing" 21 ) 22 23 func Test_isReady(t *testing.T) { 24 grid := []struct { 25 node *node 26 expected bool 27 }{ 28 { 29 node: &node{ 30 Status: nodeStatus{ 31 Conditions: []nodeCondition{}, 32 }, 33 }, 34 expected: false, 35 }, 36 { 37 node: &node{ 38 Status: nodeStatus{ 39 Conditions: []nodeCondition{ 40 {Type: "Ready", Status: "True"}, 41 }, 42 }, 43 }, 44 expected: true, 45 }, 46 { 47 node: &node{ 48 Status: nodeStatus{ 49 Conditions: []nodeCondition{ 50 {Type: "Ready", Status: "False"}, 51 }, 52 }, 53 }, 54 expected: false, 55 }, 56 { 57 node: &node{ 58 Status: nodeStatus{ 59 Conditions: []nodeCondition{ 60 {Type: "Ready", Status: "Unknown"}, 61 }, 62 }, 63 }, 64 expected: false, 65 }, 66 { 67 node: &node{ 68 Status: nodeStatus{ 69 Conditions: []nodeCondition{ 70 {Type: "Ready", Status: "not-a-known-value"}, 71 }, 72 }, 73 }, 74 expected: false, 75 }, 76 { 77 node: &node{ 78 Status: nodeStatus{ 79 Conditions: []nodeCondition{ 80 {Type: "Random", Status: "True"}, 81 }, 82 }, 83 }, 84 expected: false, 85 }, 86 { 87 node: &node{ 88 Status: nodeStatus{ 89 Conditions: []nodeCondition{ 90 {Type: "Random", Status: "Unknown"}, 91 }, 92 }, 93 }, 94 expected: false, 95 }, 96 { 97 node: &node{ 98 Status: nodeStatus{ 99 Conditions: []nodeCondition{ 100 {Type: "Random", Status: "True"}, 101 {Type: "Ready", Status: "True"}, 102 }, 103 }, 104 }, 105 expected: true, 106 }, 107 { 108 node: &node{ 109 Status: nodeStatus{ 110 Conditions: []nodeCondition{}, 111 }, 112 }, 113 expected: false, 114 }, 115 { 116 node: &node{ 117 Status: nodeStatus{}, 118 }, 119 expected: false, 120 }, 121 { 122 node: &node{}, 123 expected: false, 124 }, 125 } 126 127 for _, g := range grid { 128 actual := isReady(g.node) 129 130 if actual != g.expected { 131 t.Errorf("unexpected isReady. actual=%v, expected=%v", actual, g.expected) 132 continue 133 } 134 } 135 } 136 137 func Test_countReadyNodes(t *testing.T) { 138 readyNode := node{ 139 Status: nodeStatus{ 140 Conditions: []nodeCondition{ 141 {Type: "Ready", Status: "True"}, 142 }, 143 }, 144 } 145 notReadyNode := node{ 146 Status: nodeStatus{ 147 Conditions: []nodeCondition{ 148 {Type: "Ready", Status: "False"}, 149 }, 150 }, 151 } 152 153 grid := []struct { 154 nodes []node 155 expected int 156 }{ 157 { 158 nodes: []node{readyNode, readyNode, readyNode}, 159 expected: 3, 160 }, 161 { 162 nodes: []node{notReadyNode, notReadyNode, notReadyNode}, 163 expected: 0, 164 }, 165 { 166 nodes: []node{}, 167 expected: 0, 168 }, 169 { 170 nodes: nil, 171 expected: 0, 172 }, 173 { 174 nodes: []node{readyNode, notReadyNode, readyNode}, 175 expected: 2, 176 }, 177 { 178 nodes: []node{notReadyNode, readyNode}, 179 expected: 1, 180 }, 181 } 182 183 for _, g := range grid { 184 actual := countReadyNodes(&nodeList{Items: g.nodes}) 185 if actual != g.expected { 186 t.Errorf("unexpected countReadyNodes. actual=%v, expected=%v", actual, g.expected) 187 continue 188 } 189 } 190 }