diff --git a/pkg/platform/yaml/mockdb/db.go b/pkg/platform/yaml/mockdb/db.go index 6b0f15420..9e150bf6f 100644 --- a/pkg/platform/yaml/mockdb/db.go +++ b/pkg/platform/yaml/mockdb/db.go @@ -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 @@ -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 { diff --git a/pkg/service/replay/replay.go b/pkg/service/replay/replay.go index 4877ca9fe..f40c2f711 100644 --- a/pkg/service/replay/replay.go +++ b/pkg/service/replay/replay.go @@ -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) { + requestMockemulator = emulatorInstance } type Replayer struct { @@ -46,10 +46,9 @@ 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(NewRequestMockUtil(logger, config.Path, "mocks", config.Test.APITimeout)) } - return &Replayer{ logger: logger, testDB: testDB, @@ -121,13 +120,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) @@ -153,13 +150,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") } @@ -225,7 +223,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) @@ -423,7 +420,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 @@ -438,7 +435,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 @@ -477,7 +473,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()), Noise: testCase.Noise, Result: *testResult, } diff --git a/pkg/service/replay/service.go b/pkg/service/replay/service.go index fa272e0bb..63d0ac428 100644 --- a/pkg/service/replay/service.go +++ b/pkg/service/replay/service.go @@ -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) } diff --git a/pkg/service/replay/utils.go b/pkg/service/replay/utils.go index 1f7525476..8b7ed36d6 100644 --- a/pkg/service/replay/utils.go +++ b/pkg/service/replay/utils.go @@ -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)) @@ -66,7 +69,23 @@ 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(_ context.Context, status bool, testSetID string) { + 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(_ context.Context, testSetID string) { + t.logger.Debug("Mock file for test set", zap.String("testSetID", testSetID)) +}