Skip to content

Commit

Permalink
chore: add docker ip log and remove a switch (#1877)
Browse files Browse the repository at this point in the history
* chore: add docker ip log and remove a switch

Signed-off-by: Shubham Jain <shubhamkjain@outlook.com>

* fix: check container ip before starting tests

Signed-off-by: Shubham Jain <shubhamkjain@outlook.com>

---------

Signed-off-by: Shubham Jain <shubhamkjain@outlook.com>
  • Loading branch information
slayerjain committed May 9, 2024
1 parent 265cadf commit 82bab70
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 54 deletions.
85 changes: 42 additions & 43 deletions pkg/core/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,52 +263,49 @@ func (a *App) injectNetwork(network string) error {
return fmt.Errorf("failed to find the network:%v in the keploy container", network)
}

func (a *App) handleDockerEvents(ctx context.Context, e events.Message) (bool, error) {
var inode uint64
var iPAddress string
switch e.Action {
case "start":
// Fetch container details by inspecting using container ID to check if container is created
info, err := a.docker.ContainerInspect(ctx, e.ID)
if err != nil {
a.logger.Debug("failed to inspect container by container Id", zap.Error(err))
return false, err
}
func (a *App) extractMeta(ctx context.Context, e events.Message) (bool, error) {
if e.Action != "start" {
return false, nil
}
// Fetch container details by inspecting using container ID to check if container is created
info, err := a.docker.ContainerInspect(ctx, e.ID)
if err != nil {
a.logger.Debug("failed to inspect container by container Id", zap.Error(err))
return false, err
}

// Check if the container's name matches the desired name
if info.Name != "/"+a.container {
a.logger.Debug("ignoring container creation for unrelated container", zap.String("containerName", info.Name))
return false, nil
}
// Check if the container's name matches the desired name
if info.Name != "/"+a.container {
a.logger.Debug("ignoring container creation for unrelated container", zap.String("containerName", info.Name))
return false, nil
}

// Set Docker Container ID
a.docker.SetContainerID(e.ID)
a.logger.Debug("checking for container pid", zap.Any("containerDetails.State.Pid", info.State.Pid))
if info.State.Pid == 0 {
return false, errors.New("failed to get the pid of the container")
}
a.logger.Debug("", zap.Any("containerDetails.State.Pid", info.State.Pid), zap.String("containerName", a.container))
inode, err = getInode(info.State.Pid)
if err != nil {
return false, err
}
// Set Docker Container ID
a.docker.SetContainerID(e.ID)
a.logger.Debug("checking for container pid", zap.Any("containerDetails.State.Pid", info.State.Pid))
if info.State.Pid == 0 {
return false, errors.New("failed to get the pid of the container")
}
a.logger.Debug("", zap.Any("containerDetails.State.Pid", info.State.Pid), zap.String("containerName", a.container))
inode, err := getInode(info.State.Pid)
if err != nil {
return false, err
}

a.inodeChan <- inode
a.logger.Debug("container started and successfully extracted inode", zap.Any("inode", inode))
if info.NetworkSettings == nil || info.NetworkSettings.Networks == nil {
a.logger.Debug("container network settings not available", zap.Any("containerDetails.NetworkSettings", info.NetworkSettings))
return false, nil
}
a.inodeChan <- inode
a.logger.Debug("container started and successfully extracted inode", zap.Any("inode", inode))
if info.NetworkSettings == nil || info.NetworkSettings.Networks == nil {
a.logger.Debug("container network settings not available", zap.Any("containerDetails.NetworkSettings", info.NetworkSettings))
return false, nil
}

n, ok := info.NetworkSettings.Networks[a.containerNetwork]
if !ok || n == nil {
a.logger.Debug("container network not found", zap.Any("containerDetails.NetworkSettings.Networks", info.NetworkSettings.Networks))
return false, fmt.Errorf("container network not found: %s", fmt.Sprintf("%+v", info.NetworkSettings.Networks))
}
a.containerIPv4 = n.IPAddress
iPAddress = n.IPAddress
n, ok := info.NetworkSettings.Networks[a.containerNetwork]
if !ok || n == nil {
a.logger.Debug("container network not found", zap.Any("containerDetails.NetworkSettings.Networks", info.NetworkSettings.Networks))
return false, fmt.Errorf("container network not found: %s", fmt.Sprintf("%+v", info.NetworkSettings.Networks))
}
return inode != 0 && iPAddress != "", nil
a.containerIPv4 = n.IPAddress
return inode != 0 && n.IPAddress != "", nil
}

func (a *App) getDockerMeta(ctx context.Context) <-chan error {
Expand Down Expand Up @@ -350,11 +347,13 @@ func (a *App) getDockerMeta(ctx context.Context) <-chan error {
errCh <- ctx.Err()
return nil
case e := <-messages:
eventCaptured, err := a.handleDockerEvents(ctx, e)
done, err := a.extractMeta(ctx, e)
if err != nil {
errCh <- err
return nil
} else if eventCaptured {
}

if done {
return nil
}
// for debugging purposes
Expand Down
9 changes: 7 additions & 2 deletions pkg/core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,13 +243,18 @@ func (c *Core) Run(ctx context.Context, id uint64, _ models.RunOptions) models.A
}
}

func (c *Core) GetAppIP(_ context.Context, id uint64) (string, error) {
func (c *Core) GetContainerIP(_ context.Context, id uint64) (string, error) {

a, err := c.getApp(id)
if err != nil {
utils.LogError(c.logger, err, "failed to get app")
return "", err
}

return a.ContainerIPv4Addr(), nil
ip := a.ContainerIPv4Addr()
if ip == "" {
return "", fmt.Errorf("failed to get the IP address of the app container. Try increasing --delay (in seconds)")
}

return ip, nil
}
17 changes: 9 additions & 8 deletions pkg/service/replay/replay.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,15 @@ func (r *Replayer) RunTestSet(ctx context.Context, testSetID string, testRunID s
return models.TestSetStatusUserAbort, context.Canceled
}

cmdType := utils.FindDockerCmd(r.config.Command)
var userIP string
if cmdType == utils.Docker || cmdType == utils.DockerCompose {
userIP, err = r.instrumentation.GetContainerIP(ctx, appID)
if err != nil {
return models.TestSetStatusFailed, err
}
}

selectedTests := ArrayToMap(r.config.Test.SelectedTests[testSetID])

testCasesCount := len(testCases)
Expand Down Expand Up @@ -398,16 +407,8 @@ func (r *Replayer) RunTestSet(ctx context.Context, testSetID string, testRunID s

started := time.Now().UTC()

cmdType := utils.FindDockerCmd(r.config.Command)

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

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

testCase.HTTPReq.URL, err = replaceHostToIP(testCase.HTTPReq.URL, userIP)
if err != nil {
utils.LogError(r.logger, err, "failed to replace host to docker container's IP")
Expand Down
2 changes: 1 addition & 1 deletion pkg/service/replay/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type Instrumentation interface {
// Run is blocking call and will execute until error
Run(ctx context.Context, id uint64, opts models.RunOptions) models.AppError

GetAppIP(ctx context.Context, id uint64) (string, error)
GetContainerIP(ctx context.Context, id uint64) (string, error)
}

type Service interface {
Expand Down

0 comments on commit 82bab70

Please sign in to comment.