Skip to content

Commit e13e244

Browse files
Refactor: Eliminate Global Variable to Enhance Testability
Signed-off-by: andoriyaprashant <prashantandoriya@gmail.com>
1 parent 89d7f24 commit e13e244

File tree

9 files changed

+53
-20
lines changed

9 files changed

+53
-20
lines changed

chaoscenter/authentication/go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,8 @@ golang.org/x/crypto v0.26.0/go.mod h1:GY7jblb9wI+FOo5y8/S2oY4zWP07AkOJ4+jxCqdqn5
131131
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
132132
golang.org/x/mod v0.17.0 h1:zY54UmvipHiNd+pm+m0x9KhZ9hl1/7QNMyxXbc6ICqA=
133133
golang.org/x/mod v0.17.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
134+
golang.org/x/mod v0.16.0 h1:QX4fJ0Rr5cPQCF7O9lh9Se4pmwfwskqZfq5moyldzic=
135+
golang.org/x/mod v0.16.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
134136
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
135137
golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
136138
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=

chaoscenter/graphql/server/pkg/database/mongodb/chaos_hub/operations.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ func (c *Operator) UpdateChaosHub(ctx context.Context, query bson.D, update bson
9292

9393
// GetAggregateChaosHubs takes a mongo pipeline to retrieve the project details from the database
9494
func (c *Operator) GetAggregateChaosHubs(ctx context.Context, pipeline mongo.Pipeline) (*mongo.Cursor, error) {
95-
results, err := mongodb.Operator.Aggregate(ctx, mongodb.ChaosHubCollection, pipeline)
95+
results, err := c.operator.Aggregate(ctx, mongodb.ChaosHubCollection, pipeline)
9696
if err != nil {
9797
return nil, fmt.Errorf("error on getting the chaos hubs : %v", err)
9898
}

chaoscenter/graphql/server/pkg/database/mongodb/config/operations.go

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,32 @@ import (
99
"go.mongodb.org/mongo-driver/mongo"
1010
)
1111

12+
type Operator struct {
13+
operator mongodb.MongoOperator
14+
}
15+
16+
func NewConfigOperator(mongodbOperator mongodb.MongoOperator) *Operator {
17+
return &Operator{
18+
operator: mongodbOperator,
19+
}
20+
}
21+
22+
1223
// CreateConfig creates a new server config with unique key
13-
func CreateConfig(ctx context.Context, config *ServerConfig) error {
14-
err := mongodb.Operator.Create(ctx, mongodb.ServerConfigCollection, config)
24+
func (o *Operator) CreateConfig(ctx context.Context, config *ServerConfig) error {
25+
err := o.operator.Create(ctx, mongodb.ServerConfigCollection, config)
1526
if err != nil {
1627
return err
1728
}
1829
return nil
1930
}
2031

2132
// GetConfig returns the requested server config
22-
func GetConfig(ctx context.Context, key string) (*ServerConfig, error) {
33+
func (o *Operator) GetConfig(ctx context.Context, key string) (*ServerConfig, error) {
2334
query := bson.D{
2435
{"key", key},
2536
}
26-
results, err := mongodb.Operator.Get(ctx, mongodb.ServerConfigCollection, query)
37+
results, err := o.operator.Get(ctx, mongodb.ServerConfigCollection, query)
2738
if err != nil {
2839
return nil, err
2940
}
@@ -39,14 +50,14 @@ func GetConfig(ctx context.Context, key string) (*ServerConfig, error) {
3950
}
4051

4152
// UpdateConfig updates the required server config
42-
func UpdateConfig(ctx context.Context, key string, value interface{}) error {
53+
func (o *Operator) UpdateConfig(ctx context.Context, key string, value interface{}) error {
4354
query := bson.D{
4455
{"key", key},
4556
}
4657
update := bson.D{{"$set", bson.D{{
4758
"value", value}},
4859
}}
49-
_, err := mongodb.Operator.Update(ctx, mongodb.ServerConfigCollection, query, update)
60+
_, err := o.operator.Update(ctx, mongodb.ServerConfigCollection, query, update)
5061
if err != nil {
5162
return err
5263
}

chaoscenter/graphql/server/pkg/database/mongodb/environments/operations.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ func (e *Operator) GetEnvironmentWithProjectID(projectID string) ([]*Environment
9090
defer cancel()
9191

9292
var environments []*Environment
93-
results, err := mongodb.Operator.List(ctx, mongodb.EnvironmentCollection, query)
93+
results, err := e.operator.List(ctx, mongodb.EnvironmentCollection, query)
9494
if err != nil {
9595
return []*Environment{}, err
9696
}

chaoscenter/graphql/server/pkg/database/mongodb/operations.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,6 @@ type MongoOperations struct {
3333
MongoClient *MongoClient
3434
}
3535

36-
var (
37-
// Operator contains all the CRUD operations of the mongo database
38-
Operator MongoOperator = &MongoOperations{}
39-
)
40-
4136
func NewMongoOperations(mongoClient *MongoClient) *MongoOperations {
4237
return &MongoOperations{
4338
MongoClient: mongoClient,

chaoscenter/graphql/server/pkg/database/mongodb/probe/operations.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ import (
1010
"go.mongodb.org/mongo-driver/mongo"
1111
)
1212

13-
// Operator is the model for probe collection
13+
// Operator is the model for probe operations and collection
1414
type Operator struct {
1515
operator mongodb.MongoOperator
1616
}
1717

18-
// NewChaosProbeOperator returns a new instance of Operator
19-
func NewChaosProbeOperator(mongodbOperator mongodb.MongoOperator) *Operator {
18+
// NewProbeOperator returns a new instance of Operator
19+
func NewProbeOperator(mongodbOperator mongodb.MongoOperator) *Operator {
2020
return &Operator{
2121
operator: mongodbOperator,
2222
}

chaoscenter/graphql/server/pkg/handlers/readiness_handler.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,19 @@ import (
99
"github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/database/mongodb"
1010
)
1111

12+
// Operator encapsulates the MongoDB operations
13+
type Operator struct {
14+
mongoOperator mongodb.MongoOperator
15+
}
16+
17+
// NewOperator returns a new instance of Operator
18+
func NewOperator(mongoOperator mongodb.MongoOperator) *Operator {
19+
return &Operator{
20+
mongoOperator: mongoOperator,
21+
}
22+
}
23+
24+
// ReadinessAPIStatus represents the readiness status of the API
1225
type ReadinessAPIStatus struct {
1326
DataBase string `json:"database"`
1427
Collections string `json:"collections"`
@@ -24,10 +37,11 @@ func contains(s []string, str string) bool {
2437
return false
2538
}
2639

27-
func ReadinessHandler() gin.HandlerFunc {
40+
// ReadinessHandler returns a handler function for readiness checks
41+
func (r *Operator) ReadinessHandler() gin.HandlerFunc {
2842
return func(c *gin.Context) {
2943
var dbFlag = "up"
30-
dbs, err := mongodb.Operator.ListDataBase(context.Background(), mongodb.MgoClient)
44+
dbs, err := r.mongoOperator.ListDataBase(context.Background(), mongodb.MgoClient)
3145
if err != nil {
3246
dbFlag = "down"
3347
}

chaoscenter/graphql/server/pkg/probe/handler/handler.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,18 @@ type probeService struct {
4646
probeOperator *dbSchemaProbe.Operator
4747
}
4848

49+
type Operator struct {
50+
operator mongodb.MongoOperator
51+
}
52+
53+
// NewProbeOperator returns a new instance of Operator
54+
func NewProbeOperator(mongodbOperator mongodb.MongoOperator) *Operator {
55+
return &Operator{
56+
operator: mongodbOperator,
57+
}
58+
}
59+
60+
// NewProbeService returns a new instance of probeService
4961
func NewProbeService(probeOperator *dbSchemaProbe.Operator) Service {
5062
return &probeService{
5163
probeOperator: probeOperator,
@@ -405,7 +417,7 @@ func GetProbeExecutionHistoryInExperimentRuns(projectID string, probeName string
405417
pipeline = append(pipeline, matchIdentifierStage)
406418

407419
// Call aggregation on pipeline
408-
experimentRunOperator := dbChaosExperimentRun.NewChaosExperimentRunOperator(mongodb.Operator)
420+
experimentRunOperator := dbChaosExperimentRun.NewChaosExperimentRunOperator(NewProbeOperator.operator)
409421
expRunCursor, err := experimentRunOperator.GetAggregateExperimentRuns(pipeline)
410422
if err != nil {
411423
return nil, errors.New("DB aggregate stage error: " + err.Error())

chaoscenter/graphql/server/server.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@ func main() {
9090
mongoClient := mongodb.Client.Initialize(mongodb.MgoClient)
9191

9292
var mongodbOperator mongodb.MongoOperator = mongodb.NewMongoOperations(mongoClient)
93-
mongodb.Operator = mongodbOperator
9493

9594
if err := validateVersion(); err != nil {
9695
log.Fatal(err)

0 commit comments

Comments
 (0)