From aff6467ebd7cfd50b969159ca7746aa5035ca55c Mon Sep 17 00:00:00 2001 From: andoriyaprashant Date: Mon, 3 Jun 2024 23:40:19 +0530 Subject: [PATCH] Refactor: Eliminate Global Variable to Enhance Testability --- .../database/mongodb/chaos_hub/operations.go | 2 +- .../pkg/database/mongodb/config/operations.go | 12 +++--- .../mongodb/environments/operations.go | 2 +- .../server/pkg/database/mongodb/operations.go | 2 +- .../pkg/database/mongodb/probe/operations.go | 37 ++++++++++++------- .../server/pkg/handlers/readiness_handler.go | 20 ++++++++-- .../server/pkg/probe/handler/handler.go | 13 +++++-- chaoscenter/graphql/server/server.go | 1 - 8 files changed, 59 insertions(+), 30 deletions(-) diff --git a/chaoscenter/graphql/server/pkg/database/mongodb/chaos_hub/operations.go b/chaoscenter/graphql/server/pkg/database/mongodb/chaos_hub/operations.go index faa8c1faf06..ca5550ab6a1 100644 --- a/chaoscenter/graphql/server/pkg/database/mongodb/chaos_hub/operations.go +++ b/chaoscenter/graphql/server/pkg/database/mongodb/chaos_hub/operations.go @@ -92,7 +92,7 @@ func (c *Operator) UpdateChaosHub(ctx context.Context, query bson.D, update bson // GetAggregateChaosHubs takes a mongo pipeline to retrieve the project details from the database func (c *Operator) GetAggregateChaosHubs(ctx context.Context, pipeline mongo.Pipeline) (*mongo.Cursor, error) { - results, err := mongodb.Operator.Aggregate(ctx, mongodb.ChaosHubCollection, pipeline) + results, err := c.operator.Aggregate(ctx, mongodb.ChaosHubCollection, pipeline) if err != nil { return nil, fmt.Errorf("error on getting the chaos hubs : %v", err) } diff --git a/chaoscenter/graphql/server/pkg/database/mongodb/config/operations.go b/chaoscenter/graphql/server/pkg/database/mongodb/config/operations.go index 7033b82273b..5fd0c0ef5db 100644 --- a/chaoscenter/graphql/server/pkg/database/mongodb/config/operations.go +++ b/chaoscenter/graphql/server/pkg/database/mongodb/config/operations.go @@ -10,8 +10,8 @@ import ( ) // CreateConfig creates a new server config with unique key -func CreateConfig(ctx context.Context, config *ServerConfig) error { - err := mongodb.Operator.Create(ctx, mongodb.ServerConfigCollection, config) +func CreateConfig(ctx context.Context, config *ServerConfig, mongodbOperator mongodb.MongoOperator) error { + err := mongodbOperator.Create(ctx, mongodb.ServerConfigCollection, config) if err != nil { return err } @@ -19,11 +19,11 @@ func CreateConfig(ctx context.Context, config *ServerConfig) error { } // GetConfig returns the requested server config -func GetConfig(ctx context.Context, key string) (*ServerConfig, error) { +func GetConfig(ctx context.Context, key string, mongodbOperator mongodb.MongoOperator) (*ServerConfig, error) { query := bson.D{ {"key", key}, } - results, err := mongodb.Operator.Get(ctx, mongodb.ServerConfigCollection, query) + results, err := mongodbOperator.Get(ctx, mongodb.ServerConfigCollection, query) if err != nil { return nil, err } @@ -39,14 +39,14 @@ func GetConfig(ctx context.Context, key string) (*ServerConfig, error) { } // UpdateConfig updates the required server config -func UpdateConfig(ctx context.Context, key string, value interface{}) error { +func UpdateConfig(ctx context.Context, key string, value interface{}, mongodbOperator mongodb.MongoOperator) error { query := bson.D{ {"key", key}, } update := bson.D{{"$set", bson.D{{ "value", value}}, }} - _, err := mongodb.Operator.Update(ctx, mongodb.ServerConfigCollection, query, update) + _, err := mongodbOperator.Update(ctx, mongodb.ServerConfigCollection, query, update) if err != nil { return err } diff --git a/chaoscenter/graphql/server/pkg/database/mongodb/environments/operations.go b/chaoscenter/graphql/server/pkg/database/mongodb/environments/operations.go index 6cff68e1451..72f98abb9bc 100644 --- a/chaoscenter/graphql/server/pkg/database/mongodb/environments/operations.go +++ b/chaoscenter/graphql/server/pkg/database/mongodb/environments/operations.go @@ -90,7 +90,7 @@ func (e *Operator) GetEnvironmentWithProjectID(projectID string) ([]*Environment defer cancel() var environments []*Environment - results, err := mongodb.Operator.List(ctx, mongodb.EnvironmentCollection, query) + results, err := e.operator.List(ctx, mongodb.EnvironmentCollection, query) if err != nil { return []*Environment{}, err } diff --git a/chaoscenter/graphql/server/pkg/database/mongodb/operations.go b/chaoscenter/graphql/server/pkg/database/mongodb/operations.go index d5a5606b4e9..c8311c2d1b1 100644 --- a/chaoscenter/graphql/server/pkg/database/mongodb/operations.go +++ b/chaoscenter/graphql/server/pkg/database/mongodb/operations.go @@ -33,7 +33,7 @@ type MongoOperations struct { var ( // Operator contains all the CRUD operations of the mongo database - Operator MongoOperator = &MongoOperations{} + ) func NewMongoOperations(mongoClient *MongoClient) *MongoOperations { diff --git a/chaoscenter/graphql/server/pkg/database/mongodb/probe/operations.go b/chaoscenter/graphql/server/pkg/database/mongodb/probe/operations.go index 5f6ad864ed6..0424453d89f 100644 --- a/chaoscenter/graphql/server/pkg/database/mongodb/probe/operations.go +++ b/chaoscenter/graphql/server/pkg/database/mongodb/probe/operations.go @@ -5,15 +5,26 @@ import ( "errors" "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/database/mongodb" - "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/mongo" ) +// Operator is the model for probe operations +type Operator struct { + operator mongodb.MongoOperator +} + +// NewProbeOperator returns a new instance of Operator +func NewProbeOperator(mongodbOperator mongodb.MongoOperator) *Operator { + return &Operator{ + operator: mongodbOperator, + } +} + // CreateProbe creates a probe of a specific type (HTTP, PROM, K8s or CMD) // as a shared entity in the database -func CreateProbe(ctx context.Context, probe Probe) error { - err := mongodb.Operator.Create(ctx, mongodb.ChaosProbeCollection, probe) +func (p *Operator) CreateProbe(ctx context.Context, probe Probe) error { + err := p.operator.Create(ctx, mongodb.ChaosProbeCollection, probe) if err != nil { return err } @@ -21,8 +32,8 @@ func CreateProbe(ctx context.Context, probe Probe) error { } // GetAggregateProbes takes a mongo pipeline to retrieve the project details from the database -func GetAggregateProbes(ctx context.Context, pipeline mongo.Pipeline) (*mongo.Cursor, error) { - results, err := mongodb.Operator.Aggregate(ctx, mongodb.ChaosProbeCollection, pipeline) +func (p *Operator) GetAggregateProbes(ctx context.Context, pipeline mongo.Pipeline) (*mongo.Cursor, error) { + results, err := p.operator.Aggregate(ctx, mongodb.ChaosProbeCollection, pipeline) if err != nil { return nil, err } @@ -31,8 +42,8 @@ func GetAggregateProbes(ctx context.Context, pipeline mongo.Pipeline) (*mongo.Cu } // IsProbeUnique returns true if probe is unique -func IsProbeUnique(ctx context.Context, query bson.D) (bool, error) { - count, err := mongodb.Operator.CountDocuments(ctx, mongodb.ChaosProbeCollection, query) +func (p *Operator) IsProbeUnique(ctx context.Context, query bson.D) (bool, error) { + count, err := p.operator.CountDocuments(ctx, mongodb.ChaosProbeCollection, query) if err != nil { return false, err } @@ -44,8 +55,8 @@ func IsProbeUnique(ctx context.Context, query bson.D) (bool, error) { } // UpdateProbe updates details of a Probe -func UpdateProbe(ctx context.Context, query bson.D, updateQuery bson.D) (*mongo.UpdateResult, error) { - result, err := mongodb.Operator.Update(ctx, mongodb.ChaosProbeCollection, query, updateQuery) +func (p *Operator) UpdateProbe(ctx context.Context, query bson.D, updateQuery bson.D) (*mongo.UpdateResult, error) { + result, err := p.operator.Update(ctx, mongodb.ChaosProbeCollection, query, updateQuery) if err != nil { return nil, err } @@ -56,8 +67,8 @@ func UpdateProbe(ctx context.Context, query bson.D, updateQuery bson.D) (*mongo. } // UpdateProbes updates details of Probe -func UpdateProbes(ctx context.Context, query bson.D, updateQuery bson.D) (*mongo.UpdateResult, error) { - result, err := mongodb.Operator.UpdateMany(ctx, mongodb.ChaosProbeCollection, query, updateQuery) +func (p *Operator) UpdateProbes(ctx context.Context, query bson.D, updateQuery bson.D) (*mongo.UpdateResult, error) { + result, err := p.operator.UpdateMany(ctx, mongodb.ChaosProbeCollection, query, updateQuery) if err != nil { return nil, err } @@ -68,9 +79,9 @@ func UpdateProbes(ctx context.Context, query bson.D, updateQuery bson.D) (*mongo } // GetProbeByName fetches the details of a single Probe with its Probe Name -func GetProbeByName(ctx context.Context, probeName string, projectID string) (Probe, error) { +func (p *Operator) GetProbeByName(ctx context.Context, probeName string, projectID string) (Probe, error) { var probe Probe - result, err := mongodb.Operator.Get(ctx, mongodb.ChaosProbeCollection, bson.D{{"name", probeName}, {"project_id", projectID}, {"is_removed", false}}) + result, err := p.operator.Get(ctx, mongodb.ChaosProbeCollection, bson.D{{"name", probeName}, {"project_id", projectID}, {"is_removed", false}}) err = result.Decode(&probe) if err != nil { return Probe{}, err diff --git a/chaoscenter/graphql/server/pkg/handlers/readiness_handler.go b/chaoscenter/graphql/server/pkg/handlers/readiness_handler.go index 98ce0220058..95448cc3c72 100644 --- a/chaoscenter/graphql/server/pkg/handlers/readiness_handler.go +++ b/chaoscenter/graphql/server/pkg/handlers/readiness_handler.go @@ -5,10 +5,22 @@ import ( "net/http" "github.com/gin-gonic/gin" - "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/database/mongodb" ) +// Operator encapsulates the MongoDB operations +type Operator struct { + mongoOperator mongodb.MongoOperator +} + +// NewOperator returns a new instance of Operator +func NewOperator(mongoOperator mongodb.MongoOperator) *Operator { + return &Operator{ + mongoOperator: mongoOperator, + } +} + +// ReadinessAPIStatus represents the readiness status of the API type ReadinessAPIStatus struct { DataBase string `json:"database"` Collections string `json:"collections"` @@ -20,14 +32,14 @@ func contains(s []string, str string) bool { return true } } - return false } -func ReadinessHandler() gin.HandlerFunc { +// ReadinessHandler returns a handler function for readiness checks +func (o *Operator) ReadinessHandler() gin.HandlerFunc { return func(c *gin.Context) { var dbFlag = "up" - dbs, err := mongodb.Operator.ListDataBase(context.Background(), mongodb.MgoClient) + dbs, err := o.mongoOperator.ListDataBase(context.Background(), mongodb.MgoClient) if err != nil { dbFlag = "down" } diff --git a/chaoscenter/graphql/server/pkg/probe/handler/handler.go b/chaoscenter/graphql/server/pkg/probe/handler/handler.go index 680a1053e40..93c2d37926a 100644 --- a/chaoscenter/graphql/server/pkg/probe/handler/handler.go +++ b/chaoscenter/graphql/server/pkg/probe/handler/handler.go @@ -38,8 +38,15 @@ type Service interface { type probe struct{} -func NewProbeService() Service { - return &probe{} +type Operator struct { + operator mongodb.MongoOperator +} + +// NewProbeOperator retuurns a new instance of operator +func NewProbeOperator(mongodbOperator mongodb.MongoOperator) *Operator { + return &Operator{ + operator: mongodbOperator, + } } func Error(logFields logrus.Fields, message string) error { @@ -395,7 +402,7 @@ func GetProbeExecutionHistoryInExperimentRuns(projectID string, probeName string pipeline = append(pipeline, matchIdentifierStage) // Call aggregation on pipeline - experimentRunOperator := dbChaosExperimentRun.NewChaosExperimentRunOperator(mongodb.Operator) + experimentRunOperator := dbChaosExperimentRun.NewChaosExperimentRunOperator(NewProbeOperator.operator) expRunCursor, err := experimentRunOperator.GetAggregateExperimentRuns(pipeline) if err != nil { return nil, errors.New("DB aggregate stage error: " + err.Error()) diff --git a/chaoscenter/graphql/server/server.go b/chaoscenter/graphql/server/server.go index d137e555491..10f39c90122 100644 --- a/chaoscenter/graphql/server/server.go +++ b/chaoscenter/graphql/server/server.go @@ -97,7 +97,6 @@ func main() { mongoClient := mongodb.Client.Initialize(mongodb.MgoClient) var mongodbOperator mongodb.MongoOperator = mongodb.NewMongoOperations(mongoClient) - mongodb.Operator = mongodbOperator if err := validateVersion(); err != nil { log.Fatal(err)