Skip to content

Commit

Permalink
Add operator interface for Probe pkg (#4849)
Browse files Browse the repository at this point in the history
* refactor: add operator interface for Probe pkg

- Add probeOperator to probe pkg
- Add probeOperator to handler that uses probeUtils
- Add probe operator as parameter to functions that use probe operations in probeUtils

Signed-off-by: sukkyun2 <goobghd@gmail.com>

* test: add operator to handlers that use probeUtils

Signed-off-by: sukkyun2 <goobghd@gmail.com>

* fix: reorder golang Imports

- Apply goimports

Signed-off-by: sukkyun2 <goobghd@gmail.com>

* test: add probeOperator to unit tests

Signed-off-by: sukkyun2 <goobghd@gmail.com>

* refactor: move GenerateProbeManifest logic into ProbeService

- Updated handler to call GenerateExperimentManifestWithProbes, GenerateCronExperimentManifestWithProbes via ProbeService

Signed-off-by: sukkyun2 <goobghd@gmail.com>

* test: replace probeOperator with probeService

- add mock probeService generated by mockery

Signed-off-by: sukkyun2 <goobghd@gmail.com>

* fix: reorder goimports

Signed-off-by: sukkyun2 <goobghd@gmail.com>

---------

Signed-off-by: sukkyun2 <goobghd@gmail.com>
Co-authored-by: Namkyu Park <53862866+namkyu1999@users.noreply.github.com>
  • Loading branch information
sukkyun2 and namkyu1999 authored Nov 22, 2024
1 parent 8902dfb commit 14c9808
Show file tree
Hide file tree
Showing 14 changed files with 749 additions and 619 deletions.
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, probeService, mongodbOperator)
choasExperimentRunHandler := runHandler.NewChaosExperimentRunHandler(chaosExperimentRunService, chaosInfrastructureService, gitOpsService, chaosExperimentOperator, chaosExperimentRunOperator, probeService, 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"

dbProbeMocks "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/probe/model/mocks"

"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)
probeService = new(dbProbeMocks.ProbeService)
)
var chaosExperimentHandler = handler.NewChaosExperimentHandler(chaosExperimentService, chaosExperimentRunService, infrastructureService, gitOpsService, chaosExperimentOperator, chaosExperimentRunOperator, mongodbMockOperator)
var chaosExperimentHandler = handler.NewChaosExperimentHandler(chaosExperimentService, chaosExperimentRunService, infrastructureService, gitOpsService, chaosExperimentOperator, chaosExperimentRunOperator, probeService, mongodbMockOperator)
return &MockServices{
ChaosExperimentService: chaosExperimentService,
ChaosExperimentRunService: chaosExperimentRunService,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"strconv"
"time"

probeUtils "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/probe/utils"
probe "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/probe/handler"

"github.com/argoproj/argo-workflows/v3/pkg/apis/workflow/v1alpha1"
chaosTypes "github.com/litmuschaos/chaos-operator/api/litmuschaos/v1alpha1"
Expand Down Expand Up @@ -48,6 +48,7 @@ type ChaosExperimentHandler struct {
gitOpsService gitops.Service
chaosExperimentOperator *dbChaosExperiment.Operator
chaosExperimentRunOperator *dbChaosExperimentRun.Operator
probeService probe.Service
mongodbOperator mongodb.MongoOperator
}

Expand All @@ -59,6 +60,7 @@ func NewChaosExperimentHandler(
gitOpsService gitops.Service,
chaosExperimentOperator *dbChaosExperiment.Operator,
chaosExperimentRunOperator *dbChaosExperimentRun.Operator,
probeService probe.Service,
mongodbOperator mongodb.MongoOperator,
) *ChaosExperimentHandler {
return &ChaosExperimentHandler{
Expand All @@ -68,6 +70,7 @@ func NewChaosExperimentHandler(
gitOpsService: gitOpsService,
chaosExperimentOperator: chaosExperimentOperator,
chaosExperimentRunOperator: chaosExperimentRunOperator,
probeService: probeService,
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 = c.probeService.GenerateCronExperimentManifestWithProbes(string(updatedManifest), experiment.ProjectID)
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"

dbProbeMocks "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/probe/model/mocks"

"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)
probeService = new(dbProbeMocks.ProbeService)
)
var chaosExperimentHandler = NewChaosExperimentHandler(chaosExperimentService, chaosExperimentRunService, infrastructureService, gitOpsService, chaosExperimentOperator, chaosExperimentRunOperator, mongodbMockOperator)
var chaosExperimentHandler = NewChaosExperimentHandler(chaosExperimentService, chaosExperimentRunService, infrastructureService, gitOpsService, chaosExperimentOperator, chaosExperimentRunOperator, probeService, 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 @@ -6,6 +6,8 @@ import (
"testing"
"time"

dbProbeMocks "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/probe/model/mocks"

"github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/chaos_experiment_run/handler"
chaosInfraMocks "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/chaos_infrastructure/model/mocks"
dbChaosExperiment "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/database/mongodb/chaos_experiment"
Expand All @@ -32,6 +34,7 @@ type MockServices struct {
ChaosExperimentRunOperator *dbChaosExperimentRun.Operator
MongodbOperator *dbMocks.MongoOperator
ChaosExperimentRunHandler *handler.ChaosExperimentRunHandler
ProbeService *dbProbeMocks.ProbeService
}

func NewMockServices() *MockServices {
Expand All @@ -42,13 +45,15 @@ func NewMockServices() *MockServices {
chaosExperimentRunService = new(typesMocks.ChaosExperimentRunService)
chaosExperimentOperator = dbChaosExperiment.NewChaosExperimentOperator(mongodbMockOperator)
chaosExperimentRunOperator = dbChaosExperimentRun.NewChaosExperimentRunOperator(mongodbMockOperator)
probeService = new(dbProbeMocks.ProbeService)
)
var chaosExperimentRunHandler = handler.NewChaosExperimentRunHandler(
chaosExperimentRunService,
infrastructureService,
gitOpsService,
chaosExperimentOperator,
chaosExperimentRunOperator,
probeService,
mongodbMockOperator,
)
return &MockServices{
Expand All @@ -58,6 +63,7 @@ func NewMockServices() *MockServices {
ChaosExperimentOperator: chaosExperimentOperator,
ChaosExperimentRunOperator: chaosExperimentRunOperator,
MongodbOperator: mongodbMockOperator,
ProbeService: probeService,
ChaosExperimentRunHandler: chaosExperimentRunHandler,
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"strings"
"time"

probe "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/probe/handler"

"github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/authorization"

probeUtils "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/probe/utils"
Expand Down Expand Up @@ -41,9 +43,8 @@ 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"
)

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

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

// Generate Probe in the manifest
workflowManifest, err = probeUtils.GenerateExperimentManifestWithProbes(string(manifestString), projectID)
workflowManifest, err = c.probeService.GenerateExperimentManifestWithProbes(string(manifestString), projectID)
if err != nil {
return nil, fmt.Errorf("failed to generate probes in workflow manifest, err: %v", err)
}
Expand Down Expand Up @@ -944,7 +948,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 := c.probeService.GenerateCronExperimentManifestWithProbes(workflow.Revision[0].ExperimentManifest, workflow.ProjectID)
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,9 @@ import (
"testing"
"time"

probe "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/probe/handler"
dbProbeMocks "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/probe/model/mocks"

"github.com/gin-gonic/gin"
"github.com/google/uuid"
"github.com/litmuschaos/litmus/chaoscenter/graphql/server/graph/model"
Expand Down Expand Up @@ -38,9 +41,10 @@ var (
gitOpsService = new(dbGitOpsMocks.GitOpsService)
chaosExperimentOperator = dbChaosExperiment.NewChaosExperimentOperator(mongodbMockOperator)
chaosExperimentRunOperator = dbChaosExperimentRun.NewChaosExperimentRunOperator(mongodbMockOperator)
probeService = new(dbProbeMocks.ProbeService)
)

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

// TestMain is the entry point for testing
func TestMain(m *testing.M) {
Expand All @@ -56,6 +60,7 @@ func TestNewChaosExperimentRunHandler(t *testing.T) {
gitOpsService gitops.Service
chaosExperimentOperator *dbChaosExperiment.Operator
chaosExperimentRunOperator *dbChaosExperimentRun.Operator
probeService probe.Service
mongodbOperator mongodb.MongoOperator
}
tests := []struct {
Expand All @@ -71,6 +76,7 @@ func TestNewChaosExperimentRunHandler(t *testing.T) {
gitOpsService: gitOpsService,
chaosExperimentOperator: chaosExperimentOperator,
chaosExperimentRunOperator: chaosExperimentRunOperator,
probeService: probeService,
mongodbOperator: mongodbMockOperator,
},
want: &ChaosExperimentRunHandler{
Expand All @@ -79,13 +85,14 @@ func TestNewChaosExperimentRunHandler(t *testing.T) {
gitOpsService: gitOpsService,
chaosExperimentOperator: chaosExperimentOperator,
chaosExperimentRunOperator: chaosExperimentRunOperator,
probeService: probeService,
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.probeService, tc.args.mongodbOperator); !reflect.DeepEqual(got, tc.want) {
t.Errorf("NewChaosExperimentRunHandler() = %v, want %v", got, tc.want)
}
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"testing"

dbProbeMocks "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/probe/model/mocks"

"github.com/litmuschaos/litmus/chaoscenter/graphql/server/graph/model"
"github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/chaos_experiment/handler"
chaosExperimentMocks "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/chaos_experiment/model/mocks"
Expand Down Expand Up @@ -41,9 +43,10 @@ func NewMockServices() *MockServices {
gitOpsService = new(dbGitOpsMocks.GitOpsService)
chaosExperimentOperator = dbChaosExperiment.NewChaosExperimentOperator(mongodbMockOperator)
chaosExperimentRunOperator = dbChaosExperimentRun.NewChaosExperimentRunOperator(mongodbMockOperator)
probeService = new(dbProbeMocks.ProbeService)
chaosExperimentService = new(chaosExperimentMocks.ChaosExperimentService)
)
var chaosExperimentHandler = handler.NewChaosExperimentHandler(chaosExperimentService, chaosExperimentRunService, infrastructureService, gitOpsService, chaosExperimentOperator, chaosExperimentRunOperator, mongodbMockOperator)
var chaosExperimentHandler = handler.NewChaosExperimentHandler(chaosExperimentService, chaosExperimentRunService, infrastructureService, gitOpsService, chaosExperimentOperator, chaosExperimentRunOperator, probeService, mongodbMockOperator)
return &MockServices{
ChaosExperimentService: chaosExperimentService,
ChaosExperimentRunService: chaosExperimentRunService,
Expand Down
Loading

0 comments on commit 14c9808

Please sign in to comment.