|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "strings" |
| 7 | + |
| 8 | + "github.com/SAP/jenkins-library/pkg/command" |
| 9 | + "github.com/SAP/jenkins-library/pkg/log" |
| 10 | + "github.com/SAP/jenkins-library/pkg/telemetry" |
| 11 | +) |
| 12 | + |
| 13 | +type vaultUrl struct { |
| 14 | + URL string `json:"url"` |
| 15 | + Username string `json:"username,omitempty"` |
| 16 | + Password string `json:"password,omitempty"` |
| 17 | +} |
| 18 | + |
| 19 | +func npmExecuteTests(config npmExecuteTestsOptions, _ *telemetry.CustomData) { |
| 20 | + c := command.Command{} |
| 21 | + |
| 22 | + c.Stdout(log.Writer()) |
| 23 | + c.Stderr(log.Writer()) |
| 24 | + err := runNpmExecuteTests(&config, &c) |
| 25 | + if err != nil { |
| 26 | + log.Entry().WithError(err).Fatal("Step execution failed") |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +func runNpmExecuteTests(config *npmExecuteTestsOptions, c command.ExecRunner) error { |
| 31 | + if len(config.Envs) > 0 { |
| 32 | + c.SetEnv(config.Envs) |
| 33 | + } |
| 34 | + |
| 35 | + if len(config.Paths) > 0 { |
| 36 | + path := fmt.Sprintf("PATH=%s:%s", os.Getenv("PATH"), strings.Join(config.Paths, ":")) |
| 37 | + c.SetEnv([]string{path}) |
| 38 | + } |
| 39 | + |
| 40 | + if config.WorkingDirectory != "" { |
| 41 | + if err := os.Chdir(config.WorkingDirectory); err != nil { |
| 42 | + return fmt.Errorf("failed to change directory: %w", err) |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + installCommandTokens := strings.Fields(config.InstallCommand) |
| 47 | + if err := c.RunExecutable(installCommandTokens[0], installCommandTokens[1:]...); err != nil { |
| 48 | + return fmt.Errorf("failed to execute install command: %w", err) |
| 49 | + } |
| 50 | + |
| 51 | + parsedURLs, err := parseURLs(config.VaultURLs) |
| 52 | + if err != nil { |
| 53 | + return err |
| 54 | + } |
| 55 | + |
| 56 | + for _, app := range parsedURLs { |
| 57 | + if err := runTestForUrl(app.URL, app.Username, app.Password, config, c); err != nil { |
| 58 | + return err |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + if err := runTestForUrl(config.BaseURL, config.VaultUsername, config.VaultPassword, config, c); err != nil { |
| 63 | + return err |
| 64 | + } |
| 65 | + return nil |
| 66 | +} |
| 67 | + |
| 68 | +func runTestForUrl(url, username, password string, config *npmExecuteTestsOptions, command command.ExecRunner) error { |
| 69 | + credentialsToEnv(username, password, config.UsernameEnvVar, config.PasswordEnvVar, command) |
| 70 | + // we need to reset the env vars as the next test might not have any credentials |
| 71 | + defer resetCredentials(config.UsernameEnvVar, config.PasswordEnvVar, command) |
| 72 | + |
| 73 | + runScriptTokens := strings.Fields(config.RunCommand) |
| 74 | + if config.UrlOptionPrefix != "" { |
| 75 | + runScriptTokens = append(runScriptTokens, config.UrlOptionPrefix+url) |
| 76 | + } |
| 77 | + if err := command.RunExecutable(runScriptTokens[0], runScriptTokens[1:]...); err != nil { |
| 78 | + return fmt.Errorf("failed to execute npm script: %w", err) |
| 79 | + } |
| 80 | + |
| 81 | + return nil |
| 82 | +} |
| 83 | + |
| 84 | +func parseURLs(urls []map[string]interface{}) ([]vaultUrl, error) { |
| 85 | + parsedUrls := []vaultUrl{} |
| 86 | + |
| 87 | + for _, url := range urls { |
| 88 | + parsedUrl := vaultUrl{} |
| 89 | + urlStr, ok := url["url"].(string) |
| 90 | + if !ok { |
| 91 | + return nil, fmt.Errorf("url field is not a string") |
| 92 | + } |
| 93 | + parsedUrl.URL = urlStr |
| 94 | + if username, ok := url["username"].(string); ok { |
| 95 | + parsedUrl.Username = username |
| 96 | + } |
| 97 | + |
| 98 | + if password, ok := url["password"].(string); ok { |
| 99 | + parsedUrl.Password = password |
| 100 | + } |
| 101 | + parsedUrls = append(parsedUrls, parsedUrl) |
| 102 | + } |
| 103 | + return parsedUrls, nil |
| 104 | +} |
| 105 | + |
| 106 | +func credentialsToEnv(username, password, usernameEnv, passwordEnv string, c command.ExecRunner) { |
| 107 | + if username == "" || password == "" { |
| 108 | + return |
| 109 | + } |
| 110 | + c.SetEnv([]string{usernameEnv + "=" + username, passwordEnv + "=" + password}) |
| 111 | +} |
| 112 | + |
| 113 | +func resetCredentials(usernameEnv, passwordEnv string, c command.ExecRunner) { |
| 114 | + c.SetEnv([]string{usernameEnv + "=", passwordEnv + "="}) |
| 115 | +} |
0 commit comments