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

[hadrian.siregar|bimo.horizon] Inject Context Args to Execution Args #7

Merged
merged 3 commits into from
Oct 14, 2019
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
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