Skip to content

Commit

Permalink
Adapt API fixture before keystone helper move
Browse files Browse the repository at this point in the history
  • Loading branch information
gibizer committed Sep 22, 2023
1 parent d74c2f3 commit 820454c
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 37 deletions.
26 changes: 13 additions & 13 deletions modules/test/apis/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,37 +33,37 @@ type Handler struct {
// APIFixture is a base struct to implement OpenStack API simulators for the
// EnvTest.
type APIFixture struct {
log logr.Logger
server *FakeAPIServer
ownsServer bool
urlBase string
Log logr.Logger
Server *FakeAPIServer
OwnsServer bool
UrlBase string
}

func (f *APIFixture) logRequest(r *http.Request) {
f.log.Info("OpenStack API request", "method", r.Method, "URI", r.RequestURI)
func (f *APIFixture) LogRequest(r *http.Request) {
f.Log.Info("OpenStack API request", "method", r.Method, "URI", r.RequestURI)
}

// Cleanup stops the embedded http server if it was created by the fixture
// during setup
func (f *APIFixture) Cleanup() {
if f.ownsServer {
f.server.Cleanup()
if f.OwnsServer {
f.Server.Cleanup()
}
}

// Endpoint is the URL the fixture's embedded http server listening on
func (f *APIFixture) Endpoint() string {
return f.server.Endpoint() + f.urlBase
return f.Server.Endpoint() + f.UrlBase
}

func (f *APIFixture) unexpectedRequest(w http.ResponseWriter, r *http.Request) {
f.log.Info("Unexpected OpenStackAPI request", "method", r.Method, "URI", r.RequestURI)
func (f *APIFixture) UnexpectedRequest(w http.ResponseWriter, r *http.Request) {
f.Log.Info("Unexpected OpenStackAPI request", "method", r.Method, "URI", r.RequestURI)
w.WriteHeader(500)
fmt.Fprintf(w, "Unexpected OpenStackAPI request %s %s", r.Method, r.RequestURI)
}

func (f *APIFixture) internalError(err error, msg string, w http.ResponseWriter, r *http.Request) {
f.log.Info("Internal error", "method", r.Method, "URI", r.RequestURI, "error", err, "message", msg)
func (f *APIFixture) InternalError(err error, msg string, w http.ResponseWriter, r *http.Request) {
f.Log.Info("Internal error", "method", r.Method, "URI", r.RequestURI, "error", err, "message", msg)
w.WriteHeader(500)
fmt.Fprintf(w, "Internal error in %s %s: %s: %v", r.Method, r.RequestURI, msg, err)
}
48 changes: 24 additions & 24 deletions modules/test/apis/keystone.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func NewKeystoneAPIFixtureWithServer(log logr.Logger) *KeystoneAPIFixture {
server := &FakeAPIServer{}
server.Setup(log)
fixture := AddKeystoneAPIFixture(log, server)
fixture.ownsServer = true
fixture.OwnsServer = true
return fixture
}

Expand All @@ -78,10 +78,10 @@ func NewKeystoneAPIFixtureWithServer(log logr.Logger) *KeystoneAPIFixture {
func AddKeystoneAPIFixture(log logr.Logger, server *FakeAPIServer) *KeystoneAPIFixture {
fixture := &KeystoneAPIFixture{
APIFixture: APIFixture{
server: server,
log: log,
urlBase: "/identity",
ownsServer: false,
Server: server,
Log: log,
UrlBase: "/identity",
OwnsServer: false,
},
Users: map[string]users.User{},
Domains: map[string]domains.Domain{},
Expand Down Expand Up @@ -111,16 +111,16 @@ func (f *KeystoneAPIFixture) registerNormalHandlers() {
}

func (f *KeystoneAPIFixture) registerHandler(handler Handler) {
f.server.mux.HandleFunc(f.urlBase+handler.Pattern, handler.Func)
f.Server.AddHandler(f.UrlBase+handler.Pattern, handler.Func)
}

// HandleVersion responds with a valid keystone version response
func (f *KeystoneAPIFixture) HandleVersion(w http.ResponseWriter, r *http.Request) {
f.logRequest(r)
f.LogRequest(r)
// The /identity URL matches to every request if no handle registered with a more
// specific URL pattern
if r.RequestURI != f.urlBase+"/" {
f.unexpectedRequest(w, r)
if r.RequestURI != f.UrlBase+"/" {
f.UnexpectedRequest(w, r)
return
}

Expand Down Expand Up @@ -149,14 +149,14 @@ func (f *KeystoneAPIFixture) HandleVersion(w http.ResponseWriter, r *http.Reques
}
`, f.Endpoint())
default:
f.unexpectedRequest(w, r)
f.UnexpectedRequest(w, r)
return
}
}

// HandleToken responds with a valid keystone token
func (f *KeystoneAPIFixture) HandleToken(w http.ResponseWriter, r *http.Request) {
f.logRequest(r)
f.LogRequest(r)
switch r.Method {
case "POST":
w.Header().Add("Content-Type", "application/json")
Expand Down Expand Up @@ -185,21 +185,21 @@ func (f *KeystoneAPIFixture) HandleToken(w http.ResponseWriter, r *http.Request)
}
`, f.Endpoint())
default:
f.unexpectedRequest(w, r)
f.UnexpectedRequest(w, r)
return
}
}

// HandleUsers handles the happy path of GET /v3/users and POST /v3/users API
func (f *KeystoneAPIFixture) HandleUsers(w http.ResponseWriter, r *http.Request) {
f.logRequest(r)
f.LogRequest(r)
switch r.Method {
case "GET":
f.GetUsers(w, r)
case "POST":
f.CreateUser(w, r)
default:
f.unexpectedRequest(w, r)
f.UnexpectedRequest(w, r)
return
}
}
Expand All @@ -225,7 +225,7 @@ func (f *KeystoneAPIFixture) GetUsers(w http.ResponseWriter, r *http.Request) {

bytes, err := json.Marshal(&s)
if err != nil {
f.internalError(err, "Error during marshalling response", w, r)
f.InternalError(err, "Error during marshalling response", w, r)
return
}
w.Header().Add("Content-Type", "application/json")
Expand All @@ -237,15 +237,15 @@ func (f *KeystoneAPIFixture) GetUsers(w http.ResponseWriter, r *http.Request) {
func (f *KeystoneAPIFixture) CreateUser(w http.ResponseWriter, r *http.Request) {
bytes, err := io.ReadAll(r.Body)
if err != nil {
f.internalError(err, "Error reading request body", w, r)
f.InternalError(err, "Error reading request body", w, r)
return
}
var s struct {
User users.User `json:"user"`
}
err = json.Unmarshal(bytes, &s)
if err != nil {
f.internalError(err, "Error during unmarshalling request", w, r)
f.InternalError(err, "Error during unmarshalling request", w, r)
return
}
if s.User.ID == "" {
Expand All @@ -256,7 +256,7 @@ func (f *KeystoneAPIFixture) CreateUser(w http.ResponseWriter, r *http.Request)

bytes, err = json.Marshal(&s)
if err != nil {
f.internalError(err, "Error during marshalling response", w, r)
f.InternalError(err, "Error during marshalling response", w, r)
return
}
w.Header().Add("Content-Type", "application/json")
Expand All @@ -266,14 +266,14 @@ func (f *KeystoneAPIFixture) CreateUser(w http.ResponseWriter, r *http.Request)

// HandleDomains handles the happy path of GET /v3/domains and POST /v3/domains API
func (f *KeystoneAPIFixture) HandleDomains(w http.ResponseWriter, r *http.Request) {
f.logRequest(r)
f.LogRequest(r)
switch r.Method {
case "GET":
f.GetDomains(w, r)
case "POST":
f.CreateDomain(w, r)
default:
f.unexpectedRequest(w, r)
f.UnexpectedRequest(w, r)
return
}
}
Expand All @@ -299,7 +299,7 @@ func (f *KeystoneAPIFixture) GetDomains(w http.ResponseWriter, r *http.Request)

bytes, err := json.Marshal(&s)
if err != nil {
f.internalError(err, "Error during marshalling response", w, r)
f.InternalError(err, "Error during marshalling response", w, r)
return
}
w.Header().Add("Content-Type", "application/json")
Expand All @@ -311,15 +311,15 @@ func (f *KeystoneAPIFixture) GetDomains(w http.ResponseWriter, r *http.Request)
func (f *KeystoneAPIFixture) CreateDomain(w http.ResponseWriter, r *http.Request) {
bytes, err := io.ReadAll(r.Body)
if err != nil {
f.internalError(err, "Error reading request body", w, r)
f.InternalError(err, "Error reading request body", w, r)
return
}
var s struct {
Domain domains.Domain `json:"domain"`
}
err = json.Unmarshal(bytes, &s)
if err != nil {
f.internalError(err, "Error during unmarshalling request", w, r)
f.InternalError(err, "Error during unmarshalling request", w, r)
return
}
if s.Domain.ID == "" {
Expand All @@ -330,7 +330,7 @@ func (f *KeystoneAPIFixture) CreateDomain(w http.ResponseWriter, r *http.Request

bytes, err = json.Marshal(&s)
if err != nil {
f.internalError(err, "Error during marshalling response", w, r)
f.InternalError(err, "Error during marshalling response", w, r)
return
}
w.Header().Add("Content-Type", "application/json")
Expand Down
4 changes: 4 additions & 0 deletions modules/test/apis/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ func (s *FakeAPIServer) Setup(log logr.Logger) {
s.mux.HandleFunc("/", s.fallbackHandler)
}

func (s *FakeAPIServer) AddHandler(pattern string, handler func(http.ResponseWriter, *http.Request)) {
s.mux.HandleFunc(pattern, handler)
}

func (s *FakeAPIServer) fallbackHandler(w http.ResponseWriter, r *http.Request) {
if r == nil {
s.log.Info("Unexpected OpenStackAPI nil request")
Expand Down

0 comments on commit 820454c

Please sign in to comment.