...

Source file src/helm.sh/helm/v3/pkg/kube/fake/fake.go

Documentation: helm.sh/helm/v3/pkg/kube/fake

     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  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    26  	"k8s.io/apimachinery/pkg/runtime"
    27  	"k8s.io/cli-runtime/pkg/resource"
    28  
    29  	"helm.sh/helm/v3/pkg/kube"
    30  )
    31  
    32  // FailingKubeClient implements KubeClient for testing purposes. It also has
    33  // additional errors you can set to fail different functions, otherwise it
    34  // delegates all its calls to `PrintingKubeClient`
    35  type FailingKubeClient struct {
    36  	PrintingKubeClient
    37  	CreateError                      error
    38  	GetError                         error
    39  	WaitError                        error
    40  	DeleteError                      error
    41  	DeleteWithPropagationError       error
    42  	WatchUntilReadyError             error
    43  	UpdateError                      error
    44  	BuildError                       error
    45  	BuildTableError                  error
    46  	BuildDummy                       bool
    47  	BuildUnstructuredError           error
    48  	WaitAndGetCompletedPodPhaseError error
    49  	WaitDuration                     time.Duration
    50  }
    51  
    52  // Create returns the configured error if set or prints
    53  func (f *FailingKubeClient) Create(resources kube.ResourceList) (*kube.Result, error) {
    54  	if f.CreateError != nil {
    55  		return nil, f.CreateError
    56  	}
    57  	return f.PrintingKubeClient.Create(resources)
    58  }
    59  
    60  // Get returns the configured error if set or prints
    61  func (f *FailingKubeClient) Get(resources kube.ResourceList, related bool) (map[string][]runtime.Object, error) {
    62  	if f.GetError != nil {
    63  		return nil, f.GetError
    64  	}
    65  	return f.PrintingKubeClient.Get(resources, related)
    66  }
    67  
    68  // Waits the amount of time defined on f.WaitDuration, then returns the configured error if set or prints.
    69  func (f *FailingKubeClient) Wait(resources kube.ResourceList, d time.Duration) error {
    70  	time.Sleep(f.WaitDuration)
    71  	if f.WaitError != nil {
    72  		return f.WaitError
    73  	}
    74  	return f.PrintingKubeClient.Wait(resources, d)
    75  }
    76  
    77  // WaitWithJobs returns the configured error if set or prints
    78  func (f *FailingKubeClient) WaitWithJobs(resources kube.ResourceList, d time.Duration) error {
    79  	if f.WaitError != nil {
    80  		return f.WaitError
    81  	}
    82  	return f.PrintingKubeClient.WaitWithJobs(resources, d)
    83  }
    84  
    85  // WaitForDelete returns the configured error if set or prints
    86  func (f *FailingKubeClient) WaitForDelete(resources kube.ResourceList, d time.Duration) error {
    87  	if f.WaitError != nil {
    88  		return f.WaitError
    89  	}
    90  	return f.PrintingKubeClient.WaitForDelete(resources, d)
    91  }
    92  
    93  // Delete returns the configured error if set or prints
    94  func (f *FailingKubeClient) Delete(resources kube.ResourceList) (*kube.Result, []error) {
    95  	if f.DeleteError != nil {
    96  		return nil, []error{f.DeleteError}
    97  	}
    98  	return f.PrintingKubeClient.Delete(resources)
    99  }
   100  
   101  // WatchUntilReady returns the configured error if set or prints
   102  func (f *FailingKubeClient) WatchUntilReady(resources kube.ResourceList, d time.Duration) error {
   103  	if f.WatchUntilReadyError != nil {
   104  		return f.WatchUntilReadyError
   105  	}
   106  	return f.PrintingKubeClient.WatchUntilReady(resources, d)
   107  }
   108  
   109  // Update returns the configured error if set or prints
   110  func (f *FailingKubeClient) Update(r, modified kube.ResourceList, ignoreMe bool) (*kube.Result, error) {
   111  	if f.UpdateError != nil {
   112  		return &kube.Result{}, f.UpdateError
   113  	}
   114  	return f.PrintingKubeClient.Update(r, modified, ignoreMe)
   115  }
   116  
   117  // Build returns the configured error if set or prints
   118  func (f *FailingKubeClient) Build(r io.Reader, _ bool) (kube.ResourceList, error) {
   119  	if f.BuildError != nil {
   120  		return []*resource.Info{}, f.BuildError
   121  	}
   122  	if f.BuildDummy {
   123  		return createDummyResourceList(), nil
   124  	}
   125  	return f.PrintingKubeClient.Build(r, false)
   126  }
   127  
   128  // BuildTable returns the configured error if set or prints
   129  func (f *FailingKubeClient) BuildTable(r io.Reader, _ bool) (kube.ResourceList, error) {
   130  	if f.BuildTableError != nil {
   131  		return []*resource.Info{}, f.BuildTableError
   132  	}
   133  	return f.PrintingKubeClient.BuildTable(r, false)
   134  }
   135  
   136  // WaitAndGetCompletedPodPhase returns the configured error if set or prints
   137  func (f *FailingKubeClient) WaitAndGetCompletedPodPhase(s string, d time.Duration) (v1.PodPhase, error) {
   138  	if f.WaitAndGetCompletedPodPhaseError != nil {
   139  		return v1.PodSucceeded, f.WaitAndGetCompletedPodPhaseError
   140  	}
   141  	return f.PrintingKubeClient.WaitAndGetCompletedPodPhase(s, d)
   142  }
   143  
   144  // DeleteWithPropagationPolicy returns the configured error if set or prints
   145  func (f *FailingKubeClient) DeleteWithPropagationPolicy(resources kube.ResourceList, policy metav1.DeletionPropagation) (*kube.Result, []error) {
   146  	if f.DeleteWithPropagationError != nil {
   147  		return nil, []error{f.DeleteWithPropagationError}
   148  	}
   149  	return f.PrintingKubeClient.DeleteWithPropagationPolicy(resources, policy)
   150  }
   151  
   152  func createDummyResourceList() kube.ResourceList {
   153  	var resInfo resource.Info
   154  	resInfo.Name = "dummyName"
   155  	resInfo.Namespace = "dummyNamespace"
   156  	var resourceList kube.ResourceList
   157  	resourceList.Append(&resInfo)
   158  	return resourceList
   159  
   160  }
   161  

View as plain text