skipper: github.com/zalando/skipper/routing/testdataclient Index | Examples | Files

package testdataclient

import "github.com/zalando/skipper/routing/testdataclient"

Package testdataclient provides a test implementation for the DataClient interface of the skipper/routing package.

It uses in-memory route definitions that are passed in on construction, and can upserted/deleted programmatically.

Code:

// create a data client:
dataClient := testdataclient.New([]*eskip.Route{
    {Path: "/some/path", Backend: "https://www.example.org"}})

// (only in tests)
tl := loggingtest.New()
defer tl.Close()

// create a router:
r := routing.New(routing.Options{
    DataClients: []routing.DataClient{dataClient},
    Log:         tl})
defer r.Close()

// wait for the route data being propagated:
tl.WaitFor("route settings applied", time.Second)

// test the router:
route, _ := r.Route(&http.Request{URL: &url.URL{Path: "/some/path"}})
if route == nil {
    log.Fatal("failed to route request")
}

fmt.Println(route.Backend)

Output:

https://www.example.org

Index

Examples

Package Files

dataclient.go

type Client Uses

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

DataClient implementation.

func New Uses

func New(initial []*eskip.Route) *Client

Creates a Client with an initial set of route definitions.

Code:

testdataclient.New([]*eskip.Route{
    {Path: "/some/path", Backend: "https://www.example.org"}})

func NewDoc Uses

func NewDoc(doc string) (*Client, error)

Creates a Client with an initial set of route definitions in eskip format. If parsing the eskip document fails, returns an error.

Code:

dc, err := testdataclient.NewDoc(`Path("/some/path") -> "https://www.example.org"`)
if err != nil || dc == nil {
    log.Fatal(err, dc == nil)
}

func (*Client) FailNext Uses

func (c *Client) FailNext()

Sets the Client to fail on the next call to LoadAll or LoadUpdate. Repeated call to FailNext will result the Client to fail as many times as it was called.

Code:

// create a data client:
dc, err := testdataclient.NewDoc(`Path("/some/path") -> "https://www.example.org"`)
if err != nil || dc == nil {
    log.Fatal(err, dc == nil)
}

// set the the next two requests to fail:
dc.FailNext()
dc.FailNext()

// wait for the third request to succeed:
_, err = dc.LoadAll()
fmt.Println(err)

_, err = dc.LoadAll()
fmt.Println(err)

routes, err := dc.LoadAll()
if err != nil || len(routes) != 1 {
    log.Fatal(err, len(routes))
}

fmt.Println(routes[0].Backend)

Output:

failed to get routes
failed to get routes
https://www.example.org

func (*Client) LoadAll Uses

func (c *Client) LoadAll() ([]*eskip.Route, error)

Returns the initial/current set of route definitions.

func (*Client) LoadUpdate Uses

func (c *Client) LoadUpdate() ([]*eskip.Route, []string, error)

Returns the route definitions upserted/deleted since the last call to LoadAll.

func (*Client) Update Uses

func (c *Client) Update(upsert []*eskip.Route, deletedIDs []string)

Updates the current set of routes with new/modified and deleted route definitions.

func (*Client) UpdateDoc Uses

func (c *Client) UpdateDoc(upsertDoc string, deletedIDs []string) error

Updates the current set of routes with new/modified and deleted route definitions in eskip format. In case the parsing of the document fails, it returns an error.

Package testdataclient imports 2 packages (graph) and is imported by 6 packages. Updated 2019-04-03. Refresh now. Tools for package owners.