Skip to content

Commit

Permalink
fix test logs (#4325)
Browse files Browse the repository at this point in the history
* fix: okteto test logs

Signed-off-by: Javier Lopez <[email protected]>

* fix: logs failed execution

Signed-off-by: Javier Lopez <[email protected]>

* fix: golangci linter

Signed-off-by: Javier Lopez <[email protected]>

* logs: remove running stage logs on tests

Signed-off-by: Javier Lopez <[email protected]>

* logs: improve log messages

Signed-off-by: Javier Lopez <[email protected]>

* refactor: rename variable

Signed-off-by: Javier Lopez <[email protected]>

---------

Signed-off-by: Javier Lopez <[email protected]>
  • Loading branch information
jLopezbarb committed May 31, 2024
1 parent eb683fb commit b2e1fe6
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 20 deletions.
3 changes: 2 additions & 1 deletion cmd/test/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -359,10 +359,11 @@ func doRun(ctx context.Context, servicesToTest []string, options *Options, ioCtr
params.CacheInvalidationKey = "const"
}

ioCtrl.Out().Infof("Executing '%s'", name)
ioCtrl.Out().Infof("Executing test container '%s'", name)
if err := runner.Run(ctx, params); err != nil {
return metadata, err
}
oktetoLog.Success("Test container '%s' passed", name)
}
metadata.Success = true
return metadata, nil
Expand Down
6 changes: 3 additions & 3 deletions pkg/cmd/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,12 @@ func (ob *OktetoBuilder) GetBuilder() string {

// Run runs the build sequence
func (ob *OktetoBuilder) Run(ctx context.Context, buildOptions *types.BuildOptions, ioCtrl *io.Controller) error {
isDeployOrDestroy := buildOptions.OutputMode == DeployOutputModeOnBuild || buildOptions.OutputMode == DestroyOutputModeOnBuild
isRemoteExecution := buildOptions.OutputMode == DeployOutputModeOnBuild || buildOptions.OutputMode == DestroyOutputModeOnBuild || buildOptions.OutputMode == TestOutputModeOnBuild
buildOptions.OutputMode = setOutputMode(buildOptions.OutputMode)
depotToken := os.Getenv(DepotTokenEnvVar)
depotProject := os.Getenv(DepotProjectEnvVar)

if !isDeployOrDestroy {
if !isRemoteExecution {
builder := ob.GetBuilder()
buildMsg := fmt.Sprintf("Building '%s'", buildOptions.File)
depotEnabled := IsDepotEnabled()
Expand All @@ -108,7 +108,7 @@ func (ob *OktetoBuilder) Run(ctx context.Context, buildOptions *types.BuildOptio
// When depot is available we only go to depot if it's not a deploy or a destroy.
// On depot the workload id is not working correctly and the users would not be able to
// use the internal cluster ip as if they were running their scripts on the k8s cluster
case IsDepotEnabled() && !isDeployOrDestroy:
case IsDepotEnabled() && !isRemoteExecution:
depotManager := newDepotBuilder(depotProject, depotToken, ob.OktetoContext, ioCtrl)
return depotManager.Run(ctx, buildOptions, solveBuild)
case ob.OktetoContext.GetCurrentBuilder() == "":
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/build/buildkit.go
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ func solveBuild(ctx context.Context, c *client.Client, opt *client.SolveOpt, pro
// We need to wait until the tty channel is closed to avoid writing to stdout while the tty is being used
_, err := progressui.DisplaySolveStatus(context.TODO(), c, ioCtrl.Out(), ttyChannel)
return err
case DeployOutputModeOnBuild, DestroyOutputModeOnBuild:
case DeployOutputModeOnBuild, DestroyOutputModeOnBuild, TestOutputModeOnBuild:
err := deployDisplayer(context.TODO(), plainChannel, &types.BuildOptions{OutputMode: progress})
commandFailChannel <- err
return err
Expand Down
2 changes: 2 additions & 0 deletions pkg/cmd/build/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@ const (
DeployOutputModeOnBuild = "deploy"
// DestroyOutputModeOnBuild Defines the output mode set to BuildOptions when running a remote destroy
DestroyOutputModeOnBuild = "destroy"
// TestOutputModeOnBuild Defines the output mode set to BuildOptions when running a remote test
TestOutputModeOnBuild = "test"
)
59 changes: 48 additions & 11 deletions pkg/cmd/build/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"context"
"encoding/json"
"fmt"
"regexp"
"strings"
"time"

Expand All @@ -26,6 +27,10 @@ import (
"github.com/tonistiigi/units"
)

var (
nameRegex = regexp.MustCompile(`--name\s+"([^"]+)"`)
)

const (
// largeContextThreshold is the threshold (in bytes) by which a context is catalogued as large or not (50MB)
largeContextThreshold = 50000000
Expand All @@ -45,11 +50,17 @@ func deployDisplayer(ctx context.Context, ch chan *client.SolveStatus, o *types.
var done bool
var outputMode string

if o.OutputMode == DestroyOutputModeOnBuild {
switch o.OutputMode {
case DeployOutputModeOnBuild:
outputMode = DeployOutputModeOnBuild
case DestroyOutputModeOnBuild:
outputMode = DestroyOutputModeOnBuild
} else {
case TestOutputModeOnBuild:
outputMode = TestOutputModeOnBuild
default:
outputMode = DeployOutputModeOnBuild
}

for {
select {
case <-ctx.Done():
Expand Down Expand Up @@ -86,11 +97,15 @@ type trace struct {
}

type OktetoCommandErr struct {
Err error
Stage string
Err error
Stage string
output string
}

func (e OktetoCommandErr) Error() string {
if e.output == TestOutputModeOnBuild {
return fmt.Sprintf("test container '%s' failed", e.Stage)
}
return fmt.Sprintf("error on stage %s: %s", e.Stage, e.Err.Error())
}

Expand All @@ -107,13 +122,17 @@ func (t *trace) update(ss *client.SolveStatus) error {
v, ok := t.ongoing[rawVertex.Digest.Encoded()]
if !ok {
v = &vertexInfo{
name: rawVertex.Name,
name: rawVertex.Name,
cached: rawVertex.Cached,
}
t.ongoing[rawVertex.Digest.Encoded()] = v
}
if rawVertex.Error != "" {
return fmt.Errorf("error on stage %s: %s", rawVertex.Name, rawVertex.Error)
}
if rawVertex.Cached {
v.cached = true
}
if rawVertex.Completed != nil {
v.completed = true
continue
Expand Down Expand Up @@ -146,17 +165,21 @@ func (t *trace) display(progress string) {
currentLoadedCtx := units.Bytes(v.currentTransferedContext)
if t.showCtxAdvice && currentLoadedCtx > largeContextThreshold {
t.showCtxAdvice = false
oktetoLog.Information("You can use '.oktetodeployignore' file to optimize the context used to deploy your development environment.")
oktetoLog.Information("You can use '.oktetoignore' file to optimize the context used to deploy your development environment.")
}
oktetoLog.Spinner(fmt.Sprintf("Synchronizing context: %.2f", currentLoadedCtx))
}
}
if t.hasCommandLogs(v) {
if progress == DeployOutputModeOnBuild {
switch progress {
case DeployOutputModeOnBuild:
oktetoLog.Spinner("Deploying your development environment...")
} else {
case DestroyOutputModeOnBuild:
oktetoLog.Spinner("Destroying your development environment...")
case TestOutputModeOnBuild:
oktetoLog.Spinner("Running tests...")
}

for _, log := range v.logs {
var text oktetoLog.JSONLogFormat
if err := json.Unmarshal([]byte(log), &text); err != nil {
Expand All @@ -178,14 +201,17 @@ func (t *trace) display(progress string) {
default:
// Print the information message about the stage if needed
if _, ok := t.stages[text.Stage]; !ok {
oktetoLog.Information("Running stage '%s'", text.Stage)
if progress != TestOutputModeOnBuild {
oktetoLog.Information("Running stage '%s'", text.Stage)
}
t.stages[text.Stage] = true
}
if text.Level == "error" {
if text.Stage != "" {
t.err = OktetoCommandErr{
Stage: text.Stage,
Err: fmt.Errorf(text.Message),
Stage: text.Stage,
Err: fmt.Errorf(text.Message),
output: progress,
}
}
} else {
Expand Down Expand Up @@ -213,6 +239,16 @@ func (t trace) hasCommandLogs(v *vertexInfo) bool {
func (t *trace) removeCompletedSteps() {
for k, v := range t.ongoing {
if v.completed {
// We need to specify the command test in order to not affect okteto deploy on remote
if strings.Contains(v.name, "remote-run test") && v.cached {
match := nameRegex.FindStringSubmatch(v.name)
if len(match) > 1 {
name := match[1]
oktetoLog.Information("Skipping test container '%s', CACHED", name)
} else {
oktetoLog.Information("Skipping test container, CACHED")
}
}
delete(t.ongoing, k)
}
}
Expand All @@ -224,4 +260,5 @@ type vertexInfo struct {
currentTransferedContext int64
totalTransferedContext int64
completed bool
cached bool
}
2 changes: 1 addition & 1 deletion pkg/deployable/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ func (dr *TestRunner) RunTest(params TestParameters) error {
if err := dr.Executor.Execute(command, execEnv); err != nil {
return err
}
oktetoLog.Success("Command '%s' successfully executed", command.Name)

// Read variables that may have been written to OKTETO_ENV in the current step
envsFromOktetoEnvFile, err := envStepper.Step()
Expand All @@ -67,6 +68,5 @@ func (dr *TestRunner) RunTest(params TestParameters) error {

params.Variables = append(params.Variables, envsFromOktetoEnvFile...)
}

return nil
}
13 changes: 10 additions & 3 deletions pkg/remote/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,11 +280,18 @@ func (r *Runner) Run(ctx context.Context, params *Params) error {
return err
}

outputMode := buildCmd.DeployOutputModeOnBuild
if params.Command == DestroyCommand {
var outputMode string
switch params.Command {
case TestCommand:
outputMode = buildCmd.TestOutputModeOnBuild
case DeployCommand:
outputMode = buildCmd.DeployOutputModeOnBuild
case DestroyCommand:
outputMode = buildCmd.DestroyOutputModeOnBuild

default:
outputMode = buildCmd.DeployOutputModeOnBuild
}

buildOptions := buildCmd.OptsFromBuildInfoForRemoteDeploy(buildInfo, &types.BuildOptions{OutputMode: outputMode})
buildOptions.Manifest = params.Manifest
buildOptions.BuildArgs = append(
Expand Down

0 comments on commit b2e1fe6

Please sign in to comment.