Skip to content

Commit

Permalink
test: for substitude single quote
Browse files Browse the repository at this point in the history
  • Loading branch information
deryrahman committed Nov 1, 2023
1 parent 63f0de0 commit 9930ef7
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 17 deletions.
32 changes: 15 additions & 17 deletions client/cmd/job/run_input.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,15 +156,8 @@ func (j *jobRunInputCommand) writeInstanceResponse(jobResponse *pb.JobRunInputRe
func (j *jobRunInputCommand) writeJobResponseSecretToFile(
jobResponse *pb.JobRunInputResponse, dirPath string,
) error {
// write all secrets into a file
secretsFileContent := ""
for key, val := range jobResponse.Secrets {
if strings.Contains(val, unsubstitutedValue) {
j.keysWithUnsubstitutedValue = append(j.keysWithUnsubstitutedValue, key)
}
escapedVal := strings.ReplaceAll(val, "'", "'\\''")
secretsFileContent += fmt.Sprintf("%s='%s'\n", key, escapedVal)
}
secretsFileContent, keysWithUnsubstitutedValue := ConstructConfigEnvSourcingContent(jobResponse.Envs)
j.keysWithUnsubstitutedValue = append(j.keysWithUnsubstitutedValue, keysWithUnsubstitutedValue...)

filePath := filepath.Join(dirPath, typeSecretFileName)
writeToFileFn := utils.WriteStringToFileIndexed()
Expand All @@ -175,14 +168,8 @@ func (j *jobRunInputCommand) writeJobResponseSecretToFile(
}

func (j *jobRunInputCommand) writeJobResponseEnvToFile(jobResponse *pb.JobRunInputResponse, dirPath string) error {
envFileBlob := ""
for key, val := range jobResponse.Envs {
if strings.Contains(val, unsubstitutedValue) {
j.keysWithUnsubstitutedValue = append(j.keysWithUnsubstitutedValue, key)
}
escapedVal := strings.ReplaceAll(val, "'", "'\\''")
envFileBlob += fmt.Sprintf("%s='%s'\n", key, escapedVal)
}
envFileBlob, keysWithUnsubstitutedValue := ConstructConfigEnvSourcingContent(jobResponse.Envs)
j.keysWithUnsubstitutedValue = append(j.keysWithUnsubstitutedValue, keysWithUnsubstitutedValue...)

filePath := filepath.Join(dirPath, typeEnvFileName)
writeToFileFn := utils.WriteStringToFileIndexed()
Expand All @@ -192,6 +179,17 @@ func (j *jobRunInputCommand) writeJobResponseEnvToFile(jobResponse *pb.JobRunInp
return nil
}

func ConstructConfigEnvSourcingContent(config map[string]string) (content string, keysWithUnsubstitutedValue []string) {
for key, val := range config {
if strings.Contains(val, unsubstitutedValue) {
keysWithUnsubstitutedValue = append(keysWithUnsubstitutedValue, key)
}
escapedVal := strings.ReplaceAll(val, "'", "'\\''")
content += fmt.Sprintf("%s='%s'\n", key, escapedVal)
}
return content, keysWithUnsubstitutedValue
}

func (j *jobRunInputCommand) writeJobAssetsToFiles(
jobResponse *pb.JobRunInputResponse, dirPath string,
) error {
Expand Down
33 changes: 33 additions & 0 deletions client/cmd/job/run_input_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package job_test

import (
"testing"

"github.com/goto/optimus/client/cmd/job"

Check failure on line 6 in client/cmd/job/run_input_test.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gci`-ed with --skip-generated -s standard,default,prefix(github.com/goto/optimus) (gci)
"github.com/stretchr/testify/assert"

Check failure on line 7 in client/cmd/job/run_input_test.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gci`-ed with --skip-generated -s standard,default,prefix(github.com/goto/optimus) (gci)
)

func TestConstructConfigEnvSourcingContent(t *testing.T) {
t.Run("construct content with unsubstitude value", func(t *testing.T) {
config := map[string]string{
"EXAMPLE": "<no value>",
"ANOTHER": "hello",
}
content, keys := job.ConstructConfigEnvSourcingContent(config)
assert.Len(t, keys, 1)
assert.Equal(t, `EXAMPLE='<no value>'
ANOTHER='hello'
`, content)
})
t.Run("construct content with single quote in it", func(t *testing.T) {
config := map[string]string{
"EXAMPLE": "value with 'single quote'",
"ANOTHER": "hello",
}
content, keys := job.ConstructConfigEnvSourcingContent(config)
assert.Len(t, keys, 0)
assert.Equal(t, `EXAMPLE='value with '\''single quote'\'''
ANOTHER='hello'
`, content)
})
}

0 comments on commit 9930ef7

Please sign in to comment.