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

Refactoring db package for unit tests #247

Merged
merged 8 commits into from
Aug 12, 2020

Conversation

gauravgahlot
Copy link
Contributor

@gauravgahlot gauravgahlot commented Aug 9, 2020

Description

Changes include:

  • refactoring db package
  • added mocked implementation for the database interface

Why is this needed

For mocking database calls to write unit tests for gRPC server.

How are existing users impacted? What migration steps/scripts do we need?

No impact.

How Has This Been Tested?

I have tested the changes by executing an end-to-end workflow.

Signed-off-by: Gaurav Gahlot <gaurav.gahlot19@gmail.com>
@gauravgahlot gauravgahlot added kind/feature Categorizes issue or PR as related to a new feature. do-not-merge Signal to Mergify to block merging of the PR. labels Aug 9, 2020
@gauravgahlot gauravgahlot self-assigned this Aug 9, 2020
Signed-off-by: Gaurav Gahlot <gaurav.gahlot19@gmail.com>
Signed-off-by: Gaurav Gahlot <gaurav.gahlot19@gmail.com>
Signed-off-by: Gaurav Gahlot <gaurav.gahlot19@gmail.com>
Signed-off-by: Gaurav Gahlot <gaurav.gahlot19@gmail.com>
@gauravgahlot gauravgahlot marked this pull request as draft August 9, 2020 18:02
@gauravgahlot gauravgahlot removed the do-not-merge Signal to Mergify to block merging of the PR. label Aug 9, 2020
@gauravgahlot gauravgahlot marked this pull request as ready for review August 11, 2020 05:37
@gauravgahlot gauravgahlot changed the title Adding grpc_server tests Refactoring db package for unit tests Aug 11, 2020
db/mock_workflow.go Outdated Show resolved Hide resolved
grpc-server/tinkerbell_test.go Outdated Show resolved Hide resolved
@gauravgahlot gauravgahlot added this to In Progress in Issues List via automation Aug 11, 2020
Signed-off-by: Gaurav Gahlot <gauravgahlot0107@gmail.com>
grpc-server/tinkerbell.go Outdated Show resolved Hide resolved
grpc-server/tinkerbell.go Outdated Show resolved Hide resolved
if len(req.GetActionName()) == 0 {
return nil, status.Errorf(codes.InvalidArgument, "action_name is invalid")
}
fmt.Printf("Received action status: %s\n", req)
Copy link
Contributor

@Cbkhare Cbkhare Aug 11, 2020

Choose a reason for hiding this comment

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

Instead of fmt we should use log.

fmt.Printf("Received action status: %s\n", req)
wfContext, err := s.db.GetWorkflowContexts(context, wfID)
if err != nil {
return nil, status.Errorf(codes.InvalidArgument, "workflow context not found for workflow %s", wfID)
Copy link
Contributor

@Cbkhare Cbkhare Aug 11, 2020

Choose a reason for hiding this comment

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

this error statement seems to be valid with my above comment on same point. May be we can refine the statement and update it here as well.

grpc-server/tinkerbell.go Outdated Show resolved Hide resolved
grpc-server/tinkerbell.go Show resolved Hide resolved
grpc-server/tinkerbell.go Show resolved Hide resolved
grpc-server/tinkerbell.go Show resolved Hide resolved
grpc-server/tinkerbell.go Show resolved Hide resolved
return true
}
}
} else {
Copy link
Contributor

Choose a reason for hiding this comment

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

not necessary but we can combine this else --> else if

Signed-off-by: Gaurav Gahlot <gauravgahlot0107@gmail.com>
@@ -51,9 +50,9 @@ func SetupGRPC(ctx context.Context, log log.Logger, facility string, errCh chan<
}
logger = log
metrics.SetupMetrics(facility, logger)
db := db.ConnectDB(logger)
instance := db.Connect(logger)
Copy link
Contributor

Choose a reason for hiding this comment

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

I think the variable name should be changed since it is of type TinkDB which already has a field name instance which is a bit confusing.

@Cbkhare Cbkhare requested review from Cbkhare and removed request for Cbkhare August 11, 2020 14:03
Signed-off-by: Gaurav Gahlot <gauravgahlot0107@gmail.com>
@gauravgahlot gauravgahlot requested review from thebsdbox and removed request for gianarb August 11, 2020 14:45
Copy link
Contributor

@parauliya parauliya left a comment

Choose a reason for hiding this comment

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

LGTM

@gauravgahlot gauravgahlot removed the request for review from thebsdbox August 12, 2020 08:58
@gauravgahlot gauravgahlot added the ready-to-merge Signal to Mergify to merge the PR. label Aug 12, 2020
@mergify mergify bot merged commit 1a6ebc8 into tinkerbell:master Aug 12, 2020
Issues List automation moved this from In Progress to Just shipped Aug 12, 2020
@gauravgahlot gauravgahlot deleted the server-tests branch August 12, 2020 11:13
mergify bot added a commit that referenced this pull request Aug 19, 2020
## Description

The PR restructures mocking based on the suggestion by @gianarb  in this [thread](#247 (comment)).

## Why is this needed

To write more flexible and|or reusable mocks. 

## How Has This Been Tested?
The changes include unit tests only. 

## Coverage
```
➜ go test -coverprofile coverage.out
➜ go tool cover -func coverage.out | grep tinkerbell.go
github.com/tinkerbell/tink/grpc-server/tinkerbell.go:29:	GetWorkflowContexts	0.0%
github.com/tinkerbell/tink/grpc-server/tinkerbell.go:49:	GetWorkflowContextList	100.0%
github.com/tinkerbell/tink/grpc-server/tinkerbell.go:72:	GetWorkflowActions	100.0%
github.com/tinkerbell/tink/grpc-server/tinkerbell.go:81:	ReportActionStatus	100.0%
github.com/tinkerbell/tink/grpc-server/tinkerbell.go:138:	UpdateWorkflowData	100.0%
github.com/tinkerbell/tink/grpc-server/tinkerbell.go:155:	GetWorkflowData		100.0%
github.com/tinkerbell/tink/grpc-server/tinkerbell.go:168:	GetWorkflowMetadata	100.0%
github.com/tinkerbell/tink/grpc-server/tinkerbell.go:177:	GetWorkflowDataVersion	100.0%
github.com/tinkerbell/tink/grpc-server/tinkerbell.go:185:	getWorkflowsForWorker	100.0%
github.com/tinkerbell/tink/grpc-server/tinkerbell.go:196:	getWorkflowActions	100.0%
github.com/tinkerbell/tink/grpc-server/tinkerbell.go:206:	isApplicableToSend	100.0%
github.com/tinkerbell/tink/grpc-server/tinkerbell.go:233:	isLastAction		100.0%
```

## How are existing users impacted? What migration steps/scripts do we need?
No impact.

## Checklist:

I have:

- [ ] updated the documentation and/or roadmap (if required)
- [x] added unit or e2e tests
- [ ] provided instructions on how to upgrade
@mmlb mmlb removed the ready-to-merge Signal to Mergify to merge the PR. label Jan 25, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/feature Categorizes issue or PR as related to a new feature.
Projects
No open projects
Issues List
  
Just shipped
Development

Successfully merging this pull request may close these issues.

None yet

5 participants