Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add operator interface for Probe pkg #4849

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
8 changes: 5 additions & 3 deletions chaoscenter/graphql/server/graph/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/database/mongodb/environments"
gitops2 "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/database/mongodb/gitops"
image_registry2 "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/database/mongodb/image_registry"
dbSchemaProbe "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/database/mongodb/probe"
envHandler "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/environment/handler"
gitops3 "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/gitops"
"github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/image_registry"
Expand Down Expand Up @@ -55,9 +56,10 @@ func NewConfig(mongodbOperator mongodb.MongoOperator) generated.Config {
gitopsOperator := gitops2.NewGitOpsOperator(mongodbOperator)
imageRegistryOperator := image_registry2.NewImageRegistryOperator(mongodbOperator)
EnvironmentOperator := environments.NewEnvironmentOperator(mongodbOperator)
probeOperator := dbSchemaProbe.NewChaosProbeOperator(mongodbOperator)

//service
probeService := probe.NewProbeService()
probeService := probe.NewProbeService(probeOperator)
chaosHubService := chaoshub.NewService(chaosHubOperator)
chaosInfrastructureService := chaos_infrastructure.NewChaosInfrastructureService(chaosInfraOperator, EnvironmentOperator)
chaosExperimentService := chaos_experiment2.NewChaosExperimentService(chaosExperimentOperator, chaosInfraOperator, chaosExperimentRunOperator, probeService)
Expand All @@ -67,8 +69,8 @@ func NewConfig(mongodbOperator mongodb.MongoOperator) generated.Config {
environmentService := envHandler.NewEnvironmentService(EnvironmentOperator)

//handler
chaosExperimentHandler := handler.NewChaosExperimentHandler(chaosExperimentService, chaosExperimentRunService, chaosInfrastructureService, gitOpsService, chaosExperimentOperator, chaosExperimentRunOperator, mongodbOperator)
choasExperimentRunHandler := runHandler.NewChaosExperimentRunHandler(chaosExperimentRunService, chaosInfrastructureService, gitOpsService, chaosExperimentOperator, chaosExperimentRunOperator, mongodbOperator)
chaosExperimentHandler := handler.NewChaosExperimentHandler(chaosExperimentService, chaosExperimentRunService, chaosInfrastructureService, gitOpsService, chaosExperimentOperator, chaosExperimentRunOperator, probeOperator, mongodbOperator)
choasExperimentRunHandler := runHandler.NewChaosExperimentRunHandler(chaosExperimentRunService, chaosInfrastructureService, gitOpsService, chaosExperimentOperator, chaosExperimentRunOperator, probeOperator, mongodbOperator)

config := generated.Config{
Resolvers: &Resolver{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"testing"

dbSchemaProbe "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/database/mongodb/probe"

"github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/chaos_experiment/handler"
chaosExperimentMocks "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/chaos_experiment/model/mocks"
chaosExperimentRunMocks "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/chaos_experiment_run/model/mocks"
Expand Down Expand Up @@ -45,8 +47,9 @@ func NewMockServices() *MockServices {
chaosExperimentOperator = dbChaosExperiment.NewChaosExperimentOperator(mongodbMockOperator)
chaosExperimentRunOperator = dbChaosExperimentRun.NewChaosExperimentRunOperator(mongodbMockOperator)
chaosExperimentService = new(chaosExperimentMocks.ChaosExperimentService)
probeOperator = dbSchemaProbe.NewChaosProbeOperator(mongodbMockOperator)
)
var chaosExperimentHandler = handler.NewChaosExperimentHandler(chaosExperimentService, chaosExperimentRunService, infrastructureService, gitOpsService, chaosExperimentOperator, chaosExperimentRunOperator, mongodbMockOperator)
var chaosExperimentHandler = handler.NewChaosExperimentHandler(chaosExperimentService, chaosExperimentRunService, infrastructureService, gitOpsService, chaosExperimentOperator, chaosExperimentRunOperator, probeOperator, mongodbMockOperator)
return &MockServices{
ChaosExperimentService: chaosExperimentService,
ChaosExperimentRunService: chaosExperimentRunService,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ type ChaosExperimentHandler struct {
gitOpsService gitops.Service
chaosExperimentOperator *dbChaosExperiment.Operator
chaosExperimentRunOperator *dbChaosExperimentRun.Operator
probeOperator *dbSchemaProbe.Operator
mongodbOperator mongodb.MongoOperator
}

Expand All @@ -59,6 +60,7 @@ func NewChaosExperimentHandler(
gitOpsService gitops.Service,
chaosExperimentOperator *dbChaosExperiment.Operator,
chaosExperimentRunOperator *dbChaosExperimentRun.Operator,
probeOperator *dbSchemaProbe.Operator,
mongodbOperator mongodb.MongoOperator,
) *ChaosExperimentHandler {
return &ChaosExperimentHandler{
Expand All @@ -68,6 +70,7 @@ func NewChaosExperimentHandler(
gitOpsService: gitOpsService,
chaosExperimentOperator: chaosExperimentOperator,
chaosExperimentRunOperator: chaosExperimentRunOperator,
probeOperator: probeOperator,
mongodbOperator: mongodbOperator,
}
}
Expand Down Expand Up @@ -1347,7 +1350,7 @@ func (c *ChaosExperimentHandler) GetProbesInExperimentRun(ctx context.Context, p
}

for _, probeName := range _probe.ProbeNames {
singleProbe, err := dbSchemaProbe.GetProbeByName(ctx, probeName, projectID)
singleProbe, err := dbSchemaProbe.NewChaosProbeOperator(c.mongodbOperator).GetProbeByName(ctx, probeName, projectID)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -1456,7 +1459,7 @@ func (c *ChaosExperimentHandler) UpdateCronExperimentState(ctx context.Context,
return false, errors.New("failed to marshal workflow manifest")
}

cronWorkflowManifest, err = probeUtils.GenerateCronExperimentManifestWithProbes(string(updatedManifest), experiment.ProjectID)
cronWorkflowManifest, err = probeUtils.GenerateCronExperimentManifestWithProbes(string(updatedManifest), experiment.ProjectID, c.probeOperator)
if err != nil {
return false, fmt.Errorf("failed to unmarshal experiment manifest, error: %v", err)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"reflect"
"testing"

dbSchemaProbe "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/database/mongodb/probe"

"github.com/golang-jwt/jwt"
"github.com/google/uuid"
"github.com/litmuschaos/litmus/chaoscenter/graphql/server/graph/model"
Expand Down Expand Up @@ -46,8 +48,9 @@ func NewMockServices() *MockServices {
chaosExperimentOperator = dbChaosExperiment.NewChaosExperimentOperator(mongodbMockOperator)
chaosExperimentRunOperator = dbChaosExperimentRun.NewChaosExperimentRunOperator(mongodbMockOperator)
chaosExperimentService = new(chaosExperimentMocks.ChaosExperimentService)
probeOperator = dbSchemaProbe.NewChaosProbeOperator(mongodbMockOperator)
)
var chaosExperimentHandler = NewChaosExperimentHandler(chaosExperimentService, chaosExperimentRunService, infrastructureService, gitOpsService, chaosExperimentOperator, chaosExperimentRunOperator, mongodbMockOperator)
var chaosExperimentHandler = NewChaosExperimentHandler(chaosExperimentService, chaosExperimentRunService, infrastructureService, gitOpsService, chaosExperimentOperator, chaosExperimentRunOperator, probeOperator, mongodbMockOperator)
return &MockServices{
ChaosExperimentService: chaosExperimentService,
ChaosExperimentRunService: chaosExperimentRunService,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"reflect"
"testing"

dbSchemaProbe "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/database/mongodb/probe"

"github.com/gin-gonic/gin"
"github.com/google/uuid"
"github.com/litmuschaos/litmus/chaoscenter/graphql/server/graph/model"
Expand All @@ -28,10 +30,11 @@ import (

var (
mongodbMockOperator = new(dbMocks.MongoOperator)
probeOperator = dbSchemaProbe.NewChaosProbeOperator(mongodbMockOperator)
infraOperator = dbChaosInfra.NewInfrastructureOperator(mongodbMockOperator)
chaosExperimentOperator = dbChaosExperiment.NewChaosExperimentOperator(mongodbMockOperator)
chaosExperimentRunOperator = dbChaosExperimentRun.NewChaosExperimentRunOperator(mongodbMockOperator)
probeService = probe.NewProbeService()
probeService = probe.NewProbeService(probeOperator)
)

var chaosExperimentRunTestService = NewChaosExperimentService(chaosExperimentOperator, infraOperator, chaosExperimentRunOperator, probeService)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ import (
store "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/data-store"
dbChaosExperiment "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/database/mongodb/chaos_experiment"

dbChaosInfra "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/database/mongodb/chaos_infrastructure"

"github.com/google/uuid"
dbChaosInfra "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/database/mongodb/chaos_infrastructure"
dbSchemaProbe "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/database/mongodb/probe"
)

// ChaosExperimentRunHandler is the handler for chaos experiment
Expand All @@ -53,6 +53,7 @@ type ChaosExperimentRunHandler struct {
gitOpsService gitops.Service
chaosExperimentOperator *dbChaosExperiment.Operator
chaosExperimentRunOperator *dbChaosExperimentRun.Operator
probeOperator *dbSchemaProbe.Operator
mongodbOperator mongodb.MongoOperator
}

Expand All @@ -63,6 +64,7 @@ func NewChaosExperimentRunHandler(
gitOpsService gitops.Service,
chaosExperimentOperator *dbChaosExperiment.Operator,
chaosExperimentRunOperator *dbChaosExperimentRun.Operator,
probeOperator *dbSchemaProbe.Operator,
mongodbOperator mongodb.MongoOperator,
) *ChaosExperimentRunHandler {
return &ChaosExperimentRunHandler{
Expand All @@ -71,6 +73,7 @@ func NewChaosExperimentRunHandler(
gitOpsService: gitOpsService,
chaosExperimentOperator: chaosExperimentOperator,
chaosExperimentRunOperator: chaosExperimentRunOperator,
probeOperator: probeOperator,
mongodbOperator: mongodbOperator,
}
}
Expand Down Expand Up @@ -911,7 +914,7 @@ func (c *ChaosExperimentRunHandler) RunChaosWorkFlow(ctx context.Context, projec
}

// Generate Probe in the manifest
workflowManifest, err = probeUtils.GenerateExperimentManifestWithProbes(string(manifestString), projectID)
workflowManifest, err = probeUtils.GenerateExperimentManifestWithProbes(string(manifestString), projectID, c.probeOperator)
if err != nil {
return nil, fmt.Errorf("failed to generate probes in workflow manifest, err: %v", err)
}
Expand Down Expand Up @@ -944,7 +947,7 @@ func (c *ChaosExperimentRunHandler) RunCronExperiment(ctx context.Context, proje
return workflow.Revision[i].UpdatedAt > workflow.Revision[j].UpdatedAt
})

cronExperimentManifest, err := probeUtils.GenerateCronExperimentManifestWithProbes(workflow.Revision[0].ExperimentManifest, workflow.ProjectID)
cronExperimentManifest, err := probeUtils.GenerateCronExperimentManifestWithProbes(workflow.Revision[0].ExperimentManifest, workflow.ProjectID, c.probeOperator)
if err != nil {
return errors.New("failed to unmarshal experiment manifest")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"testing"
"time"

dbSchemaProbe "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/database/mongodb/probe"

"github.com/gin-gonic/gin"
"github.com/google/uuid"
"github.com/litmuschaos/litmus/chaoscenter/graphql/server/graph/model"
Expand All @@ -36,11 +38,12 @@ var (
infrastructureService = new(chaosInfraMocks.InfraService)
chaosExperimentRunService = new(choasExperimentRunMocks.ChaosExperimentRunService)
gitOpsService = new(dbGitOpsMocks.GitOpsService)
probeOperator = dbSchemaProbe.NewChaosProbeOperator(mongodbMockOperator)
chaosExperimentOperator = dbChaosExperiment.NewChaosExperimentOperator(mongodbMockOperator)
chaosExperimentRunOperator = dbChaosExperimentRun.NewChaosExperimentRunOperator(mongodbMockOperator)
)

var chaosExperimentRunHandler = NewChaosExperimentRunHandler(chaosExperimentRunService, infrastructureService, gitOpsService, chaosExperimentOperator, chaosExperimentRunOperator, mongodbMockOperator)
var chaosExperimentRunHandler = NewChaosExperimentRunHandler(chaosExperimentRunService, infrastructureService, gitOpsService, chaosExperimentOperator, chaosExperimentRunOperator, probeOperator, mongodbMockOperator)

// TestMain is the entry point for testing
func TestMain(m *testing.M) {
Expand All @@ -56,6 +59,7 @@ func TestNewChaosExperimentRunHandler(t *testing.T) {
gitOpsService gitops.Service
chaosExperimentOperator *dbChaosExperiment.Operator
chaosExperimentRunOperator *dbChaosExperimentRun.Operator
probeOperator *dbSchemaProbe.Operator
mongodbOperator mongodb.MongoOperator
}
tests := []struct {
Expand All @@ -71,6 +75,7 @@ func TestNewChaosExperimentRunHandler(t *testing.T) {
gitOpsService: gitOpsService,
chaosExperimentOperator: chaosExperimentOperator,
chaosExperimentRunOperator: chaosExperimentRunOperator,
probeOperator: probeOperator,
mongodbOperator: mongodbMockOperator,
},
want: &ChaosExperimentRunHandler{
Expand All @@ -79,13 +84,14 @@ func TestNewChaosExperimentRunHandler(t *testing.T) {
gitOpsService: gitOpsService,
chaosExperimentOperator: chaosExperimentOperator,
chaosExperimentRunOperator: chaosExperimentRunOperator,
probeOperator: probeOperator,
mongodbOperator: mongodbMockOperator,
},
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
if got := NewChaosExperimentRunHandler(tc.args.chaosExperimentRunService, tc.args.infrastructureService, tc.args.gitOpsService, tc.args.chaosExperimentOperator, tc.args.chaosExperimentRunOperator, tc.args.mongodbOperator); !reflect.DeepEqual(got, tc.want) {
if got := NewChaosExperimentRunHandler(tc.args.chaosExperimentRunService, tc.args.infrastructureService, tc.args.gitOpsService, tc.args.chaosExperimentOperator, tc.args.chaosExperimentRunOperator, tc.args.probeOperator, tc.args.mongodbOperator); !reflect.DeepEqual(got, tc.want) {
t.Errorf("NewChaosExperimentRunHandler() = %v, want %v", got, tc.want)
}
})
Expand Down
36 changes: 24 additions & 12 deletions chaoscenter/graphql/server/pkg/database/mongodb/probe/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,31 @@ import (
"go.mongodb.org/mongo-driver/mongo"
)

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

// NewChaosProbeOperator returns a new instance of Operator
func NewChaosProbeOperator(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 +43,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 +56,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 +68,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 +80,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
Loading
Loading