Skip to content

Commit

Permalink
Updating testapp.yml, build and test
Browse files Browse the repository at this point in the history
  • Loading branch information
lovestaco committed Jul 20, 2023
1 parent cbe799f commit 58fbc92
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 24 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/testapp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
66 changes: 43 additions & 23 deletions tests/env_command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,56 +4,84 @@ import (
"bytes"
"encoding/json"
"fmt"
"os"
"os/exec"
"testing"
"os"

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

type EnvData struct {
Src string `json:"src"`
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)

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 {
Expand All @@ -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 {
Expand All @@ -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...)
}

0 comments on commit 58fbc92

Please sign in to comment.