-
Notifications
You must be signed in to change notification settings - Fork 23
/
stub.go
173 lines (146 loc) · 5.36 KB
/
stub.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
// Copyright The Enterprise Contract Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0
package stub
import (
"context"
"errors"
"fmt"
"os"
"strings"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/tools/clientcmd/api"
"github.com/enterprise-contract/ec-cli/acceptance/git"
"github.com/enterprise-contract/ec-cli/acceptance/kubernetes/types"
"github.com/enterprise-contract/ec-cli/acceptance/registry"
"github.com/enterprise-contract/ec-cli/acceptance/wiremock"
)
type stubCluster struct {
}
// stubApiserverRunning starts the stub apiserver using WireMock
func Start(ctx context.Context) (context.Context, types.Cluster, error) {
ctx, err := wiremock.StartWiremock(ctx)
return ctx, stubCluster{}, err
}
func (s stubCluster) CreateNamespace(ctx context.Context) (context.Context, error) {
// no-op, we can record or stub any namespace API calls if/when needed
return ctx, nil
}
// CreateNamedPolicy stubs a response from the apiserver to fetch a EnterpriseContractPolicy
// custom resource from the `acceptance` namespace with the given name and specification
// the specification part can be templated using ${...} notation and supports
// `GITHOST` and `REGISTRY` variable substitution
func (s stubCluster) CreateNamedPolicy(ctx context.Context, name string, specification string) error {
ns := "acceptance" // TODO: namespace support
return wiremock.StubFor(ctx, wiremock.Get(wiremock.URLPathEqualTo(fmt.Sprintf("/apis/appstudio.redhat.com/v1alpha1/namespaces/%s/enterprisecontractpolicies/%s", ns, name))).
WillReturnResponse(wiremock.NewResponse().WithBody(fmt.Sprintf(`{
"apiVersion": "appstudio.redhat.com/v1alpha1",
"kind": "EnterpriseContractPolicy",
"metadata": {
"name": "%s",
"namespace": "%s"
},
"spec": %s
}`, name, ns, os.Expand(specification, func(key string) string {
switch key {
case "GITHOST":
return git.Host(ctx)
case "REGISTRY":
uri, err := registry.Url(ctx)
if err != nil {
panic(err)
}
return uri
}
return ""
}))).WithHeaders(map[string]string{"Content-Type": "application/json"}).WithStatus(200)))
}
// CreateNamedSnapshot stubs a response from the apiserver to fetch a Snapshot
// custom resource from the `acceptance` namespace with the given name and specification
func (s stubCluster) CreateNamedSnapshot(ctx context.Context, name string, specification string) error {
ns := "acceptance"
return wiremock.StubFor(ctx, wiremock.Get(wiremock.URLPathEqualTo(fmt.Sprintf("/apis/appstudio.redhat.com/v1alpha1/namespaces/%s/snapshots/%s", ns, name))).
WillReturnResponse(wiremock.NewResponse().WithBody(fmt.Sprintf(`{
"apiVersion": "appstudio.redhat.com/v1alpha1",
"kind": "Snapshot",
"metadata": {
"name": "%s",
"namespace": "%s"
},
"spec": %s
}`, name, ns, os.Expand(specification, func(key string) string {
if key == "REGISTRY" {
if registryUrl, err := registry.StubRegistry(ctx); err != nil {
panic("No stub registry state, did you run the stub?")
} else {
return registryUrl
}
}
return ""
}))).WithHeaders(map[string]string{"Content-Type": "application/json"}).WithStatus(200)))
}
func (s stubCluster) CreatePolicy(_ context.Context, _ string) error {
return errors.New("use `Given policy configuration named \"<name>\" with specification` when using the stub Kubernetes")
}
func (s stubCluster) RunTask(_ context.Context, _, _, _ string, _ map[string]string) error {
return errors.New("can't run tasks when using the stub Kubernetes")
}
func (s stubCluster) AwaitUntilTaskIsDone(context.Context) (bool, error) {
return false, errors.New("can't run tasks when using the stub Kubernetes")
}
func (s stubCluster) TaskInfo(context.Context) (*types.TaskInfo, error) {
return nil, errors.New("can't run tasks when using the stub Kubernetes")
}
// KubeConfig returns a valid kubeconfig configuration file in YAML format that
// points to the stubbed apiserver and uses no authentication
func (s stubCluster) KubeConfig(ctx context.Context) (string, error) {
endpoint, err := wiremock.Endpoint(ctx)
if err != nil {
return "", err
}
server := strings.Replace(endpoint, "localhost", "apiserver.localhost", 1)
if err != nil {
return "", err
}
cluster := "my-cluster"
context := "my-context"
kubeconfig := api.Config{
CurrentContext: context,
Clusters: map[string]*api.Cluster{
cluster: {
Server: server,
},
},
Contexts: map[string]*api.Context{
context: {
Cluster: cluster,
},
},
}
b, err := clientcmd.Write(kubeconfig)
if err != nil {
return "", err
}
return string(b), nil
}
func (s stubCluster) Up(ctx context.Context) bool {
return wiremock.IsRunning(ctx)
}
func (s stubCluster) Stop(ctx context.Context) (context.Context, error) {
return ctx, nil
}
func (s stubCluster) Registry(ctx context.Context) (string, error) {
return registry.Url(ctx)
}