github.com/Beeketing/helm@v2.12.1+incompatible/pkg/releasetesting/environment_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 releasetesting
    18  
    19  import (
    20  	"bytes"
    21  	"errors"
    22  	"io"
    23  	"io/ioutil"
    24  	"testing"
    25  
    26  	"k8s.io/helm/pkg/proto/hapi/release"
    27  	"k8s.io/helm/pkg/proto/hapi/services"
    28  	tillerEnv "k8s.io/helm/pkg/tiller/environment"
    29  )
    30  
    31  func TestCreateTestPodSuccess(t *testing.T) {
    32  	env := testEnvFixture()
    33  	test := testFixture()
    34  
    35  	err := env.createTestPod(test)
    36  	if err != nil {
    37  		t.Errorf("Expected no error, got an error: %s", err)
    38  	}
    39  }
    40  
    41  func TestCreateTestPodFailure(t *testing.T) {
    42  	env := testEnvFixture()
    43  	env.KubeClient = newCreateFailingKubeClient()
    44  	test := testFixture()
    45  
    46  	err := env.createTestPod(test)
    47  	if err == nil {
    48  		t.Errorf("Expected error, got no error")
    49  	}
    50  
    51  	if test.result.Info == "" {
    52  		t.Errorf("Expected error to be saved in test result info but found empty string")
    53  	}
    54  
    55  	if test.result.Status != release.TestRun_FAILURE {
    56  		t.Errorf("Expected test result status to be failure but got: %v", test.result.Status)
    57  	}
    58  }
    59  
    60  func TestDeleteTestPods(t *testing.T) {
    61  	mockTestSuite := testSuiteFixture([]string{manifestWithTestSuccessHook})
    62  	mockTestEnv := newMockTestingEnvironment()
    63  	mockTestEnv.KubeClient = newGetFailingKubeClient()
    64  
    65  	mockTestEnv.DeleteTestPods(mockTestSuite.TestManifests)
    66  
    67  	stream := mockTestEnv.Stream.(*mockStream)
    68  	if len(stream.messages) != 0 {
    69  		t.Errorf("Expected 0 errors, got at least one: %v", stream.messages)
    70  	}
    71  
    72  	for _, testManifest := range mockTestSuite.TestManifests {
    73  		if _, err := mockTestEnv.KubeClient.Get(mockTestEnv.Namespace, bytes.NewBufferString(testManifest)); err == nil {
    74  			t.Error("Expected error, got nil")
    75  		}
    76  	}
    77  }
    78  
    79  func TestDeleteTestPodsFailingDelete(t *testing.T) {
    80  	mockTestSuite := testSuiteFixture([]string{manifestWithTestSuccessHook})
    81  	mockTestEnv := newMockTestingEnvironment()
    82  	mockTestEnv.KubeClient = newDeleteFailingKubeClient()
    83  
    84  	mockTestEnv.DeleteTestPods(mockTestSuite.TestManifests)
    85  
    86  	stream := mockTestEnv.Stream.(*mockStream)
    87  	if len(stream.messages) != 1 {
    88  		t.Errorf("Expected 1 error, got: %v", len(stream.messages))
    89  	}
    90  }
    91  
    92  func TestStreamMessage(t *testing.T) {
    93  	mockTestEnv := newMockTestingEnvironment()
    94  
    95  	expectedMessage := "testing streamMessage"
    96  	expectedStatus := release.TestRun_SUCCESS
    97  	err := mockTestEnv.streamMessage(expectedMessage, expectedStatus)
    98  	if err != nil {
    99  		t.Errorf("Expected no errors, got 1: %s", err)
   100  	}
   101  
   102  	stream := mockTestEnv.Stream.(*mockStream)
   103  	if len(stream.messages) != 1 {
   104  		t.Errorf("Expected 1 message, got: %v", len(stream.messages))
   105  	}
   106  
   107  	if stream.messages[0].Msg != expectedMessage {
   108  		t.Errorf("Expected message: %s, got: %s", expectedMessage, stream.messages[0])
   109  	}
   110  	if stream.messages[0].Status != expectedStatus {
   111  		t.Errorf("Expected status: %v, got: %v", expectedStatus, stream.messages[0].Status)
   112  	}
   113  }
   114  
   115  type MockTestingEnvironment struct {
   116  	*Environment
   117  }
   118  
   119  func newMockTestingEnvironment() *MockTestingEnvironment {
   120  	tEnv := mockTillerEnvironment()
   121  
   122  	return &MockTestingEnvironment{
   123  		Environment: &Environment{
   124  			Namespace:  "default",
   125  			KubeClient: tEnv.KubeClient,
   126  			Timeout:    5,
   127  			Stream:     &mockStream{},
   128  		},
   129  	}
   130  }
   131  
   132  func (mte MockTestingEnvironment) streamRunning(name string) error       { return nil }
   133  func (mte MockTestingEnvironment) streamError(info string) error         { return nil }
   134  func (mte MockTestingEnvironment) streamFailed(name string) error        { return nil }
   135  func (mte MockTestingEnvironment) streamSuccess(name string) error       { return nil }
   136  func (mte MockTestingEnvironment) streamUnknown(name, info string) error { return nil }
   137  func (mte MockTestingEnvironment) streamMessage(msg string, status release.TestRun_Status) error {
   138  	mte.Stream.Send(&services.TestReleaseResponse{Msg: msg, Status: status})
   139  	return nil
   140  }
   141  
   142  type getFailingKubeClient struct {
   143  	tillerEnv.PrintingKubeClient
   144  }
   145  
   146  func newGetFailingKubeClient() *getFailingKubeClient {
   147  	return &getFailingKubeClient{
   148  		PrintingKubeClient: tillerEnv.PrintingKubeClient{Out: ioutil.Discard},
   149  	}
   150  }
   151  
   152  func (p *getFailingKubeClient) Get(ns string, r io.Reader) (string, error) {
   153  	return "", errors.New("in the end, they did not find Nemo")
   154  }
   155  
   156  type deleteFailingKubeClient struct {
   157  	tillerEnv.PrintingKubeClient
   158  }
   159  
   160  func newDeleteFailingKubeClient() *deleteFailingKubeClient {
   161  	return &deleteFailingKubeClient{
   162  		PrintingKubeClient: tillerEnv.PrintingKubeClient{Out: ioutil.Discard},
   163  	}
   164  }
   165  
   166  func (p *deleteFailingKubeClient) Delete(ns string, r io.Reader) error {
   167  	return errors.New("delete failed")
   168  }
   169  
   170  type createFailingKubeClient struct {
   171  	tillerEnv.PrintingKubeClient
   172  }
   173  
   174  func newCreateFailingKubeClient() *createFailingKubeClient {
   175  	return &createFailingKubeClient{
   176  		PrintingKubeClient: tillerEnv.PrintingKubeClient{Out: ioutil.Discard},
   177  	}
   178  }
   179  
   180  func (p *createFailingKubeClient) Create(ns string, r io.Reader, t int64, shouldWait bool) error {
   181  	return errors.New("We ran out of budget and couldn't create finding-nemo")
   182  }