Skip to content
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
7 changes: 6 additions & 1 deletion playground/backend/configs/SDK_GO.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
{
"compile_cmd": "go",
"run_cmd": "",
"test_cmd": "go",
"compile_args": [
"build",
"-o",
"bin"
],
"run_args": [
],
"test_args": [
"test",
"-v"
]
}
}
6 changes: 2 additions & 4 deletions playground/backend/containers/go/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
# limitations under the License.
###############################################################################
#Dokerfile to set up the Beam Go SDK
ARG BASE_IMAGE golang:1.16-buster
ARG BASE_IMAGE=golang:1.16-buster
FROM $BASE_IMAGE

# Setup Go Environment
Expand Down Expand Up @@ -56,6 +56,4 @@ ENV BEAM_SDK="SDK_GO"
## Copy build result
COPY src/configs /opt/playground/backend/configs/

ENTRYPOINT ["/opt/playground/backend/server_go_backend"]


ENTRYPOINT ["/opt/playground/backend/server_go_backend"]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add an empty string.

51 changes: 32 additions & 19 deletions playground/backend/internal/code_processing/code_processing.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func Process(ctx context.Context, cacheService cache.Cache, lc *fs_tool.LifeCycl

go cancelCheck(ctxWithTimeout, pipelineId, cancelChannel, cacheService)

executorBuilder, err := builder.SetupExecutorBuilder(lc.GetAbsoluteSourceFilePath(), lc.GetAbsoluteBaseFolderPath(), lc.GetAbsoluteExecutableFilePath(), utils.ReduceWhiteSpacesToSinge(pipelineOptions), sdkEnv)
executorBuilder, err := builder.SetupExecutorBuilder(lc, utils.ReduceWhiteSpacesToSinge(pipelineOptions), sdkEnv)
if err != nil {
_ = processSetupError(err, pipelineId, cacheService, ctxWithTimeout)
return
Expand All @@ -88,14 +88,20 @@ func Process(ctx context.Context, cacheService cache.Cache, lc *fs_tool.LifeCycl
_ = processError(ctxWithTimeout, errorChannel, pipelineId, cacheService, "Validate", pb.Status_STATUS_VALIDATION_ERROR)
return
}
// Check if unit test
isUnitTest := false
valResult, ok := validationResults.Load(validators.UnitTestValidatorName)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
valResult, ok := validationResults.Load(validators.UnitTestValidatorName)
validateIsUnitTest, ok := validationResults.Load(validators.UnitTestValidatorName)

if ok && valResult.(bool) {
isUnitTest = true
}
Comment on lines +91 to +96
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe wrap it like a separate method?

if err := processSuccess(ctxWithTimeout, pipelineId, cacheService, "Validate", pb.Status_STATUS_PREPARING); err != nil {
return
}

// Prepare
logger.Infof("%s: Prepare() ...\n", pipelineId)
prepareFunc := executor.Prepare()
go prepareFunc(successChannel, errorChannel)
go prepareFunc(successChannel, errorChannel, isUnitTest)

ok, err = processStep(ctxWithTimeout, pipelineId, cacheService, cancelChannel, successChannel)
if err != nil {
Expand All @@ -111,24 +117,31 @@ func Process(ctx context.Context, cacheService cache.Cache, lc *fs_tool.LifeCycl

switch sdkEnv.ApacheBeamSdk {
case pb.Sdk_SDK_JAVA, pb.Sdk_SDK_GO:
// Compile
logger.Infof("%s: Compile() ...\n", pipelineId)
compileCmd := executor.Compile(ctxWithTimeout)
var compileError bytes.Buffer
var compileOutput bytes.Buffer
runCmdWithOutput(compileCmd, &compileOutput, &compileError, successChannel, errorChannel)

ok, err = processStep(ctxWithTimeout, pipelineId, cacheService, cancelChannel, successChannel)
if err != nil {
return
}
if !ok {
_ = processCompileError(ctxWithTimeout, errorChannel, compileError.Bytes(), pipelineId, cacheService)
return
}
if err := processCompileSuccess(ctxWithTimeout, compileOutput.Bytes(), pipelineId, cacheService); err != nil {
return
if sdkEnv.ApacheBeamSdk == pb.Sdk_SDK_GO && isUnitTest {
if err := processCompileSuccess(ctxWithTimeout, []byte(""), pipelineId, cacheService); err != nil {
return
}
} else {
// Compile
logger.Infof("%s: Compile() ...\n", pipelineId)
compileCmd := executor.Compile(ctxWithTimeout)
var compileError bytes.Buffer
var compileOutput bytes.Buffer
runCmdWithOutput(compileCmd, &compileOutput, &compileError, successChannel, errorChannel)

ok, err = processStep(ctxWithTimeout, pipelineId, cacheService, cancelChannel, successChannel)
if err != nil {
return
}
if !ok {
_ = processCompileError(ctxWithTimeout, errorChannel, compileError.Bytes(), pipelineId, cacheService)
return
}
if err := processCompileSuccess(ctxWithTimeout, compileOutput.Bytes(), pipelineId, cacheService); err != nil {
return
}
}

case pb.Sdk_SDK_PYTHON:
if err := processCompileSuccess(ctxWithTimeout, []byte(""), pipelineId, cacheService); err != nil {
return
Expand Down
5 changes: 3 additions & 2 deletions playground/backend/internal/executors/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,10 @@ func (ex *Executor) Validate() func(chan bool, chan error, *sync.Map) {
}

// Prepare returns the function that applies all preparations of executor
func (ex *Executor) Prepare() func(chan bool, chan error) {
return func(doneCh chan bool, errCh chan error) {
func (ex *Executor) Prepare() func(chan bool, chan error, bool) {
return func(doneCh chan bool, errCh chan error, isUnitTest bool) {
for _, preparator := range ex.preparators {
preparator.Args = append(preparator.Args, isUnitTest)
err := preparator.Prepare(preparator.Args...)
if err != nil {
errCh <- err
Expand Down
8 changes: 8 additions & 0 deletions playground/backend/internal/executors/executor_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,14 @@ func (b *UnitTestExecutorBuilder) WithArgs(testArgs []string) *UnitTestExecutorB
return b
}

// WithWorkingDir adds dir path to executor
func (b *UnitTestExecutorBuilder) WithWorkingDir(dir string) *UnitTestExecutorBuilder {
b.actions = append(b.actions, func(e *Executor) {
e.testArgs.workingDir = dir
})
return b
}

//WithGraphOutput adds the need of graph output to executor
func (b *UnitTestExecutorBuilder) WithGraphOutput() *UnitTestExecutorBuilder {
b.actions = append(b.actions, func(e *Executor) {
Expand Down
9 changes: 5 additions & 4 deletions playground/backend/internal/executors/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ func BaseExecutorBuilder(envs environment.BeamEnvs, workingDir string, filePath
builder := NewExecutorBuilder().
WithExecutableFileName("HelloWorld").
WithWorkingDir(workingDir).
WithValidator().
WithSdkValidators(validatorsFuncs).
WithPreparator().
WithSdkPreparators(preparatorsFuncs).
WithCompiler().
WithCommand(envs.ExecutorConfig.CompileCmd).
WithArgs(envs.ExecutorConfig.CompileArgs).
Expand All @@ -62,10 +66,7 @@ func BaseExecutorBuilder(envs environment.BeamEnvs, workingDir string, filePath
WithTestRunner().
WithCommand(envs.ExecutorConfig.TestCmd).
WithArgs(envs.ExecutorConfig.TestArgs).
WithValidator().
WithSdkValidators(validatorsFuncs).
WithPreparator().
WithSdkPreparators(preparatorsFuncs).
WithWorkingDir(workingDir).
ExecutorBuilder
return &builder
}
Expand Down
6 changes: 6 additions & 0 deletions playground/backend/internal/fs_tool/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,3 +166,9 @@ func (l *LifeCycle) GetAbsoluteLogFilePath() string {
absoluteFilePath, _ := filepath.Abs(filePath)
return absoluteFilePath
}

// GetAbsoluteSourceFolderPath returns absolute path to executable folder (/path/to/workingDir/executable_files/{pipelineId}/src).
func (l *LifeCycle) GetAbsoluteSourceFolderPath() string {
absoluteFilePath, _ := filepath.Abs(l.Folder.SourceFileFolder)
return absoluteFilePath
}
28 changes: 24 additions & 4 deletions playground/backend/internal/preparators/go_preparators.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,51 @@ package preparators

import (
"errors"
"fmt"
"os/exec"
"path/filepath"
"strings"
)

const (
nameBinGo = "go"
fmtArgs = "fmt"
goName = "go"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe goFileExtension instead of goName?

fmtArgs = "fmt"
mvCmd = "mv"
sep = "."
)

// GetGoPreparators returns reparation methods that should be applied to Go code
func GetGoPreparators(filePath string) *[]Preparator {
preparatorArgs := make([]interface{}, 1)
preparatorArgs[0] = filePath
formatCodePreparator := Preparator{Prepare: formatCode, Args: preparatorArgs}
return &[]Preparator{formatCodePreparator}
changeNamePreparator := Preparator{Prepare: changeFileName, Args: preparatorArgs}
return &[]Preparator{formatCodePreparator, changeNamePreparator}
}

// formatCode formats go code
func formatCode(args ...interface{}) error {
filePath := args[0].(string)
cmd := exec.Command(nameBinGo, fmtArgs, filepath.Base(filePath))
cmd := exec.Command(goName, fmtArgs, filepath.Base(filePath))
cmd.Dir = filepath.Dir(filePath)
stdout, err := cmd.CombinedOutput()
if err != nil {
return errors.New(string(stdout))
}
return nil
}

func changeFileName(args ...interface{}) error {
filePath := args[0].(string)
isUnitTest := args[1].(bool)
if isUnitTest {
testFileName := fmt.Sprintf("%s_test.%s", strings.Split(filePath, sep)[0], goName)
cmd := exec.Command(mvCmd, filePath, testFileName)
fmt.Println(cmd.String())
stdout, err := cmd.CombinedOutput()
if err != nil {
return errors.New(string(stdout))
}
}
return nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ func getPreparedArgs(args ...interface{}) []interface{} {
}

func TestGetGoPreparators(t *testing.T) {
expectedPreparator := Preparator{Prepare: formatCode, Args: nil}
type args struct {
filePath string
}
Expand All @@ -102,7 +101,7 @@ func TestGetGoPreparators(t *testing.T) {
// getting the expected preparator
name: "get expected preparator",
args: args{filePath: ""},
want: &[]Preparator{expectedPreparator},
want: &[]Preparator{{Prepare: formatCode, Args: nil}, {Prepare: changeFileName, Args: nil}},
},
}
for _, tt := range tests {
Expand Down
18 changes: 10 additions & 8 deletions playground/backend/internal/setup_tools/builder/setup_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
pb "beam.apache.org/playground/backend/internal/api/v1"
"beam.apache.org/playground/backend/internal/environment"
"beam.apache.org/playground/backend/internal/executors"
"beam.apache.org/playground/backend/internal/fs_tool"
"beam.apache.org/playground/backend/internal/utils"
"fmt"
"path/filepath"
Expand All @@ -31,48 +32,49 @@ const (
)

// SetupExecutorBuilder return executor with set args for validator, preparator, compiler and runner
func SetupExecutorBuilder(srcFilePath, baseFolderPath, execFilePath, pipelineOptions string, sdkEnv *environment.BeamEnvs) (*executors.ExecutorBuilder, error) {
func SetupExecutorBuilder(lc *fs_tool.LifeCycle, pipelineOptions string, sdkEnv *environment.BeamEnvs) (*executors.ExecutorBuilder, error) {
sdk := sdkEnv.ApacheBeamSdk

if sdk == pb.Sdk_SDK_JAVA {
pipelineOptions = utils.ReplaceSpacesWithEquals(pipelineOptions)
}

val, err := utils.GetValidators(sdk, srcFilePath)
val, err := utils.GetValidators(sdk, lc.GetAbsoluteSourceFilePath())
if err != nil {
return nil, err
}
prep, err := utils.GetPreparators(sdk, srcFilePath)
prep, err := utils.GetPreparators(sdk, lc.GetAbsoluteSourceFilePath())
if err != nil {
return nil, err
}
executorConfig := sdkEnv.ExecutorConfig
builder := executors.NewExecutorBuilder().
WithExecutableFileName(execFilePath).
WithWorkingDir(baseFolderPath).
WithExecutableFileName(lc.GetAbsoluteExecutableFilePath()).
WithWorkingDir(lc.GetAbsoluteBaseFolderPath()).
WithValidator().
WithSdkValidators(val).
WithPreparator().
WithSdkPreparators(prep).
WithCompiler().
WithCommand(executorConfig.CompileCmd).
WithArgs(executorConfig.CompileArgs).
WithFileName(srcFilePath).
WithFileName(lc.GetAbsoluteSourceFilePath()).
WithRunner().
WithCommand(executorConfig.RunCmd).
WithArgs(executorConfig.RunArgs).
WithPipelineOptions(strings.Split(pipelineOptions, " ")).
WithTestRunner().
WithCommand(executorConfig.TestCmd).
WithArgs(executorConfig.TestArgs).
WithWorkingDir(lc.GetAbsoluteSourceFolderPath()).
ExecutorBuilder

switch sdk {
case pb.Sdk_SDK_JAVA: // Executable name for java class will be known after compilation
args := make([]string, 0)
for _, arg := range executorConfig.RunArgs {
if strings.Contains(arg, javaLogConfigFilePlaceholder) {
logConfigFilePath := filepath.Join(baseFolderPath, javaLogConfigFileName)
logConfigFilePath := filepath.Join(lc.GetAbsoluteBaseFolderPath(), javaLogConfigFileName)
arg = strings.Replace(arg, javaLogConfigFilePlaceholder, logConfigFilePath, 1)
}
args = append(args, arg)
Expand All @@ -82,7 +84,7 @@ func SetupExecutorBuilder(srcFilePath, baseFolderPath, execFilePath, pipelineOpt
builder = builder.
WithExecutableFileName("").
WithRunner().
WithCommand(execFilePath).ExecutorBuilder
WithCommand(lc.GetAbsoluteExecutableFilePath()).ExecutorBuilder
case pb.Sdk_SDK_PYTHON:
// Nothing is needed for Python
case pb.Sdk_SDK_SCIO:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,11 @@ func TestSetupExecutor(t *testing.T) {
WithTestRunner().
WithCommand(executorConfig.TestCmd).
WithArgs(executorConfig.TestArgs).
WithWorkingDir(lc.GetAbsoluteSourceFolderPath()).
ExecutorBuilder

type args struct {
srcFilePath string
baseFolderPath string
execFilePath string
lc *fs_tool.LifeCycle
pipelineOptions string
sdkEnv *environment.BeamEnvs
}
Expand All @@ -93,22 +92,22 @@ func TestSetupExecutor(t *testing.T) {
// Test case with calling Setup with incorrect SDK.
// As a result, want to receive an error.
name: "incorrect sdk",
args: args{lc.GetAbsoluteSourceFilePath(), lc.GetAbsoluteBaseFolderPath(), lc.GetAbsoluteExecutableFilePath(), pipelineOptions, environment.NewBeamEnvs(pb.Sdk_SDK_UNSPECIFIED, executorConfig, "")},
args: args{lc, pipelineOptions, environment.NewBeamEnvs(pb.Sdk_SDK_UNSPECIFIED, executorConfig, "")},
want: nil,
wantErr: true,
},
{
// Test case with calling Setup with correct SDK.
// As a result, want to receive an expected builder.
name: "correct sdk",
args: args{lc.GetAbsoluteSourceFilePath(), lc.GetAbsoluteBaseFolderPath(), lc.GetAbsoluteExecutableFilePath(), pipelineOptions, sdkEnv},
args: args{lc, pipelineOptions, sdkEnv},
want: &wantExecutor,
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := SetupExecutorBuilder(tt.args.srcFilePath, tt.args.baseFolderPath, tt.args.execFilePath, tt.args.pipelineOptions, tt.args.sdkEnv)
got, err := SetupExecutorBuilder(tt.args.lc, tt.args.pipelineOptions, tt.args.sdkEnv)
if (err != nil) != tt.wantErr {
t.Errorf("SetupExecutorBuilder() error = %v, wantErr %v", err, tt.wantErr)
return
Expand Down
2 changes: 1 addition & 1 deletion playground/backend/internal/utils/validators_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func GetValidators(sdk pb.Sdk, filepath string) (*[]validators.Validator, error)
case pb.Sdk_SDK_JAVA:
val = validators.GetJavaValidators(filepath)
case pb.Sdk_SDK_GO:
val = validators.GetGoValidators()
val = validators.GetGoValidators(filepath)
case pb.Sdk_SDK_PYTHON:
val = validators.GetPythonValidators()
default:
Expand Down
Loading