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

fix: fixed re-record for docker #1876

Merged
merged 9 commits into from
May 20, 2024
Merged
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
23 changes: 21 additions & 2 deletions pkg/service/record/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ func (r *Recorder) Start(ctx context.Context) error {
})
go func() {
if len(r.config.ReRecord) != 0 {
err = r.ReRecord(reRecordCtx)
err = r.ReRecord(reRecordCtx, appID)
reRecordCancel()

}
Expand Down Expand Up @@ -335,7 +335,7 @@ func (r *Recorder) StartMock(ctx context.Context) error {
return fmt.Errorf(stopReason)
}

func (r *Recorder) ReRecord(ctx context.Context) error {
func (r *Recorder) ReRecord(ctx context.Context, appID uint64) error {

tcs, err := r.testDB.GetTestCases(ctx, r.config.ReRecord)
if err != nil {
Expand All @@ -348,6 +348,10 @@ func (r *Recorder) ReRecord(ctx context.Context) error {
return nil

}
cmdType := utils.FindDockerCmd(r.config.Command)
if cmdType == utils.Docker || cmdType == utils.DockerCompose {
host = r.config.ContainerName
}

if err := waitForPort(ctx, host, port); err != nil {
r.logger.Error("Waiting for port failed", zap.String("host", host), zap.String("port", port), zap.Error(err))
Expand All @@ -356,6 +360,21 @@ func (r *Recorder) ReRecord(ctx context.Context) error {

allTestCasesRecorded := true
for _, tc := range tcs {
if cmdType == utils.Docker || cmdType == utils.DockerCompose {

userIP, err := r.instrumentation.GetContainerIP(ctx, appID)
if err != nil {
utils.LogError(r.logger, err, "failed to get the app ip")
break
}

tc.HTTPReq.URL, err = utils.ReplaceHostToIP(tc.HTTPReq.URL, userIP)
if err != nil {
utils.LogError(r.logger, err, "failed to replace host to docker container's IP")
break
}
r.logger.Debug("", zap.Any("replaced URL in case of docker env", tc.HTTPReq.URL))
}

resp, err := pkg.SimulateHTTP(ctx, *tc, r.config.ReRecord, r.logger, r.config.Test.APITimeout)
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion pkg/service/record/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ type Instrumentation interface {
GetOutgoing(ctx context.Context, id uint64, opts models.OutgoingOptions) (<-chan *models.Mock, error)
// Run is blocking call and will execute until error
Run(ctx context.Context, id uint64, opts models.RunOptions) models.AppError
GetContainerIP(ctx context.Context, id uint64) (string, error)
}

type Service interface {
Start(ctx context.Context) error
StartMock(ctx context.Context) error
ReRecord(ctx context.Context) error
ReRecord(ctx context.Context, appID uint64) error
}

type TestDB interface {
Expand Down
3 changes: 2 additions & 1 deletion pkg/service/record/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func extractHostAndPort(curlCmd string) (string, string, error) {
return "", "", fmt.Errorf("no URL found in CURL command")
}

func waitForPort(ctx context.Context, host, port string) error {
func waitForPort(ctx context.Context, host string, port string) error {
ticker := time.NewTicker(1 * time.Second)
defer ticker.Stop()

Expand All @@ -43,6 +43,7 @@ func waitForPort(ctx context.Context, host, port string) error {
case <-ctx.Done():
return ctx.Err()
case <-ticker.C:

conn, err := net.DialTimeout("tcp", net.JoinHostPort(host, port), 1*time.Second)
if err == nil {
err := conn.Close()
Expand Down
2 changes: 1 addition & 1 deletion pkg/service/replay/replay.go
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ func (r *Replayer) RunTestSet(ctx context.Context, testSetID string, testRunID s

if cmdType == utils.Docker || cmdType == utils.DockerCompose {

testCase.HTTPReq.URL, err = replaceHostToIP(testCase.HTTPReq.URL, userIP)
testCase.HTTPReq.URL, err = utils.ReplaceHostToIP(testCase.HTTPReq.URL, userIP)
if err != nil {
utils.LogError(r.logger, err, "failed to replace host to docker container's IP")
break
Expand Down
21 changes: 0 additions & 21 deletions pkg/service/replay/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package replay
import (
"context"
"fmt"
"net/url"
"strings"

"go.keploy.io/server/v2/config"
"go.keploy.io/server/v2/pkg"
Expand Down Expand Up @@ -43,25 +41,6 @@ func LeftJoinNoise(globalNoise config.GlobalNoise, tsNoise config.GlobalNoise) c
return noise
}

func replaceHostToIP(currentURL string, ipAddress string) (string, error) {
// Parse the current URL
parsedURL, err := url.Parse(currentURL)

if err != nil {
// Return the original URL if parsing fails
return currentURL, err
}

if ipAddress == "" {
return currentURL, fmt.Errorf("failed to replace url in case of docker env")
}

// Replace hostname with the IP address
parsedURL.Host = strings.Replace(parsedURL.Host, parsedURL.Hostname(), ipAddress, 1)
// Return the modified URL
return parsedURL.String(), nil
}

type testUtils struct {
logger *zap.Logger
apiTimeout uint64
Expand Down
20 changes: 20 additions & 0 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"io"
"net"
"net/http"
"net/url"
"os"
"os/exec"
"path/filepath"
Expand All @@ -30,6 +31,25 @@ import (

var WarningSign = "\U000026A0"

func ReplaceHostToIP(currentURL string, ipAddress string) (string, error) {
// Parse the current URL
parsedURL, err := url.Parse(currentURL)

if err != nil {
// Return the original URL if parsing fails
return currentURL, err
}

if ipAddress == "" {
return currentURL, fmt.Errorf("failed to replace url in case of docker env")
}

// Replace hostname with the IP address
parsedURL.Host = strings.Replace(parsedURL.Host, parsedURL.Hostname(), ipAddress, 1)
// Return the modified URL
return parsedURL.String(), nil
}

func BindFlagsToViper(logger *zap.Logger, cmd *cobra.Command, viperKeyPrefix string) error {
var bindErr error
cmd.Flags().VisitAll(func(flag *pflag.Flag) {
Expand Down