Skip to content

Commit

Permalink
Refactor: Eliminate Global Variable to Enhance Testability
Browse files Browse the repository at this point in the history
  • Loading branch information
andoriyaprashant committed Dec 17, 2024
1 parent 89d7f24 commit 918c112
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 20 deletions.
2 changes: 2 additions & 0 deletions chaoscenter/authentication/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ golang.org/x/crypto v0.26.0/go.mod h1:GY7jblb9wI+FOo5y8/S2oY4zWP07AkOJ4+jxCqdqn5
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.17.0 h1:zY54UmvipHiNd+pm+m0x9KhZ9hl1/7QNMyxXbc6ICqA=
golang.org/x/mod v0.17.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/mod v0.16.0 h1:QX4fJ0Rr5cPQCF7O9lh9Se4pmwfwskqZfq5moyldzic=
golang.org/x/mod v0.16.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
Expand Down
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 @@ -9,21 +9,32 @@ import (
"go.mongodb.org/mongo-driver/mongo"
)

type Operator struct {
operator mongodb.MongoOperator
}

func NewConfigOperator(mongodbOperator mongodb.MongoOperator) *Operator {
return &Operator{
operator: mongodbOperator,
}
}


// 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 (o *Operator) CreateConfig(ctx context.Context, config *ServerConfig) error {
err := o.operator.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 (o *Operator) GetConfig(ctx context.Context, key string) (*ServerConfig, error) {
query := bson.D{
{"key", key},
}
results, err := mongodb.Operator.Get(ctx, mongodb.ServerConfigCollection, query)
results, err := o.operator.Get(ctx, mongodb.ServerConfigCollection, query)
if err != nil {
return nil, err
}
Expand All @@ -39,14 +50,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 (o *Operator) UpdateConfig(ctx context.Context, key string, value interface{}) error {
query := bson.D{
{"key", key},
}
update := bson.D{{"$set", bson.D{{
"value", value}},
}}
_, err := mongodb.Operator.Update(ctx, mongodb.ServerConfigCollection, query, update)
_, err := o.operator.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
5 changes: 0 additions & 5 deletions chaoscenter/graphql/server/pkg/database/mongodb/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,6 @@ type MongoOperations struct {
MongoClient *MongoClient
}

var (
// Operator contains all the CRUD operations of the mongo database
Operator MongoOperator = &MongoOperations{}
)

func NewMongoOperations(mongoClient *MongoClient) *MongoOperations {
return &MongoOperations{
MongoClient: mongoClient,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import (
"go.mongodb.org/mongo-driver/mongo"
)

// Operator is the model for probe collection
// Operator is the model for probe operations and collection
type Operator struct {
operator mongodb.MongoOperator
}

// NewChaosProbeOperator returns a new instance of Operator
func NewChaosProbeOperator(mongodbOperator mongodb.MongoOperator) *Operator {
// NewProbeOperator returns a new instance of Operator
func NewProbeOperator(mongodbOperator mongodb.MongoOperator) *Operator {
return &Operator{
operator: mongodbOperator,
}
Expand Down
18 changes: 16 additions & 2 deletions chaoscenter/graphql/server/pkg/handlers/readiness_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,19 @@ import (
"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 @@ -24,10 +37,11 @@ func contains(s []string, str string) bool {
return false
}

func ReadinessHandler() gin.HandlerFunc {
// ReadinessHandler returns a handler function for readiness checks
func (r *Operator) ReadinessHandler() gin.HandlerFunc {
return func(c *gin.Context) {
var dbFlag = "up"
dbs, err := mongodb.Operator.ListDataBase(context.Background(), mongodb.MgoClient)
dbs, err := r.mongoOperator.ListDataBase(context.Background(), mongodb.MgoClient)
if err != nil {
dbFlag = "down"
}
Expand Down
14 changes: 13 additions & 1 deletion chaoscenter/graphql/server/pkg/probe/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,18 @@ type probeService struct {
probeOperator *dbSchemaProbe.Operator
}

type Operator struct {
operator mongodb.MongoOperator
}

// NewProbeOperator returns a new instance of Operator
func NewProbeOperator(mongodbOperator mongodb.MongoOperator) *Operator {
return &Operator{
operator: mongodbOperator,
}
}

// NewProbeService returns a new instance of probeService
func NewProbeService(probeOperator *dbSchemaProbe.Operator) Service {
return &probeService{
probeOperator: probeOperator,
Expand Down Expand Up @@ -405,7 +417,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 @@ -90,7 +90,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 918c112

Please sign in to comment.