Skip to content

Commit

Permalink
[feat]: [CI-13679]: Refactor RunTestV2 Code to make it reusable (#304)
Browse files Browse the repository at this point in the history
* [feat]: [CI-13679]: Refactor RunTestV2 Code to make it resusable

* Update runtestsV2.go

* Update runtestsV2.go
  • Loading branch information
anurag-harness authored Jul 31, 2024
1 parent dddbee4 commit b3e2501
Showing 1 changed file with 63 additions and 56 deletions.
119 changes: 63 additions & 56 deletions pipeline/runtime/runtestsV2.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,73 +46,27 @@ const (
dotNetConfigV2Dir = "%s/ti/v2/dotnet/config"
)

// Ignoring optimization state for now
//
//nolint:funlen,gocritic,gocyclo
//nolint:gocritic,gocyclo
func executeRunTestsV2Step(ctx context.Context, f RunFunc, r *api.StartStepRequest, out io.Writer,
tiConfig *tiCfg.Cfg) (*runtime.State, map[string]string, map[string]string, []byte, []*api.OutputV2, string, error) {
start := time.Now()
tmpFilePath := tiConfig.GetDataDir()
fs := filesystem.New()
log := logrus.New()
log.Out = out
optimizationState := types.DISABLED
step := toStep(r)
setTiEnvVariables(step, tiConfig)
agentPaths := make(map[string]string)
step.Entrypoint = r.RunTestsV2.Entrypoint
if r.RunTestsV2.IntelligenceMode {
links, err := instrumentation.GetV2AgentDownloadLinks(ctx, tiConfig)
if err != nil {
return nil, nil, nil, nil, nil, string(optimizationState), fmt.Errorf("failed to get AgentV2 URL from TI")
}
if len(links) < agentV2LinkLength {
return nil, nil, nil, nil, nil, string(optimizationState), fmt.Errorf("error: Could not get agent V2 links from TI")
}

err = downloadJavaAgent(ctx, tmpFilePath, links[0].URL, fs, log)
if err != nil {
return nil, nil, nil, nil, nil, string(optimizationState), fmt.Errorf("failed to download Java agent")
}

rubyArtifactDir, err := downloadRubyAgent(ctx, tmpFilePath, links[2].URL, fs, log)
if err != nil || rubyArtifactDir == "" {
return nil, nil, nil, nil, nil, string(optimizationState), fmt.Errorf("failed to download Ruby agent")
}
agentPaths["ruby"] = rubyArtifactDir

pythonArtifactDir, err := downloadPythonAgent(ctx, tmpFilePath, links[1].URL, fs, log)
if err != nil {
return nil, nil, nil, nil, nil, string(optimizationState), fmt.Errorf("failed to download Python agent")
}
agentPaths["python"] = pythonArtifactDir

if len(links) > dotNetAgentLinkIndex {
var dotNetArtifactDir string
dotNetArtifactDir, err = downloadDotNetAgent(ctx, tmpFilePath, links[dotNetAgentLinkIndex].URL, fs, log)
if err == nil {
agentPaths["dotnet"] = dotNetArtifactDir
} else {
log.Warningln(".net agent installation failed. Continuing without .net support.")
}
}

isPsh := IsPowershell(step.Entrypoint)
preCmd, filterfilePath, err := getPreCmd(r.WorkingDir, tmpFilePath, fs, log, r.Envs, agentPaths, isPsh, tiConfig)
if err != nil || pythonArtifactDir == "" {
return nil, nil, nil, nil, nil, string(optimizationState), fmt.Errorf("failed to set config file or env variable to inject agent, %s", err)
}

commands := fmt.Sprintf("%s\n%s", preCmd, r.RunTestsV2.Command[0])
step.Command = []string{commands}

err = createSelectedTestFile(ctx, fs, step.Name, r.WorkingDir, log, tiConfig, tmpFilePath, r.Envs, &r.RunTestsV2, filterfilePath)
if err != nil {
return nil, nil, nil, nil, nil, string(optimizationState), fmt.Errorf("error while creating filter file %s", err)
}
} else {
step.Command = []string{r.RunTestsV2.Command[0]}
preCmd, err := SetupRunTestV2(ctx, &r.RunTestsV2, step.Name, r.WorkingDir, log, r.Envs, tiConfig)
if err != nil {
return nil, nil, nil, nil, nil, string(optimizationState), err
}
command := r.RunTestsV2.Command[0]
if preCmd != "" {
command = fmt.Sprintf("%s\n%s", preCmd, command)
}
step.Command = []string{command}

exportEnvFile := fmt.Sprintf("%s/%s-export.env", pipeline.SharedVolPath, step.ID)
step.Envs["DRONE_ENV"] = exportEnvFile

Expand Down Expand Up @@ -178,6 +132,59 @@ func executeRunTestsV2Step(ctx context.Context, f RunFunc, r *api.StartStepReque
return exited, nil, exportEnvs, artifact, nil, string(optimizationState), err
}

func SetupRunTestV2(ctx context.Context, config *api.RunTestsV2Config, stepID, workspace string, log *logrus.Logger, envs map[string]string, tiConfig *tiCfg.Cfg) (string, error) {
agentPaths := make(map[string]string)
fs := filesystem.New()
tmpFilePath := tiConfig.GetDataDir()
var preCmd, filterfilePath string
if config.IntelligenceMode {
links, err := instrumentation.GetV2AgentDownloadLinks(ctx, tiConfig)
if err != nil {
return preCmd, fmt.Errorf("failed to get AgentV2 URL from TI")
}
if len(links) < agentV2LinkLength {
return preCmd, fmt.Errorf("error: Could not get agent V2 links from TI")
}

err = downloadJavaAgent(ctx, tmpFilePath, links[0].URL, fs, log)
if err != nil {
return preCmd, fmt.Errorf("failed to download Java agent")
}

rubyArtifactDir, err := downloadRubyAgent(ctx, tmpFilePath, links[2].URL, fs, log)
if err != nil || rubyArtifactDir == "" {
return preCmd, fmt.Errorf("failed to download Ruby agent")
}
agentPaths["ruby"] = rubyArtifactDir

pythonArtifactDir, err := downloadPythonAgent(ctx, tmpFilePath, links[1].URL, fs, log)
if err != nil {
return preCmd, fmt.Errorf("failed to download Python agent")
}
agentPaths["python"] = pythonArtifactDir

if len(links) > dotNetAgentLinkIndex {
var dotNetArtifactDir string
dotNetArtifactDir, err = downloadDotNetAgent(ctx, tmpFilePath, links[dotNetAgentLinkIndex].URL, fs, log)
if err == nil {
agentPaths["dotnet"] = dotNetArtifactDir
} else {
log.Warningln(".net agent installation failed. Continuing without .net support.")
}
}
isPsh := IsPowershell(config.Entrypoint)
preCmd, filterfilePath, err = getPreCmd(workspace, tmpFilePath, fs, log, envs, agentPaths, isPsh, tiConfig)
if err != nil || pythonArtifactDir == "" {
return preCmd, fmt.Errorf("failed to set config file or env variable to inject agent, %s", err)
}
err = createSelectedTestFile(ctx, fs, stepID, workspace, log, tiConfig, tmpFilePath, envs, config, filterfilePath)
if err != nil {
return preCmd, fmt.Errorf("error while creating filter file %s", err)
}
}
return preCmd, nil
}

// Second parameter in return type (bool) is will be used to decide whether the filter file should be created or not.
// In case of running all the cases no filter file should be created.
func getTestsSelection(ctx context.Context, fs filesystem.FileSystem, stepID, workspace string, log *logrus.Logger,
Expand Down

0 comments on commit b3e2501

Please sign in to comment.