apis

package
v0.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 19, 2023 License: Apache-2.0 Imports: 10 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type APIFixture

type APIFixture struct {
	// contains filtered or unexported fields
}

APIFixture is a base struct to implement OpenStack API simulators for the EnvTest.

func (*APIFixture) Cleanup

func (f *APIFixture) Cleanup()

Cleanup stops the embedded http server if it was created by the fixture during setup

func (*APIFixture) Endpoint

func (f *APIFixture) Endpoint() string

Endpoint is the URL the fixture's embedded http server listening on

type FakeAPIServer

type FakeAPIServer struct {
	// contains filtered or unexported fields
}

FakeAPIServer represents an embedded http server to simulate http services

func (*FakeAPIServer) Cleanup

func (s *FakeAPIServer) Cleanup()

Cleanup stops the embedded http server

func (*FakeAPIServer) Endpoint

func (s *FakeAPIServer) Endpoint() string

Endpoint is the URL the embedded http server listening on

func (*FakeAPIServer) Setup

func (s *FakeAPIServer) Setup(log logr.Logger)

Setup creates and starts the embedded http server on localhost and on a random port

type Handler

type Handler struct {
	// Pattern is the request URL to handle. If two patters are matching the
	// same request then the handler for the longer pattern will be executed.
	// Using the same pattern in two handlers will cause a panic.
	Pattern string
	// Func the the function that handles the request by writing a response
	Func func(http.ResponseWriter, *http.Request)
}

Handler defines which URL patter is handled by which function

type KeystoneAPIFixture

type KeystoneAPIFixture struct {
	APIFixture
	// Users is the map of user objects known by the fixture keyed by the user
	// name
	Users map[string]users.User
	// Domains is the map of domain objects known by the fixture keyed by the
	// domain name
	Domains map[string]domains.Domain
}

KeystoneAPIFixture is simulator for the OpenStack Keystone API for EnvTest You can simulate the happy path with the following code snippet:

f := NewKeystoneAPIFixtureWithServer(logger)
f.Setup()
DeferCleanup(f.Cleanup)

 name := th.CreateKeystoneAPIWithFixture(namespace, f)
DeferCleanup(th.DeleteKeystoneAPI, name)

But you can also inject failures by passing custom handlers to Setup:

f := NewKeystoneAPIFixtureWithServer(logger)
f.Setup(
    Handler{Patter: "/", Func: f.HandleVersion},
    Handler{Patter: "/v3/auth/tokens", Func: f.HandleToken},
    Handler{Patter: "/v3/users", Func: func(w http.ResponseWriter, r *http.Request) {
        switch r.Method {
        case "GET":
            f.GetUsers(w, r)
        case "POST":
            w.WriteHeader(409)
        }
    }},
)
DeferCleanup(f.Cleanup)

func AddKeystoneAPIFixture

func AddKeystoneAPIFixture(log logr.Logger, server *FakeAPIServer) *KeystoneAPIFixture

AddKeystoneAPIFixture adds a keystone-api simulator to an already created http server. This is useful if you need to have more than one openstack API simulated but you don't want to start a separate http server for each.

func NewKeystoneAPIFixtureWithServer

func NewKeystoneAPIFixtureWithServer(log logr.Logger) *KeystoneAPIFixture

NewKeystoneAPIFixtureWithServer set up a keystone-api simulator with an embedded http server

func (*KeystoneAPIFixture) CreateDomain

func (f *KeystoneAPIFixture) CreateDomain(w http.ResponseWriter, r *http.Request)

CreateDomain handles POST /v3/domains and records the created domain in memory

func (*KeystoneAPIFixture) CreateUser

func (f *KeystoneAPIFixture) CreateUser(w http.ResponseWriter, r *http.Request)

CreateUser handles POST /v3/users and records the created user in memory

func (*KeystoneAPIFixture) GetDomains

func (f *KeystoneAPIFixture) GetDomains(w http.ResponseWriter, r *http.Request)

GetDomains handles GET /v3/domains based on the fixture internal state

func (*KeystoneAPIFixture) GetUsers

func (f *KeystoneAPIFixture) GetUsers(w http.ResponseWriter, r *http.Request)

GetUsers handles GET /v3/users based on the fixture internal state

func (*KeystoneAPIFixture) HandleDomains

func (f *KeystoneAPIFixture) HandleDomains(w http.ResponseWriter, r *http.Request)

HandleDomains handles the happy path of GET /v3/domains and POST /v3/domains API

func (*KeystoneAPIFixture) HandleToken

func (f *KeystoneAPIFixture) HandleToken(w http.ResponseWriter, r *http.Request)

HandleToken responds with a valid keystone token

func (*KeystoneAPIFixture) HandleUsers

func (f *KeystoneAPIFixture) HandleUsers(w http.ResponseWriter, r *http.Request)

HandleUsers handles the happy path of GET /v3/users and POST /v3/users API

func (*KeystoneAPIFixture) HandleVersion

func (f *KeystoneAPIFixture) HandleVersion(w http.ResponseWriter, r *http.Request)

HandleVersion responds with a valid keystone version response

func (*KeystoneAPIFixture) Setup

func (f *KeystoneAPIFixture) Setup(handlers ...Handler)

Setup adds the API request handlers to the fixture. If no handlers is passed then a basic set of well behaving handlers are added that will simulate the happy path. If you need to customize the behavior of the fixture, e.g. to inject faults, then you can pass a list of handlers to register instead.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL