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

test: cover project id filtering on bulk actions [ET-138] #9870

Merged
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
223 changes: 177 additions & 46 deletions master/internal/experiment/bulk_action_intg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,30 +30,64 @@ func TestGetExperimentsEditableByUser(t *testing.T) {
testUser := db.RequireMockUser(t, db.SingleDB())
testWorkspaceID, _ := db.RequireMockWorkspaceID(t, db.SingleDB(), "TestGetExperimentsEditableByUser-"+nameExt.String())
testProjectID, _ := db.RequireMockProjectID(t, db.SingleDB(), testWorkspaceID, false)
testProjectID2, _ := db.RequireMockProjectID(t, db.SingleDB(), testWorkspaceID, false)

testModelStates := []model.State{
model.ActiveState,
model.CanceledState,
model.CompletedState,
model.ErrorState,
model.PausedState,
model.StoppingCanceledState,
model.DeletingState,
model.RunningState,
statePtr := func(s model.State) *model.State { return &s }
testModels := map[string]db.MockExperimentParams{
"project_1_state_active": {
ProjectID: &testProjectID,
State: statePtr(model.ActiveState),
},
"project_1_state_canceled": {
ProjectID: &testProjectID,
State: statePtr(model.CanceledState),
},
"project_1_state_completed": {
ProjectID: &testProjectID,
State: statePtr(model.CompletedState),
},
"project_1_state_error": {
ProjectID: &testProjectID,
State: statePtr(model.ErrorState),
},
"project_1_state_paused": {
ProjectID: &testProjectID,
State: statePtr(model.PausedState),
},
"project_1_state_stoppingCanceled": {
ProjectID: &testProjectID,
State: statePtr(model.StoppingCanceledState),
},
"project_1_state_deleting": {
ProjectID: &testProjectID,
State: statePtr(model.DeletingState),
},
"project_1_state_running": {
ProjectID: &testProjectID,
State: statePtr(model.RunningState),
},
"project_2_state_active": {
ProjectID: &testProjectID2,
State: statePtr(model.ActiveState),
},
"project_2_state_paused": {
ProjectID: &testProjectID2,
State: statePtr(model.PausedState),
},
}

allExperimentIds := make([]int, len(testModelStates))
editableExperiments := map[model.State]*model.Experiment{}
for i, state := range testModelStates {
allExperimentIds := make([]int, len(testModels))
editableExperiments := map[string]*model.Experiment{}
i := 0
for name, model := range testModels {
exp := db.RequireMockExperimentParams(
t, db.SingleDB(), testUser,
db.MockExperimentParams{
State: &state,
},
testProjectID,
model,
*model.ProjectID,
)
editableExperiments[state] = exp
editableExperiments[name] = exp
allExperimentIds[i] = exp.ID
i++
Comment on lines +79 to +90
Copy link
Contributor

Choose a reason for hiding this comment

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

The most minor of nits: you could get rid of i if you allocate allExperimentIds with len of zero and capacity of len(testModels) and then just append().

allExperimentIds := make([]int, 0, len(testModels))
editableExperiments := map[string]*model.Experiment{}

for name, model := range testModels {
	exp := db.RequireMockExperimentParams(
		t, db.SingleDB(), testUser,
		model,
		*model.ProjectID,
	)
	editableExperiments[name] = exp
	allExperimentIds = append(allExperimentIds, exp.ID)
}

}

defer func(ids []int) {
Expand All @@ -71,25 +105,25 @@ func TestGetExperimentsEditableByUser(t *testing.T) {
args: args{
projectID: int32(testProjectID),
experimentIDs: []int32{
int32(editableExperiments[model.ActiveState].ID),
int32(editableExperiments[model.CanceledState].ID),
int32(editableExperiments[model.CompletedState].ID),
int32(editableExperiments[model.ErrorState].ID),
int32(editableExperiments[model.PausedState].ID),
int32(editableExperiments[model.StoppingCanceledState].ID),
int32(editableExperiments[model.DeletingState].ID),
int32(editableExperiments[model.RunningState].ID),
int32(editableExperiments["project_1_state_active"].ID),
int32(editableExperiments["project_1_state_canceled"].ID),
int32(editableExperiments["project_1_state_completed"].ID),
int32(editableExperiments["project_1_state_error"].ID),
int32(editableExperiments["project_1_state_paused"].ID),
int32(editableExperiments["project_1_state_stoppingCanceled"].ID),
int32(editableExperiments["project_1_state_deleting"].ID),
int32(editableExperiments["project_1_state_running"].ID),
},
},
expected: []int32{
int32(editableExperiments[model.ActiveState].ID),
int32(editableExperiments[model.CanceledState].ID),
int32(editableExperiments[model.CompletedState].ID),
int32(editableExperiments[model.ErrorState].ID),
int32(editableExperiments[model.PausedState].ID),
int32(editableExperiments[model.StoppingCanceledState].ID),
int32(editableExperiments[model.DeletingState].ID),
int32(editableExperiments[model.RunningState].ID),
int32(editableExperiments["project_1_state_active"].ID),
int32(editableExperiments["project_1_state_canceled"].ID),
int32(editableExperiments["project_1_state_completed"].ID),
int32(editableExperiments["project_1_state_error"].ID),
int32(editableExperiments["project_1_state_paused"].ID),
int32(editableExperiments["project_1_state_stoppingCanceled"].ID),
int32(editableExperiments["project_1_state_deleting"].ID),
int32(editableExperiments["project_1_state_running"].ID),
},
},
{
Expand All @@ -108,27 +142,27 @@ func TestGetExperimentsEditableByUser(t *testing.T) {
expected: []int32{},
},
{
name: "exclude finished",
name: "exclude finished by ID",
args: args{
projectID: int32(testProjectID),
filters: &apiv1.BulkExperimentFilters{
ExcludedExperimentIds: []int32{
int32(editableExperiments[model.CanceledState].ID),
int32(editableExperiments[model.CompletedState].ID),
int32(editableExperiments[model.ErrorState].ID),
int32(editableExperiments["project_1_state_canceled"].ID),
int32(editableExperiments["project_1_state_completed"].ID),
int32(editableExperiments["project_1_state_error"].ID),
},
},
},
expected: []int32{
int32(editableExperiments[model.ActiveState].ID),
int32(editableExperiments[model.PausedState].ID),
int32(editableExperiments[model.StoppingCanceledState].ID),
int32(editableExperiments[model.DeletingState].ID),
int32(editableExperiments[model.RunningState].ID),
int32(editableExperiments["project_1_state_active"].ID),
int32(editableExperiments["project_1_state_paused"].ID),
int32(editableExperiments["project_1_state_stoppingCanceled"].ID),
int32(editableExperiments["project_1_state_deleting"].ID),
int32(editableExperiments["project_1_state_running"].ID),
},
},
{
name: "include only finished",
name: "include only finished by state",
args: args{
projectID: int32(testProjectID),
filters: &apiv1.BulkExperimentFilters{
Expand All @@ -140,9 +174,24 @@ func TestGetExperimentsEditableByUser(t *testing.T) {
},
},
expected: []int32{
int32(editableExperiments[model.CanceledState].ID),
int32(editableExperiments[model.CompletedState].ID),
int32(editableExperiments[model.ErrorState].ID),
int32(editableExperiments["project_1_state_canceled"].ID),
int32(editableExperiments["project_1_state_completed"].ID),
int32(editableExperiments["project_1_state_error"].ID),
},
},
{
name: "active in project 2",
args: args{
projectID: int32(testProjectID2),
filters: &apiv1.BulkExperimentFilters{
ProjectId: int32(testProjectID2),
States: []experimentv1.State{
experimentv1.State_STATE_ACTIVE,
},
},
},
expected: []int32{
int32(editableExperiments["project_2_state_active"].ID),
},
},
}
Expand All @@ -159,3 +208,85 @@ func TestGetExperimentsEditableByUser(t *testing.T) {
})
}
}

func TestIntegrationKillAllExperimentsInProject(t *testing.T) {
ctx := context.Background()
nameExt := uuid.New()
testUser := db.RequireMockUser(t, db.SingleDB())

// override experimentsEditableByUser to bypass dependency on context with attached auth
defer func(originalFunc func(context.Context, int32, []int32, *apiv1.BulkExperimentFilters) ([]int32, error)) {
experimentsEditableByUser = originalFunc
}(experimentsEditableByUser)
experimentsEditableByUser = func(
ctx context.Context,
projectID int32,
experimentIDs []int32,
filters *apiv1.BulkExperimentFilters,
) ([]int32, error) {
exps, err := getExperimentsEditableByUser(
ctx, &testUser, projectID, experimentIDs, filters,
)
return exps, err
}

testWorkspaceID, _ := db.RequireMockWorkspaceID(t, db.SingleDB(), "TestIntegrationKillExperiment-"+nameExt.String())
testProjectID, _ := db.RequireMockProjectID(t, db.SingleDB(), testWorkspaceID, false)
otherProjectID, _ := db.RequireMockProjectID(t, db.SingleDB(), testWorkspaceID, false)

statePtr := func(s model.State) *model.State { return &s }
testModels := map[string]db.MockExperimentParams{
// already in terminal state, will not be affected
"project_1_state_completed": {
ProjectID: &testProjectID,
State: statePtr(model.CompletedState),
},
// expect this to be killed
"project_1_state_active": {
ProjectID: &testProjectID,
State: statePtr(model.ActiveState),
},
// in a different project, will not be affected
"project_2_state_active": {
ProjectID: &otherProjectID,
State: statePtr(model.ActiveState),
},
}

allExperimentIds := make([]int, 0, len(testModels))
editableExperiments := map[string]*model.Experiment{}
for name, model := range testModels {
exp := db.RequireMockExperimentParams(
t, db.SingleDB(), testUser,
model,
*model.ProjectID,
)
editableExperiments[name] = exp
allExperimentIds = append(allExperimentIds, exp.ID)
}
defer func(ids []int) {
_ = db.SingleDB().DeleteExperiments(ctx, ids)
}(allExperimentIds)

for _, expID := range allExperimentIds {
exp := experimentMock{}
if expID == editableExperiments["project_1_state_active"].ID {
exp.On("KillExperiment").Return(nil)
}
require.NoError(t, ExperimentRegistry.Add(expID, &exp))
defer exp.AssertExpectations(t)
defer ExperimentRegistry.Delete(expID) //nolint:errcheck
}

filters := &apiv1.BulkExperimentFilters{
ProjectId: int32(testProjectID),
}

killed, err := KillExperiments(ctx, int32(testProjectID), []int32{}, filters)
require.NoError(t, err)
require.ElementsMatch(t, killed, []ExperimentActionResult{
{
ID: int32(editableExperiments["project_1_state_active"].ID),
},
})
}
4 changes: 2 additions & 2 deletions master/internal/experiment/bulk_action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func TestActivateExperiments(t *testing.T) {
expectedErr: true,
},
{
name: "three experiments selected, one found",
name: "three experiments selected, one eligible",
args: args{
projectID: 1,
experimentIds: []int32{132, 142, 152},
Expand All @@ -111,7 +111,7 @@ func TestActivateExperiments(t *testing.T) {
},
},
{
name: "filters are used",
name: "basic label filter",
args: args{
projectID: 1,
experimentIds: []int32{132, 142, 152},
Expand Down
Loading