Skip to content

Commit

Permalink
Merge pull request #222 from galasa-dev/mcobbett-image-render-bug-fix
Browse files Browse the repository at this point in the history
Running local test creates excessive image rendering.
  • Loading branch information
techcobweb authored Apr 2, 2024
2 parents e1b3d5b + b508ba3 commit 4c7e5f1
Show file tree
Hide file tree
Showing 35 changed files with 546 additions and 458 deletions.
68 changes: 26 additions & 42 deletions build-locally.sh
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,7 @@ function launch_test_on_ecosystem {
cmd="${BASEDIR}/bin/${galasactl_command} runs submit \
--bootstrap $GALASA_BOOTSTRAP \
--class dev.galasa.example.banking.account/dev.galasa.example.banking.account.TestAccountExtended \
--class dev.galasa.example.banking.account/dev.galasa.example.banking.account.TestAccount \
--log -"

info "Command is: $cmd"
Expand Down Expand Up @@ -483,19 +484,33 @@ function generate_galasactl_documentation {


#--------------------------------------------------------------------------
# Run test using the galasactl locally in a JVM
function submit_local_test {
function check_artifact_saved_in_ras {
# The extended type of test saves an artifact into the RAS store
# The artifact has "Hello Galasa !" inside.
# Lets check the RAS to make sure that file is present.
expected_string_in_test_artifact="Hello Galasa \!"
grep -R "$expected_string_in_test_artifact" $GALASA_HOME/ras > /dev/null
rc=$?
if [[ "${rc}" != "0" ]]; then
error "Failed to find the string \'$expected_string_in_test_artifact\" in RAS. Test case should have generated it."
exit 1
fi
success "Confirmed that test case saved a test artifact to RAS"
}

function run_test_locally_using_galasactl {
export LOG_FILE=$1

h2 "Submitting a local test using galasactl in a local JVM"
h2 "Submitting 2 local tests using galasactl in a local JVM"

cd ${BASEDIR}/temp/*banking

BUNDLE=$1
JAVA_CLASS=$2
OBR_GROUP_ID=$3
OBR_ARTIFACT_ID=$4
OBR_VERSION=$5
LOG_FILE=$6
BUNDLE=dev.galasa.example.banking.payee
JAVA_CLASS=dev.galasa.example.banking.payee.TestPayeeExtended
JAVA_CLASS_2=dev.galasa.example.banking.payee.TestPayee
OBR_GROUP_ID=dev.galasa.example.banking
OBR_ARTIFACT_ID=dev.galasa.example.banking.obr
OBR_VERSION=0.0.1-SNAPSHOT

# Could get this bootjar from https://development.galasa.dev/main/maven-repo/obr/dev/galasa/galasa-boot/
read_boot_jar_version
Expand All @@ -516,6 +531,7 @@ function submit_local_test {
cmd="${BASEDIR}/bin/${galasactl_command} runs submit local \
--obr mvn:${OBR_GROUP_ID}/${OBR_ARTIFACT_ID}/${OBR_VERSION}/obr \
--class ${BUNDLE}/${JAVA_CLASS} \
--class ${BUNDLE}/${JAVA_CLASS_2} \
--remoteMaven ${REMOTE_MAVEN} \
--throttle 1 \
--requesttype MikeCLI \
Expand All @@ -532,40 +548,8 @@ function submit_local_test {

info "Command is ${cmd}"
$cmd
rc=$?
if [[ "${rc}" != "0" ]]; then
error "Failed to run the test. See details in log file ${LOG_FILE}"
exit 1
fi
rc=$? ; if [[ "${rc}" != "0" ]]; then error "Failed to run the test. See details in log file ${LOG_FILE}" ; exit 1 ; fi
success "Test ran OK"
}

function check_artifact_saved_in_ras {
# The extended type of test saves an artifact into the RAS store
# The artifact has "Hello Galasa !" inside.
# Lets check the RAS to make sure that file is present.
expected_string_in_test_artifact="Hello Galasa \!"
grep -R "$expected_string_in_test_artifact" $GALASA_HOME/ras > /dev/null
rc=$?
if [[ "${rc}" != "0" ]]; then
error "Failed to find the string \'$expected_string_in_test_artifact\" in RAS. Test case should have generated it."
exit 1
fi
success "Confirmed that test case saved a test artifact to RAS"
}

function run_test_locally_using_galasactl {
export LOG_FILE=$1

# Run the Payee tests.
export TEST_BUNDLE=dev.galasa.example.banking.payee
export TEST_JAVA_CLASS=dev.galasa.example.banking.payee.TestPayeeExtended
export TEST_OBR_GROUP_ID=dev.galasa.example.banking
export TEST_OBR_ARTIFACT_ID=dev.galasa.example.banking.obr
export TEST_OBR_VERSION=0.0.1-SNAPSHOT


submit_local_test $TEST_BUNDLE $TEST_JAVA_CLASS $TEST_OBR_GROUP_ID $TEST_OBR_ARTIFACT_ID $TEST_OBR_VERSION $LOG_FILE

check_artifact_saved_in_ras
}
Expand Down
3 changes: 1 addition & 2 deletions docs/generated/errors-list.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ The `galasactl` tool can generate the following errors:
- GAL1140E: The namespace, '{}', provided does not match formatting requirements. The namespace must start with a character in the 'a-z' range, followed by characters in the 'a'-'z' or '0'-'9' ranges only.
- GAL1141E: Unable to compile the regex pattern for Galasa Property field '{}'. Reason: '{}'
- GAL1142E: The {} field value, '{}', provided does not match formatting requirements. The {} field value must start with a character in the 'a-z' or 'A-Z' range, followed by any characters in the 'a'-'z', 'A'-'Z', '0'-'9', '.' (period), '-' (dash) or '_' (underscore) ranges only.
- GAL1143E: Could not query run results. Server returned a non-200 code ({})
- GAL1225E: Failed to open file '{}' cause: {}. Check that this file exists, and that you have read permissions.
- GAL1226E: Internal failure. Contents of gzip could be read, but not decoded. New gzip reader failed: file: {} error: {}
- GAL1227E: Internal failure. Contents of gzip could not be decoded. {} error: {}
Expand All @@ -145,8 +146,6 @@ The `galasactl` tool can generate the following errors:
- GAL2000W: Warning: Maven configuration file settings.xml should contain a reference to a Galasa repository so that the galasa OBR can be resolved. The official release repository is '{}', and 'pre-release' repository is '{}'
- GAL2501I: Downloaded {} artifacts to folder '{}'

- GAL2502I: Rendered {} image files.

- GAL2503I: The request to reset run '{}' has been accepted by the server.

- GAL2504I: The request to cancel run '{}' has been accepted by the server.
Expand Down
2 changes: 1 addition & 1 deletion docs/generated/galasactl_runs_prepare.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ galasactl runs prepare [flags]
```
--append Append tests to existing portfolio
--bundle strings bundles of which tests will be selected from, bundles are selected if the name contains this string, or if --regex is specified then matches the regex
--class strings test class names to run from the specified stream or portfolio. The format of each entry is osgi-bundle-name/java-class-name . Java class names are fully qualified. No .class suffix is needed.
--class strings test class names to run from the specified stream or portfolio. The format of each entry is {osgi-bundle-name}/{java-class-name}. Multiple values can be supplied using a comma-separated list of values, or by using multiple instances of the --class flag. Java class names are fully qualified. No .class suffix is needed.
--gherkin strings Gherkin feature file URL. Should start with 'file://'.
-h, --help Displays the options for the 'runs prepare' command.
--override strings overrides to be sent with the tests (overrides in the portfolio will take precedence)
Expand Down
2 changes: 1 addition & 1 deletion docs/generated/galasactl_runs_submit.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ galasactl runs submit [flags]

```
--bundle strings bundles of which tests will be selected from, bundles are selected if the name contains this string, or if --regex is specified then matches the regex
--class strings test class names to run from the specified stream or portfolio. The format of each entry is osgi-bundle-name/java-class-name . Java class names are fully qualified. No .class suffix is needed.
--class strings test class names to run from the specified stream or portfolio. The format of each entry is {osgi-bundle-name}/{java-class-name}. Multiple values can be supplied using a comma-separated list of values, or by using multiple instances of the --class flag. Java class names are fully qualified. No .class suffix is needed.
--gherkin strings Gherkin feature file URL. Should start with 'file://'.
-g, --group string the group name to assign the test runs to, if not provided, a psuedo unique id will be generated
-h, --help Displays the options for the 'runs submit' command.
Expand Down
6 changes: 3 additions & 3 deletions pkg/auth/authProperties.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
)

const (
TOKEN_PROPERTY = "GALASA_TOKEN"
TOKEN_PROPERTY = "GALASA_TOKEN"
TOKEN_SEPARATOR = ":"
)

Expand All @@ -32,7 +32,7 @@ func GetAuthProperties(fileSystem files.FileSystem, galasaHome utils.GalasaHome,
galasactlPropertiesFilePath := filepath.Join(galasaHome.GetNativeFolderPath(), "galasactl.properties")

// Get the file-based token property if we can
tokenProperty, fileAccessErr := getPropertyFromFile(fileSystem, galasactlPropertiesFilePath, env, TOKEN_PROPERTY)
tokenProperty, fileAccessErr := getPropertyFromFile(fileSystem, galasactlPropertiesFilePath, TOKEN_PROPERTY)

// Over-write the token property value if there is an environment variable set to do that.
tokenProperty = getPropertyWithOverride(env, tokenProperty, galasactlPropertiesFilePath, TOKEN_PROPERTY)
Expand Down Expand Up @@ -85,7 +85,7 @@ func getPropertyWithOverride(env utils.Environment, valueFromFile string, filePa
}

// Gets a property from the user's galasactl.properties file
func getPropertyFromFile(fileSystem files.FileSystem, galasactlPropertiesFilePath string, env utils.Environment, propertyName string) (string, error) {
func getPropertyFromFile(fileSystem files.FileSystem, galasactlPropertiesFilePath string, propertyName string) (string, error) {
var err error = nil
var galasactlProperties props.JavaProperties
galasactlProperties, err = props.ReadPropertiesFile(fileSystem, galasactlPropertiesFilePath)
Expand Down
117 changes: 59 additions & 58 deletions pkg/auth/bearerTokenFile.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,84 +19,85 @@ import (
)

type BearerTokenJson struct {
Jwt string `json:"jwt"`
Jwt string `json:"jwt"`
}

const (
TOKEN_EXPIRY_BUFFER_MINUTES = 10
TOKEN_EXPIRY_BUFFER_MINUTES = 10
)

// Writes a new bearer-token.json file containing a JWT in the following format:
// {
// "jwt": "<bearer-token-here>"
// }
//
// {
// "jwt": "<bearer-token-here>"
// }
func WriteBearerTokenJsonFile(fileSystem files.FileSystem, galasaHome utils.GalasaHome, jwt string) error {
bearerTokenFilePath := filepath.Join(galasaHome.GetNativeFolderPath(), "bearer-token.json")
bearerTokenFilePath := filepath.Join(galasaHome.GetNativeFolderPath(), "bearer-token.json")

log.Printf("Writing bearer token to file '%s'", bearerTokenFilePath)
err := fileSystem.WriteTextFile(bearerTokenFilePath, jwt)
log.Printf("Writing bearer token to file '%s'", bearerTokenFilePath)
err := fileSystem.WriteTextFile(bearerTokenFilePath, jwt)

if err == nil {
log.Printf("Written bearer token to file '%s' OK", bearerTokenFilePath)
} else {
log.Printf("Failed to write bearer token file '%s'", bearerTokenFilePath)
err = galasaErrors.NewGalasaError(galasaErrors.GALASA_ERROR_FAILED_TO_WRITE_FILE, bearerTokenFilePath, err.Error())
}
return err
if err == nil {
log.Printf("Written bearer token to file '%s' OK", bearerTokenFilePath)
} else {
log.Printf("Failed to write bearer token file '%s'", bearerTokenFilePath)
err = galasaErrors.NewGalasaError(galasaErrors.GALASA_ERROR_FAILED_TO_WRITE_FILE, bearerTokenFilePath, err.Error())
}
return err
}

// Gets the JWT from the bearer-token.json file if it exists, errors if the file does not exist or if the token is invalid
func GetBearerTokenFromTokenJsonFile(fileSystem files.FileSystem, galasaHome utils.GalasaHome, timeService utils.TimeService) (string, error) {
var err error = nil
var bearerToken string = ""
var bearerTokenJsonContents string = ""
var err error = nil
var bearerToken string = ""
var bearerTokenJsonContents string = ""

bearerTokenFilePath := filepath.Join(galasaHome.GetNativeFolderPath(), "bearer-token.json")
bearerTokenFilePath := filepath.Join(galasaHome.GetNativeFolderPath(), "bearer-token.json")

log.Printf("Retrieving bearer token from file '%s'", bearerTokenFilePath)
bearerTokenJsonContents, err = fileSystem.ReadTextFile(bearerTokenFilePath)
if err == nil {
var bearerTokenJson BearerTokenJson
err = json.Unmarshal([]byte(bearerTokenJsonContents), &bearerTokenJson)
if err == nil {
bearerToken = bearerTokenJson.Jwt
log.Printf("Retrieved bearer token from file '%s' OK", bearerTokenFilePath)
}
}
log.Printf("Retrieving bearer token from file '%s'", bearerTokenFilePath)
bearerTokenJsonContents, err = fileSystem.ReadTextFile(bearerTokenFilePath)
if err == nil {
var bearerTokenJson BearerTokenJson
err = json.Unmarshal([]byte(bearerTokenJsonContents), &bearerTokenJson)
if err == nil {
bearerToken = bearerTokenJson.Jwt
log.Printf("Retrieved bearer token from file '%s' OK", bearerTokenFilePath)
}
}

if err != nil {
log.Printf("Could not retrieve bearer token from file '%s'", bearerTokenFilePath)
err = galasaErrors.NewGalasaError(galasaErrors.GALASA_ERROR_RETRIEVING_BEARER_TOKEN_FROM_FILE, bearerTokenFilePath, err.Error())
} else {
log.Printf("Validating bearer token retrieved from file '%s'", bearerTokenFilePath)
if (!IsBearerTokenValid(bearerToken, timeService)) {
err = galasaErrors.NewGalasaError(galasaErrors.GALASA_ERROR_INVALID_BEARER_TOKEN)
} else {
log.Printf("Validated bearer token retrieved from file '%s' OK", bearerTokenFilePath)
}
}
if err != nil {
log.Printf("Could not retrieve bearer token from file '%s'", bearerTokenFilePath)
err = galasaErrors.NewGalasaError(galasaErrors.GALASA_ERROR_RETRIEVING_BEARER_TOKEN_FROM_FILE, bearerTokenFilePath, err.Error())
} else {
log.Printf("Validating bearer token retrieved from file '%s'", bearerTokenFilePath)
if !IsBearerTokenValid(bearerToken, timeService) {
err = galasaErrors.NewGalasaError(galasaErrors.GALASA_ERROR_INVALID_BEARER_TOKEN)
} else {
log.Printf("Validated bearer token retrieved from file '%s' OK", bearerTokenFilePath)
}
}

return bearerToken, err
return bearerToken, err
}

// Checks whether a given bearer token is valid or not, returning true if it is valid and false otherwise
func IsBearerTokenValid(bearerTokenString string, timeService utils.TimeService) bool {
var err error = nil
var bearerToken *jwt.Token
var err error = nil
var bearerToken *jwt.Token

// Decode the bearer token without verifying its signature
bearerToken, _, err = jwt.NewParser().ParseUnverified(bearerTokenString, jwt.MapClaims{})
if err == nil {
var tokenExpiry *jwt.NumericDate
tokenExpiry, err = bearerToken.Claims.GetExpirationTime()
if err == nil {
// Add a buffer to the current time to make sure the bearer token does not expire within
// this buffer (e.g. if the buffer is 10 mins, make sure the token doesn't expire within 10 mins)
acceptableExpiryTime := timeService.Now().Add(time.Duration(TOKEN_EXPIRY_BUFFER_MINUTES) * time.Minute)
if ((tokenExpiry.Time).After(acceptableExpiryTime)) {
return true
}
}
}
return false
// Decode the bearer token without verifying its signature
bearerToken, _, err = jwt.NewParser().ParseUnverified(bearerTokenString, jwt.MapClaims{})
if err == nil {
var tokenExpiry *jwt.NumericDate
tokenExpiry, err = bearerToken.Claims.GetExpirationTime()
if err == nil {
// Add a buffer to the current time to make sure the bearer token does not expire within
// this buffer (e.g. if the buffer is 10 mins, make sure the token doesn't expire within 10 mins)
acceptableExpiryTime := timeService.Now().Add(time.Duration(TOKEN_EXPIRY_BUFFER_MINUTES) * time.Minute)
if (tokenExpiry.Time).After(acceptableExpiryTime) {
return true
}
}
}
return false
}
Loading

0 comments on commit 4c7e5f1

Please sign in to comment.