testdataclient

package
v0.10.69 Latest Latest
Warning

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

Go to latest
Published: Sep 1, 2018 License: Apache-2.0, MIT Imports: 2 Imported by: 0

Documentation

Overview

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.

Example
package main

import (
	"fmt"
	"log"
	"net/http"
	"net/url"
	"time"

	"github.com/zalando/skipper/eskip"
	"github.com/zalando/skipper/logging/loggingtest"
	"github.com/zalando/skipper/routing"
	"github.com/zalando/skipper/routing/testdataclient"
)

func main() {
	// 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

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

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

DataClient implementation.

func New

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

Creates a Client with an initial set of route definitions.

Example
package main

import (
	"github.com/zalando/skipper/eskip"
	"github.com/zalando/skipper/routing/testdataclient"
)

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

func NewDoc

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.

Example
package main

import (
	"log"

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

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

func (*Client) FailNext

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.

Example
package main

import (
	"fmt"
	"log"

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

func main() {
	// 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

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

Returns the initial/current set of route definitions.

func (*Client) LoadUpdate

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

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

func (*Client) Update

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

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.

Jump to

Keyboard shortcuts

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