Skip to content

Commit

Permalink
Test/mocks: added mocks for test cases (#4133)
Browse files Browse the repository at this point in the history
* fix: use local mongo operator

Signed-off-by: SohamRatnaparkhi <soham.ratnaparkhi@gmail.com>

* feat: gitops mocks added

Signed-off-by: SohamRatnaparkhi <soham.ratnaparkhi@gmail.com>

* feat: mongo mocks added

Signed-off-by: SohamRatnaparkhi <soham.ratnaparkhi@gmail.com>

* feat: chaos experiment run mocks added

Signed-off-by: SohamRatnaparkhi <soham.ratnaparkhi@gmail.com>

* feat: infra mocks added

Signed-off-by: SohamRatnaparkhi <soham.ratnaparkhi@gmail.com>

* feat: chaos experiment mocks added

Signed-off-by: SohamRatnaparkhi <soham.ratnaparkhi@gmail.com>

---------

Signed-off-by: SohamRatnaparkhi <soham.ratnaparkhi@gmail.com>
Co-authored-by: Amit Kumar Das <amit.das@harness.io>
Co-authored-by: Namkyu Park <53862866+namkyu1999@users.noreply.github.com>
Co-authored-by: Saranya Jena <saranya.jena@harness.io>
  • Loading branch information
4 people authored Sep 15, 2023
1 parent ba00736 commit ff599d4
Show file tree
Hide file tree
Showing 8 changed files with 354 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package mocks

import (
"context"

"github.com/litmuschaos/litmus/chaoscenter/graphql/server/graph/model"
store "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/data-store"
dbChaosExperiment "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/database/mongodb/chaos_experiment"
"github.com/stretchr/testify/mock"
"go.mongodb.org/mongo-driver/bson"
)

type ChaosExperimentService struct {
mock.Mock
}

func (m *ChaosExperimentService) ProcessExperiment(workflow *model.ChaosExperimentRequest, projectID string, revID string) (*model.ChaosExperimentRequest, *dbChaosExperiment.ChaosExperimentType, error) {
args := m.Called(workflow, projectID, revID)
return args.Get(0).(*model.ChaosExperimentRequest), args.Get(1).(*dbChaosExperiment.ChaosExperimentType), args.Error(2)
}

func (m *ChaosExperimentService) ProcessExperimentCreation(ctx context.Context, input *model.ChaosExperimentRequest, username string, projectID string, wfType *dbChaosExperiment.ChaosExperimentType, revisionID string, r *store.StateData) error {
args := m.Called(ctx, input, username, projectID, wfType, revisionID, r)
return args.Error(0)
}

func (m *ChaosExperimentService) ProcessExperimentUpdate(workflow *model.ChaosExperimentRequest, username string, wfType *dbChaosExperiment.ChaosExperimentType, revisionID string, updateRevision bool, projectID string, r *store.StateData) error {
args := m.Called(workflow, username, wfType, revisionID, updateRevision, projectID, r)
return args.Error(0)
}

func (m *ChaosExperimentService) ProcessExperimentDelete(query bson.D, workflow dbChaosExperiment.ChaosExperimentRequest, username string, r *store.StateData) error {
args := m.Called(query, workflow, username, r)
return args.Error(0)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package mocks

import (
"context"

"github.com/litmuschaos/litmus/chaoscenter/graphql/server/graph/model"
store "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/data-store"
dbChaosInfra "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/database/mongodb/chaos_infrastructure"
"github.com/stretchr/testify/mock"
"go.mongodb.org/mongo-driver/bson"
)

type InfraService struct {
mock.Mock
}

func (s *InfraService) RegisterInfra(c context.Context, projectID string, input model.RegisterInfraRequest) (*model.RegisterInfraResponse, error) {
args := s.Called(c, projectID, input)
return args.Get(0).(*model.RegisterInfraResponse), args.Error(1)
}

func (s *InfraService) ConfirmInfraRegistration(request model.InfraIdentity, r store.StateData) (*model.ConfirmInfraRegistrationResponse, error) {
args := s.Called(request, r)
return args.Get(0).(*model.ConfirmInfraRegistrationResponse), args.Error(1)
}

func (s *InfraService) VerifyInfra(identity model.InfraIdentity) (*dbChaosInfra.ChaosInfra, error) {
args := s.Called(identity)
return args.Get(0).(*dbChaosInfra.ChaosInfra), args.Error(1)
}

func (s *InfraService) DeleteInfra(ctx context.Context, projectID string, infraId string, r store.StateData) (string, error) {
args := s.Called(ctx, projectID, infraId, r)
return args.String(0), args.Error(1)
}

func (s *InfraService) ListInfras(projectID string, request *model.ListInfraRequest) (*model.ListInfraResponse, error) {
args := s.Called(projectID, request)
return args.Get(0).(*model.ListInfraResponse), args.Error(1)
}

func (s *InfraService) GetInfraDetails(ctx context.Context, infraID string, projectID string) (*model.Infra, error) {
args := s.Called(ctx, infraID, projectID)
return args.Get(0).(*model.Infra), args.Error(1)
}

func (s *InfraService) SendInfraEvent(eventType, eventName, description string, infra model.Infra, r store.StateData) {
s.Called(eventType, eventName, description, infra, r)
}

func (s *InfraService) GetManifest(token string) ([]byte, int, error) {
args := s.Called(token)
return args.Get(0).([]byte), args.Int(1), args.Error(2)
}

func (s *InfraService) GetManifestWithInfraID(infraID string, accessKey string) ([]byte, error) {
args := s.Called(infraID, accessKey)
return args.Get(0).([]byte), args.Error(1)
}

func (s *InfraService) GetInfra(ctx context.Context, projectID string, infraID string) (*model.Infra, error) {
args := s.Called(ctx, projectID, infraID)
return args.Get(0).(*model.Infra), args.Error(1)
}

func (s *InfraService) GetInfraStats(ctx context.Context, projectID string) (*model.GetInfraStatsResponse, error) {
args := s.Called(ctx, projectID)
return args.Get(0).(*model.GetInfraStatsResponse), args.Error(1)
}

func (s *InfraService) GetVersionDetails() (*model.InfraVersionDetails, error) {
args := s.Called()
return args.Get(0).(*model.InfraVersionDetails), args.Error(1)
}

func (s *InfraService) QueryServerVersion(ctx context.Context) (*model.ServerVersionResponse, error) {
args := s.Called(ctx)
return args.Get(0).(*model.ServerVersionResponse), args.Error(1)
}

func (s *InfraService) PodLog(request model.PodLog, r store.StateData) (string, error) {
args := s.Called(request, r)
return args.String(0), args.Error(1)
}

func (s *InfraService) KubeObj(request model.KubeObjectData, r store.StateData) (string, error) {
args := s.Called(request, r)
return args.String(0), args.Error(1)
}

func (s *InfraService) UpdateInfra(query bson.D, update bson.D) error {
args := s.Called(query, update)
return args.Error(0)
}

func (s *InfraService) GetDBInfra(infraID string) (dbChaosInfra.ChaosInfra, error) {
args := s.Called(infraID)
return args.Get(0).(dbChaosInfra.ChaosInfra), args.Error(1)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package mocks

import (
"context"

types "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/choas_experiment_run"
store "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/data-store"
dbChaosExperiment "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/database/mongodb/chaos_experiment"
dbChaosExperimentRun "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/database/mongodb/chaos_experiment_run"
"github.com/stretchr/testify/mock"
"go.mongodb.org/mongo-driver/bson"
)

type ChaosExperimentRunService struct {
mock.Mock
}

// ProcessExperimentRunDelete mocks the ProcessExperimentRunDelete of chaos-experiment-run service
func (c *ChaosExperimentRunService) ProcessExperimentRunDelete(ctx context.Context, query bson.D, workflowRunID *string, experimentRun dbChaosExperimentRun.ChaosExperimentRun, workflow dbChaosExperiment.ChaosExperimentRequest, username string, r *store.StateData) error {
args := c.Called(ctx, query, workflowRunID, experimentRun, workflow, username, r)
return args.Error(0)
}

// ProcessCompletedExperimentRun mocks the ProcessCompletedExperimentRun of chaos-experiment-run service
func (c *ChaosExperimentRunService) ProcessCompletedExperimentRun(execData types.ExecutionData, wfID string, runID string) (types.ExperimentRunMetrics, error) {
args := c.Called(execData, wfID, runID)
return args.Get(0).(types.ExperimentRunMetrics), args.Error(1)
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (c *Operator) GetExperiments(query bson.D) ([]ChaosExperimentRequest, error
ctx, cancel := context.WithTimeout(backgroundContext, 10*time.Second)
defer cancel()

results, err := mongodb.Operator.List(ctx, mongodb.ChaosExperimentCollection, query)
results, err := c.operator.List(ctx, mongodb.ChaosExperimentCollection, query)
if err != nil {
return nil, err
}
Expand All @@ -51,7 +51,7 @@ func (c *Operator) GetExperiments(query bson.D) ([]ChaosExperimentRequest, error
// GetExperiment takes a query parameter to retrieve the experiment details from the database
func (c *Operator) GetExperiment(ctx context.Context, query bson.D) (ChaosExperimentRequest, error) {
var experiment ChaosExperimentRequest
results, err := mongodb.Operator.Get(ctx, mongodb.ChaosExperimentCollection, query)
results, err := c.operator.Get(ctx, mongodb.ChaosExperimentCollection, query)
if err != nil {
return ChaosExperimentRequest{}, err
}
Expand All @@ -69,7 +69,7 @@ func (c *Operator) GetAggregateExperiments(pipeline mongo.Pipeline) (*mongo.Curs
ctx, cancel := context.WithTimeout(backgroundContext, 10*time.Second)
defer cancel()

results, err := mongodb.Operator.Aggregate(ctx, mongodb.ChaosExperimentCollection, pipeline)
results, err := c.operator.Aggregate(ctx, mongodb.ChaosExperimentCollection, pipeline)
if err != nil {
return nil, err
}
Expand All @@ -83,7 +83,7 @@ func (c *Operator) GetExperimentsByInfraID(infraID string) ([]ChaosExperimentReq
defer cancel()

query := bson.D{{"infra_id", infraID}}
results, err := mongodb.Operator.List(ctx, mongodb.ChaosExperimentCollection, query)
results, err := c.operator.List(ctx, mongodb.ChaosExperimentCollection, query)
if err != nil {
return nil, err
}
Expand All @@ -98,7 +98,7 @@ func (c *Operator) GetExperimentsByInfraID(infraID string) ([]ChaosExperimentReq

// InsertChaosExperiment takes details of a experiment and inserts into the database collection
func (c *Operator) InsertChaosExperiment(ctx context.Context, chaosExperiment ChaosExperimentRequest) error {
err := mongodb.Operator.Create(ctx, mongodb.ChaosExperimentCollection, chaosExperiment)
err := c.operator.Create(ctx, mongodb.ChaosExperimentCollection, chaosExperiment)
if err != nil {
return err
}
Expand All @@ -107,7 +107,7 @@ func (c *Operator) InsertChaosExperiment(ctx context.Context, chaosExperiment Ch

// UpdateChaosExperiment takes query and update parameters to update the experiment details in the database
func (c *Operator) UpdateChaosExperiment(ctx context.Context, query bson.D, update bson.D, opts ...*options.UpdateOptions) error {
_, err := mongodb.Operator.Update(ctx, mongodb.ChaosExperimentCollection, query, update, opts...)
_, err := c.operator.Update(ctx, mongodb.ChaosExperimentCollection, query, update, opts...)
if err != nil {
return err
}
Expand All @@ -117,7 +117,7 @@ func (c *Operator) UpdateChaosExperiment(ctx context.Context, query bson.D, upda
// UpdateChaosExperiments takes query and update parameters to updates multiple experiment's details in the database
func (c *Operator) UpdateChaosExperiments(ctx context.Context, query bson.D, update bson.D) error {

_, err := mongodb.Operator.UpdateMany(ctx, mongodb.ChaosExperimentCollection, query, update)
_, err := c.operator.UpdateMany(ctx, mongodb.ChaosExperimentCollection, query, update)
if err != nil {
return err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func NewChaosExperimentRunOperator(mongodbOperator mongodb.MongoOperator) *Opera
}

func (c *Operator) CreateExperimentRun(ctx context.Context, wfRun ChaosExperimentRun) error {
err := mongodb.Operator.Create(ctx, mongodb.ChaosExperimentRunsCollection, wfRun)
err := c.operator.Create(ctx, mongodb.ChaosExperimentRunsCollection, wfRun)
if err != nil {
return err
}
Expand All @@ -37,7 +37,7 @@ func (c *Operator) GetExperimentRuns(query bson.D) ([]ChaosExperimentRun, error)
defer cancel()
var experiments []ChaosExperimentRun

results, err := mongodb.Operator.List(ctx, mongodb.ChaosExperimentRunsCollection, query)
results, err := c.operator.List(ctx, mongodb.ChaosExperimentRunsCollection, query)
if err != nil {
return nil, err
}
Expand All @@ -50,7 +50,7 @@ func (c *Operator) GetExperimentRuns(query bson.D) ([]ChaosExperimentRun, error)
}

func (c *Operator) CountExperimentRuns(ctx context.Context, query bson.D) (int64, error) {
results, err := mongodb.Operator.CountDocuments(ctx, mongodb.ChaosExperimentRunsCollection, query)
results, err := c.operator.CountDocuments(ctx, mongodb.ChaosExperimentRunsCollection, query)
if err != nil {
return 0, err
}
Expand All @@ -62,7 +62,7 @@ func (c *Operator) GetExperimentRun(query bson.D) (ChaosExperimentRun, error) {
defer cancel()

var experimentRun ChaosExperimentRun
results, err := mongodb.Operator.Get(ctx, mongodb.ChaosExperimentRunsCollection, query)
results, err := c.operator.Get(ctx, mongodb.ChaosExperimentRunsCollection, query)
if err != nil {
return ChaosExperimentRun{}, err
}
Expand All @@ -88,7 +88,7 @@ func (c *Operator) UpdateExperimentRun(ctx context.Context, wfRun ChaosExperimen
}
}

count, err := mongodb.Operator.CountDocuments(ctx, mongodb.ChaosExperimentRunsCollection, query)
count, err := c.operator.CountDocuments(ctx, mongodb.ChaosExperimentRunsCollection, query)
if err != nil {
return 0, err
}
Expand All @@ -97,7 +97,7 @@ func (c *Operator) UpdateExperimentRun(ctx context.Context, wfRun ChaosExperimen
if count == 0 {
//Audit details for first time creation
wfRun.CreatedAt = time.Now().UnixMilli()
err := mongodb.Operator.Create(ctx, mongodb.ChaosExperimentRunsCollection, wfRun)
err := c.operator.Create(ctx, mongodb.ChaosExperimentRunsCollection, wfRun)
if err != nil {
return 0, err
}
Expand Down Expand Up @@ -134,7 +134,7 @@ func (c *Operator) UpdateExperimentRun(ctx context.Context, wfRun ChaosExperimen
{"is_removed", wfRun.IsRemoved},
}}}

result, err := mongodb.Operator.Update(ctx, mongodb.ChaosExperimentRunsCollection, updateQuery, update)
result, err := c.operator.Update(ctx, mongodb.ChaosExperimentRunsCollection, updateQuery, update)
if err != nil {
return 0, err
}
Expand All @@ -145,7 +145,7 @@ func (c *Operator) UpdateExperimentRun(ctx context.Context, wfRun ChaosExperimen
}

func (c *Operator) UpdateExperimentRunWithQuery(ctx context.Context, query bson.D, update bson.D) error {
_, err := mongodb.Operator.Update(ctx, mongodb.ChaosExperimentRunsCollection, query, update)
_, err := c.operator.Update(ctx, mongodb.ChaosExperimentRunsCollection, query, update)
if err != nil {
return err
}
Expand All @@ -155,7 +155,7 @@ func (c *Operator) UpdateExperimentRunWithQuery(ctx context.Context, query bson.

func (c *Operator) UpdateExperimentRunsWithQuery(ctx context.Context, query bson.D, update bson.D) error {

_, err := mongodb.Operator.UpdateMany(ctx, mongodb.ChaosExperimentRunsCollection, query, update)
_, err := c.operator.UpdateMany(ctx, mongodb.ChaosExperimentRunsCollection, query, update)
if err != nil {
return err
}
Expand All @@ -169,7 +169,7 @@ func (c *Operator) GetExperimentRunsByInfraID(infraID string) ([]ChaosExperiment
defer cancel()

query := bson.D{{"infra_id", infraID}}
results, err := mongodb.Operator.List(ctx, mongodb.ChaosExperimentRunsCollection, query)
results, err := c.operator.List(ctx, mongodb.ChaosExperimentRunsCollection, query)
if err != nil {
return nil, err
}
Expand All @@ -187,7 +187,7 @@ func (c *Operator) GetAggregateExperimentRuns(pipeline mongo.Pipeline) (*mongo.C
ctx, cancel := context.WithTimeout(backgroundContext, 10*time.Second)
defer cancel()

results, err := mongodb.Operator.Aggregate(ctx, mongodb.ChaosExperimentRunsCollection, pipeline)
results, err := c.operator.Aggregate(ctx, mongodb.ChaosExperimentRunsCollection, pipeline)
if err != nil {
return nil, err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func NewInfrastructureOperator(mongodbOperator mongodb.MongoOperator) *Operator

// InsertInfra takes details of a chaos_infra and inserts into the database collection
func (c *Operator) InsertInfra(ctx context.Context, infra ChaosInfra) error {
err := mongodb.Operator.Create(ctx, mongodb.ChaosInfraCollection, infra)
err := c.operator.Create(ctx, mongodb.ChaosInfraCollection, infra)
if err != nil {
return err
}
Expand All @@ -44,7 +44,7 @@ func (c *Operator) GetInfra(infraID string) (ChaosInfra, error) {
query := bson.D{{"infra_id", infraID}}

var infra ChaosInfra
result, err := mongodb.Operator.Get(ctx, mongodb.ChaosInfraCollection, query)
result, err := c.operator.Get(ctx, mongodb.ChaosInfraCollection, query)
err = result.Decode(&infra)
if err != nil {
return ChaosInfra{}, err
Expand All @@ -61,7 +61,7 @@ func (c *Operator) GetInfraDetails(ctx context.Context, infraID string, projectI
}

var infra ChaosInfra
result, err := mongodb.Operator.Get(ctx, mongodb.ChaosInfraCollection, query)
result, err := c.operator.Get(ctx, mongodb.ChaosInfraCollection, query)
err = result.Decode(&infra)
if err != nil {
return ChaosInfra{}, err
Expand All @@ -72,7 +72,7 @@ func (c *Operator) GetInfraDetails(ctx context.Context, infraID string, projectI

// UpdateInfra takes query and update parameters to update the chaos_infra details in the database
func (c *Operator) UpdateInfra(ctx context.Context, query bson.D, update bson.D) error {
_, err := mongodb.Operator.UpdateMany(ctx, mongodb.ChaosInfraCollection, query, update)
_, err := c.operator.UpdateMany(ctx, mongodb.ChaosInfraCollection, query, update)
if err != nil {
return err
}
Expand All @@ -93,7 +93,7 @@ func (c *Operator) GetInfraWithProjectID(projectID string) ([]*ChaosInfra, error
defer cancel()

var infras []*ChaosInfra
results, err := mongodb.Operator.List(ctx, mongodb.ChaosInfraCollection, query)
results, err := c.operator.List(ctx, mongodb.ChaosInfraCollection, query)
if err != nil {
return []*ChaosInfra{}, err
}
Expand All @@ -109,7 +109,7 @@ func (c *Operator) GetInfraWithProjectID(projectID string) ([]*ChaosInfra, error
// GetInfras returns all the infras matching the query
func (c *Operator) GetInfras(ctx context.Context, query bson.D) ([]ChaosInfra, error) {
var infras []ChaosInfra
results, err := mongodb.Operator.List(ctx, mongodb.ChaosInfraCollection, query)
results, err := c.operator.List(ctx, mongodb.ChaosInfraCollection, query)
if err != nil {
return []ChaosInfra{}, err
}
Expand All @@ -125,7 +125,7 @@ func (c *Operator) GetAggregateInfras(pipeline mongo.Pipeline) (*mongo.Cursor, e
ctx, cancel := context.WithTimeout(backgroundContext, 10*time.Second)
defer cancel()

results, err := mongodb.Operator.Aggregate(ctx, mongodb.ChaosInfraCollection, pipeline)
results, err := c.operator.Aggregate(ctx, mongodb.ChaosInfraCollection, pipeline)
if err != nil {
return nil, err
}
Expand Down
Loading

0 comments on commit ff599d4

Please sign in to comment.