helpers

package
v1.4.1 Latest Latest
Warning

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

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

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ExecuteRequest executes request
	ExecuteRequest = helpers.ExecuteRequest
	// CheckResponseBodyJSON checks response body
	CheckResponseBodyJSON = helpers.CheckResponseBodyJSON
	// AssertReportResponsesEqual fails if report responses aren't equal
	AssertReportResponsesEqual = helpers.AssertReportResponsesEqual
	// AssertReportResponsesEqualCustomElementsChecker fails if report responses aren't equal
	AssertReportResponsesEqualCustomElementsChecker = helpers.AssertReportResponsesEqualCustomElementsChecker
	// AssertRuleResponsesEqual fails if rule responses aren't equal
	AssertRuleResponsesEqual = helpers.AssertRuleResponsesEqual
	// NewGockRequestMatcher returns a new matcher for github.com/h2non/gock to match requests
	// with provided method, url and jsonBody
	NewGockRequestMatcher = helpers.NewGockRequestMatcher
	// GockExpectAPIRequest makes gock expect the request with the baseURL and sends back the response
	GockExpectAPIRequest = helpers.GockExpectAPIRequest
	// MakeXRHTokenString converts types.Token to a token string(base64 encoded)
	MakeXRHTokenString = helpers.MakeXRHTokenString
)
View Source
var AssertStringsAreEqualJSON = helpers.AssertStringsAreEqualJSON

AssertStringsAreEqualJSON fails if strings represent different jsons

View Source
var CleanAfterGock = helpers.CleanAfterGock

CleanAfterGock cleans after gock library and prints all unmatched requests

View Source
var DefaultServerConfig = server.Configuration{
	Address:                      ":8080",
	APIPrefix:                    "/api/test/",
	APISpecFile:                  "openapi.json",
	Debug:                        true,
	Auth:                         false,
	MaximumFeedbackMessageLength: 255,
	OrgOverviewLimitHours:        2,
}

DefaultServerConfig is a default config used by AssertAPIRequest

View Source
var DefaultServerConfigAuth = server.Configuration{
	Address:                      ":8080",
	APIPrefix:                    "/api/test/",
	APISpecFile:                  "openapi.json",
	Debug:                        true,
	Auth:                         true,
	AuthType:                     "xrh",
	MaximumFeedbackMessageLength: 255,
	OrgOverviewLimitHours:        2,
}

DefaultServerConfigAuth is a default config used by AssertAPIRequest with authentication set to true

View Source
var FailOnError = helpers.FailOnError

FailOnError fails on error

View Source
var IsStringJSON = helpers.IsStringJSON

IsStringJSON checks if a string is a valid JSON

View Source
var RunTestWithTimeout = helpers.RunTestWithTimeout

RunTestWithTimeout runs test with timeout

View Source
var ToJSONString = helpers.ToJSONString

ToJSONString converts anything to JSON or panics if it's not possible isOutputPretty makes output indented

Functions

func AssertAPIRequest

func AssertAPIRequest(
	t testing.TB,
	mockStorage storage.OCPRecommendationsStorage,
	serverConfig *server.Configuration,
	request *helpers.APIRequest,
	expectedResponse *helpers.APIResponse,
)

AssertAPIRequest creates new server with provided mockStorage (which you can keep nil so it will be created automatically) and provided serverConfig(you can leave it empty to use the default one) sends api request and checks api response (see docs for APIRequest and APIResponse)

func GetHandlersMapForMockConsumer

func GetHandlersMapForMockConsumer(t testing.TB, mockBroker *sarama.MockBroker, topicName string) map[string]sarama.MockResponse

GetHandlersMapForMockConsumer returns handlers for mock broker to successfully create a new consumer

func MustCloseConsumer

func MustCloseConsumer(t *testing.T, mockConsumer consumer.Consumer)

MustCloseConsumer closes consumer and raises error if it wasn't successful

func MustCloseMockDBWithExpects

func MustCloseMockDBWithExpects(
	t *testing.T, db *sql.DB, expects sqlmock.Sqlmock,
)

MustCloseMockDBWithExpects closes mock db with expects and panics if it wasn't successful

func MustCloseMockStorageWithExpects

func MustCloseMockStorageWithExpects(
	t *testing.T, mockStorage storage.OCPRecommendationsStorage, expects sqlmock.Sqlmock,
)

MustCloseMockStorageWithExpects closes mock storage with expects and panics if it wasn't successful

func MustCloseMockStorageWithExpectsDVO added in v1.4.0

func MustCloseMockStorageWithExpectsDVO(
	t *testing.T, mockStorage storage.DVORecommendationsStorage, expects sqlmock.Sqlmock,
)

MustCloseMockStorageWithExpectsDVO same asMustCloseMockStorageWithExpects

func MustCloseStorage

func MustCloseStorage(tb testing.TB, s storage.Storage)

MustCloseStorage closes the storage and calls t.Fatal on error

func MustGetMockDBWithExpects

func MustGetMockDBWithExpects(t *testing.T) (*sql.DB, sqlmock.Sqlmock)

MustGetMockDBWithExpects returns mock db with a driver "github.com/DATA-DOG/go-sqlmock" which requires you to write expect before each query, so first try to use MustGetPostgresStorage don't forget to call MustCloseMockDBWithExpects

func MustGetMockStorageWithExpects

func MustGetMockStorageWithExpects(t *testing.T) (storage.OCPRecommendationsStorage, sqlmock.Sqlmock)

MustGetMockStorageWithExpects returns mock db storage with a driver "github.com/DATA-DOG/go-sqlmock" which requires you to write expect before each query, so first try to use MustGetPostgresStorage don't forget to call MustCloseMockStorageWithExpects

func MustGetMockStorageWithExpectsForDriver

func MustGetMockStorageWithExpectsForDriver(
	t *testing.T, driverType types.DBDriver,
) (storage.OCPRecommendationsStorage, sqlmock.Sqlmock)

MustGetMockStorageWithExpectsForDriver returns mock db storage with specified driver type and with a driver "github.com/DATA-DOG/go-sqlmock" which requires you to write expect before each query, so first try to use MustGetPostgresStorage don't forget to call MustCloseMockStorageWithExpects

func MustGetMockStorageWithExpectsForDriverDVO added in v1.4.0

func MustGetMockStorageWithExpectsForDriverDVO(
	t *testing.T, driverType types.DBDriver,
) (storage.DVORecommendationsStorage, sqlmock.Sqlmock)

MustGetMockStorageWithExpectsForDriverDVO same as MustGetMockStorageWithExpectsForDriver but for DVO

func MustGetPostgresStorage

func MustGetPostgresStorage(tb testing.TB, init bool) (storage.OCPRecommendationsStorage, func())

MustGetPostgresStorage creates test postgres storage with credentials from config-devel

func MustGetPostgresStorageDVO added in v1.4.0

func MustGetPostgresStorageDVO(tb testing.TB, init bool) (storage.DVORecommendationsStorage, func())

MustGetPostgresStorageDVO creates test postgres storage with credentials from config-devel for DVO storage

func PrepareDB added in v1.4.0

func PrepareDB(t *testing.T) (*storage.OCPRecommendationsDBStorage, func())

PrepareDB prepares mock OCPRecommendationsDBStorage

func PrepareDBAndInfo added in v1.4.0

func PrepareDBAndInfo(t *testing.T) (
	*sql.DB,
	types.DBDriver,
	migration.Schema,
	func(),
)

PrepareDBAndInfo prepares mock OCPRecommendationsDBStorage and info table

func PrepareDBDVO added in v1.4.0

func PrepareDBDVO(t *testing.T) (*storage.DVORecommendationsDBStorage, func())

PrepareDBDVO prepares mock DVORecommendationsDBStorage

func WaitForMockConsumerToHaveNConsumedMessages

func WaitForMockConsumerToHaveNConsumedMessages(mockConsumer *MockKafkaConsumer, nMessages uint64)

WaitForMockConsumerToHaveNConsumedMessages waits until mockConsumer has at least N consumed(either successfully or not) messages

Types

type APIRequest

type APIRequest = helpers.APIRequest

APIRequest is a type for APIRequest

type APIResponse

type APIResponse = helpers.APIResponse

APIResponse is a type for APIResponse

type MockKafkaConsumer

type MockKafkaConsumer struct {
	KafkaConsumer consumer.KafkaConsumer
	// contains filtered or unexported fields
}

MockKafkaConsumer is mock consumer

func GetMockDVOConsumerWithExpectedMessages added in v1.4.0

func GetMockDVOConsumerWithExpectedMessages(
	t testing.TB, topic string, orgAllowlist mapset.Set, messages []string,
) (*MockKafkaConsumer, func(), error)

GetMockDVOConsumerWithExpectedMessages same as GetMockOCPRulesConsumerWithExpectedMessages but for DVO

func GetMockOCPRulesConsumerWithExpectedMessages added in v1.4.0

func GetMockOCPRulesConsumerWithExpectedMessages(
	t testing.TB, topic string, orgAllowlist mapset.Set, messages []string,
) (*MockKafkaConsumer, func(), error)

GetMockOCPRulesConsumerWithExpectedMessages creates mocked OCP rules consumer which produces list of messages automatically

func MustGetMockDVOConsumerWithExpectedMessages added in v1.4.0

func MustGetMockDVOConsumerWithExpectedMessages(
	t testing.TB,
	topic string,
	orgAllowlist mapset.Set,
	messages []string,
) (*MockKafkaConsumer, func())

MustGetMockDVOConsumerWithExpectedMessages same as MustGetMockOCPRulesConsumerWithExpectedMessages but for DVO

func MustGetMockOCPRulesConsumerWithExpectedMessages added in v1.4.0

func MustGetMockOCPRulesConsumerWithExpectedMessages(
	t testing.TB,
	topic string,
	orgAllowlist mapset.Set,
	messages []string,
) (*MockKafkaConsumer, func())

MustGetMockOCPRulesConsumerWithExpectedMessages creates mocked OCP rules consumer which produces list of messages automatically calls t.Fatal on error

func (*MockKafkaConsumer) Close

func (mockKafkaConsumer *MockKafkaConsumer) Close(t testing.TB)

Close closes mock consumer

func (*MockKafkaConsumer) Serve

func (mockKafkaConsumer *MockKafkaConsumer) Serve()

Serve simulates sending messages

Jump to

Keyboard shortcuts

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