Refactor: Eliminate Global Variable to Enhance Testability
Signed-off-by: andoriyaprashant <prashantandoriya@gmail.com>
This commit is contained in:
parent
91e910f3a0
commit
e4480622cb
|
|
@ -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=
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,9 +9,20 @@ 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
|
||||
}
|
||||
|
|
@ -19,11 +30,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 (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
|
||||
}
|
||||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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"`
|
||||
|
|
@ -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"
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
@ -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())
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
Loading…
Reference in New Issue