Skip to content

Commit 49e72f6

Browse files
authored
Merge pull request #267 from gibizer/fake-keystone-api
Add KeystoneAPI simulator
2 parents e4c3100 + 2ca3d11 commit 49e72f6

File tree

6 files changed

+523
-1
lines changed

6 files changed

+523
-1
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
Copyright 2023 Red Hat
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
14+
package apis
15+
16+
import (
17+
"fmt"
18+
"net/http"
19+
20+
"github.com/go-logr/logr"
21+
)
22+
23+
// Handler defines which URL patter is handled by which function
24+
type Handler struct {
25+
// Pattern is the request URL to handle. If two patters are matching the
26+
// same request then the handler for the longer pattern will be executed.
27+
// Using the same pattern in two handlers will cause a panic.
28+
Pattern string
29+
// Func the the function that handles the request by writing a response
30+
Func func(http.ResponseWriter, *http.Request)
31+
}
32+
33+
// APIFixture is a base struct to implement OpenStack API simulators for the
34+
// EnvTest.
35+
type APIFixture struct {
36+
log logr.Logger
37+
server *FakeAPIServer
38+
ownsServer bool
39+
urlBase string
40+
}
41+
42+
func (f *APIFixture) logRequest(r *http.Request) {
43+
f.log.Info("OpenStack API request", "method", r.Method, "URI", r.RequestURI)
44+
}
45+
46+
// Cleanup stops the embedded http server if it was created by the fixture
47+
// during setup
48+
func (f *APIFixture) Cleanup() {
49+
if f.ownsServer {
50+
f.server.Cleanup()
51+
}
52+
}
53+
54+
// Endpoint is the URL the fixture's embedded http server listening on
55+
func (f *APIFixture) Endpoint() string {
56+
return f.server.Endpoint() + f.urlBase
57+
}
58+
59+
func (f *APIFixture) unexpectedRequest(w http.ResponseWriter, r *http.Request) {
60+
f.log.Info("Unexpected OpenStackAPI request", "method", r.Method, "URI", r.RequestURI)
61+
w.WriteHeader(500)
62+
fmt.Fprintf(w, "Unexpected OpenStackAPI request %s %s", r.Method, r.RequestURI)
63+
}
64+
65+
func (f *APIFixture) internalError(err error, msg string, w http.ResponseWriter, r *http.Request) {
66+
f.log.Info("Internal error", "method", r.Method, "URI", r.RequestURI, "error", err, "message", msg)
67+
w.WriteHeader(500)
68+
fmt.Fprintf(w, "Internal error in %s %s: %s: %v", r.Method, r.RequestURI, msg, err)
69+
}

0 commit comments

Comments
 (0)