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

Added tests for environments handler #4175

Merged
merged 19 commits into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@ func NewEnvironmentOperator(mongodbOperator mongodb.MongoOperator) *Operator {
}
}

// Inside the dbOperationsEnvironment package

type EnvironmentOperatorInterface interface {
InsertEnvironment(ctx context.Context, env Environment) error
GetEnvironments(ctx context.Context, query bson.D) ([]Environment, error)
UpdateEnvironment(ctx context.Context, query bson.D, update bson.D) error
GetEnvironment(query bson.D) (Environment, error)
GetAggregateEnvironments(pipeline mongo.Pipeline) (*mongo.Cursor, error)
GetEnvironmentDetails(ctx context.Context, environmentID string, projectID string) (Environment, error)
GetEnvironmentWithProjectID(projectID string) ([]*Environment, error)
}

// InsertEnvironment takes details of a chaos_environment and inserts into the database collection
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO, we don't need this code if we adopt below code

import (
  dbMocks "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/database/mongodb/mocks"
  dbOperationsEnvironment "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/database/mongodb/environments"
)

mongodbMockOperator := new(dbMocks.MongoOperator)
environmentOperator := dbOperationsEnvironment.NewEnvironmentOperator(mongodbMockOperator)

we don't also need MockEnvironmentOperator struct and relevant functions in chaoscenter/graphql/server/pkg/environment/handler/handler_test.go if we adopt the above.

I think reducing unnecessary code is a great way to make open source understandable to many people.

How do you think @Saranya-jena @gdsoumya @Freedisch ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make sense. I updated the code to reflect it

func (e *Operator) InsertEnvironment(ctx context.Context, environment Environment) error {
err := e.operator.Create(ctx, mongodb.EnvironmentCollection, environment)
Expand Down
4 changes: 2 additions & 2 deletions chaoscenter/graphql/server/pkg/environment/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ type EnvironmentHandler interface {
}

type EnvironmentService struct {
EnvironmentOperator *dbOperationsEnvironment.Operator
EnvironmentOperator dbOperationsEnvironment.EnvironmentOperatorInterface
}

func NewEnvironmentService(EnvironmentOperator *dbOperationsEnvironment.Operator) EnvironmentHandler {
func NewEnvironmentService(EnvironmentOperator dbOperationsEnvironment.EnvironmentOperatorInterface) EnvironmentHandler {
return &EnvironmentService{
EnvironmentOperator: EnvironmentOperator,
}
Expand Down
170 changes: 170 additions & 0 deletions chaoscenter/graphql/server/pkg/environment/handler/handler_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
package handler_test

import (
"context"
"errors"
"testing"

"github.com/litmuschaos/litmus/chaoscenter/graphql/server/graph/model"
"github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/authorization"
"github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/database/mongodb/environments"
"github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/environment/handler"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
)

type MockEnvironmentOperator struct {
Copy link
Member

@namkyu1999 namkyu1999 Oct 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about mocking the Mongo Operator instead of mocking the Environment Operator? This allows you to write test cases that are flexible to future changes in the Environment Operator. How do you think?

import (
  dbMocks "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/database/mongodb/mocks"
  dbOperationsEnvironment "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/database/mongodb/environments"
)

mongodbMockOperator := new(dbMocks.MongoOperator)
environmentOperator := dbOperationsEnvironment.NewEnvironmentOperator(mongodbMockOperator)

reference: https://github.com/litmuschaos/litmus/blob/master/chaoscenter/graphql/server/pkg/chaos_experiment_run/service_test.go

Copy link
Contributor Author

@Freedisch Freedisch Oct 31, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I could stick with Environment Operator since I already updated the handler to consider an environment Mockoperator

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay, thanks

InsertEnvironmentFunc func(ctx context.Context, env environments.Environment) error
GetEnvironmentsFunc func(ctx context.Context, query bson.D) ([]environments.Environment, error)
UpdateEnvironmentFunc func(ctx context.Context, query bson.D, update bson.D) error
GetEnvironmentFunc func(query bson.D) (environments.Environment, error)
GetAggregateEnvironmentsFunc func(pipeline mongo.Pipeline) (*mongo.Cursor, error)
GetEnvironmentWithProjectIDFunc func(projectID string) ([]*environments.Environment, error)
GetEnvironmentDetailFunc func(ctx context.Context, environmentID string, projectID string) (environments.Environment, error)
}

func (m *MockEnvironmentOperator) InsertEnvironment(ctx context.Context, env environments.Environment) error {
if m.InsertEnvironmentFunc != nil {
return m.InsertEnvironmentFunc(ctx, env)
}
return nil
}

func (m *MockEnvironmentOperator) GetEnvironment(query bson.D) (environments.Environment, error) {
if m.GetEnvironmentFunc != nil {
return m.GetEnvironmentFunc(query)
}
return environments.Environment{}, nil
}

func (m *MockEnvironmentOperator) GetEnvironments(ctx context.Context, query bson.D) ([]environments.Environment, error) {
if m.GetEnvironmentsFunc != nil {
return m.GetEnvironmentsFunc(ctx, query)
}
return nil, nil
}

func (m *MockEnvironmentOperator) UpdateEnvironment(ctx context.Context, query bson.D, update bson.D) error {
if m.UpdateEnvironmentFunc != nil {
return m.UpdateEnvironmentFunc(ctx, query, update)
}
return nil
}

func (m *MockEnvironmentOperator) GetAggregateEnvironments(pipeline mongo.Pipeline) (*mongo.Cursor, error) {
if m.GetAggregateEnvironmentsFunc != nil {
return m.GetAggregateEnvironmentsFunc(pipeline)
}
return nil, nil
}

func (m *MockEnvironmentOperator) GetEnvironmentWithProjectID(projectID string) ([]*environments.Environment, error) {
if m.GetEnvironmentWithProjectIDFunc != nil {
return m.GetEnvironmentWithProjectIDFunc(projectID)
}
return nil, nil
}

func (m *MockEnvironmentOperator) GetEnvironmentDetails(ctx context.Context, environmentID string, projectID string) (environments.Environment, error) {
if m.GetEnvironmentDetailFunc != nil {
return m.GetEnvironmentDetailFunc(ctx, environmentID, projectID)
}
return environments.Environment{}, nil
}

func TestCreateEnvironment(t *testing.T) {
testCases := []struct {
Freedisch marked this conversation as resolved.
Show resolved Hide resolved
name string
projectID string
input *model.CreateEnvironmentRequest
token string
mockInsertFunc func(ctx context.Context, env environments.Environment) error
expectedEnv *model.Environment
expectedErr error
}{
{
name: "Failed environment creation due to invalid token",
projectID: "testProject",
input: &model.CreateEnvironmentRequest{
EnvironmentID: "testEnvID",
Name: "testName",
},
token: "invalidToken",
mockInsertFunc: func(ctx context.Context, env environments.Environment) error {
return nil
},
expectedEnv: nil,
expectedErr: errors.New("invalid Token"),
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
ctx := context.WithValue(context.Background(), authorization.AuthKey, tc.token)
mockOperator := &MockEnvironmentOperator{
InsertEnvironmentFunc: tc.mockInsertFunc,
}
service := handler.NewEnvironmentService(mockOperator)
//service := handler.NewEnvironmentService(operator)
Freedisch marked this conversation as resolved.
Show resolved Hide resolved

env, err := service.CreateEnvironment(ctx, tc.projectID, tc.input)
if (err != nil && tc.expectedErr == nil) ||
(err == nil && tc.expectedErr != nil) ||
(err != nil && tc.expectedErr != nil && err.Error() != tc.expectedErr.Error()) {
t.Fatalf("Expected error %v, but got %v", tc.expectedErr, err)
}

if tc.expectedEnv != nil && (env.EnvironmentID != tc.expectedEnv.EnvironmentID ||
env.Name != tc.expectedEnv.Name) {
t.Fatalf("Expected environment %+v, but got %+v", tc.expectedEnv, env)
}
})
}
}

func TestDeleteEnvironment(t *testing.T) {
testCases := []struct {
Freedisch marked this conversation as resolved.
Show resolved Hide resolved
name string
projectID string
environmentID string
token string
mockGetEnvironmentFunc func(query bson.D) (environments.Environment, error)
mockUpdateFunc func(ctx context.Context, query bson.D, update bson.D) error
expectedResult string
expectedErr error
}{
{
name: "Failed environment deletion due to invalid token",
projectID: "testProject",
environmentID: "testEnvID",
token: "invalidToken",
mockGetEnvironmentFunc: func(query bson.D) (environments.Environment, error) {
return environments.Environment{
EnvironmentID: "testEnvID",
}, nil
},
mockUpdateFunc: func(ctx context.Context, query bson.D, update bson.D) error {
return nil
},
expectedErr: errors.New("invalid Token"),
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
ctx := context.WithValue(context.Background(), authorization.AuthKey, tc.token)

mockOperator := &MockEnvironmentOperator{
UpdateEnvironmentFunc: tc.mockUpdateFunc,
}
service := handler.NewEnvironmentService(mockOperator)

_, err := service.DeleteEnvironment(ctx, tc.projectID, tc.environmentID)
if (err != nil && tc.expectedErr == nil) ||
(err == nil && tc.expectedErr != nil) ||
(err != nil && tc.expectedErr != nil && err.Error() != tc.expectedErr.Error()) {
t.Fatalf("Expected error %v, but got %v", tc.expectedErr, err)
}
})
}
}