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