Skip to content

Commit

Permalink
fix: reads the correct yaml file for tcs (#178)
Browse files Browse the repository at this point in the history
* logging keploy

* logging keploy

Signed-off-by: unknown <sharmapuru0642@gmail.com>

* feat: adds mock grpc methods for mocking infra in unit_tests (#162)

* feat: adds mock grpc methods for mocking infra in unit_tests

Signed-off-by: re-Tick <jain.ritik.1001@gmail.com>

* fix: encodes binary into base64 to save in yaml file

Signed-off-by: re-Tick <jain.ritik.1001@gmail.com>

Signed-off-by: re-Tick <jain.ritik.1001@gmail.com>
Signed-off-by: unknown <sharmapuru0642@gmail.com>

* fix(gha):  update cosign to solve signing issues (#166)

Signed-off-by: unknown <sharmapuru0642@gmail.com>

* fix: skips the unusual tele call on disabling telemetry (#169)

* fix(telemetry): fixes the unusual tele call on disabling telemetry

Signed-off-by: re-Tick <jain.ritik.1001@gmail.com>

* chore(logger): sets logger to info level

sets the zap logger to info so that debug are not logged

Signed-off-by: re-Tick <jain.ritik.1001@gmail.com>

* refactor(logger): adds env config to set logger to debug level

By default the logger level is set to info level. But using env variable level can be set to debug.

Signed-off-by: re-Tick <jain.ritik.1001@gmail.com>

Signed-off-by: re-Tick <jain.ritik.1001@gmail.com>
Signed-off-by: unknown <sharmapuru0642@gmail.com>

* feat: generates mock name if empty and logs in the terminal with yaml file path (#170)

Signed-off-by: re-Tick <jain.ritik.1001@gmail.com>

Signed-off-by: re-Tick <jain.ritik.1001@gmail.com>
Signed-off-by: unknown <sharmapuru0642@gmail.com>

* adding logging

Signed-off-by: unknown <sharmapuru0642@gmail.com>

* adding logs

Signed-off-by: unknown <sharmapuru0642@gmail.com>

* feat(keploy): adding logging functionality of failed test-cases

Signed-off-by: unknown <sharmapuru0642@gmail.com>

* feat(keploy): adding logging functionality of failed test-cases

Signed-off-by: unknown <sharmapuru0642@gmail.com>

* feat(keploy): update adding logging functionality of failed test-cases

Signed-off-by: unknown <sharmapuru0642@gmail.com>

* feat(keploy): updated the logging functionality

Signed-off-by: unknown <sharmapuru0642@gmail.com>

* feat(keploy): Updated the logging functionality

Signed-off-by: Pradhyuman-sharma <sharmapuru0642@gmail.com>

* feat(keploy): Updated the logging functionality

Signed-off-by: Pradhyuman-sharma <sharmapuru0642@gmail.com>

* fix: reads the correct yaml file for tcs

stores the correct yaml file name in a map for testcase with different id. And reads the actual yaml
file countaining the testcase with different id than yaml name

Signed-off-by: re-Tick <jain.ritik.1001@gmail.com>

* fix: store yaml tcs in map

Puts the yaml testcases for runid in a sync.map during start testrun call. Then, load the yaml
testcase from map with corresponding runid and testcaseid during test call.

Signed-off-by: re-Tick <jain.ritik.1001@gmail.com>

* refactor: creates a generic function to check path validity

Signed-off-by: re-Tick <jain.ritik.1001@gmail.com>

* refactor: integerates an interface for yaml file handling

Signed-off-by: re-Tick <jain.ritik.1001@gmail.com>

* fix: fixes the identation of github-action yml

Signed-off-by: re-Tick <jain.ritik.1001@gmail.com>

* fix: adds env variable to switch testsuit in ci

Signed-off-by: re-Tick <jain.ritik.1001@gmail.com>

* fix: fixes typo of env variable in server config

Signed-off-by: re-Tick <jain.ritik.1001@gmail.com>

* fix: merges the coverage analysis into a txt file

Signed-off-by: re-Tick <jain.ritik.1001@gmail.com>

* fix: fixes the coverage analysis in ci

Signed-off-by: re-Tick <jain.ritik.1001@gmail.com>

* refactor: removes the debug statement in main_test

Signed-off-by: re-Tick <jain.ritik.1001@gmail.com>

* ci: merges the coverage file into one

Signed-off-by: re-Tick <jain.ritik.1001@gmail.com>

* ci: adds coverage for test-export only

Signed-off-by: re-Tick <jain.ritik.1001@gmail.com>

* ci: adds coveralls for all actions

Signed-off-by: re-Tick <jain.ritik.1001@gmail.com>

* ci: removes the mode line from appending into overall coverage.txt

Signed-off-by: re-Tick <jain.ritik.1001@gmail.com>

* ci: fixes the typo in ignoring mode statement

Signed-off-by: re-Tick <jain.ritik.1001@gmail.com>

* ci: fixes the order of coverage txt files

Signed-off-by: re-Tick <jain.ritik.1001@gmail.com>

* style: removes unwanted commented code

Signed-off-by: re-Tick <jain.ritik.1001@gmail.com>

* refactor: adds a run db method to get testrun with id to log

Signed-off-by: re-Tick <jain.ritik.1001@gmail.com>

* refactor: improves naming and adds nil pointer check

adds nil pointer check to hres array in pkg/utils

Signed-off-by: re-Tick <jain.ritik.1001@gmail.com>

Signed-off-by: unknown <sharmapuru0642@gmail.com>
Signed-off-by: re-Tick <jain.ritik.1001@gmail.com>
Signed-off-by: Pradhyuman-sharma <sharmapuru0642@gmail.com>
Co-authored-by: unknown <sharmapuru0642@gmail.com>
Co-authored-by: Shubham Jain <shubhamkjain@outlook.com>
Co-authored-by: Pradhyuman Sharma <90783566+Pradhyuman-sharma@users.noreply.github.com>
  • Loading branch information
4 people committed Sep 21, 2022
1 parent ec96ca9 commit a3def85
Show file tree
Hide file tree
Showing 20 changed files with 475 additions and 309 deletions.
24 changes: 19 additions & 5 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,31 @@ jobs:

- name: Build
run: go build -v ./...

- name: Test-Export
run: |
go test -coverpkg=./... -coverprofile=coverage1.tmp.txt -covermode=atomic ./...
cat coverage1.tmp.txt | grep -v -e "generated.go" -e "_gen.go" | grep -v -e "services.pb.go" -e "services_grpc.pb.go" > coverage1.txt
env:
KEPLOY_API_KEY: 81f83aeeedddf453966347dc136c66
ENABLE_DEDUP: false
ENABLE_TEST_EXPORT: true
KEPLOY_APP_NAME: Keploy-Test-App-2

- name: Test
run: |
go test -coverpkg=./... -coverprofile=coverage.tmp.txt -covermode=atomic ./...
cat coverage.tmp.txt | grep -v -e "generated.go" -e "_gen.go" | grep -v -e "services.pb.go" -e "services_grpc.pb.go" > coverage.txt
go test -coverpkg=./... -coverprofile=coverage2.tmp.txt -covermode=atomic ./...
cat coverage2.tmp.txt | grep -v "mode: atomic" | grep -v -e "generated.go" -e "_gen.go" | grep -v -e "services.pb.go" -e "services_grpc.pb.go" > coverage2.txt
env:
KEPLOY_API_KEY: 81f83aeeedddf453966347dc136c66
ENABLE_DEDUP: false
ENABLE_TEST_EXPORT: false

- name: Test-withDeDupEnabled
run: |
go test -coverpkg=./... -coverprofile=coverage.tmp.txt -covermode=atomic ./...
cat coverage.tmp.txt | grep -v -e "generated.go" -e "_gen.go" | grep -v -e "services.pb.go" -e "services_grpc.pb.go" > coverage.txt
go test -coverpkg=./... -coverprofile=coverage3.tmp.txt -covermode=atomic ./...
cat coverage3.tmp.txt | grep -v "mode: atomic" | grep -v -e "generated.go" -e "_gen.go" | grep -v -e "services.pb.go" -e "services_grpc.pb.go" > coverage3.txt
cat coverage1.txt coverage2.txt coverage3.txt > coverage.txt
env:
KEPLOY_API_KEY: 81f83aeeedddf453966347dc136c66
ENABLE_DEDUP: true
Expand All @@ -55,7 +66,10 @@ jobs:
- name: Send coverage
env:
COVERALLS_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: goveralls -coverprofile=coverage.txt -service=github
run: |
goveralls -coverprofile=coverage.txt -service=github
# - name: Install extra dependencies
# run: npm install -g @semantic-release/exec
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ github.com/tidwall/gjson v1.14.0 h1:6aeJ0bzojgWLa82gDQHcx3S0Lr/O51I9bJ5nv6JFx5w=
github.com/tidwall/gjson v1.14.0/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
github.com/tidwall/pretty v1.0.0 h1:HsD+QiTn7sK6flMKIvNmpqz1qrpP3Ps6jOKIKMooyg4=
github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs=
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
Expand Down
45 changes: 32 additions & 13 deletions grpc/grpcserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (

"github.com/google/uuid"
"go.keploy.io/server/graph"
mock2 "go.keploy.io/server/grpc/mock"
grpcMock "go.keploy.io/server/grpc/mock"
proto "go.keploy.io/server/grpc/regression"
"go.keploy.io/server/grpc/utils"
"go.keploy.io/server/pkg/models"
Expand All @@ -28,7 +28,7 @@ import (

type Server struct {
logger *zap.Logger
TestExport bool
testExport bool
svc regression2.Service
run run.Service
mock mock.Service
Expand All @@ -39,7 +39,7 @@ func New(logger *zap.Logger, svc regression2.Service, run run.Service, m mock.Se

// create an instance for grpc server
srv := grpc.NewServer()
proto.RegisterRegressionServiceServer(srv, &Server{logger: logger, svc: svc, run: run, mock: m, TestExport: testExport})
proto.RegisterRegressionServiceServer(srv, &Server{logger: logger, svc: svc, run: run, mock: m, testExport: testExport})
reflection.Register(srv)
err := srv.Serve(listener)
return err
Expand All @@ -63,7 +63,11 @@ func (srv *Server) StartMocking(ctx context.Context, request *proto.StartMockReq

func (srv *Server) PutMock(ctx context.Context, request *proto.PutMockReq) (*proto.PutMockResp, error) {
// writes to yaml file
err := srv.mock.Put(ctx, request.Path, mock2.Encode(request.Mock, srv.logger), request.Mock.Spec.Metadata)
doc, err := grpcMock.Encode(request.Mock)
if err != nil {
srv.logger.Error(err.Error())
}
err = srv.mock.Put(ctx, request.Path, doc, request.Mock.Spec.Metadata)
if err != nil {
return &proto.PutMockResp{}, err
}
Expand All @@ -76,10 +80,15 @@ func (srv *Server) GetMocks(ctx context.Context, request *proto.GetMockReq) (*pr
if err != nil {
return &proto.GetMockResp{}, err
}
resp := &proto.GetMockResp{
Mocks: mock2.Decode(mocks, srv.logger),
res, err := grpcMock.Decode(mocks)
if err != nil {
srv.logger.Error(err.Error())
return &proto.GetMockResp{}, err
}
return resp, nil
response := &proto.GetMockResp{
Mocks: res,
}
return response, nil
}

func (srv *Server) End(ctx context.Context, request *proto.EndRequest) (*proto.EndResponse, error) {
Expand All @@ -89,6 +98,9 @@ func (srv *Server) End(ctx context.Context, request *proto.EndRequest) (*proto.E
stat = run.TestRunStatusPassed
}
now := time.Now().Unix()
if srv.testExport {
srv.svc.StopTestRun(ctx, id)
}
err := srv.run.Put(ctx, run.TestRun{
ID: id,
Updated: now,
Expand All @@ -112,6 +124,9 @@ func (srv *Server) Start(ctx context.Context, request *proto.StartRequest) (*pro
}
id := uuid.New().String()
now := time.Now().Unix()
if srv.testExport {
srv.svc.StartTestRun(ctx, id, request.TestCasePath, request.MockPath)
}
err = srv.run.Put(ctx, run.TestRun{
ID: id,
Created: now,
Expand Down Expand Up @@ -229,7 +244,7 @@ func (srv *Server) GetTCS(ctx context.Context, request *proto.GetTCSRequest) (*p
}
}

switch srv.TestExport {
switch srv.testExport {
case false:
tcs, err = srv.svc.GetAll(ctx, graph.DEFAULT_COMPANY, app, &offset, &limit)
if err != nil {
Expand Down Expand Up @@ -262,7 +277,7 @@ func GetHttpHeader(m map[string]*proto.StrArr) map[string][]string {
}

func (srv *Server) PostTC(ctx context.Context, request *proto.TestCaseReq) (*proto.PostTCResponse, error) {
if srv.TestExport {
if srv.testExport {
var (
id = uuid.New().String()
tc = []models.Mock{{
Expand All @@ -273,7 +288,11 @@ func (srv *Server) PostTC(ctx context.Context, request *proto.TestCaseReq) (*pro
mocks = []string{}
)
for i, j := range request.Mocks {
tc = append(tc, mock2.Encode(j, srv.logger))
doc, err := grpcMock.Encode(j)
if err != nil {
srv.logger.Error(err.Error())
}
tc = append(tc, doc)
m := id + "-" + strconv.Itoa(i)
tc[len(tc)-1].Name = m
mocks = append(mocks, m)
Expand All @@ -288,14 +307,14 @@ func (srv *Server) PostTC(ctx context.Context, request *proto.TestCaseReq) (*pro
URL: request.HttpReq.URL,
URLParams: request.HttpReq.URLParams,
Body: request.HttpReq.Body,
Header: mock2.ToMockHeader(utils.GetHttpHeader(request.HttpReq.Header)),
Header: grpcMock.ToMockHeader(utils.GetHttpHeader(request.HttpReq.Header)),
},
Response: models.MockHttpResp{
StatusCode: int(request.HttpResp.StatusCode),
Body: request.HttpResp.Body,
Header: mock2.ToMockHeader(utils.GetHttpHeader(request.HttpResp.Header)),
Header: grpcMock.ToMockHeader(utils.GetHttpHeader(request.HttpResp.Header)),
},
Objects: mock2.ToModelObjects([]*proto.Mock_Object{{ // TODO: remove this. after making range check in go-sdk http interceptor logic check cause there 0th index is picked up directly. ELse it will panic
Objects: grpcMock.ToModelObjects([]*proto.Mock_Object{{ // TODO: remove this. after making range check in go-sdk http interceptor logic check cause there 0th index is picked up directly. ELse it will panic
Type: "error",
Data: []byte{},
}}),
Expand Down
34 changes: 18 additions & 16 deletions grpc/mock/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@ import (
proto "go.keploy.io/server/grpc/regression"
"go.keploy.io/server/grpc/utils"
"go.keploy.io/server/pkg/models"
"go.uber.org/zap"
)

func Encode(doc *proto.Mock, log *zap.Logger) models.Mock {
func Encode(doc *proto.Mock) (models.Mock, error) {
res := models.Mock{
Version: doc.Version,
Kind: doc.Kind,
Expand Down Expand Up @@ -45,20 +44,20 @@ func Encode(doc *proto.Mock, log *zap.Logger) models.Mock {
}
err := res.Spec.Encode(&spec)
if err != nil {
log.Error(fmt.Sprint("Failed to encode http spec for mock with name: ", doc.Name), zap.Error(err))
return res, fmt.Errorf("Failed to encode http spec for mock with name: %s. error: %s", doc.Name, err.Error())
}
case string(models.GENERIC_EXPORT):
err := res.Spec.Encode(&models.GenericSpec{
Metadata: doc.Spec.Metadata,
Objects: ToModelObjects(doc.Spec.Objects),
})
if err != nil {
log.Error(fmt.Sprint("Failed to encode generic spec for mock with name: ", doc.Name), zap.Error(err))
return res, fmt.Errorf("Failed to encode generic spec for mock with name: %s. error: %s", doc.Name, err.Error())
}
default:
log.Error(fmt.Sprint("Mock with name ", doc.Name, " is not of a valid kind"))
return res, fmt.Errorf("Mock with name %s is not of a valid kind", doc.Name)
}
return res
return res, nil
}

func ToModelObjects(objs []*proto.Mock_Object) []models.Object {
Expand All @@ -72,23 +71,22 @@ func ToModelObjects(objs []*proto.Mock_Object) []models.Object {
return res
}

func toProtoObjects(objs []models.Object, log *zap.Logger) []*proto.Mock_Object {
func toProtoObjects(objs []models.Object) ([]*proto.Mock_Object, error) {
res := []*proto.Mock_Object{}
for _, j := range objs {
bin, err := base64.StdEncoding.DecodeString(j.Data)
if err != nil {
log.Error("failed to decode base64 data from yaml file into byte array", zap.Error(err))
continue
return res, fmt.Errorf("failed to decode base64 data from yaml file into byte array. error: %s", err.Error())
}
res = append(res, &proto.Mock_Object{
Type: j.Type,
Data: bin,
})
}
return res
return res, nil
}

func Decode(doc []models.Mock, log *zap.Logger) []*proto.Mock {
func Decode(doc []models.Mock) ([]*proto.Mock, error) {
res := []*proto.Mock{}
for _, j := range doc {
mock := &proto.Mock{
Expand All @@ -101,7 +99,7 @@ func Decode(doc []models.Mock, log *zap.Logger) []*proto.Mock {
spec := &models.HttpSpec{}
err := j.Spec.Decode(spec)
if err != nil {
log.Error(fmt.Sprint("Failed to decode the http spec of mock with name: ", j.Name), zap.Error(err))
return res, fmt.Errorf("Failed to decode the http spec of mock with name: %s. error: %s", j.Name, err.Error())
}
mock.Spec = &proto.Mock_SpecSchema{
Metadata: spec.Metadata,
Expand Down Expand Up @@ -130,18 +128,22 @@ func Decode(doc []models.Mock, log *zap.Logger) []*proto.Mock {
spec := &models.GenericSpec{}
err := j.Spec.Decode(spec)
if err != nil {
log.Error(fmt.Sprint("Failed to decode the generic spec of mock with name: ", j.Name), zap.Error(err))
return res, fmt.Errorf("Failed to decode the generic spec of mock with name: %s. error: %s", j.Name, err.Error())
}
obj, err := toProtoObjects(spec.Objects)
if err != nil {
return res, err
}
mock.Spec = &proto.Mock_SpecSchema{
Metadata: spec.Metadata,
Objects: toProtoObjects(spec.Objects, log),
Objects: obj,
}
default:
log.Error(fmt.Sprint("Mock with name ", j.Name, " is not of a valid kind"))
return res, fmt.Errorf("Mock with name %s is not of a valid kind", j.Name)
}
res = append(res, mock)
}
return res
return res, nil
}

func ToHttpHeader(mockHeader map[string]string) http.Header {
Expand Down
28 changes: 24 additions & 4 deletions grpc/regression/services.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions grpc/regression/services.proto
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ message endResponse {
message startRequest {
string total = 1;
string app = 2;
string TestCasePath = 3;
string MockPath = 4;
}

message startResponse {
Expand Down

0 comments on commit a3def85

Please sign in to comment.