-
Notifications
You must be signed in to change notification settings - Fork 593
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Introduce new npmExecuteTests step #5124
Merged
Merged
Changes from all commits
Commits
Show all changes
64 commits
Select commit
Hold shift + click to select a range
da9d73b
feat: Introduce new npmExecuteTests step
20e00eb
fix: comma
f24b876
fix: parens
7cadbf8
fix: error handling
221840f
fix: paren
17f0810
fix: config keys
0dc2cee
fix: rename key
49d42fb
Merge branch 'master' into phgermanov/npm-execute-tests-go
phgermanov 47ca127
test: failng tests
187d850
fix: handle empty openFile call
aeef66b
refactor: clean install rather than install
56dfffe
refactor: full run command in config
65f7cba
build: use lts-bookworm
20f05d8
Merge branch 'master' into phgermanov/npm-execute-tests-go
phgermanov 045b530
feat: get credentials from vault
6bf1d06
feat: remove wdi5 install script
64f063f
fix: groovy tests
f67b7a8
fix: report type
phgermanov 8e4ba08
docs: remove defaults from desc
1a3e5ea
feat: remove branch params
c22d874
refactor: rename appSecrets to vaultMetadata
8c2456d
feat: param for custom env var prefix
7c14e86
refactor: change app urls map to a list
d8db284
refactor: rename appURLs for clarity
44f8cd9
refactor: user can specify url option param
ec8369a
docs: go generate
5768f0f
feat: add support for user defined env vars and paths
84cf942
refactor: rename runScript to runCommand
d4cf486
refactor: expose username and password env vars
cf6af6f
refactor: rename envVars to envs
5dc002b
docs: add documentation for npmExecuteTests step
17175ff
feat: better handle vault secrets
3d9c038
refactor: flatten vault secrets
0f8f9c3
docs: add npmExecuteTests docs to mkdocs
1a682fc
feat: add npmExecuteTests to piper cmd
db6c087
Merge branch 'master' into phgermanov/npm-execute-tests-go
phgermanov 23ac975
chore: go generate
e1a3653
fix: modify working dir
970f340
fix: change npm cache dir
803f18d
fix: use os home dir
bc61bf3
fix: remove working dir
103f0e5
fix: remove npm set cache
9c482a4
feat: add param for changing dir
84879a5
Merge branch 'master' into phgermanov/npm-execute-tests-go
phgermanov 14f1527
chore: log current directory
c778adf
chore: fix log
51548a4
test: debug log
d1ac1e7
fix: cnange workdir param
ce98cff
fix: default workdir
d7e4bc0
test: add param
636be4b
test: log config
13a287e
S C O P E
52ed146
chore: remove debug logs
4da465d
feat: add sidecar
d135127
fix: sidecar config
9b637e2
fix: remove sidecar
44754da
feat: add placeholder sidecar
2766216
refactor: remove unnecessary logs
2469441
feat: defer credentials reset
9aec770
docs: selenium grid
c215bac
feat: add selenium image
a205ec9
chore: remove debug log
d768424
Merge branch 'master' into phgermanov/npm-execute-tests-go
phgermanov 6fc71b3
docs: add beta warning
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"strings" | ||
|
||
"github.com/SAP/jenkins-library/pkg/command" | ||
"github.com/SAP/jenkins-library/pkg/log" | ||
"github.com/SAP/jenkins-library/pkg/telemetry" | ||
) | ||
|
||
type vaultUrl struct { | ||
URL string `json:"url"` | ||
Username string `json:"username,omitempty"` | ||
Password string `json:"password,omitempty"` | ||
} | ||
|
||
func npmExecuteTests(config npmExecuteTestsOptions, _ *telemetry.CustomData) { | ||
c := command.Command{} | ||
|
||
c.Stdout(log.Writer()) | ||
c.Stderr(log.Writer()) | ||
err := runNpmExecuteTests(&config, &c) | ||
if err != nil { | ||
log.Entry().WithError(err).Fatal("Step execution failed") | ||
} | ||
} | ||
|
||
func runNpmExecuteTests(config *npmExecuteTestsOptions, c command.ExecRunner) error { | ||
if len(config.Envs) > 0 { | ||
c.SetEnv(config.Envs) | ||
} | ||
|
||
if len(config.Paths) > 0 { | ||
path := fmt.Sprintf("PATH=%s:%s", os.Getenv("PATH"), strings.Join(config.Paths, ":")) | ||
c.SetEnv([]string{path}) | ||
} | ||
|
||
if config.WorkingDirectory != "" { | ||
if err := os.Chdir(config.WorkingDirectory); err != nil { | ||
return fmt.Errorf("failed to change directory: %w", err) | ||
} | ||
} | ||
|
||
installCommandTokens := strings.Fields(config.InstallCommand) | ||
if err := c.RunExecutable(installCommandTokens[0], installCommandTokens[1:]...); err != nil { | ||
return fmt.Errorf("failed to execute install command: %w", err) | ||
} | ||
|
||
parsedURLs, err := parseURLs(config.VaultURLs) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for _, app := range parsedURLs { | ||
if err := runTestForUrl(app.URL, app.Username, app.Password, config, c); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
if err := runTestForUrl(config.BaseURL, config.VaultUsername, config.VaultPassword, config, c); err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func runTestForUrl(url, username, password string, config *npmExecuteTestsOptions, command command.ExecRunner) error { | ||
credentialsToEnv(username, password, config.UsernameEnvVar, config.PasswordEnvVar, command) | ||
// we need to reset the env vars as the next test might not have any credentials | ||
defer resetCredentials(config.UsernameEnvVar, config.PasswordEnvVar, command) | ||
|
||
runScriptTokens := strings.Fields(config.RunCommand) | ||
if config.UrlOptionPrefix != "" { | ||
runScriptTokens = append(runScriptTokens, config.UrlOptionPrefix+url) | ||
} | ||
if err := command.RunExecutable(runScriptTokens[0], runScriptTokens[1:]...); err != nil { | ||
return fmt.Errorf("failed to execute npm script: %w", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func parseURLs(urls []map[string]interface{}) ([]vaultUrl, error) { | ||
parsedUrls := []vaultUrl{} | ||
|
||
for _, url := range urls { | ||
parsedUrl := vaultUrl{} | ||
urlStr, ok := url["url"].(string) | ||
if !ok { | ||
return nil, fmt.Errorf("url field is not a string") | ||
} | ||
parsedUrl.URL = urlStr | ||
if username, ok := url["username"].(string); ok { | ||
parsedUrl.Username = username | ||
} | ||
|
||
if password, ok := url["password"].(string); ok { | ||
parsedUrl.Password = password | ||
} | ||
parsedUrls = append(parsedUrls, parsedUrl) | ||
} | ||
return parsedUrls, nil | ||
} | ||
|
||
func credentialsToEnv(username, password, usernameEnv, passwordEnv string, c command.ExecRunner) { | ||
if username == "" || password == "" { | ||
return | ||
} | ||
c.SetEnv([]string{usernameEnv + "=" + username, passwordEnv + "=" + password}) | ||
} | ||
|
||
func resetCredentials(usernameEnv, passwordEnv string, c command.ExecRunner) { | ||
c.SetEnv([]string{usernameEnv + "=", passwordEnv + "="}) | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks unrelated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is, but it's been missed in some old PR and it's a hard bug to catch, might add it as a new PR