From b3e2501d28abd53142b0ed23146c8d3b1755f09d Mon Sep 17 00:00:00 2001 From: Anurag Madnawat Date: Tue, 30 Jul 2024 18:22:53 -0700 Subject: [PATCH] [feat]: [CI-13679]: Refactor RunTestV2 Code to make it reusable (#304) * [feat]: [CI-13679]: Refactor RunTestV2 Code to make it resusable * Update runtestsV2.go * Update runtestsV2.go --- pipeline/runtime/runtestsV2.go | 119 +++++++++++++++++---------------- 1 file changed, 63 insertions(+), 56 deletions(-) diff --git a/pipeline/runtime/runtestsV2.go b/pipeline/runtime/runtestsV2.go index 72ba9273..9a1994ff 100644 --- a/pipeline/runtime/runtestsV2.go +++ b/pipeline/runtime/runtestsV2.go @@ -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 @@ -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,