Skip to content

Commit

Permalink
Rewrite all tests with table driven approach
Browse files Browse the repository at this point in the history
test cases are now easily readable

Signed-off-by: Andrea Waltlova <[email protected]>
  • Loading branch information
andywaltlova committed Jul 31, 2023
1 parent 1050628 commit 9553f9c
Show file tree
Hide file tree
Showing 4 changed files with 489 additions and 311 deletions.
104 changes: 63 additions & 41 deletions src/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,50 +9,72 @@ import (
)

func TestSetupLogger(t *testing.T) {
// Create a temporary directory for the log folder
logFolderName := "log-test"
logFileName := "log-file"
defer os.RemoveAll(logFolderName)

// Test case 1: YGG_LOG_LEVEL doesn't exist, info level should be set to info
setupLogger(logFolderName, logFileName)
level := log.CurrentLevel()
if log.CurrentLevel() != log.LevelInfo {
t.Errorf("Incorrect log level. Expected: %v, Got: %v", log.LevelInfo, level)
}
// Test case 2: Unparsable level in env variable
os.Setenv("YGG_LOG_LEVEL", "....")
setupLogger(logFolderName, logFileName)
level = log.CurrentLevel()
if log.CurrentLevel() != log.LevelInfo {
t.Errorf("Incorrect log level. Expected: %v, Got: %v", log.LevelInfo, level)
testCases := []struct {
name string
logLevelEnv string
expectedLevel log.Level
}{
{
name: "YGG_LOG_LEVEL doesn't exist, info level should be set to info",
logLevelEnv: "",
expectedLevel: log.LevelInfo,
},
{
name: "Unparsable level in env variable",
logLevelEnv: "....",
expectedLevel: log.LevelInfo,
},
{
name: "Everything set up correctly",
logLevelEnv: "debug",
expectedLevel: log.LevelDebug,
},
}

// Test case 3: Everything set up correctly
os.Setenv("YGG_LOG_LEVEL", "debug")
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
// Create a temporary directory for the log folder
logFolderName := "log-test"
logFileName := "log-file"
defer os.RemoveAll(logFolderName)

logfile := setupLogger(logFolderName, logFileName)
if _, err := os.Stat(logFolderName); os.IsNotExist(err) {
t.Errorf("Log folder not created: %v", err)
}
logFilePath := filepath.Join(logFolderName, logFileName)
if _, err := os.Stat(logFilePath); os.IsNotExist(err) {
t.Errorf("Log file not created: %v", err)
}
level = log.CurrentLevel()
if level != log.LevelDebug {
t.Errorf("Incorrect log level. Expected: %v, Got: %v", log.LevelDebug, level)
}
flags := log.Flags()
expectedFlags := log.Lshortfile | log.LstdFlags
if flags != expectedFlags {
t.Errorf("Incorrect log flags. Expected: %v, Got: %v", expectedFlags, flags)
}
// Set YGG_LOG_LEVEL environment variable
if tc.logLevelEnv != "" {
os.Setenv("YGG_LOG_LEVEL", tc.logLevelEnv)
}

logfile := setupLogger(logFolderName, logFileName)

// Verify log level
level := log.CurrentLevel()
if level != tc.expectedLevel {
t.Errorf("Incorrect log level. Expected: %v, Got: %v", tc.expectedLevel, level)
}

// Verify log file and folder
if _, err := os.Stat(logFolderName); os.IsNotExist(err) {
t.Errorf("Log folder not created: %v", err)
}
logFilePath := filepath.Join(logFolderName, logFileName)
if _, err := os.Stat(logFilePath); os.IsNotExist(err) {
t.Errorf("Log file not created: %v", err)
}

// Verify log flags
flags := log.Flags()
expectedFlags := log.Lshortfile | log.LstdFlags
if flags != expectedFlags {
t.Errorf("Incorrect log flags. Expected: %v, Got: %v", expectedFlags, flags)
}

// Cleanup - close the log file
defer os.Unsetenv("YGG_LOG_LEVEL")
err := logfile.Close()
if err != nil {
t.Errorf("Failed to close log file: %v", err)
// Cleanup - close the log file and unset the YGG_LOG_LEVEL environment variable
defer func() {
os.Unsetenv("YGG_LOG_LEVEL")
err := logfile.Close()
if err != nil {
t.Errorf("Failed to close log file: %v", err)
}
}()
})
}
}
155 changes: 93 additions & 62 deletions src/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,22 @@ import (
)

func TestProcessSignedScript(t *testing.T) {
shouldVerifyYaml := false
shouldDoInsightsCoreGPGCheck := false
temporaryWorkerDirectory := "test-dir"
config = &Config{
VerifyYAML: &shouldVerifyYaml,
TemporaryWorkerDirectory: &temporaryWorkerDirectory,
InsightsCoreGPGCheck: &shouldDoInsightsCoreGPGCheck,
}

defer os.RemoveAll(temporaryWorkerDirectory)

// Test case 1: verification disabled, no yaml data supplied = empty output
yamlData := []byte{}
expectedResult := ""
result := processSignedScript(yamlData)
if result != expectedResult {
t.Errorf("Expected %q, but got %q", expectedResult, result)
}

// Test case 2: verification disabled, yaml data supplied = non-empty output
yamlData = []byte(`
testCases := []struct {
name string
verifyYAML bool
yamlData []byte
expectedResult string
}{
{
name: "verification disabled, no yaml data supplied = empty output",
verifyYAML: false,
yamlData: []byte{},
expectedResult: "",
},
{
name: "verification disabled, yaml data supplied = non-empty output",
verifyYAML: false,
yamlData: []byte(`
vars:
_insights_signature: "invalid-signature"
_insights_signature_exclude: "/vars/insights_signature,/vars/content_vars"
Expand All @@ -35,55 +30,91 @@ vars:
echo "$RHC_WORKER_FOO $RHC_WORKER_BAR!"
content_vars:
FOO: Hello
BAR: World`)
expectedResult = "Hello World!\n"
result = processSignedScript(yamlData)
if result != expectedResult {
t.Errorf("Expected %q, but got %q", expectedResult, result)
BAR: World`),
expectedResult: "Hello World!\n",
},
{
name: "verification enabled, invalid signature = error msg returned",
verifyYAML: true,
yamlData: []byte(`
vars:
_insights_signature: "invalid-signature"
_insights_signature_exclude: "/vars/insights_signature,/vars/content_vars"
content: |
#!/bin/sh
echo "$RHC_WORKER_FOO $RHC_WORKER_BAR!"
content_vars:
FOO: Hello
BAR: World`),
expectedResult: "Signature of yaml file is invalid",
},
}

// FIXME: This is false success because verification fails on missing insighs-client
// Test case 3: verification enabled, invalid signature = error msg returned
shouldVerifyYaml = true
shouldDoInsightsCoreGPGCheck = true
expectedResult = "Signature of yaml file is invalid"
result = processSignedScript(yamlData)
if result != expectedResult {
t.Errorf("Expected %q, but got %q", expectedResult, result)
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
shouldVerifyYaml := tc.verifyYAML
shouldDoInsightsCoreGPGCheck := tc.verifyYAML // Assume the same value for simplicity
temporaryWorkerDirectory := "test-dir"
config = &Config{
VerifyYAML: &shouldVerifyYaml,
TemporaryWorkerDirectory: &temporaryWorkerDirectory,
InsightsCoreGPGCheck: &shouldDoInsightsCoreGPGCheck,
}

defer os.RemoveAll(temporaryWorkerDirectory)

result := processSignedScript(tc.yamlData)
if result != tc.expectedResult {
t.Errorf("Expected %q, but got %q", tc.expectedResult, result)
}
})
}
}

func TestVerifyYamlFile(t *testing.T) {
shouldVerifyYaml := false
shouldDoInsightsCoreGPGCheck := false

config = &Config{
VerifyYAML: &shouldVerifyYaml,
InsightsCoreGPGCheck: &shouldDoInsightsCoreGPGCheck,
}
// Test case 1: verification disabled
expectedResult := true
result := verifyYamlFile([]byte{})
if result != expectedResult {
t.Errorf("Expected %v, but got %v", expectedResult, result)
testCases := []struct {
name string
verifyYAML bool
yamlData []byte
expectedResult bool
}{
{
name: "verification disabled",
verifyYAML: false,
yamlData: []byte{},
expectedResult: true,
},
// FIXME: This should succedd but now verification fails on missing insighs-client
// We also need valid signature
{
name: "verification enabled and verification succeeds",
verifyYAML: true,
yamlData: []byte("valid-yaml"),
expectedResult: false,
},
// FIXME: Valid test case but fails because of missing insights-client
{
name: "verification is enabled and verification fails",
verifyYAML: true,
yamlData: []byte("invalid-yaml"),
expectedResult: false,
},
}

// Test case 2: verification enabled and verification succeeds
shouldVerifyYaml = true
// FIXME: This should succedd but now verification fails on missing insighs-client
// We also need valid signature
expectedResult = false
result = verifyYamlFile([]byte("valid-yaml"))
if result != expectedResult {
t.Errorf("Expected %v, but got %v", expectedResult, result)
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
shouldVerifyYaml := tc.verifyYAML
shouldDoInsightsCoreGPGCheck := false // Reset to false for each test case

config = &Config{
VerifyYAML: &shouldVerifyYaml,
InsightsCoreGPGCheck: &shouldDoInsightsCoreGPGCheck,
}

// FIXME: Valid test case but fails because of missing insights-client
// Test case 3: sverification is enabled and verification fails
// shouldVerifyYaml = true
expectedResult = false
result = verifyYamlFile([]byte("invalid-yaml")) // Replace with your YAML data
if result != expectedResult {
t.Errorf("Expected %v, but got %v", expectedResult, result)
result := verifyYamlFile(tc.yamlData)
if result != tc.expectedResult {
t.Errorf("Expected %v, but got %v", tc.expectedResult, result)
}
})
}
}
Loading

0 comments on commit 9553f9c

Please sign in to comment.