Skip to content

Commit

Permalink
Extracting test common functions, coderefactor
Browse files Browse the repository at this point in the history
  • Loading branch information
lovestaco committed Aug 16, 2023
1 parent 5a0227c commit 2b6e35e
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 110 deletions.
40 changes: 2 additions & 38 deletions tests/basic_commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,52 +2,16 @@
package tests

import (
"bytes"
"os/exec"
"strings"
"testing"

"github.com/rs/zerolog/log"
testutils "github.com/HexmosTech/lama2/tests/utils"
)

func runL2CommandAndGetOutput(t *testing.T, cmdArgs ...string) string {

// Get the full path to the l2 binary
l2BinPath := "../build/l2"

// Check if the l2 binary file exists
if err := checkL2BinaryExists(l2BinPath); err != nil {
t.Error(err)
return ""
}

// Code to run the l2 command
cmd := exec.Command(l2BinPath, cmdArgs...)

var stdout bytes.Buffer
cmd.Stdout = &stdout

var stderr bytes.Buffer
cmd.Stderr = &stderr

// Execute the command
err := cmd.Run()
if err != nil {
log.Error().Str("Error", stderr.String()).Msg("Error running l2 command")
t.Errorf("Error running l2 command: %v\n", err)
return ""
}

// Retrieve the captured stdout
stdoutOutput := stdout.String()
log.Info().Str("Test env_command", stdoutOutput).Msg("output from command")
return stdoutOutput
}

func TestNormalExecution(t *testing.T) {
fpath := "../elfparser/ElfTestSuite/y_0000_basic_get.l2"
cmdArgs := []string{fpath}
output := runL2CommandAndGetOutput(t, cmdArgs...)
output := testutils.RunL2CommandAndGetOutput(t, cmdArgs...)

expectedOutputPart := "\"url\": \"http://httpbin.org/get\""
if !strings.Contains(output, expectedOutputPart) {
Expand Down
2 changes: 1 addition & 1 deletion tests/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func TestCmdBasic(t *testing.T) {
expected := lama2cmd.Opts{
Verbose: []bool{true},
Nocolor: false,
Env: "UNSET",
Env: "UNSET_VU5TRVQ",
Positional: struct {
LamaAPIFile string
}{LamaAPIFile: "../elfparser/ElfTestSuite/y_0000_basic_get.l2"},
Expand Down
82 changes: 11 additions & 71 deletions tests/env_command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,15 @@
package tests

import (
"bytes"
"encoding/json"
"fmt"
"os"
"os/exec"
"testing"

"github.com/rs/zerolog/log"
testutils "github.com/HexmosTech/lama2/tests/utils"
)

type EnvData struct {
Src string `json:"src"`
Val string `json:"val"`
}

func TestL2EnvCommand(t *testing.T) {
fpath := "../elfparser/ElfTestSuite/root_variable_override/api/y_0020_root_override.l2"
cmdArgs := []string{"-e=", fpath}
envMap := runL2CommandAndParseJSON(t, cmdArgs...)
envMap := testutils.RunL2CommandAndParseJSON(t, cmdArgs...)

// Check the "AHOST" key is present
checkAHost(t, envMap)
Expand All @@ -32,7 +22,7 @@ func TestL2EnvCommand(t *testing.T) {
func TestL2RelevantEnvForAString(t *testing.T) {
fpath := "../elfparser/ElfTestSuite/root_variable_override/api/y_0020_root_override.l2"
cmdArgs := []string{"-e=A", fpath}
envMap := runL2CommandAndParseJSON(t, cmdArgs...)
envMap := testutils.RunL2CommandAndParseJSON(t, cmdArgs...)

// Check the "AHOST" key is present
checkAHost(t, envMap)
Expand All @@ -44,7 +34,7 @@ func TestL2RelevantEnvForAString(t *testing.T) {
func TestL2RelevantEnvForBString(t *testing.T) {
fpath := "../elfparser/ElfTestSuite/root_variable_override/api/y_0020_root_override.l2"
cmdArgs := []string{"-e=B", fpath}
envMap := runL2CommandAndParseJSON(t, cmdArgs...)
envMap := testutils.RunL2CommandAndParseJSON(t, cmdArgs...)

// Check the "BHOST" key is present
checkBHost(t, envMap)
Expand All @@ -56,7 +46,7 @@ func TestL2RelevantEnvForBString(t *testing.T) {
func TestL2EnvCommandVerbose(t *testing.T) {
fpath := "../elfparser/ElfTestSuite/root_variable_override/api/y_0020_root_override.l2"
cmdArgs := []string{"-e=", "-v", fpath}
envMap := runL2CommandAndParseJSON(t, cmdArgs...)
envMap := testutils.RunL2CommandAndParseJSON(t, cmdArgs...)

// Check the "AHOST" key is present
checkAHost(t, envMap)
Expand All @@ -68,7 +58,7 @@ func TestL2EnvCommandVerbose(t *testing.T) {
func TestL2EnvWithoutL2config(t *testing.T) {
fpath := "../elfparser/ElfTestSuite/no_l2config/api/y_0021_no_l2config.l2"
cmdArgs := []string{"-e=", fpath}
envMap := runL2CommandAndParseJSON(t, cmdArgs...)
envMap := testutils.RunL2CommandAndParseJSON(t, cmdArgs...)

// Check the "AHOST" key is present
checkAHost(t, envMap)
Expand All @@ -77,64 +67,14 @@ func TestL2EnvWithoutL2config(t *testing.T) {
func TestL2EnvWithoutL2env(t *testing.T) {
fpath := "../elfparser/ElfTestSuite/no_l2env/api/y_0022_no_l2env.l2"
cmdArgs := []string{"-e=", fpath}
envMap := runL2CommandAndParseJSON(t, cmdArgs...)
envMap := testutils.RunL2CommandAndParseJSON(t, cmdArgs...)

// Check the "BHOST" key is present
checkBHost(t, envMap)
}

func runL2CommandAndParseJSON(t *testing.T, cmdArgs ...string) map[string]EnvData {
// Get the full path to the l2 binary
l2BinPath := "../build/l2"

// Check if the l2 binary file exists
if err := checkL2BinaryExists(l2BinPath); err != nil {
t.Error(err)
return make(map[string]EnvData)
}

// Your existing code to run the l2 command and parse JSON
cmd := exec.Command(l2BinPath, cmdArgs...)

var stdout bytes.Buffer
cmd.Stdout = &stdout

// Execute the command
err := cmd.Run()
if err != nil {
// Handle the error if needed
t.Errorf("Error running l2 command: %v\n", err)
return make(map[string]EnvData)
}

// Retrieve the captured stdout
stdoutOutput := stdout.String()

log.Debug().Str("Test env_command", stdoutOutput).Msg("output from command")

// Convert the stdoutOutput string to []byte slice
outputBytes := []byte(stdoutOutput)

envMap := make(map[string]EnvData)
err = json.Unmarshal(outputBytes, &envMap)
if err != nil {
t.Fatalf("Error unmarshaling JSON env: %v\nOutput:\n%s", err, stdoutOutput)
}

return envMap
}

// checkL2BinaryExists checks if the l2 binary file exists in the specified path
func checkL2BinaryExists(l2BinPath string) error {
// Check if the l2 binary file exists
if _, err := os.Stat(l2BinPath); os.IsNotExist(err) {
return fmt.Errorf("l2 binary not found in the build folder %s, please change the path", l2BinPath)
}
return nil
}

// checkAHost checks the "AHOST" key in the JSON map
func checkAHost(t *testing.T, envMap map[string]EnvData) {
func checkAHost(t *testing.T, envMap map[string]testutils.EnvData) {
if ahost, ok := envMap["AHOST"]; !ok {
t.Error("Expected 'AHOST' key in the JSON, but it was not found")
} else {
Expand All @@ -149,7 +89,7 @@ func checkAHost(t *testing.T, envMap map[string]EnvData) {
}

// checkBHost checks the "BHOST" key in the JSON map
func checkBHost(t *testing.T, envMap map[string]EnvData) {
func checkBHost(t *testing.T, envMap map[string]testutils.EnvData) {
if bhost, ok := envMap["BHOST"]; !ok {
t.Error("Expected 'BHOST' key in the JSON, but it was not found")
} else {
Expand All @@ -163,13 +103,13 @@ func checkBHost(t *testing.T, envMap map[string]EnvData) {
}
}

func checkBHostDoesNotExist(t *testing.T, envMap map[string]EnvData) {
func checkBHostDoesNotExist(t *testing.T, envMap map[string]testutils.EnvData) {
if _, ok := envMap["BHOST"]; ok {
t.Error("Expected 'BHOST' key not to be present in the JSON, but it was found")
}
}

func checkAHostDoesNotExist(t *testing.T, envMap map[string]EnvData) {
func checkAHostDoesNotExist(t *testing.T, envMap map[string]testutils.EnvData) {
if _, ok := envMap["AHOST"]; ok {
t.Error("Expected 'AHOST' key not to be present in the JSON, but it was found")
}
Expand Down
100 changes: 100 additions & 0 deletions tests/utils/test_utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package testutils

import (
"bytes"
"encoding/json"
"fmt"
"os"
"os/exec"
"testing"

"github.com/rs/zerolog/log"
)

type EnvData struct {
Src string `json:"src"`
Val string `json:"val"`
}

func RunL2CommandAndGetOutput(t *testing.T, cmdArgs ...string) string {

// Get the full path to the l2 binary
l2BinPath := "../build/l2"

// Check if the l2 binary file exists
if err := checkL2BinaryExists(l2BinPath); err != nil {
t.Error(err)
return ""
}

// Code to run the l2 command
cmd := exec.Command(l2BinPath, cmdArgs...)

var stdout bytes.Buffer
cmd.Stdout = &stdout

var stderr bytes.Buffer
cmd.Stderr = &stderr

// Execute the command
err := cmd.Run()
if err != nil {
log.Error().Str("Error", stderr.String()).Msg("Error running l2 command")
t.Errorf("Error running l2 command: %v\n", err)
return ""
}

// Retrieve the captured stdout
stdoutOutput := stdout.String()
log.Info().Str("Test env_command", stdoutOutput).Msg("output from command")
return stdoutOutput
}

func RunL2CommandAndParseJSON(t *testing.T, cmdArgs ...string) map[string]EnvData {
// Get the full path to the l2 binary
l2BinPath := "../build/l2"

// Check if the l2 binary file exists
if err := checkL2BinaryExists(l2BinPath); err != nil {
t.Error(err)
return make(map[string]EnvData)
}

// Your existing code to run the l2 command and parse JSON
cmd := exec.Command(l2BinPath, cmdArgs...)

var stdout bytes.Buffer
cmd.Stdout = &stdout

// Execute the command
err := cmd.Run()
if err != nil {
// Handle the error if needed
t.Errorf("Error running l2 command: %v\n", err)
return make(map[string]EnvData)
}

// Retrieve the captured stdout
stdoutOutput := stdout.String()

log.Debug().Str("Test env_command", stdoutOutput).Msg("output from command")

// Convert the stdoutOutput string to []byte slice
outputBytes := []byte(stdoutOutput)

envMap := make(map[string]EnvData)
err = json.Unmarshal(outputBytes, &envMap)
if err != nil {
t.Fatalf("Error unmarshaling JSON env: %v\nOutput:\n%s", err, stdoutOutput)
}

return envMap
}

func checkL2BinaryExists(l2BinPath string) error {
// Check if the l2 binary file exists
if _, err := os.Stat(l2BinPath); os.IsNotExist(err) {
return fmt.Errorf("l2 binary not found in the build folder %s, please change the path", l2BinPath)
}
return nil
}

0 comments on commit 2b6e35e

Please sign in to comment.