Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add: support of custom mock name #1835

Merged
merged 6 commits into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 0 additions & 2 deletions pkg/platform/yaml/mockdb/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,6 @@ func (ys *MockYaml) GetFilteredMocks(ctx context.Context, testSetID string, afte

var tcsMocks = make([]*models.Mock, 0)
var filteredTcsMocks = make([]*models.Mock, 0)

mockFileName := "mocks"
if ys.MockName != "" {
mockFileName = ys.MockName
Expand Down Expand Up @@ -196,7 +195,6 @@ func (ys *MockYaml) GetFilteredMocks(ctx context.Context, testSetID string, afte
}
}
}

filteredTcsMocks, _ = ys.filterByTimeStamp(ctx, tcsMocks, afterTime, beforeTime, ys.Logger)

sort.SliceStable(filteredTcsMocks, func(i, j int) bool {
Expand Down
25 changes: 11 additions & 14 deletions pkg/service/replay/replay.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ var totalTestFailed int

// emulator contains the struct instance that implements RequestEmulator interface. This is done for
// attaching the objects dynamically as plugins.
var emulator RequestEmulator
var requestMockemulator RequestMockHandler

func SetTestUtilInstance(instance RequestEmulator) {
emulator = instance
func SetTestUtilInstance(emulatorInstance RequestMockHandler) {
charankamarapu marked this conversation as resolved.
Show resolved Hide resolved
requestMockemulator = emulatorInstance
}

type Replayer struct {
Expand All @@ -46,10 +46,10 @@ type Replayer struct {

func NewReplayer(logger *zap.Logger, testDB TestDB, mockDB MockDB, reportDB ReportDB, telemetry Telemetry, instrumentation Instrumentation, config config.Config) Service {
// set the request emulator for simulating test case requests, if not set
if emulator == nil {
SetTestUtilInstance(NewTestUtils(config.Test.APITimeout, logger))
if requestMockemulator == nil {
// SetTestUtilInstance()
requestMockemulator = NewRequestMockUtil(logger, config.Path, "mocks", config.Test.APITimeout)
}

return &Replayer{
logger: logger,
testDB: testDB,
Expand Down Expand Up @@ -121,13 +121,11 @@ func (r *Replayer) Start(ctx context.Context) error {
testSetResult := false
testRunResult := true
abortTestRun := false

for _, testSetID := range testSetIDs {

if _, ok := r.config.Test.SelectedTests[testSetID]; !ok && len(r.config.Test.SelectedTests) != 0 {
continue
}

requestMockemulator.ProcessMockFile(ctx, testSetID)
testSetStatus, err := r.RunTestSet(ctx, testSetID, testRunID, appID, false)
if err != nil {
stopReason = fmt.Sprintf("failed to run test set: %v", err)
Expand All @@ -153,13 +151,14 @@ func (r *Replayer) Start(ctx context.Context) error {
testSetResult = false
case models.TestSetStatusPassed:
testSetResult = true
requestMockemulator.ProcessTestRunStatus(ctx, testSetResult, testSetID)
}
testRunResult = testRunResult && testSetResult
if abortTestRun {
break
}

_, err = emulator.AfterTestHook(ctx, testRunID, testSetID, len(testSetIDs))
_, err = requestMockemulator.AfterTestHook(ctx, testRunID, testSetID, len(testSetIDs))
if err != nil {
utils.LogError(r.logger, err, "failed to get after test hook")
}
Expand Down Expand Up @@ -225,7 +224,6 @@ func (r *Replayer) GetAllTestSetIDs(ctx context.Context) ([]string, error) {
}

func (r *Replayer) RunTestSet(ctx context.Context, testSetID string, testRunID string, appID uint64, serveTest bool) (models.TestSetStatus, error) {

// creating error group to manage proper shutdown of all the go routines and to propagate the error to the caller
runTestSetErrGrp, runTestSetCtx := errgroup.WithContext(ctx)
runTestSetCtx = context.WithValue(runTestSetCtx, models.ErrGroupKey, runTestSetErrGrp)
Expand Down Expand Up @@ -423,7 +421,7 @@ func (r *Replayer) RunTestSet(ctx context.Context, testSetID string, testRunID s
r.logger.Debug("", zap.Any("replaced URL in case of docker env", testCase.HTTPReq.URL))
}

resp, loopErr := emulator.SimulateRequest(runTestSetCtx, appID, testCase, testSetID)
resp, loopErr := requestMockemulator.SimulateRequest(runTestSetCtx, appID, testCase, testSetID)
if loopErr != nil {
utils.LogError(r.logger, err, "failed to simulate request")
break
Expand All @@ -438,7 +436,6 @@ func (r *Replayer) RunTestSet(ctx context.Context, testSetID string, testRunID s
totalConsumedMocks[mockName] = true
}
}

testPass, testResult = r.compareResp(testCase, resp, testSetID)
if !testPass {
// log the consumed mocks during the test run of the test case for test set
Expand Down Expand Up @@ -477,7 +474,7 @@ func (r *Replayer) RunTestSet(ctx context.Context, testSetID string, testRunID s
},
Res: *resp,
TestCasePath: filepath.Join(r.config.Path, testSetID),
MockPath: filepath.Join(r.config.Path, testSetID, "mocks.yaml"),
MockPath: filepath.Join(r.config.Path, testSetID, requestMockemulator.FetchMockName()),
charankamarapu marked this conversation as resolved.
Show resolved Hide resolved
Noise: testCase.Noise,
Result: *testResult,
}
Expand Down
5 changes: 4 additions & 1 deletion pkg/service/replay/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ type Telemetry interface {

// RequestEmulator is used to simulate the API requests to the user API. The requests are read from
// the recorded test case of the user app.
type RequestEmulator interface {
type RequestMockHandler interface {
SimulateRequest(ctx context.Context, appID uint64, tc *models.TestCase, testSetID string) (*models.HTTPResp, error)
ProcessTestRunStatus(ctx context.Context, status bool, testSetID string)
FetchMockName() string
ProcessMockFile(ctx context.Context, testSetID string)
AfterTestHook(ctx context.Context, testRunID, testSetID string, totalTestSets int) (*models.TestReport, error)
}
39 changes: 33 additions & 6 deletions pkg/service/replay/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,22 @@ func LeftJoinNoise(globalNoise config.GlobalNoise, tsNoise config.GlobalNoise) c
return noise
}

type testUtils struct {
type requestMockUtil struct {
logger *zap.Logger
path string
mockName string
apiTimeout uint64
}

func NewTestUtils(apiTimeout uint64, logger *zap.Logger) RequestEmulator {
return &testUtils{
func NewRequestMockUtil(logger *zap.Logger, path, mockName string, apiTimeout uint64) RequestMockHandler {
return &requestMockUtil{
path: path,
logger: logger,
mockName: mockName,
apiTimeout: apiTimeout,
}
}

func (t *testUtils) SimulateRequest(ctx context.Context, _ uint64, tc *models.TestCase, testSetID string) (*models.HTTPResp, error) {
func (t *requestMockUtil) SimulateRequest(ctx context.Context, _ uint64, tc *models.TestCase, testSetID string) (*models.HTTPResp, error) {
switch tc.Kind {
case models.HTTP:
t.logger.Debug("Before simulating the request", zap.Any("Test case", tc))
Expand All @@ -66,7 +69,31 @@ func (t *testUtils) SimulateRequest(ctx context.Context, _ uint64, tc *models.Te
return nil, nil
}

func (t *testUtils) AfterTestHook(_ context.Context, testRunID, testSetID string, tsCnt int) (*models.TestReport, error) {
func (t *requestMockUtil) AfterTestHook(_ context.Context, testRunID, testSetID string, tsCnt int) (*models.TestReport, error) {
t.logger.Debug("AfterTestHook", zap.Any("testRunID", testRunID), zap.Any("testSetID", testSetID), zap.Any("totTestSetCount", tsCnt))
return nil, nil
}

func (t *requestMockUtil) ProcessTestRunStatus(ctx context.Context, status bool, testSetID string) {
if err := ctx.Err(); err != nil {
charankamarapu marked this conversation as resolved.
Show resolved Hide resolved
t.logger.Debug("Operation cancelled or timed out", zap.Error(err))
return
}
if status {
t.logger.Debug("Test case passed for", zap.String("testSetID", testSetID))
} else {
t.logger.Debug("Test case failed for", zap.String("testSetID", testSetID))
}
}

func (t *requestMockUtil) FetchMockName() string {
return t.mockName
}

func (t *requestMockUtil) ProcessMockFile(ctx context.Context, testSetID string) {
if err := ctx.Err(); err != nil {
t.logger.Debug("Operation cancelled or timed out", zap.Error(err))
return
}
t.logger.Debug("Mock file for test set", zap.String("testSetID", testSetID))
}