Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/390 real time trace #708

Merged
merged 3 commits into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 0 additions & 49 deletions src/toolchain/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ package toolchain

import (
"errors"
"github.com/codeskyblue/go-sh"
"github.com/murex/tcr/report"
"os"
"os/exec"
"path/filepath"
Expand All @@ -50,33 +48,6 @@ type (
Path string
Arguments []string
}

// CommandStatus is the result status of a Command execution
CommandStatus string

// CommandResult contains the result from running a Command
// - Status
CommandResult struct {
Status CommandStatus
Output string
}
)

// Failed indicates is a Command failed
func (r CommandResult) Failed() bool {
return r.Status == CommandStatusFail
}

// Passed indicates is a Command passed
func (r CommandResult) Passed() bool {
return r.Status == CommandStatusPass
}

// List of possible values for CommandStatus
const (
CommandStatusPass CommandStatus = "pass"
CommandStatusFail CommandStatus = "fail"
CommandStatusUnknown CommandStatus = "unknown"
)

// List of possible values for OsName
Expand Down Expand Up @@ -129,26 +100,6 @@ func (command Command) runsWithArch(archName ArchName) bool {
return false
}

func (command Command) run() (result CommandResult) {
result = CommandResult{Status: CommandStatusUnknown, Output: ""}
report.PostText(command.asCommandLine())

session := sh.NewSession().SetDir(GetWorkDir())
outputBytes, err := session.Command(command.Path, command.Arguments).CombinedOutput()

if err == nil {
result.Status = CommandStatusPass
} else {
result.Status = CommandStatusFail
}

if outputBytes != nil {
result.Output = string(outputBytes)
report.PostText(result.Output)
}
return result
}

func (command Command) check() error {
if err := command.checkPath(); err != nil {
return err
Expand Down
123 changes: 123 additions & 0 deletions src/toolchain/command_runner.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/*
Copyright (c) 2024 Murex

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

package toolchain

import (
"bufio"
"github.com/murex/tcr/report"
"io"
"os/exec"
)

type (
// CommandRunner is in charge of managing the lifecycle of a command
CommandRunner struct {
command *exec.Cmd
}

// CommandStatus is the result status of a Command execution
CommandStatus string

// CommandResult contains the result from running a Command
// - Status
CommandResult struct {
Status CommandStatus
Output string
}
)

// Failed indicates is a Command failed
func (r CommandResult) Failed() bool {
return r.Status == CommandStatusFail
}

// Passed indicates is a Command passed
func (r CommandResult) Passed() bool {
return r.Status == CommandStatusPass
}

// List of possible values for CommandStatus
const (
CommandStatusPass CommandStatus = "pass"
CommandStatusFail CommandStatus = "fail"
CommandStatusUnknown CommandStatus = "unknown"
)

// commandRunner singleton instance
var commandRunner = getCommandRunner()

func getCommandRunner() *CommandRunner {
return &CommandRunner{
command: nil,
}
}

// Run launches the execution of the provided command
func (r *CommandRunner) Run(cmd *Command) (result CommandResult) {
result = CommandResult{Status: CommandStatusUnknown, Output: ""}
report.PostText(cmd.asCommandLine())

// Prepare the command
r.command = exec.Command(cmd.Path, cmd.Arguments...) //nolint:gosec
r.command.Dir = GetWorkDir()

// Allow simultaneous trace and capture of command's stdout and stderr
outReader, _ := r.command.StdoutPipe()
errReader, _ := r.command.StderrPipe()
r.reportCommandTrace(outReader)
r.reportCommandTrace(errReader)

// Start the command asynchronously
errStart := r.command.Start()
if errStart != nil {
report.PostError("Failed to run command: ", errStart.Error())
// We currently return fail status when command cannot be launched.
// This is to replicate previous implementation's behaviour where
// we could not differentiate between failure to launch and failure from execution.
// We could later on use a different return value in this situation,
// but we need to ensure first that TCR engine can handle it correctly.
result.Status = CommandStatusFail
r.command = nil
return result
}

// Wait for the command to finish
errWait := r.command.Wait()
if errWait != nil {
result.Status = CommandStatusFail
} else {
result.Status = CommandStatusPass
}

r.command = nil
return result
}

func (*CommandRunner) reportCommandTrace(readCloser io.ReadCloser) {
scanner := bufio.NewScanner(readCloser)
go func() {
for scanner.Scan() {
report.PostText(scanner.Text())
}
}()
}
49 changes: 49 additions & 0 deletions src/toolchain/command_runner_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
Copyright (c) 2024 Murex

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

package toolchain

import (
"fmt"
"github.com/stretchr/testify/assert"
"testing"
)

func Test_command_result_outcome(t *testing.T) {

testFlags := []struct {
status CommandStatus
expectedPassed bool
expectedFailed bool
}{
{"pass", true, false},
{"fail", false, true},
{"unknown", false, false},
}
for _, tt := range testFlags {
t.Run(fmt.Sprint(tt.status, "_status"), func(t *testing.T) {
result := CommandResult{Status: tt.status}
assert.Equal(t, tt.expectedPassed, result.Passed())
assert.Equal(t, tt.expectedFailed, result.Failed())
})
}
}
20 changes: 0 additions & 20 deletions src/toolchain/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,23 +133,3 @@ func Test_a_command_arch_cannot_be_empty(t *testing.T) {
func Test_a_valid_command_should_have_path_os_and_arch_non_empty(t *testing.T) {
assert.NoError(t, ACommand(WithPath("some-path"), WithOs("some-os"), WithArch("some-arch")).check())
}

func Test_command_result_outcome(t *testing.T) {

testFlags := []struct {
status CommandStatus
expectedPassed bool
expectedFailed bool
}{
{"pass", true, false},
{"fail", false, true},
{"unknown", false, false},
}
for _, tt := range testFlags {
t.Run(fmt.Sprint(tt.status, "_status"), func(t *testing.T) {
result := CommandResult{Status: tt.status}
assert.Equal(t, tt.expectedPassed, result.Passed())
assert.Equal(t, tt.expectedFailed, result.Failed())
})
}
}
44 changes: 41 additions & 3 deletions src/toolchain/smoke_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (c) 2023 Murex
Copyright (c) 2024 Murex

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -31,19 +31,57 @@ const (
toolchainName = "gradle-wrapper"
)

func Test_toolchain_returns_error_when_build_fails(t *testing.T) {
func Test_toolchain_returns_error_when_build_command_is_not_found(t *testing.T) {
// Command does not exist in testDataRootDir
assertErrorWhenBuildFails(t, toolchainName, testDataRootDir)
}

func Test_toolchain_returns_error_when_build_command_fails(t *testing.T) {
tchn, _ := Get(toolchainName)
// Add a built-in toolchain from a valid one, but with invalid build command arguments.
// This allows to actually run the build command and get an execution error out of it
failingName := tchn.GetName() + "-failing-build"
var failingCommands []Command
for _, cmd := range tchn.GetBuildCommands() {
failingCommands = append(failingCommands, Command{
Os: cmd.Os,
Arch: cmd.Arch,
Path: cmd.Path,
Arguments: []string{"-invalid-argument"},
})
}
_ = addBuiltIn(New(failingName, failingCommands, tchn.GetTestCommands(), tchn.GetTestResultDir()))
assertErrorWhenBuildFails(t, failingName, testDataDirJava)
}

func Test_toolchain_returns_ok_when_build_passes(t *testing.T) {
utils.SlowTestTag(t)
assertNoErrorWhenBuildPasses(t, toolchainName, testDataDirJava)
}

func Test_toolchain_returns_error_when_tests_fail(t *testing.T) {
func Test_toolchain_returns_error_when_test_command_is_not_found(t *testing.T) {
// Command does not exist in testDataRootDir
assertErrorWhenTestFails(t, toolchainName, testDataRootDir)
}

func Test_toolchain_returns_error_when_test_command_fails(t *testing.T) {
// Add a built-in toolchain from a valid one, but with invalid test command arguments.
// This allows to actually run the test command and get an execution error out of it
tchn, _ := Get(toolchainName)
failingName := tchn.GetName() + "-failing-test"
var failingCommands []Command
for _, cmd := range tchn.GetTestCommands() {
failingCommands = append(failingCommands, Command{
Os: cmd.Os,
Arch: cmd.Arch,
Path: cmd.Path,
Arguments: []string{"-invalid-argument"},
})
}
_ = addBuiltIn(New(failingName, tchn.GetBuildCommands(), failingCommands, tchn.GetTestResultDir()))
assertErrorWhenTestFails(t, failingName, testDataDirJava)
}

func Test_toolchain_returns_ok_when_tests_pass(t *testing.T) {
utils.SlowTestTag(t)
assertNoErrorWhenTestPasses(t, toolchainName, testDataDirJava)
Expand Down
6 changes: 4 additions & 2 deletions src/toolchain/toolchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,12 +150,14 @@ func (tchn Toolchain) GetTestCommands() []Command {

// RunBuild runs the build with this toolchain
func (tchn Toolchain) RunBuild() CommandResult {
return findCompatibleCommand(tchn.buildCommands).run()
cmd := findCompatibleCommand(tchn.buildCommands)
return commandRunner.Run(cmd)
}

// RunTests runs the tests with this toolchain
func (tchn Toolchain) RunTests() TestCommandResult {
result := findCompatibleCommand(tchn.testCommands).run()
cmd := findCompatibleCommand(tchn.testCommands)
result := commandRunner.Run(cmd)
testStats, _ := tchn.parseTestReport()
return TestCommandResult{result, testStats}
}
Expand Down