-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Jenson] Refactor Auditing to build upon an object
- Previously, it was using context for this purpose Closes #64
- Loading branch information
Showing
17 changed files
with
196 additions
and
208 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,23 @@ | ||
package audit | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/gojektech/proctor/proctord/storage/postgres" | ||
"github.com/stretchr/testify/mock" | ||
) | ||
|
||
type MockAuditor struct { | ||
mock.Mock | ||
} | ||
|
||
func (m *MockAuditor) AuditJobsExecution(ctx context.Context) { | ||
m.Called(ctx) | ||
func (m *MockAuditor) JobsExecution(JobsExecutionAuditLog *postgres.JobsExecutionAuditLog) { | ||
m.Called(JobsExecutionAuditLog) | ||
} | ||
|
||
func (m *MockAuditor) JobsExecutionAndStatus(JobsExecutionAuditLog *postgres.JobsExecutionAuditLog) { | ||
m.Called(JobsExecutionAuditLog) | ||
} | ||
|
||
func (m *MockAuditor) AuditJobExecutionStatus(jobExecutionID string) (string, error) { | ||
func (m *MockAuditor) JobsExecutionStatus(jobExecutionID string) (string, error) { | ||
args := m.Called(jobExecutionID) | ||
return args.String(0), args.Error(1) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,68 +1,67 @@ | ||
package audit | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/gojektech/proctor/proctord/kubernetes" | ||
"github.com/gojektech/proctor/proctord/storage" | ||
"github.com/gojektech/proctor/proctord/storage/postgres" | ||
"github.com/gojektech/proctor/proctord/utility" | ||
) | ||
|
||
func TestExecutionAuditor(t *testing.T) { | ||
func TestJobsExecutionAuditing(t *testing.T) { | ||
mockStore := &storage.MockStore{} | ||
mockKubeClient := &kubernetes.MockClient{} | ||
testAuditor := New(mockStore, mockKubeClient) | ||
jobsExecutionAuditLog := &postgres.JobsExecutionAuditLog{ | ||
JobName: "any-job-name", | ||
} | ||
|
||
jobName := "any-job-name" | ||
executedJobName := "proctor-123" | ||
imageName := "any/image:name" | ||
jobArgs := map[string]string{"key": "value"} | ||
userEmail := "mrproctor@example.com" | ||
mockStore.On("AuditJobsExecution", jobsExecutionAuditLog).Return(nil).Once() | ||
|
||
ctx := context.WithValue(context.Background(), utility.JobSubmissionStatusContextKey, utility.JobSubmissionSuccess) | ||
ctx = context.WithValue(ctx, utility.JobNameContextKey, jobName) | ||
ctx = context.WithValue(ctx, utility.JobNameSubmittedForExecutionContextKey, executedJobName) | ||
ctx = context.WithValue(ctx, utility.ImageNameContextKey, imageName) | ||
ctx = context.WithValue(ctx, utility.JobArgsContextKey, jobArgs) | ||
ctx = context.WithValue(ctx, utility.UserEmailContextKey, userEmail) | ||
|
||
mockStore.On("JobsExecutionAuditLog", utility.JobSubmissionSuccess, utility.JobWaiting, jobName, userEmail, executedJobName, imageName, jobArgs).Return(nil).Once() | ||
|
||
testAuditor.AuditJobsExecution(ctx) | ||
testAuditor.JobsExecution(jobsExecutionAuditLog) | ||
|
||
mockStore.AssertExpectations(t) | ||
mockKubeClient.AssertExpectations(t) | ||
} | ||
|
||
func TestExecutionAuditorClientError(t *testing.T) { | ||
func TestAuditJobsExecutionStatusAuditing(t *testing.T) { | ||
mockStore := &storage.MockStore{} | ||
mockKubeClient := &kubernetes.MockClient{} | ||
testAuditor := New(mockStore, mockKubeClient) | ||
userEmail := "mrproctor@example.com" | ||
|
||
ctx := context.WithValue(context.Background(), utility.JobSubmissionStatusContextKey, utility.JobSubmissionClientError) | ||
ctx = context.WithValue(ctx, utility.UserEmailContextKey, userEmail) | ||
jobExecutionID := "job-execution-id" | ||
jobExecutionStatus := "job-execution-status" | ||
|
||
mockStore.On("JobsExecutionAuditLog", utility.JobSubmissionClientError, utility.JobFailed, "", userEmail, "", "", map[string]string{}).Return(nil).Once() | ||
mockKubeClient.On("JobExecutionStatus", jobExecutionID).Return(jobExecutionStatus, nil) | ||
mockStore.On("UpdateJobsExecutionAuditLog", jobExecutionID, jobExecutionStatus).Return(nil).Once() | ||
|
||
testAuditor.AuditJobsExecution(ctx) | ||
testAuditor.JobsExecutionStatus(jobExecutionID) | ||
|
||
mockStore.AssertExpectations(t) | ||
mockKubeClient.AssertExpectations(t) | ||
} | ||
|
||
func TestExecutionAuditorServerError(t *testing.T) { | ||
func TestAuditJobsExecutionAndStatusAuditing(t *testing.T) { | ||
mockStore := &storage.MockStore{} | ||
mockKubeClient := &kubernetes.MockClient{} | ||
testAuditor := New(mockStore, mockKubeClient) | ||
userEmail := "mrproctor@example.com" | ||
|
||
ctx := context.WithValue(context.Background(), utility.JobSubmissionStatusContextKey, utility.JobSubmissionServerError) | ||
ctx = context.WithValue(ctx, utility.UserEmailContextKey, userEmail) | ||
jobExecutionID := "job-execution-id" | ||
jobExecutionStatus := "job-execution-status" | ||
jobsExecutionAuditLog := &postgres.JobsExecutionAuditLog{ | ||
JobName: "any-job-name", | ||
ExecutionID: postgres.StringToSQLString(jobExecutionID), | ||
JobSubmissionStatus: utility.JobSubmissionSuccess, | ||
} | ||
|
||
mockStore.On("JobsExecutionAuditLog", utility.JobSubmissionServerError, utility.JobFailed, "", userEmail, "", "", map[string]string{}).Return(nil).Once() | ||
mockStore.On("AuditJobsExecution", jobsExecutionAuditLog).Return(nil).Once() | ||
|
||
testAuditor.AuditJobsExecution(ctx) | ||
mockKubeClient.On("JobExecutionStatus", jobExecutionID).Return(jobExecutionStatus, nil) | ||
mockStore.On("UpdateJobsExecutionAuditLog", jobExecutionID, jobExecutionStatus).Return(nil).Once() | ||
|
||
testAuditor.JobsExecutionAndStatus(jobsExecutionAuditLog) | ||
|
||
mockStore.AssertExpectations(t) | ||
mockKubeClient.AssertExpectations(t) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,15 @@ | ||
package execution | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/gojektech/proctor/proctord/storage/postgres" | ||
"github.com/stretchr/testify/mock" | ||
) | ||
|
||
type MockExecutioner struct { | ||
mock.Mock | ||
} | ||
|
||
func (m *MockExecutioner) Execute(ctx context.Context, jobName, userEmail string, jobArgs map[string]string) (string, error) { | ||
args := m.Called(ctx, jobName, userEmail, jobArgs) | ||
func (m *MockExecutioner) Execute(jobExecutionAuditLog *postgres.JobsExecutionAuditLog, jobName string, jobArgs map[string]string) (string, error) { | ||
args := m.Called(jobExecutionAuditLog, jobName, jobArgs) | ||
return args.String(0), args.Error(1) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.