Skip to content

Commit

Permalink
feat: add denoise functionality (#1965)
Browse files Browse the repository at this point in the history
* feat: add a separate denoise functionality for apis

Signed-off-by: Sarthak160 <rocksarthak45@gmail.com>

* fix: fix linter issue

Signed-off-by: Sarthak160 <rocksarthak45@gmail.com>

* fix: nitpick

Signed-off-by: charankamarapu <kamarapucharan@gmail.com>

---------

Signed-off-by: Sarthak160 <rocksarthak45@gmail.com>
Signed-off-by: charankamarapu <kamarapucharan@gmail.com>
Co-authored-by: Charan Kamarapu <kamarapucharan@gmail.com>
  • Loading branch information
Sarthak160 and charankamarapu committed Jun 17, 2024
1 parent 90f0a28 commit 3ae66a7
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 7 deletions.
Binary file removed keploy.zip
Binary file not shown.
13 changes: 13 additions & 0 deletions pkg/models/testcase.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,16 @@ type TestCase struct {
func (tc *TestCase) GetKind() string {
return string(tc.Kind)
}

type NoiseParams struct {
TestCaseIDs string `json:"testCaseIDs"`
EditedBy string `json:"editedBy"`
Assertion map[string][]string `json:"assertion"`
Ops string `json:"ops"`
}

// enum for ops
const (
OpsAdd = "ADD"
OpsRemove = "REMOVE"
)
47 changes: 40 additions & 7 deletions pkg/service/replay/replay.go
Original file line number Diff line number Diff line change
Expand Up @@ -752,6 +752,36 @@ func (r *Replayer) RunApplication(ctx context.Context, appID uint64, opts models
return r.instrumentation.Run(ctx, appID, opts)
}

func (r *Replayer) DenoiseTestCases(ctx context.Context, testSetID string, noiseParams []models.NoiseParams) error {

testCases, err := r.testDB.GetTestCases(ctx, testSetID)
if err != nil {
return fmt.Errorf("failed to get test cases: %w", err)
}

for _, v := range testCases {
for _, noiseParam := range noiseParams {
if v.Name == noiseParam.TestCaseIDs {

// append the noise map
if noiseParam.Ops == string(models.OpsAdd) {
v.Noise = mergeMaps(v.Noise, noiseParam.Assertion)
} else {
// remove from the original noise map
v.Noise = removeFromMap(v.Noise, noiseParam.Assertion)
}
err = r.testDB.UpdateTestCase(ctx, v, testSetID)
if err != nil {
return fmt.Errorf("failed to update test case: %w", err)
}
}
}

}

return nil
}

func (r *Replayer) Normalize(ctx context.Context) error {

var testRun string
Expand Down Expand Up @@ -782,7 +812,7 @@ func (r *Replayer) Normalize(ctx context.Context) error {
for _, testSet := range r.config.Normalize.SelectedTests {
testSetID := testSet.TestSet
testCases := testSet.Tests
err := r.normalizeTestCases(ctx, testRun, testSetID, testCases)
err := r.NormalizeTestCases(ctx, testRun, testSetID, testCases, nil)
if err != nil {
return err
}
Expand All @@ -791,15 +821,18 @@ func (r *Replayer) Normalize(ctx context.Context) error {
return nil
}

func (r *Replayer) normalizeTestCases(ctx context.Context, testRun string, testSetID string, selectedTestCaseIDs []string) error {
func (r *Replayer) NormalizeTestCases(ctx context.Context, testRun string, testSetID string, selectedTestCaseIDs []string, testCaseResults []models.TestResult) error {

testReport, err := r.reportDB.GetReport(ctx, testRun, testSetID)
if err != nil {
return fmt.Errorf("failed to get test report: %w", err)
if len(testCaseResults) == 0 {
testReport, err := r.reportDB.GetReport(ctx, testRun, testSetID)
if err != nil {
return fmt.Errorf("failed to get test report: %w", err)
}

testCaseResults = testReport.Tests
}
testCaseResults := testReport.Tests
testCaseResultMap := make(map[string]models.TestResult)

testCaseResultMap := make(map[string]models.TestResult)
testCases, err := r.testDB.GetTestCases(ctx, testSetID)
if err != nil {
return fmt.Errorf("failed to get test cases: %w", err)
Expand Down
2 changes: 2 additions & 0 deletions pkg/service/replay/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ type Service interface {
GetTestSetStatus(ctx context.Context, testRunID string, testSetID string) (models.TestSetStatus, error)
RunApplication(ctx context.Context, appID uint64, opts models.RunOptions) models.AppError
Normalize(ctx context.Context) error
DenoiseTestCases(ctx context.Context, testSetID string, noiseParams []models.NoiseParams) error
NormalizeTestCases(ctx context.Context, testRun string, testSetID string, selectedTestCaseIDs []string, testResult []models.TestResult) error
}

type TestDB interface {
Expand Down
18 changes: 18 additions & 0 deletions pkg/service/replay/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,21 @@ func (t *requestMockUtil) ProcessMockFile(_ context.Context, testSetID string) {
}
t.logger.Debug("Mock file for test set", zap.String("testSetID", testSetID))
}

func mergeMaps(map1, map2 map[string][]string) map[string][]string {
for key, values := range map2 {
if _, exists := map1[key]; exists {
map1[key] = append(map1[key], values...)
} else {
map1[key] = values
}
}
return map1
}

func removeFromMap(map1, map2 map[string][]string) map[string][]string {
for key := range map2 {
delete(map1, key)
}
return map1
}

0 comments on commit 3ae66a7

Please sign in to comment.