github.com/stefanmcshane/helm@v0.0.0-20221213002717-88a4a2c6e77d/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  	"github.com/stefanmcshane/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  	WaitDuration                     time.Duration
    44  }
    45  
    46  // Create returns the configured error if set or prints
    47  func (f *FailingKubeClient) Create(resources kube.ResourceList) (*kube.Result, error) {
    48  	if f.CreateError != nil {
    49  		return nil, f.CreateError
    50  	}
    51  	return f.PrintingKubeClient.Create(resources)
    52  }
    53  
    54  // Waits the amount of time defined on f.WaitDuration, then returns the configured error if set or prints.
    55  func (f *FailingKubeClient) Wait(resources kube.ResourceList, d time.Duration) error {
    56  	time.Sleep(f.WaitDuration)
    57  	if f.WaitError != nil {
    58  		return f.WaitError
    59  	}
    60  	return f.PrintingKubeClient.Wait(resources, d)
    61  }
    62  
    63  // WaitWithJobs returns the configured error if set or prints
    64  func (f *FailingKubeClient) WaitWithJobs(resources kube.ResourceList, d time.Duration) error {
    65  	if f.WaitError != nil {
    66  		return f.WaitError
    67  	}
    68  	return f.PrintingKubeClient.WaitWithJobs(resources, d)
    69  }
    70  
    71  // WaitForDelete returns the configured error if set or prints
    72  func (f *FailingKubeClient) WaitForDelete(resources kube.ResourceList, d time.Duration) error {
    73  	if f.WaitError != nil {
    74  		return f.WaitError
    75  	}
    76  	return f.PrintingKubeClient.WaitForDelete(resources, d)
    77  }
    78  
    79  // Delete returns the configured error if set or prints
    80  func (f *FailingKubeClient) Delete(resources kube.ResourceList) (*kube.Result, []error) {
    81  	if f.DeleteError != nil {
    82  		return nil, []error{f.DeleteError}
    83  	}
    84  	return f.PrintingKubeClient.Delete(resources)
    85  }
    86  
    87  // WatchUntilReady returns the configured error if set or prints
    88  func (f *FailingKubeClient) WatchUntilReady(resources kube.ResourceList, d time.Duration) error {
    89  	if f.WatchUntilReadyError != nil {
    90  		return f.WatchUntilReadyError
    91  	}
    92  	return f.PrintingKubeClient.WatchUntilReady(resources, d)
    93  }
    94  
    95  // Update returns the configured error if set or prints
    96  func (f *FailingKubeClient) Update(r, modified kube.ResourceList, ignoreMe bool) (*kube.Result, error) {
    97  	if f.UpdateError != nil {
    98  		return &kube.Result{}, f.UpdateError
    99  	}
   100  	return f.PrintingKubeClient.Update(r, modified, ignoreMe)
   101  }
   102  
   103  // Build returns the configured error if set or prints
   104  func (f *FailingKubeClient) Build(r io.Reader, _ bool) (kube.ResourceList, error) {
   105  	if f.BuildError != nil {
   106  		return []*resource.Info{}, f.BuildError
   107  	}
   108  	return f.PrintingKubeClient.Build(r, false)
   109  }
   110  
   111  // WaitAndGetCompletedPodPhase returns the configured error if set or prints
   112  func (f *FailingKubeClient) WaitAndGetCompletedPodPhase(s string, d time.Duration) (v1.PodPhase, error) {
   113  	if f.WaitAndGetCompletedPodPhaseError != nil {
   114  		return v1.PodSucceeded, f.WaitAndGetCompletedPodPhaseError
   115  	}
   116  	return f.PrintingKubeClient.WaitAndGetCompletedPodPhase(s, d)
   117  }