Skip to content

Commit

Permalink
Refactor: Eliminate Global Variable to Enhance Testability
Browse files Browse the repository at this point in the history
Signed-off-by: andoriyaprashant <[email protected]>
  • Loading branch information
andoriyaprashant committed Jun 4, 2024
1 parent 720214c commit c471726
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,20 @@ 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
}
return nil
}

// 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
}
Expand All @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
37 changes: 24 additions & 13 deletions chaoscenter/graphql/server/pkg/database/mongodb/probe/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,35 @@ 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
}
return nil
}

// 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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
Expand Down
20 changes: 16 additions & 4 deletions chaoscenter/graphql/server/pkg/handlers/readiness_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand All @@ -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"
}
Expand Down
13 changes: 10 additions & 3 deletions chaoscenter/graphql/server/pkg/probe/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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())
Expand Down
1 change: 0 additions & 1 deletion chaoscenter/graphql/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit c471726

Please sign in to comment.