github.com/cloudposse/helm@v2.2.3+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/kubernetes/pkg/api"
    23  	"k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/fake"
    24  )
    25  
    26  func mockTillerPod() api.Pod {
    27  	return api.Pod{
    28  		ObjectMeta: api.ObjectMeta{
    29  			Name:      "orca",
    30  			Namespace: api.NamespaceDefault,
    31  			Labels:    map[string]string{"app": "helm", "name": "tiller"},
    32  		},
    33  		Status: api.PodStatus{
    34  			Phase: api.PodRunning,
    35  			Conditions: []api.PodCondition{
    36  				{
    37  					Status: api.ConditionTrue,
    38  					Type:   api.PodReady,
    39  				},
    40  			},
    41  		},
    42  	}
    43  }
    44  
    45  func mockTillerPodPending() api.Pod {
    46  	p := mockTillerPod()
    47  	p.Name = "blue"
    48  	p.Status.Conditions[0].Status = api.ConditionFalse
    49  	return p
    50  }
    51  
    52  func TestGetFirstPod(t *testing.T) {
    53  	tests := []struct {
    54  		name     string
    55  		pods     []api.Pod
    56  		expected string
    57  		err      bool
    58  	}{
    59  		{
    60  			name:     "with a ready pod",
    61  			pods:     []api.Pod{mockTillerPod()},
    62  			expected: "orca",
    63  		},
    64  		{
    65  			name: "without a ready pod",
    66  			pods: []api.Pod{mockTillerPodPending()},
    67  			err:  true,
    68  		},
    69  		{
    70  			name: "without a pod",
    71  			pods: []api.Pod{},
    72  			err:  true,
    73  		},
    74  	}
    75  
    76  	for _, tt := range tests {
    77  		client := fake.NewSimpleClientset(&api.PodList{Items: tt.pods})
    78  		name, err := getTillerPodName(client.Core(), api.NamespaceDefault)
    79  		if (err != nil) != tt.err {
    80  			t.Errorf("%q. expected error: %v, got %v", tt.name, tt.err, err)
    81  		}
    82  		if name != tt.expected {
    83  			t.Errorf("%q. expected %q, got %q", tt.name, tt.expected, name)
    84  		}
    85  	}
    86  }