matcher

package
v0.33.1 Latest Latest
Warning

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

Go to latest
Published: Mar 19, 2024 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package matcher implements Gomega matchers for lxkns information model artifacts, such as containers and container groups (pods in particular). These matchers can be used in unit tests of applications using the lxkns API and information model.

While these matchers almost always can be easily written using the existing Gomega matchers these new matchers offer to spell expectations out in a domain-specific language.

For instance, instead of:

Expect(actual).To(ContainElement(And(HaveField("Name", "foo"), HaveField("Type", "docker.com"))))

more succinctly:

Expect(actual).To(ContainElement(BeADockerContainer(WithName("foo"))))

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func BeAContainer

func BeAContainer(options ...types.GomegaMatcher) types.GomegaMatcher

BeAContainer succeeds if actual is a model.Container or *model.Container and also satisfies all its option matchers. A typical use is to use BeAContainer in combination with the "WithX" matchers, such as WithName and WithType. Of course, any other matcher can be specified as an option matcher to BeAContainer as needed.

Example
var container = model.Container{
	ID:     "1234567890",
	Name:   "foo_bar",
	Type:   "ducker.io",
	Flavor: "fluffy",
	Groups: []*model.Group{
		{Name: "fluffy", Type: "group.io", Flavor: "fluffy.io"},
	},
	Paused: true,
}

Expect(container).To(BeAContainer())
Expect(container).To(BeAContainer(HaveField("ID", container.ID)))
Output:

Example (Slice)
var container = model.Container{
	ID:     "1234567890",
	Name:   "foo_bar",
	Type:   "ducker.io",
	Flavor: "fluffy",
	Groups: []*model.Group{
		{Name: "fluffy", Type: "group.io", Flavor: "fluffy.io"},
	},
	Paused: true,
}
containers := []*model.Container{&container}

Expect(containers).To(ContainElement(BeAContainer(WithName("foo_bar"))))
Expect(containers).NotTo(ContainElement(BeAContainer(WithName("baz"))))
Output:

func BeADockerContainer

func BeADockerContainer(options ...types.GomegaMatcher) types.GomegaMatcher

BeADockerContainer succeeds if actual is a Docker container and also satisfy all its option matchers.

Example
var dockerC = model.Container{
	ID:     "1234567890",
	Name:   "foo_bar",
	Type:   moby.Type,
	Flavor: "fluffy",
}
otherC := dockerC
otherC.Type = "cry-oh.io"

Expect(dockerC).To(BeADockerContainer())
Expect(otherC).NotTo(BeADockerContainer())
Output:

func BeAPod

func BeAPod(options ...types.GomegaMatcher) types.GomegaMatcher

BeAPod succeeds if actual is a model.Group or a *model.Group with the Kubernetes pod type, and also satisfies all specified option matchers.

Example
group := model.Group{
	Name: "test/foopod",
	Type: kuhbernetes.PodGroupType,
}
composergroup := model.Group{
	Name: "foobar-project",
	Type: composer.ComposerGroupType,
}
Expect(group).To(BeAPod())
Expect(composergroup).NotTo(BeAPod())
Output:

func BeInAGroup

func BeInAGroup(opts ...types.GomegaMatcher) types.GomegaMatcher

BeInAGroup succeeds if actual is a model.Container or *model.Container and the specified option matchers all succeed on a group the actual container belongs to.

Expect(c).To(BeInAGroup(WithName("my_project")))
Example
var container = model.Container{
	ID:     "1234567890",
	Name:   "foo_bar",
	Type:   "ducker.io",
	Flavor: "fluffy",
	Groups: []*model.Group{
		{Name: "fluffy", Type: "group.io", Flavor: "fluffy.io"},
	},
}

Expect(container).To(BeInAGroup(WithName("fluffy")))
Expect(container).NotTo(BeInAGroup(WithType(kuhbernetes.PodGroupType)))
Output:

func BeInAPod

func BeInAPod(opts ...types.GomegaMatcher) types.GomegaMatcher

BeInAPod succeeds if actual is a model.Container or *model.Container and the container is grouped by a Kubernetes/k8s pod for which all the option matchers also succeed.

Expect(c).To(BeInAPod(WithName("default/mypod")))
Example
var container = model.Container{
	ID:     "1234567890",
	Name:   "foo_bar",
	Type:   moby.Type,
	Flavor: moby.Type,
	Groups: []*model.Group{
		{
			Name:   "space/pod",
			Type:   kuhbernetes.PodGroupType,
			Flavor: kuhbernetes.PodGroupType,
		},
	},
}
container.Groups[0].Containers = []*model.Container{&container}
pod := container.Groups[0]

Expect(container).To(BeInAPod())
Expect(container).To(BeInAPod(WithName(pod.Name)))
Expect(container).NotTo(BeInAPod(WithName("Bielefeld")))
Output:

func BePaused

func BePaused() types.GomegaMatcher

BePaused succeeds if actual is a model.Container or *model.Container and the container is paused.

Expect(c).To(BePaused())
Example
var container = model.Container{
	ID:     "1234567890",
	Name:   "foo_bar",
	Type:   "ducker.io",
	Flavor: "fluffy",
	Groups: []*model.Group{
		{Name: "fluffy", Type: "group.io", Flavor: "fluffy.io"},
	},
	Paused: true,
}
cp := container
cp.Paused = false

Expect(container).To(BePaused())
Expect(cp).NotTo(BePaused())
Output:

func HaveLabel

func HaveLabel(name string, value ...string) types.GomegaMatcher

HaveLabel succeeds if actual has a "Labels" field that contains a map key with the specified name and the optionally specified value.

Expect(container).To(HaveLabel("foobar"))
Expect(container).To(HaveLabel("foo", "bar"))
Example
var container = model.Container{
	ID:     "1234567890",
	Name:   "foo_bar",
	Type:   "ducker.io",
	Flavor: "fluffy",
	Labels: model.Labels{
		"foo": "FOO",
	},
}

Expect(container).To(HaveLabel("foo"))        // any value
Expect(container).To(HaveLabel("foo", "FOO")) // specific value

Expect(container).NotTo(HaveLabel("bar"))        // not at all
Expect(container).NotTo(HaveLabel("foo", "BAR")) // not this value
Output:

func WithFlavor

func WithFlavor(flavor string) types.GomegaMatcher

WithFlavor succeeds if actual has a Flavor field and the specified flavor matches this field.

Example
var container = model.Container{
	Type:   moby.Type,
	Flavor: "moby",
}

Expect(container).To(WithFlavor("moby"))
Expect(container).NotTo(WithFlavor(moby.Type))
Output:

func WithName

func WithName(nameid string) types.GomegaMatcher

WithName succeeds if actual has a Name field and optionally an ID field, and the specified nameid matches at least one of these fields.

Example
var container = model.Container{
	ID:   "1234abcde",
	Name: "morbid_moby",
}

Expect(container).To(WithName("1234abcde"))
Expect(container).To(WithName("morbid_moby"))
Output:

func WithStrictType

func WithStrictType(typ string) types.GomegaMatcher

WithStrictType succeeds if actual has a Type field and the specified type matches it.

Example
var container = model.Container{
	Type:   moby.Type,
	Flavor: "moby",
}

Expect(container).To(WithStrictType(container.Type))
Expect(container).NotTo(WithStrictType(container.Flavor))
Output:

func WithType

func WithType(typeflavor string) types.GomegaMatcher

WithType succeeds if actual has a Type field and optionally a Flavor field, and the specified typeflavor matches at least one of these fields. If you want to check only for the specific Type but not accept it as a Flavor, then use the WithStrictType matcher instead.

Example
var container = model.Container{
	Type:   moby.Type,
	Flavor: "moby",
}

Expect(container).To(WithType(container.Type))
Expect(container).To(WithType(container.Flavor))
Output:

Types

This section is empty.

Jump to

Keyboard shortcuts

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