Skip to content
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

Open
wants to merge 42 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
da9d73b
feat: Introduce new npmExecuteTests step
Oct 1, 2024
20e00eb
fix: comma
Oct 1, 2024
f24b876
fix: parens
Oct 1, 2024
7cadbf8
fix: error handling
Oct 1, 2024
221840f
fix: paren
Oct 1, 2024
17f0810
fix: config keys
Oct 1, 2024
0dc2cee
fix: rename key
Oct 1, 2024
49d42fb
Merge branch 'master' into phgermanov/npm-execute-tests-go
phgermanov Oct 2, 2024
47ca127
test: failng tests
Oct 2, 2024
187d850
fix: handle empty openFile call
Oct 4, 2024
aeef66b
refactor: clean install rather than install
Oct 4, 2024
56dfffe
refactor: full run command in config
Oct 4, 2024
65f7cba
build: use lts-bookworm
Oct 4, 2024
20f05d8
Merge branch 'master' into phgermanov/npm-execute-tests-go
phgermanov Oct 4, 2024
045b530
feat: get credentials from vault
Oct 8, 2024
6bf1d06
feat: remove wdi5 install script
Oct 8, 2024
64f063f
fix: groovy tests
Oct 8, 2024
f67b7a8
fix: report type
phgermanov Oct 8, 2024
8e4ba08
docs: remove defaults from desc
Oct 8, 2024
1a3e5ea
feat: remove branch params
Oct 8, 2024
c22d874
refactor: rename appSecrets to vaultMetadata
Oct 8, 2024
8c2456d
feat: param for custom env var prefix
Oct 8, 2024
7c14e86
refactor: change app urls map to a list
Oct 8, 2024
d8db284
refactor: rename appURLs for clarity
Oct 8, 2024
44f8cd9
refactor: user can specify url option param
Oct 8, 2024
ec8369a
docs: go generate
Oct 8, 2024
5768f0f
feat: add support for user defined env vars and paths
Oct 10, 2024
84cf942
refactor: rename runScript to runCommand
Oct 10, 2024
d4cf486
refactor: expose username and password env vars
Oct 10, 2024
cf6af6f
refactor: rename envVars to envs
Oct 10, 2024
5dc002b
docs: add documentation for npmExecuteTests step
Oct 10, 2024
17175ff
feat: better handle vault secrets
Oct 10, 2024
3d9c038
refactor: flatten vault secrets
Oct 11, 2024
0f8f9c3
docs: add npmExecuteTests docs to mkdocs
Oct 11, 2024
1a682fc
feat: add npmExecuteTests to piper cmd
Oct 11, 2024
db6c087
Merge branch 'master' into phgermanov/npm-execute-tests-go
phgermanov Oct 11, 2024
23ac975
chore: go generate
Oct 14, 2024
e1a3653
fix: modify working dir
Oct 15, 2024
970f340
fix: change npm cache dir
Oct 15, 2024
803f18d
fix: use os home dir
Oct 15, 2024
bc61bf3
fix: remove working dir
Oct 15, 2024
103f0e5
fix: remove npm set cache
Oct 17, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmd/metadata_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

100 changes: 100 additions & 0 deletions cmd/npmExecuteTests.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package cmd

import (
"encoding/json"
"fmt"
"os"
"strings"

"github.com/SAP/jenkins-library/pkg/command"
"github.com/SAP/jenkins-library/pkg/log"
"github.com/SAP/jenkins-library/pkg/orchestrator"
"github.com/SAP/jenkins-library/pkg/telemetry"
)

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 {
type AppURL struct {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
type AppURL struct {
type AppUnderTest struct {

URL string `json:"url"`
Username string `json:"username"`
Password string `json:"password"`
}
var appURLs []AppURL
err := json.Unmarshal([]byte(config.AppURLs), &appURLs)
if err != nil {
return fmt.Errorf("failed to parse app URLs: %w", err)
}

provider, err := orchestrator.GetOrchestratorConfigProvider(nil)
if err != nil {
return fmt.Errorf("failed to get orchestrator config provider: %w", err)
}

env := provider.Branch()
if config.OnlyRunInProductiveBranch && config.ProductiveBranch != env {
log.Entry().Info("Skipping execution because it is configured to run only in the productive branch.")
return nil
}

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)
}

for _, appUrl := range appURLs {
credentialsToEnv(appUrl.Username, appUrl.Password, config.Wdi5)
err := runTestForUrl(appUrl.URL, config, c)
if err != nil {
return err
}
}

if err := runTestForUrl(config.BaseURL, config, c); err != nil {
return err
}
return nil
}

func runTestForUrl(url string, config *npmExecuteTestsOptions, command command.ExecRunner) error {
log.Entry().Infof("Running end to end tests for URL: %s", url)

if config.Wdi5 {
// install wdi5 and all required WebdriverIO peer dependencies
// add a config file (wdio.conf.js) to your current working directory, using http://localhost:8080/index.html as baseUrl,
// looking for tests in $ui5-app/webapp/test/**/* that follow the name pattern *.test.js
// set an npm script named “wdi5” to run wdi5 so you can immediately do npm run wdi5
if err := command.RunExecutable("npm", "init", "wdi5@latest", "--baseUrl", url); err != nil {
return fmt.Errorf("failed to install wdi5: %w", err)
}
if err := command.RunExecutable("npm", "run", "wdi5"); err != nil {
return fmt.Errorf("failed to execute wdi5: %w", err)
}
return nil
}

// Execute the npm script
options := "--baseUrl_" + url
if err := command.RunExecutable("npm", "run", config.RunScript, options); err != nil {
return fmt.Errorf("failed to execute npm script: %w", err)
}
return nil
}

func credentialsToEnv(username, password string, wdi5 bool) {
prefix := "e2e"
if wdi5 {
prefix = "wdi5"
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about leaving this to the user by exposing a credentialEnvVarPrefix parameter?

os.Setenv(prefix+"_username", username)
os.Setenv(prefix+"_password", password)
}
Loading
Loading