Skip to content

Commit

Permalink
Read log level from config file
Browse files Browse the repository at this point in the history
Try to read the script log level from config file, if that fails, use
yggdrasil default log level, and if that's not set, use "INFO" as the
default one.
  • Loading branch information
r0x0d committed Apr 3, 2024
1 parent 7b7d840 commit 928ed10
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 5 deletions.
6 changes: 3 additions & 3 deletions src/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ func verifyYamlFile(yamlData []byte) bool {
return true
}

func setEnvVariablesFromYaml(cmd *exec.Cmd, variables map[string]string) {
func setEnvVariablesForCommand(cmd *exec.Cmd, variables map[string]string) {
cmd.Env = os.Environ()
getEnvVarName := func(key string) string {
return fmt.Sprintf("RHC_WORKER_%s", strings.ToUpper(key))
}
Expand Down Expand Up @@ -128,8 +129,7 @@ func processSignedScript(incomingContent []byte) string {

variables := yamlContent.Vars.ContentVars
variables["LOG_LEVEL"] = *config.ScriptLogLevel
setEnvVariablesFromYaml(cmd, variables)
log.Infoln("Script is being executed with following env variables: ", cmd.Env)
setEnvVariablesForCommand(cmd, variables)

out, err := cmd.CombinedOutput()
if err != nil {
Expand Down
6 changes: 4 additions & 2 deletions src/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,13 @@ func TestProcessSignedScript(t *testing.T) {

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
scriptLogLevel := "info"
shouldVerifyYaml := tc.verifyYAML
temporaryWorkerDirectory := t.TempDir()
config = &Config{
VerifyYAML: &shouldVerifyYaml,
TemporaryWorkerDirectory: &temporaryWorkerDirectory,
ScriptLogLevel: &scriptLogLevel,
}

defer os.RemoveAll(temporaryWorkerDirectory)
Expand Down Expand Up @@ -161,8 +163,8 @@ func TestSetEnvVariablesForCommand(t *testing.T) {
anotherCmd := exec.Command("echo", "Bye, World!")

// Call the functions to set env variables for the commands
setEnvVariablesFromYaml(cmd, tc.variables)
setEnvVariablesFromYaml(anotherCmd, tc.anotherCmdVariables)
setEnvVariablesForCommand(cmd, tc.variables)
setEnvVariablesForCommand(anotherCmd, tc.anotherCmdVariables)

// Check if the global environment variables are unchanged
if !areStringSlicesSubset(originalEnv, os.Environ()) {
Expand Down
2 changes: 2 additions & 0 deletions src/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,13 @@ func TestProcessData(t *testing.T) {

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
scriptLogLevel := "info"
shouldVerifyYaml := false
temporaryWorkerDirectory := t.TempDir()
config = &Config{
VerifyYAML: &shouldVerifyYaml,
TemporaryWorkerDirectory: &temporaryWorkerDirectory,
ScriptLogLevel: &scriptLogLevel,
}

returnURL := "bar"
Expand Down

0 comments on commit 928ed10

Please sign in to comment.