...
1
16
17
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
33
34
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
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
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
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
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
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
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
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
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
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
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
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
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