test

package
v0.0.0-...-e5246a7 Latest Latest
Warning

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

Go to latest
Published: Apr 25, 2024 License: Apache-2.0 Imports: 26 Imported by: 18

Documentation

Index

Constants

View Source
const (
	NameHost   = "dsaas"
	NameMember = "east"
)
View Source
const (
	HostClusterName    = "host-cluster"
	MemberOperatorNs   = "toolchain-member-operator"
	MemberClusterName  = "member-cluster"
	Member2ClusterName = "member2-cluster"
	HostOperatorNs     = "toolchain-host-operator"
)

Variables

View Source
var GetReposCommitsByOwnerByRepoByRef mock.EndpointPattern = mock.EndpointPattern{
	Pattern: "/repos/{owner}/{repo}/commits/{ref}",
	Method:  "GET",
}

Functions

func AssertConditionsMatch

func AssertConditionsMatch(t T, actual []toolchainv1alpha1.Condition, expected ...toolchainv1alpha1.Condition)

AssertConditionsMatch asserts that the specified list A of conditions is equal to specified list B of conditions ignoring the order of the elements. We can't use assert.ElementsMatch because the LastTransitionTime of the actual conditions can be modified but the conditions still should be treated as matched

func AssertConditionsMatchAndRecentTimestamps

func AssertConditionsMatchAndRecentTimestamps(t T, actual []toolchainv1alpha1.Condition, expected ...toolchainv1alpha1.Condition)

AssertConditionsMatchAndRecentTimestamps asserts that the specified list of conditions match AND asserts that the timestamps are recent

func AssertContainsCondition

func AssertContainsCondition(t T, conditions []toolchainv1alpha1.Condition, contains toolchainv1alpha1.Condition)

AssertContainsCondition asserts that the specified list of conditions contains the specified condition. LastTransitionTime is ignored.

func AssertContainsMember

func AssertContainsMember(t T, members []toolchainv1alpha1.Member, contains toolchainv1alpha1.Member)

AssertContainsMember asserts that the specified list of members contains the specified member. It compares only the list of conditions and resource usage. LastTransitionTime is ignored.

func AssertHostOperatorStatusMatch

func AssertHostOperatorStatusMatch(t T, actual toolchainv1alpha1.HostOperatorStatus, expected toolchainv1alpha1.HostOperatorStatus)

AssertHostOperatorStatusMatch asserts that the specified host operator status matches the expected host operator status

func AssertMembersMatch

func AssertMembersMatch(t T, actual []toolchainv1alpha1.Member, expected ...toolchainv1alpha1.Member)

AssertMembersMatch asserts that the specified list A of members is equal to specified list B of members ignoring the order of the elements. It compares only the list of conditions and resource usage. We can't use assert.ElementsMatch because the LastTransitionTime of the actual conditions can be modified but the conditions still should be treated as matched

func AssertRegistrationServiceDeploymentStatusMatch

func AssertRegistrationServiceDeploymentStatusMatch(t T, actual toolchainv1alpha1.RegistrationServiceDeploymentStatus, expected toolchainv1alpha1.RegistrationServiceDeploymentStatus)

AssertRegistrationServiceDeploymentStatusMatch asserts that the specified registration service deployment status matches the expected one

func AssertRegistrationServiceHealthStatusMatch

func AssertRegistrationServiceHealthStatusMatch(t T, actual toolchainv1alpha1.RegistrationServiceHealth, expected toolchainv1alpha1.RegistrationServiceHealth)

AssertRegistrationServiceHealthStatusMatch asserts that the specified registration service health status matches the expected one

func AssertRegistrationServiceResourcesStatusMatch

func AssertRegistrationServiceResourcesStatusMatch(t T, actual toolchainv1alpha1.RegistrationServiceResourcesStatus, expected toolchainv1alpha1.RegistrationServiceResourcesStatus)

AssertRegistrationServiceResourcesStatusMatch asserts that the specified registration service resources status matches the expected one

func AssertRegistrationServiceRevisionMatch

func AssertRegistrationServiceRevisionMatch(t T, actual toolchainv1alpha1.RevisionCheck, expected toolchainv1alpha1.RevisionCheck)

AssertRegistrationServiceRevisionMatch asserts that the specified registration service revision check matches the expected one

func AssertRegistrationServiceStatusMatch

func AssertRegistrationServiceStatusMatch(t T, actual toolchainv1alpha1.HostRegistrationServiceStatus, expected toolchainv1alpha1.HostRegistrationServiceStatus)

AssertRegistrationServiceStatusMatch asserts that the specified registration service status matches the expected one

func AssertTimestampsAreRecent

func AssertTimestampsAreRecent(t T, conditions []toolchainv1alpha1.Condition)

AssertTimestampsAreRecent asserts that the timestamps for the provided list of conditions are recent

func ConditionsMatch

func ConditionsMatch(actual []toolchainv1alpha1.Condition, expected ...toolchainv1alpha1.Condition) bool

ConditionsMatch returns true if the specified list A of conditions is equal to specified list B of conditions ignoring the order of the elements

func ContainsCondition

func ContainsCondition(conditions []toolchainv1alpha1.Condition, contains toolchainv1alpha1.Condition) bool

ContainsCondition returns true if the specified list of conditions contains the specified condition. LastTransitionTime is ignored.

func Create

func Create(ctx context.Context, cl *FakeClient, obj client.Object, opts ...client.CreateOption) error

func CreateSecret

func CreateSecret(name, namespace string, data map[string][]byte) *v1.Secret

func CreateTemplate

func CreateTemplate(innerObjects TemplateObjects, params TemplateParams) string

CreateTemplate joins template skeleton with the given template objects and template params and returns a string containing YAML template

func DecodeTemplate

func DecodeTemplate(decoder runtime.Decoder, tmplContent string) (*templatev1.Template, error)

DecodeTemplate decodes the given string using the given decoder to an instance of a Template

func MockGitHubClientForRepositoryCommits

func MockGitHubClientForRepositoryCommits(githubCommitSHA string, commitTimestamp time.Time) client.GetGitHubClientFunc

MockGitHubClientForRepositoryCommits provides a GitHub client which will return the given commit and commit timestamp as a response.

func MockGithubRepositoryCommit

func MockGithubRepositoryCommit(repositoryCommit *github.RepositoryCommit) *http.Client

MockGithubRepositoryCommit creates a http handler that returns a commit for a given org/repo.

func NamespacedName

func NamespacedName(namespace, name string) types.NamespacedName

func NewMockedGithubCommit

func NewMockedGithubCommit(commitSHA string, commitTimestamp time.Time) *github.RepositoryCommit

NewMockedGithubCommit create a GitHub.Commit object with given SHA and timestamp

func NewRESTClient

func NewRESTClient(token, apiEndpoint string) (*rest.RESTClient, error)

NewRESTClient returns a new kube api rest client.

func NewToolchainCluster

func NewToolchainCluster(name, tcNs, secName string, status toolchainv1alpha1.ToolchainClusterStatus, labels map[string]string) (*toolchainv1alpha1.ToolchainCluster, *corev1.Secret)

func NewToolchainClusterWithEndpoint

func NewToolchainClusterWithEndpoint(name, tcNs, secName, apiEndpoint string, status toolchainv1alpha1.ToolchainClusterStatus, labels map[string]string) (*toolchainv1alpha1.ToolchainCluster, *corev1.Secret)

func SetEnvVarAndRestore

func SetEnvVarAndRestore(t *testing.T, key, newValue string) func()

SetEnvVarAndRestore sets the given environment variable with the key to the given value. It returns a function to be called whenever you want to restore the original environment (ideally using defer).

In a test you can use this to set an environment variable:

func TestFoo(t *testing.T) {
    restoreFunc := SetEnvVarAndRestore(t, "foo", "bar")
    defer restoreFunc()
    // continue as if foo=bar
}

func SetEnvVarsAndRestore

func SetEnvVarsAndRestore(t *testing.T, envs ...EnvVariable) func()

SetEnvVarsAndRestore sets the given environment variables with the keys to the given values. It returns a function to be called whenever you want to restore the original environment (ideally using defer).

In a test you can use this to set an environment variables:

func TestFoo(t *testing.T) {
    restoreFunc := SetEnvVarsAndRestore(t, Env("foo", "bar"), Env("boo", "far"))
    defer restoreFunc()
    // continue as if foo=bar and boo=far
}

func SetupGockForServiceAccounts

func SetupGockForServiceAccounts(t *testing.T, apiEndpoint string, sas ...types.NamespacedName)

SetupGockForServiceAccounts registers the /namespaces/<namespace>/serviceaccounts/<serviceaccount>/token endpoint with gock. so that token request are intercepted by gock.

func SetupGockWithCleanup

func SetupGockWithCleanup(t *testing.T, apiEndpoint string, path string, body string, statusCode int) *gock.Response

func UnsetEnvVarAndRestore

func UnsetEnvVarAndRestore(t *testing.T, key string) func()

UnsetEnvVarAndRestore unsets the given environment variable with the key (if present). It returns a function to be called whenever you want to restore the original environment (ideally using defer).

In a test you can use this to temporarily set an environment variable:

func TestFoo(t *testing.T) {
    restoreFunc := UnsetEnvVarAndRestore(t, "foo")
    defer restoreFunc()
    os.Setenv(key, "bar")

    // continue as if foo=bar
}

func Update

func Update(ctx context.Context, cl *FakeClient, obj client.Object, opts ...client.UpdateOption) error

Types

type EnvVariable

type EnvVariable func() (key, value string)

func Env

func Env(key, value string) EnvVariable

type FakeClient

type FakeClient struct {
	client.Client
	T                T
	MockGet          func(ctx context.Context, key client.ObjectKey, obj client.Object, opts ...client.GetOption) error
	MockList         func(ctx context.Context, list client.ObjectList, opts ...client.ListOption) error
	MockCreate       func(ctx context.Context, obj client.Object, opts ...client.CreateOption) error
	MockUpdate       func(ctx context.Context, obj client.Object, opts ...client.UpdateOption) error
	MockPatch        func(ctx context.Context, obj client.Object, patch client.Patch, opts ...client.PatchOption) error
	MockStatusUpdate func(ctx context.Context, obj client.Object, opts ...client.UpdateOption) error
	MockStatusPatch  func(ctx context.Context, obj client.Object, patch client.Patch, opts ...client.PatchOption) error
	MockDelete       func(ctx context.Context, obj client.Object, opts ...client.DeleteOption) error
	MockDeleteAllOf  func(ctx context.Context, obj client.Object, opts ...client.DeleteAllOfOption) error
}

func NewFakeClient

func NewFakeClient(t T, initObjs ...runtime.Object) *FakeClient

NewFakeClient creates a fake K8s client with ability to override specific Get/List/Create/Update/StatusUpdate/Delete functions

func (*FakeClient) Create

func (c *FakeClient) Create(ctx context.Context, obj client.Object, opts ...client.CreateOption) error

func (*FakeClient) Delete

func (c *FakeClient) Delete(ctx context.Context, obj client.Object, opts ...client.DeleteOption) error

func (*FakeClient) DeleteAllOf

func (c *FakeClient) DeleteAllOf(ctx context.Context, obj client.Object, opts ...client.DeleteAllOfOption) error

func (*FakeClient) Get

func (c *FakeClient) Get(ctx context.Context, key client.ObjectKey, obj client.Object, opts ...client.GetOption) error

func (*FakeClient) List

func (c *FakeClient) List(ctx context.Context, list client.ObjectList, opts ...client.ListOption) error

func (*FakeClient) Patch

func (c *FakeClient) Patch(ctx context.Context, obj client.Object, patch client.Patch, opts ...client.PatchOption) error

func (*FakeClient) Status

func (c *FakeClient) Status() client.StatusWriter

func (*FakeClient) Update

func (c *FakeClient) Update(ctx context.Context, obj client.Object, opts ...client.UpdateOption) error

type MockT

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

func NewMockT

func NewMockT() *MockT

func (*MockT) CalledErrorf

func (t *MockT) CalledErrorf() bool

func (*MockT) CalledFailNow

func (t *MockT) CalledFailNow() bool

func (*MockT) CalledFatalf

func (t *MockT) CalledFatalf() bool

func (*MockT) CalledLogf

func (t *MockT) CalledLogf() bool

func (*MockT) Errorf

func (t *MockT) Errorf(_ string, _ ...interface{})

func (*MockT) Fail

func (t *MockT) Fail()

func (*MockT) FailNow

func (t *MockT) FailNow()

func (*MockT) Fatalf

func (t *MockT) Fatalf(_ string, _ ...interface{})

func (*MockT) Log

func (t *MockT) Log(_ ...interface{})

func (*MockT) Logf

func (t *MockT) Logf(_ string, _ ...interface{})

type T

type T interface {
	Log(args ...interface{})
	Logf(format string, args ...interface{})
	Errorf(format string, args ...interface{})
	FailNow()
	Fail()
	Fatalf(format string, args ...interface{})
}

T our minimal testing interface for our custom assertions

type TemplateObject

type TemplateObject string
const (
	Namespace TemplateObject = `` /* 288-byte string literal not displayed */

	ServiceAccount TemplateObject = `` /* 152-byte string literal not displayed */

	RoleBinding TemplateObject = `` /* 268-byte string literal not displayed */

	RoleBindingWithExtraUser TemplateObject = `` /* 303-byte string literal not displayed */

	Service TemplateObject = `` /* 230-byte string literal not displayed */

	ConfigMap TemplateObject = `` /* 212-byte string literal not displayed */

	NamespaceObj TemplateObject = `` /* 350-byte string literal not displayed */

	RolebindingObj TemplateObject = `` /* 347-byte string literal not displayed */

)

template objects

type TemplateObjects

type TemplateObjects string

func WithObjects

func WithObjects(innerObjects ...TemplateObject) TemplateObjects

WithObjects takes list of template objects and joins them together

type TemplateParam

type TemplateParam string
const (
	UsernameParam TemplateParam = `
- name: USERNAME
  value: toolchain-dev
  required: true`

	UsernameParamWithoutValue TemplateParam = `
- name: USERNAME
  required: true`

	CommitParam TemplateParam = `
- name: COMMIT
  value: 123abc
  required: true`

	NamespaceParam TemplateParam = `
- name: NAMESPACE
  value: toolchain-host-operator`

	ServSelectorParam TemplateParam = `
- name: SERVICE_SELECTOR
  value: registration-service`
)

template parameters

type TemplateParams

type TemplateParams string

func WithParams

func WithParams(parameters ...TemplateParam) TemplateParams

WithParams takes list of template parameters and joins them together

Jump to

Keyboard shortcuts

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