mockxl

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Jan 7, 2021 License: AGPL-3.0 Imports: 9 Imported by: 0

Documentation

Overview

Package mockxl provides a simple xlist.List implementation that can be used to test other components or test configurations.

This package is a work in progress and makes no API stability promises.

Index

Examples

Constants

View Source
const ComponentClass = "mock"

ComponentClass registered.

Variables

This section is empty.

Functions

func Builder

func Builder() xlistd.BuildListFn

Builder returns a builder function.

Example
package main

import (
	"context"
	"fmt"
	"log"

	"github.com/luids-io/api/xlist"
	"github.com/luids-io/core/apiservice"
	"github.com/luids-io/xlist/pkg/xlistd"
)

func main() {
	// instande builder and register class
	b := xlistd.NewBuilder(apiservice.NewRegistry())

	// create a definition for a list that checks ipv4
	// and returns true to all checks
	listdef1 := xlistd.ListDef{
		ID:        "list1",
		Class:     "mock",
		Resources: []xlist.Resource{xlist.IPv4},
		Source:    "true",
	}
	rbl1, err := b.Build(listdef1)
	if err != nil {
		log.Fatalln("this should not happen")
	}

	resp, err := rbl1.Check(context.Background(), "10.10.10.10", xlist.IPv4)
	if err != nil || !resp.Result {
		log.Fatalln("this should not happen")
	}
	fmt.Println("check ip4 rbl1:", resp.Result, resp.Reason)

	resp, err = rbl1.Check(context.Background(), "www.google.com", xlist.Domain)
	if err != xlist.ErrNotSupported {
		log.Fatalln("this should not happen")
	}
	fmt.Println("check domain rbl1:", err)

	// create a definition for a list that checks domain
	// and fails
	listdef2 := xlistd.ListDef{
		ID:        "list2",
		Class:     "mock",
		Resources: []xlist.Resource{xlist.IPv4, xlist.Domain},
		Opts: map[string]interface{}{
			"fail": true,
		},
	}

	rbl2, err := b.Build(listdef2)
	if err != nil {
		log.Fatalln("this should not happen")
	}

	resp, err = rbl2.Check(context.Background(), "www.google.com", xlist.Domain)
	if err != xlist.ErrInternal {
		log.Fatalln("this should not happen")
	}
	fmt.Println("check domain rbl2:", err)

}
Output:

check ip4 rbl1: true The resource is on the mockup list
check domain rbl1: xlist: resource not supported
check domain rbl2: xlist: internal error

Types

type List

type List struct {
	Identifier string
	// ResourceList that this list checks
	ResourceList []xlist.Resource
	// Results is the sequence of results that the list returns on checks
	Results []bool
	// Fail setup a list that fails
	Fail bool
	// Lazy sets a delay on checks, having a cancellation
	Lazy time.Duration
	// Sleep sets a delay on checks, having NO cancellation
	Sleep time.Duration
	// ForceValidation forces validation in each check
	ForceValidation bool
	// Reason changes the default reason
	Reason string
	// TTL sets a ttl value on checks
	TTL int
	// contains filtered or unexported fields
}

List is a mockup list that implements xlistd.List, see examples.

Example
package main

import (
	"context"
	"fmt"
	"log"
	"time"

	"github.com/luids-io/api/xlist"
	"github.com/luids-io/xlist/pkg/xlistd/components/mockxl"
)

func main() {
	// creating a list that fails all the time
	testlist := &mockxl.List{Fail: true}
	err := testlist.Ping()
	if err != nil {
		fmt.Println("ping 1:", err)
	}

	// creating a list that checks ipv4 resources and returns
	// a sequence of one postive result and two negative results
	testlist = &mockxl.List{
		ResourceList: []xlist.Resource{xlist.IPv4},
		Reason:       "hey, it's on the list",
		Results:      []bool{true, false, false},
	}

	err = testlist.Ping()
	if err != nil {
		log.Fatalln("this should not happen")
	}
	fmt.Println("ping 2:", err)

	resources, err := testlist.Resources(context.Background())
	if err != nil {
		log.Fatalln("this should not happen")
	}
	fmt.Println("resources:", resources)

	resp, err := testlist.Check(context.Background(), "10.10.10.10", xlist.IPv4)
	if err != nil || !resp.Result {
		log.Fatalln("this should not happen")
	}
	fmt.Println("check 1:", resp.Result, resp.Reason)

	resp, err = testlist.Check(context.Background(), "10.10.10.10", xlist.IPv4)
	if err != nil || resp.Result {
		log.Fatalln("this should not happen")
	}
	fmt.Println("check 2:", resp.Result)

	// now, we setup lazy responses...
	testlist.Lazy = 100 * time.Millisecond

	resp, err = testlist.Check(context.Background(), "10.10.10.10", xlist.IPv4)
	if err != nil || resp.Result {
		log.Fatalln("this should not happen")
	}
	fmt.Println("check 3:", resp.Result)

	// if we setup a timeout...
	timeoutCtx, cancelctx := context.WithTimeout(context.Background(), 10*time.Millisecond)
	defer cancelctx()

	resp, err = testlist.Check(timeoutCtx, "10.10.10.10", xlist.IPv4)
	if err == nil {
		log.Fatalln("this sould not nappen")
	}
	fmt.Println("check 4:", err)

}
Output:

ping 1: xlist: not available
ping 2: <nil>
resources: [ip4]
check 1: true hey, it's on the list
check 2: false
check 3: false
check 4: xlist: canceled request

func (*List) Check

func (l *List) Check(ctx context.Context, name string, res xlist.Resource) (xlist.Response, error)

Check implements xlist.Checker.

func (*List) Class

func (l *List) Class() string

Class implements xlistd.List interface.

func (*List) ID

func (l *List) ID() string

ID implements xlistd.List interface.

func (*List) Ping

func (l *List) Ping() error

Ping implements xlistd.List.

func (*List) Resources

func (l *List) Resources(ctx context.Context) ([]xlist.Resource, error)

Resources implements xlist.Checker.

Jump to

Keyboard shortcuts

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