github.com/latiif/helm@v2.15.0+incompatible/pkg/helm/portforwarder/portforwarder_test.go (about)

     1  /*
     2  Copyright The Helm 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 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.CoreV1(), 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  }
    88  
    89  func TestGetTillerPodImage(t *testing.T) {
    90  	tests := []struct {
    91  		name     string
    92  		podSpec  v1.PodSpec
    93  		expected string
    94  		err      bool
    95  	}{
    96  		{
    97  			name: "pod with tiller container image",
    98  			podSpec: v1.PodSpec{
    99  				Containers: []v1.Container{
   100  					{
   101  						Name:  "tiller",
   102  						Image: "gcr.io/kubernetes-helm/tiller:v2.0.0",
   103  					},
   104  				},
   105  			},
   106  			expected: "gcr.io/kubernetes-helm/tiller:v2.0.0",
   107  			err:      false,
   108  		},
   109  		{
   110  			name: "pod without tiller container image",
   111  			podSpec: v1.PodSpec{
   112  				Containers: []v1.Container{
   113  					{
   114  						Name:  "not_tiller",
   115  						Image: "gcr.io/kubernetes-helm/not_tiller:v1.0.0",
   116  					},
   117  				},
   118  			},
   119  			expected: "",
   120  			err:      true,
   121  		},
   122  	}
   123  
   124  	for _, tt := range tests {
   125  		t.Run(tt.name, func(t *testing.T) {
   126  			mockPod := mockTillerPod()
   127  			mockPod.Spec = tt.podSpec
   128  			client := fake.NewSimpleClientset(&v1.PodList{Items: []v1.Pod{mockPod}})
   129  			imageName, err := GetTillerPodImage(client.CoreV1(), v1.NamespaceDefault)
   130  			if (err != nil) != tt.err {
   131  				t.Errorf("%q. expected error: %v, got %v", tt.name, tt.err, err)
   132  			}
   133  			if imageName != tt.expected {
   134  				t.Errorf("%q. expected %q, got %q", tt.name, tt.expected, imageName)
   135  			}
   136  		})
   137  	}
   138  }