github.com/uhthomas/helm@v3.0.0-beta.3+incompatible/pkg/kube/fake/fake.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 fake implements various fake KubeClients for use in testing
    18  package fake
    19  
    20  import (
    21  	"io"
    22  	"time"
    23  
    24  	v1 "k8s.io/api/core/v1"
    25  	"k8s.io/cli-runtime/pkg/resource"
    26  
    27  	"helm.sh/helm/pkg/kube"
    28  )
    29  
    30  // FailingKubeClient implements KubeClient for testing purposes. It also has
    31  // additional errors you can set to fail different functions, otherwise it
    32  // delegates all its calls to `PrintingKubeClient`
    33  type FailingKubeClient struct {
    34  	PrintingKubeClient
    35  	CreateError                      error
    36  	WaitError                        error
    37  	DeleteError                      error
    38  	WatchUntilReadyError             error
    39  	UpdateError                      error
    40  	BuildError                       error
    41  	BuildUnstructuredError           error
    42  	WaitAndGetCompletedPodPhaseError error
    43  }
    44  
    45  // Create returns the configured error if set or prints
    46  func (f *FailingKubeClient) Create(resources kube.ResourceList) (*kube.Result, error) {
    47  	if f.CreateError != nil {
    48  		return nil, f.CreateError
    49  	}
    50  	return f.PrintingKubeClient.Create(resources)
    51  }
    52  
    53  // Wait returns the configured error if set or prints
    54  func (f *FailingKubeClient) Wait(resources kube.ResourceList, d time.Duration) error {
    55  	if f.WaitError != nil {
    56  		return f.WaitError
    57  	}
    58  	return f.PrintingKubeClient.Wait(resources, d)
    59  }
    60  
    61  // Delete returns the configured error if set or prints
    62  func (f *FailingKubeClient) Delete(resources kube.ResourceList) (*kube.Result, []error) {
    63  	if f.DeleteError != nil {
    64  		return nil, []error{f.DeleteError}
    65  	}
    66  	return f.PrintingKubeClient.Delete(resources)
    67  }
    68  
    69  // WatchUntilReady returns the configured error if set or prints
    70  func (f *FailingKubeClient) WatchUntilReady(resources kube.ResourceList, d time.Duration) error {
    71  	if f.WatchUntilReadyError != nil {
    72  		return f.WatchUntilReadyError
    73  	}
    74  	return f.PrintingKubeClient.WatchUntilReady(resources, d)
    75  }
    76  
    77  // Update returns the configured error if set or prints
    78  func (f *FailingKubeClient) Update(r, modified kube.ResourceList, ignoreMe bool) (*kube.Result, error) {
    79  	if f.UpdateError != nil {
    80  		return nil, f.UpdateError
    81  	}
    82  	return f.PrintingKubeClient.Update(r, modified, ignoreMe)
    83  }
    84  
    85  // Build returns the configured error if set or prints
    86  func (f *FailingKubeClient) Build(r io.Reader) (kube.ResourceList, error) {
    87  	if f.BuildError != nil {
    88  		return []*resource.Info{}, f.BuildError
    89  	}
    90  	return f.PrintingKubeClient.Build(r)
    91  }
    92  
    93  // WaitAndGetCompletedPodPhase returns the configured error if set or prints
    94  func (f *FailingKubeClient) WaitAndGetCompletedPodPhase(s string, d time.Duration) (v1.PodPhase, error) {
    95  	if f.WaitAndGetCompletedPodPhaseError != nil {
    96  		return v1.PodSucceeded, f.WaitAndGetCompletedPodPhaseError
    97  	}
    98  	return f.PrintingKubeClient.WaitAndGetCompletedPodPhase(s, d)
    99  }