-
Notifications
You must be signed in to change notification settings - Fork 4
/
fake_client.go
176 lines (160 loc) · 3.35 KB
/
fake_client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package cluster
import (
"context"
"errors"
"fmt"
"github.com/segmentio/terraform-provider-kubeapply/pkg/cluster/diff"
)
var _ Client = (*FakeClient)(nil)
// FakeClient is a fake implementation of a Client. For testing purposes only.
type FakeClient struct {
clusterConfig *Config
subpathOverride string
kubectlErr error
NoDiffs bool
Calls []FakeClientCall
}
// FakeClientCall records a call that was made using the FakeClient.
type FakeClientCall struct {
CallType string
Paths []string
}
// NewFakeClient returns a FakeClient that works without errors.
func NewFakeClient(
ctx context.Context,
config *ClientConfig,
) (Client, error) {
return &FakeClient{
clusterConfig: config.Config,
}, nil
}
// NewFakeClientError returns a FakeClient that simulates an error when
// running kubectl.
func NewFakeClientError(
ctx context.Context,
config *ClientConfig,
) (Client, error) {
return &FakeClient{
clusterConfig: config.Config,
kubectlErr: errors.New("kubectl error"),
}, nil
}
// Apply runs a fake apply using the configs in the argument path.
func (cc *FakeClient) Apply(
ctx context.Context,
paths []string,
serverSide bool,
) ([]byte, error) {
cc.Calls = append(
cc.Calls,
FakeClientCall{
CallType: "Apply",
Paths: paths,
},
)
return []byte(
fmt.Sprintf(
"apply result for %s with paths %+v",
cc.clusterConfig.Cluster,
paths,
),
),
cc.kubectlErr
}
// Delete deletes the resources associated with one or more configs.
func (cc *FakeClient) Delete(
ctx context.Context,
ids []string,
) ([]byte, error) {
cc.Calls = append(
cc.Calls,
FakeClientCall{
CallType: "Delete",
Paths: ids,
},
)
return []byte(
fmt.Sprintf(
"delete result for %s with ids %+v",
cc.clusterConfig.Cluster,
ids,
),
),
cc.kubectlErr
}
// Diff gets the diffs between the configs at the given path and the actual state of resources
// in the cluster. It returns the raw output.
func (cc *FakeClient) Diff(
ctx context.Context,
paths []string,
serverSide bool,
) ([]byte, error) {
cc.Calls = append(
cc.Calls,
FakeClientCall{
CallType: "Diff",
Paths: paths,
},
)
if cc.NoDiffs {
return []byte{}, nil
}
return []byte(
fmt.Sprintf(
"diff result for %s with paths %+v",
cc.clusterConfig.Cluster,
paths,
),
),
cc.kubectlErr
}
// DiffStructured gets the diffs between the configs at the given path and the actual state of
// resources in the cluster. It returns structured output.
func (cc *FakeClient) DiffStructured(
ctx context.Context,
paths []string,
serverSide bool,
) ([]diff.Result, error) {
cc.Calls = append(
cc.Calls,
FakeClientCall{
CallType: "DiffStructured",
Paths: paths,
},
)
if cc.NoDiffs {
return []diff.Result{}, nil
}
return []diff.Result{
{
Name: "result",
RawDiff: fmt.Sprintf(
// Don't include paths since this can lead to terraform diff
// instability
"structured diff result for %s",
cc.clusterConfig.Cluster,
),
},
},
cc.kubectlErr
}
// Config returns this client's cluster config.
func (cc *FakeClient) Config() *Config {
cc.Calls = append(
cc.Calls,
FakeClientCall{
CallType: "Config",
},
)
return cc.clusterConfig
}
// Close closes the client.
func (cc *FakeClient) Close() error {
cc.Calls = append(
cc.Calls,
FakeClientCall{
CallType: "Close",
},
)
return nil
}