/
fake_cmd_context.go
152 lines (130 loc) · 4.39 KB
/
fake_cmd_context.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
// Copyright (c) 2022, 2023, Oracle and/or its affiliates.
// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
package helpers
import (
"bytes"
"encoding/json"
"fmt"
"io"
"k8s.io/client-go/discovery"
discoveryFake "k8s.io/client-go/discovery/fake"
"net/http"
"os"
"strings"
"time"
"github.com/spf13/cobra"
"github.com/verrazzano/verrazzano/tools/vz/pkg/github"
"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/fake"
"sigs.k8s.io/controller-runtime/pkg/client"
)
type FakeRootCmdContext struct {
client client.Client
kubeClient kubernetes.Interface
dynamicClient dynamic.Interface
genericclioptions.IOStreams
}
// GetOutputStream - return the output stream
func (rc *FakeRootCmdContext) GetOutputStream() io.Writer {
return rc.IOStreams.Out
}
// GetErrorStream - return the error stream
func (rc *FakeRootCmdContext) GetErrorStream() io.Writer {
return rc.IOStreams.ErrOut
}
// GetInputStream - return the input stream
func (rc *FakeRootCmdContext) GetInputStream() io.Reader {
return rc.IOStreams.In
}
// GetClient - return a controller runtime client that supports the schemes used by the CLI
func (rc *FakeRootCmdContext) GetClient(cmd *cobra.Command) (client.Client, error) {
return rc.client, nil
}
// GetKubeClient - return a Kubernetes clientset for use with the fake go-client
func (rc *FakeRootCmdContext) GetKubeClient(cmd *cobra.Command) (kubernetes.Interface, error) {
return rc.kubeClient, nil
}
// SetClient - set the client
func (rc *FakeRootCmdContext) SetClient(client client.Client) {
rc.client = client
}
// RoundTripFunc - define the type for the Transport function
type RoundTripFunc func(req *http.Request) *http.Response
// RoundTrip - define the implementation for the Transport function
func (f RoundTripFunc) RoundTrip(req *http.Request) (*http.Response, error) {
return f(req), nil
}
// GetHTTPClient - return an HTTP client for testing that always responds with a 200
// and a pre-defined list of releases
func (rc *FakeRootCmdContext) GetHTTPClient() *http.Client {
// Predefined response for the list of releases
releaseResponse := []github.ReleaseAsset{
{
TagName: "v1.3.0",
},
{
TagName: "v1.2.0",
},
{
TagName: "v1.3.1",
},
}
jsonResp, _ := json.Marshal(releaseResponse)
// Predefined response for getting operator.yaml
jsonOperResp, err := os.ReadFile("../../test/testdata/operator-file-fake.yaml")
if err != nil {
panic(err)
}
return &http.Client{
Timeout: time.Second * 30,
Transport: RoundTripFunc(func(req *http.Request) *http.Response {
if strings.Contains(req.URL.Path, "/releases/download") {
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewBuffer(jsonOperResp)),
Header: http.Header{"Content-Type": {"application/octet-stream"}},
}
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewBuffer(jsonResp)),
Header: http.Header{"Content-Type": {"application/json"}},
}
}),
}
}
// GetDynamicClient - return a dynamic client for use with the fake go-client
func (rc *FakeRootCmdContext) GetDynamicClient(cmd *cobra.Command) (dynamic.Interface, error) {
return rc.dynamicClient, nil
}
// SetDynamicClient - set a dynamic client for use with the fake go-client (used for testing)
func (rc *FakeRootCmdContext) SetDynamicClient(dynClient dynamic.Interface) {
rc.dynamicClient = dynClient
}
func NewFakeRootCmdContext(streams genericclioptions.IOStreams) *FakeRootCmdContext {
return &FakeRootCmdContext{
IOStreams: streams,
kubeClient: fake.NewSimpleClientset(),
}
}
func (rc *FakeRootCmdContext) GetDiscoveryClient(cmd *cobra.Command) (discovery.DiscoveryInterface, error) {
client := rc.kubeClient
discoveryClient, ok := client.Discovery().(*discoveryFake.FakeDiscovery)
if !ok {
return nil, fmt.Errorf("DiscoveryClient was not successfully created")
}
return discoveryClient, nil
}
func (rc *FakeRootCmdContext) VerifyCLIArgsNil(cmd *cobra.Command) error {
cmd.Args = func(cmd *cobra.Command, args []string) error {
// In unit-tests the TestName is an arg. So to avoid tests failing because of the TestName
// we're now checking that the arg list is greater than 1.
if len(args) > 1 {
return fmt.Errorf("invalid arguments specified: %s", args)
}
return nil
}
return nil
}