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

feat: skip app run multiple times #1806

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 2 additions & 3 deletions .github/workflows/test_workflow_scripts/golang-docker.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ docker build -t gin-mongo .
docker rm -f ginApp 2>/dev/null || true

for i in {1..2}; do
container_name="ginApp_${i}"
sudo -E env PATH=$PATH ./../../keployv2 record -c "docker run -p8080:8080 --net keploy-network --rm --name ${container_name} gin-mongo" --containerName "${container_name}" --generateGithubActions=false &
sudo -E env PATH=$PATH ./../../keployv2 record -c "docker run -p8080:8080 --net keploy-network --rm --name ginApp_test gin-mongo" --containerName "ginApp_test" --generateGithubActions=false &

sleep 5

Expand Down Expand Up @@ -59,7 +58,7 @@ for i in {1..2}; do

# Stop keploy.
docker rm -f keploy-v2
docker rm -f "${container_name}"
docker rm -f ginApp_test
done

# Start the keploy in test mode.
Expand Down
24 changes: 14 additions & 10 deletions cli/provider/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
mockdb "go.keploy.io/server/v2/pkg/platform/yaml/mockdb"
reportdb "go.keploy.io/server/v2/pkg/platform/yaml/reportdb"
testdb "go.keploy.io/server/v2/pkg/platform/yaml/testdb"
"go.keploy.io/server/v2/pkg/platform/yaml/userdb"

"go.keploy.io/server/v2/pkg/service/record"
"go.keploy.io/server/v2/pkg/service/replay"
Expand All @@ -23,28 +24,29 @@ import (
)

type ServiceProvider struct {
logger *zap.Logger
configDb *configdb.ConfigDb
cfg *config.Config
logger *zap.Logger
userDb *userdb.UserDb
cfg *config.Config
}

type CommonInternalService struct {
YamlTestDB *testdb.TestYaml
YamlMockDb *mockdb.MockYaml
YamlReportDb *reportdb.TestReport
YamlConfigDb *configdb.ConfigYaml
Instrumentation *core.Core
}

func NewServiceProvider(logger *zap.Logger, configDb *configdb.ConfigDb, cfg *config.Config) *ServiceProvider {
func NewServiceProvider(logger *zap.Logger, userDb *userdb.UserDb, cfg *config.Config) *ServiceProvider {
return &ServiceProvider{
logger: logger,
configDb: configDb,
cfg: cfg,
logger: logger,
userDb: userDb,
cfg: cfg,
}
}

func (n *ServiceProvider) GetTelemetryService(ctx context.Context, config config.Config) (*telemetry.Telemetry, error) {
installationID, err := n.configDb.GetInstallationID(ctx)
installationID, err := n.userDb.GetInstallationID(ctx)
if err != nil {
return nil, errors.New("failed to get installation id")
}
Expand All @@ -65,11 +67,13 @@ func (n *ServiceProvider) GetCommonServices(config config.Config) *CommonInterna
testDB := testdb.New(n.logger, config.Path)
mockDB := mockdb.New(n.logger, config.Path, "")
reportDB := reportdb.New(n.logger, config.Path+"/reports")
configDB := configdb.New(n.logger, config.Path)
return &CommonInternalService{
Instrumentation: instrumentation,
YamlTestDB: testDB,
YamlMockDb: mockDB,
YamlReportDb: reportDB,
YamlConfigDb: configDB,
}
}

Expand All @@ -86,10 +90,10 @@ func (n *ServiceProvider) GetService(ctx context.Context, cmd string) (interface
case "record", "test", "mock":
commonServices := n.GetCommonServices(*n.cfg)
if cmd == "record" {
return record.New(n.logger, commonServices.YamlTestDB, commonServices.YamlMockDb, tel, commonServices.Instrumentation, *n.cfg), nil
return record.New(n.logger, commonServices.YamlTestDB, commonServices.YamlMockDb, commonServices.YamlConfigDb, tel, commonServices.Instrumentation, *n.cfg), nil
}
if cmd == "test" {
return replay.NewReplayer(n.logger, commonServices.YamlTestDB, commonServices.YamlMockDb, commonServices.YamlReportDb, tel, commonServices.Instrumentation, *n.cfg), nil
return replay.NewReplayer(n.logger, commonServices.YamlTestDB, commonServices.YamlMockDb, commonServices.YamlReportDb, commonServices.YamlConfigDb, tel, commonServices.Instrumentation, *n.cfg), nil
}
return nil, errors.New("invalid command")
default:
Expand Down
4 changes: 4 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ type Config struct {
KeployNetwork string `json:"keployNetwork" yaml:"keployNetwork" mapstructure:"keployNetwork"`
}

type TestSetConfig struct {
Cmd string `json:"cmd" yaml:"cmd" mapstructure:"cmd"`
}

type Record struct {
Filters []Filter `json:"filters" yaml:"filters" mapstructure:"filters"`
RecordTimer time.Duration `json:"recordTimer" yaml:"recordTimer" mapstructure:"recordTimer"`
Expand Down
6 changes: 3 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"go.keploy.io/server/v2/cli"
"go.keploy.io/server/v2/cli/provider"
"go.keploy.io/server/v2/config"
"go.keploy.io/server/v2/pkg/platform/yaml/configdb"
"go.keploy.io/server/v2/pkg/platform/yaml/userdb"
"go.keploy.io/server/v2/utils"
"go.keploy.io/server/v2/utils/log"
//pprof for debugging
Expand Down Expand Up @@ -65,13 +65,13 @@ func start(ctx context.Context) {
}
defer utils.DeleteLogs(logger)
defer utils.Recover(logger)
configDb := configdb.NewConfigDb(logger)
userDb := userdb.NewUserDb(logger)
if dsn != "" {
utils.SentryInit(logger, dsn)
//logger = utils.ModifyToSentryLogger(ctx, logger, sentry.CurrentHub().Client(), configDb)
}
conf := config.New()
svcProvider := provider.NewServiceProvider(logger, configDb, conf)
svcProvider := provider.NewServiceProvider(logger, userDb, conf)
cmdConfigurator := provider.NewCmdConfigurator(logger, conf)
rootCmd := cli.Root(ctx, logger, svcProvider, cmdConfigurator)
if err := rootCmd.Execute(); err != nil {
Expand Down
16 changes: 15 additions & 1 deletion pkg/core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ func (c *Core) Setup(ctx context.Context, cmd string, opts models.SetupOptions)
DockerDelay: opts.DockerDelay,
})
c.apps.Store(id, a)

err := a.Setup(ctx)
if err != nil {
utils.LogError(c.logger, err, "failed to setup app")
Expand All @@ -52,6 +51,21 @@ func (c *Core) Setup(ctx context.Context, cmd string, opts models.SetupOptions)
return id, nil
}

func (c *Core) UpdateAppInfo(ctx context.Context, id uint64, cmd string, opts models.SetupOptions) error {
a := app.NewApp(c.logger, id, cmd, app.Options{
DockerNetwork: opts.DockerNetwork,
Container: opts.Container,
DockerDelay: opts.DockerDelay,
})
c.apps.Store(id, a)
err := a.Setup(ctx)
if err != nil {
utils.LogError(c.logger, err, "failed to setup app")
return err
}
return nil
}

func (c *Core) getApp(id uint64) (*app.App, error) {
a, ok := c.apps.Load(id)
if !ok {
Expand Down
4 changes: 1 addition & 3 deletions pkg/core/proxy/integrations/mongo/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package mongo

import (
"context"
"errors"
"fmt"
"io"
"net"
Expand Down Expand Up @@ -56,8 +55,7 @@ func decodeMongo(ctx context.Context, logger *zap.Logger, reqBuf []byte, clientC
readRequestDelay = time.Since(started)
}
if len(reqBuf) == 0 {
errCh <- errors.New("the request buffer is empty")
return
continue
}
logger.Debug(fmt.Sprintf("the loop starts with the time delay: %v", time.Since(startedDecoding)))
opReq, requestHeader, mongoRequest, err := Decode(reqBuf, logger)
Expand Down
2 changes: 1 addition & 1 deletion pkg/graph/schema.resolvers.go

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

98 changes: 26 additions & 72 deletions pkg/platform/yaml/configdb/db.go
Original file line number Diff line number Diff line change
@@ -1,99 +1,53 @@
// Package configdb provides functionality for working with keploy configuration databases.
// Package configdb provides a config database implementation.
package configdb

import (
"context"
"errors"
"fmt"
"io"
"io/fs"
"os"
"path/filepath"
"runtime"

"go.keploy.io/server/v2/config"
"go.keploy.io/server/v2/pkg/platform/yaml"
"go.keploy.io/server/v2/utils"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.uber.org/zap"
yamlLib "gopkg.in/yaml.v3"
)

type ConfigDb struct {
logger *zap.Logger
type ConfigYaml struct {
ConfigPath string
Logger *zap.Logger
}

func UserHomeDir() string {

configFolder := "/.keploy"
if runtime.GOOS == "windows" {
home := os.Getenv("HOMEDRIVE") + os.Getenv("HOMEPATH")
if home == "" {
home = os.Getenv("USERPROFILE")
}
return home + configFolder
}
return os.Getenv("HOME") + configFolder
}

func NewConfigDb(logger *zap.Logger) *ConfigDb {
return &ConfigDb{
logger: logger,
func New(logger *zap.Logger, configPath string) *ConfigYaml {
return &ConfigYaml{
ConfigPath: configPath,
Logger: logger,
}
}

func (cdb *ConfigDb) GetInstallationID(ctx context.Context) (string, error) {
var id string
id = getInstallationFromFile(cdb.logger)
if id == "" {
id = primitive.NewObjectID().String()
err := cdb.setInstallationID(ctx, id)
if err != nil {
return "", fmt.Errorf("failed to set installation id in file. error: %s", err.Error())
}
}
return id, nil
}

func getInstallationFromFile(logger *zap.Logger) string {
var (
path = UserHomeDir()
id = ""
)

file, err := os.OpenFile(filepath.Join(path, "installation-id.yaml"), os.O_RDONLY, fs.ModePerm)
func (c *ConfigYaml) InsertConfig(ctx context.Context, testSetID string, testSetConfig config.TestSetConfig) error {
configPath := filepath.Join(c.ConfigPath, testSetID)
configFileName := "config"
data, err := yamlLib.Marshal(&testSetConfig)
if err != nil {
return id
}
defer func() {
if err := file.Close(); err != nil {
utils.LogError(logger, err, "failed to close file")
}
}()
decoder := yamlLib.NewDecoder(file)
err = decoder.Decode(&id)
if errors.Is(err, io.EOF) {
return id
return err
}
err = yaml.WriteFile(ctx, c.Logger, configPath, configFileName, data, false)
if err != nil {
return id
return err
}
return id
return nil
}

func (cdb *ConfigDb) setInstallationID(ctx context.Context, id string) error {
path := UserHomeDir()
data := []byte{}

d, err := yamlLib.Marshal(&id)
func (c *ConfigYaml) GetConfig(ctx context.Context, testSetID string) (config.TestSetConfig, error) {
configPath := filepath.Join(c.ConfigPath, testSetID)
configFileName := "config"
data, err := yaml.ReadFile(ctx, c.Logger, configPath, configFileName)
if err != nil {
return fmt.Errorf("failed to marshal document to yaml. error: %s", err.Error())
return config.TestSetConfig{}, err
}
data = append(data, d...)
err = yaml.WriteFile(ctx, cdb.logger, path, "installation-id", data, false)
var testSetConfig config.TestSetConfig
err = yamlLib.Unmarshal(data, &testSetConfig)
if err != nil {
utils.LogError(cdb.logger, err, "failed to write installation id in yaml file")
return err
return config.TestSetConfig{}, err
}

return nil
return testSetConfig, nil
}