Skip to content

Commit

Permalink
Merge pull request #7 from jasoet/feature/add-proctor-author-to-env-var
Browse files Browse the repository at this point in the history
[hadrian.siregar|bimo.horizon] Inject Context Args to Execution Args
  • Loading branch information
jasoet authored Oct 14, 2019
2 parents 27ef13f + e827d70 commit 91fb85f
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 10 deletions.
9 changes: 4 additions & 5 deletions internal/app/service/execution/handler/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,10 @@ func (httpHandler *executionHTTPHandler) GetStatus() http.HandlerFunc {

response.WriteHeader(http.StatusOK)

responseJson, err := json.Marshal(responseBody)
responseJSON, err := json.Marshal(responseBody)
logger.LogErrors(err, "marshal json from: ", responseBody)

_, _ = response.Write(responseJson)
_, _ = response.Write(responseJSON)

}
}
Expand All @@ -179,7 +179,6 @@ func (httpHandler *executionHTTPHandler) Post() http.HandlerFunc {
_, _ = response.Write([]byte(status.MalformedRequest))
return
}

context, executionName, err := httpHandler.service.Execute(job.Name, userEmail, job.Args)

logger.LogErrors(err, "execute job: ", job)
Expand All @@ -205,10 +204,10 @@ func (httpHandler *executionHTTPHandler) Post() http.HandlerFunc {

response.WriteHeader(http.StatusCreated)

responseJson, err := json.Marshal(responseBody)
responseJSON, err := json.Marshal(responseBody)
logger.LogErrors(err, "marshal json from: ", responseBody)

_, _ = response.Write(responseJson)
_, _ = response.Write(responseJSON)
return
}
}
8 changes: 6 additions & 2 deletions internal/app/service/execution/handler/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,9 @@ func (suite *ExecutionHTTPHandlerTestSuite) TestSuccessfulJobExecutionPostHTTPHa
userEmail := "[email protected]"
job := parameter.Job{
Name: "sample-job-name",
Args: map[string]string{"argOne": "sample-arg"},
Args: map[string]string{
"argOne": "sample-arg",
},
}
context := &model.ExecutionContext{
UserEmail: userEmail,
Expand Down Expand Up @@ -321,7 +323,9 @@ func (suite *ExecutionHTTPHandlerTestSuite) TestGenericErrorJobExecutionPostHTTP
userEmail := "[email protected]"
job := parameter.Job{
Name: "sample-job-name",
Args: map[string]string{"argOne": "sample-arg"},
Args: map[string]string{
"argOne": "sample-arg",
},
}
context := &model.ExecutionContext{
UserEmail: userEmail,
Expand Down
4 changes: 3 additions & 1 deletion internal/app/service/execution/model/execution_context.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package model

import (
"time"

sqlxTypes "github.com/jmoiron/sqlx/types"

"proctor/internal/app/service/execution/status"
dbTypes "proctor/internal/app/service/infra/db/types"
"time"
)

type ExecutionContext struct {
Expand Down
10 changes: 10 additions & 0 deletions internal/app/service/execution/service/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,16 @@ func (service *executionService) ExecuteWithCommand(jobName string, userEmail st

executionArgs := mergeArgs(args, secret)

contextArgsMap := map[string]string{
"EXECUTION_ID": fmt.Sprint(context.ExecutionID),
"JOB_NAME": context.JobName,
"EXECUTION_NAME": context.Name,
"USER_EMAIL": context.UserEmail,
"IMAGE_TAG": context.ImageTag,
}

executionArgs = mergeArgs(executionArgs, contextArgsMap)

context.Status = status.Created
executionName, err := service.kubernetesClient.ExecuteJobWithCommand(metadata.ImageName, executionArgs, commands)
logger.Info("Executed Job on Kubernetes got ", executionName, " execution jobName and ", err, "errors")
Expand Down
4 changes: 2 additions & 2 deletions internal/app/service/execution/service/execution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func (suite *TestExecutionServiceSuite) TestExecuteJobFailed() {
suite.mockMetadataRepository.On("GetByName", jobName).Return(fakeMetadata, nil).Once()
suite.mockSecretRepository.On("GetByJobName", jobName).Return(map[string]string{}, nil).Once()
suite.mockRepository.On("Insert", mock.Anything).Return(0, nil).Once()
suite.mockKubernetesClient.On("ExecuteJobWithCommand", imageName, jobArgs, []string{}).Return("", errors.New("Execution Failed"))
suite.mockKubernetesClient.On("ExecuteJobWithCommand", imageName, mock.Anything, []string{}).Return("", errors.New("Execution Failed"))

context, _, err := suite.service.Execute(jobName, userEmail, jobArgs)
assert.Error(t, err, "error when executing image")
Expand Down Expand Up @@ -143,7 +143,7 @@ func (suite *TestExecutionServiceSuite) TestExecuteJobSuccess() {
suite.mockRepository.On("GetById", mock.Anything).Return(0, nil).Times(3)

executionName := "execution-name"
suite.mockKubernetesClient.On("ExecuteJobWithCommand", imageName, jobArgs, []string{}).Return(executionName, nil)
suite.mockKubernetesClient.On("ExecuteJobWithCommand", imageName, mock.Anything, []string{}).Return(executionName, nil)
suite.mockKubernetesClient.On("WaitForReadyJob", executionName, mock.Anything).Return(nil)
podDetail := &v1.Pod{}
suite.mockKubernetesClient.On("WaitForReadyPod", executionName, mock.Anything).Return(podDetail, nil)
Expand Down

0 comments on commit 91fb85f

Please sign in to comment.