From 58fbc92c91241606cbee4af429c53b4c54b0224d Mon Sep 17 00:00:00 2001 From: lovestaco Date: Thu, 20 Jul 2023 20:16:54 +0530 Subject: [PATCH] Updating testapp.yml, build and test --- .github/workflows/testapp.yml | 5 ++- tests/env_command_test.go | 66 +++++++++++++++++++++++------------ 2 files changed, 47 insertions(+), 24 deletions(-) diff --git a/.github/workflows/testapp.yml b/.github/workflows/testapp.yml index 2c78a4d0..248c4df6 100644 --- a/.github/workflows/testapp.yml +++ b/.github/workflows/testapp.yml @@ -18,6 +18,9 @@ jobs: - name: Build run: go build -v ./... - name: Test with the Go CLI - run: go test -v ./tests/ + run: | + go mod tidy + go build -o build/l2 -ldflags "-X main.version=`git tag --sort=-version:refname | head -n 1`" l2.go + go test -v ./tests/ - name: Deploy hexmos doc run: curl -X POST --fail -F token=${{ secrets.TRIGGER_TOKEN }} -F ref=main https://git.apps.hexmos.com/api/v4/projects/85/trigger/pipeline \ No newline at end of file diff --git a/tests/env_command_test.go b/tests/env_command_test.go index 32813473..19f9b335 100644 --- a/tests/env_command_test.go +++ b/tests/env_command_test.go @@ -4,10 +4,11 @@ import ( "bytes" "encoding/json" "fmt" + "os" "os/exec" "testing" - "os" + "github.com/rs/zerolog/log" ) type EnvData struct { @@ -15,34 +16,44 @@ type EnvData struct { Val string `json:"val"` } +func TestL2EnvCommand(t *testing.T) { + cmdArgs := []string{"-e", "../elfparser/ElfTestSuite/root_variable_override/api/y_0020_root_override.l2"} + runL2CommandAndParseJSON(t, cmdArgs...) +} + +func TestL2EnvCommandVerbose(t *testing.T) { + cmdArgs := []string{"-ev", "../elfparser/ElfTestSuite/root_variable_override/api/y_0020_root_override.l2"} + runL2CommandAndParseJSON(t, cmdArgs...) +} + func runL2CommandAndParseJSON(t *testing.T, cmdArgs ...string) { // Get the full path to the l2 binary l2BinPath := "/home/runner/work/Lama2/Lama2/build/l2" // Check if the l2 binary file exists - if _, err := os.Stat(l2BinPath); os.IsNotExist(err) { - fmt.Printf("Error: l2 binary not found in the build folder %s, please change the path.\n", l2BinPath) + if err := checkL2BinaryExists(l2BinPath); err != nil { + t.Error(err) return } // 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 - fmt.Printf("Error running l2 command: %v\n", err) + // Handle the error if needed + t.Errorf("Error running l2 command: %v\n", err) return } // Retrieve the captured stdout stdoutOutput := stdout.String() - fmt.Println(stdoutOutput) + log.Debug().Str("Test env_command", stdoutOutput).Msg("output from command") // Convert the stdoutOutput string to []byte slice outputBytes := []byte(stdoutOutput) @@ -50,10 +61,27 @@ func runL2CommandAndParseJSON(t *testing.T, cmdArgs ...string) { envMap := make(map[string]EnvData) err = json.Unmarshal(outputBytes, &envMap) if err != nil { - t.Fatalf("Error unmarshaling JSON: %v\nOutput:\n%s", err, stdoutOutput) + t.Fatalf("Error unmarshaling JSON env: %v\nOutput:\n%s", err, stdoutOutput) + } + + // Check the "AHOST" key + checkAHost(t, envMap) + + // Check the "BHOST" key + checkBHost(t, 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 +} - // Example assertion: Check the "AHOST" key +// checkAHost checks the "AHOST" key in the JSON map +func checkAHost(t *testing.T, envMap map[string]EnvData) { if ahost, ok := envMap["AHOST"]; !ok { t.Error("Expected 'AHOST' key in the JSON, but it was not found") } else { @@ -62,10 +90,13 @@ func runL2CommandAndParseJSON(t *testing.T, cmdArgs ...string) { t.Errorf(`Expected "src" value to be "l2env" for "AHOST", but got: %v`, ahost.Src) } if ahost.Val != "`echo http://httpbin.org`" { - t.Errorf("Expected \"val\" value to be \"`echo http://httpbin.org`\" for \"AHOST\", but got: %v", ahost.Val) + t.Errorf(`Expected "val" value to be "echo http://httpbin.org" for "AHOST", but got: %v`, ahost.Val) } } - // Example assertion: Check the "BHOST" key +} + +// checkBHost checks the "BHOST" key in the JSON map +func checkBHost(t *testing.T, envMap map[string]EnvData) { if bhost, ok := envMap["BHOST"]; !ok { t.Error("Expected 'BHOST' key in the JSON, but it was not found") } else { @@ -78,14 +109,3 @@ func runL2CommandAndParseJSON(t *testing.T, cmdArgs ...string) { } } } - - -func TestL2EnvCommand(t *testing.T) { - cmdArgs := []string{"-e", "../elfparser/ElfTestSuite/root_variable_override/api/y_0020_root_override.l2"} - runL2CommandAndParseJSON(t, cmdArgs...) -} - -func TestL2EnvCommandVerbose(t *testing.T) { - cmdArgs := []string{"-ev", "../elfparser/ElfTestSuite/root_variable_override/api/y_0020_root_override.l2"} - runL2CommandAndParseJSON(t, cmdArgs...) -}