github.com/darkowlzz/helm@v2.5.1-0.20171213183701-6707fe0468d4+incompatible/pkg/helm/portforwarder/portforwarder_test.go (about) 1 /* 2 Copyright 2016 The Kubernetes Authors All rights reserved. 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 portforwarder 18 19 import ( 20 "testing" 21 22 "k8s.io/api/core/v1" 23 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 24 "k8s.io/client-go/kubernetes/fake" 25 ) 26 27 func mockTillerPod() v1.Pod { 28 return v1.Pod{ 29 ObjectMeta: metav1.ObjectMeta{ 30 Name: "orca", 31 Namespace: v1.NamespaceDefault, 32 Labels: tillerPodLabels, 33 }, 34 Status: v1.PodStatus{ 35 Phase: v1.PodRunning, 36 Conditions: []v1.PodCondition{ 37 { 38 Status: v1.ConditionTrue, 39 Type: v1.PodReady, 40 }, 41 }, 42 }, 43 } 44 } 45 46 func mockTillerPodPending() v1.Pod { 47 p := mockTillerPod() 48 p.Name = "blue" 49 p.Status.Conditions[0].Status = v1.ConditionFalse 50 return p 51 } 52 53 func TestGetFirstPod(t *testing.T) { 54 tests := []struct { 55 name string 56 pods []v1.Pod 57 expected string 58 err bool 59 }{ 60 { 61 name: "with a ready pod", 62 pods: []v1.Pod{mockTillerPod()}, 63 expected: "orca", 64 }, 65 { 66 name: "without a ready pod", 67 pods: []v1.Pod{mockTillerPodPending()}, 68 err: true, 69 }, 70 { 71 name: "without a pod", 72 pods: []v1.Pod{}, 73 err: true, 74 }, 75 } 76 77 for _, tt := range tests { 78 client := fake.NewSimpleClientset(&v1.PodList{Items: tt.pods}) 79 name, err := getTillerPodName(client.Core(), v1.NamespaceDefault) 80 if (err != nil) != tt.err { 81 t.Errorf("%q. expected error: %v, got %v", tt.name, tt.err, err) 82 } 83 if name != tt.expected { 84 t.Errorf("%q. expected %q, got %q", tt.name, tt.expected, name) 85 } 86 } 87 }