|
| 1 | +package command |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "errors" |
| 7 | + "os" |
| 8 | + "path/filepath" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/evergreen-ci/evergreen/agent/internal" |
| 12 | + "github.com/evergreen-ci/evergreen/agent/internal/client" |
| 13 | + "github.com/evergreen-ci/evergreen/model" |
| 14 | + "github.com/evergreen-ci/evergreen/model/task" |
| 15 | + "github.com/evergreen-ci/utility" |
| 16 | + "github.com/stretchr/testify/assert" |
| 17 | + "github.com/stretchr/testify/require" |
| 18 | +) |
| 19 | + |
| 20 | +func setupTestEnv(t *testing.T) (context.Context, context.CancelFunc, *internal.TaskConfig, *client.Mock, client.LoggerProducer) { |
| 21 | + comm := client.NewMock("http://localhost.com") |
| 22 | + comm.SelectTestsResponse = []string{"test1", "test3"} |
| 23 | + conf := &internal.TaskConfig{ |
| 24 | + Task: task.Task{ |
| 25 | + Id: "task_id", |
| 26 | + Secret: "task_secret", |
| 27 | + Project: "project_id", |
| 28 | + Version: "version", |
| 29 | + BuildVariant: "build_variant", |
| 30 | + DisplayName: "display_name", |
| 31 | + }, |
| 32 | + ProjectRef: model.ProjectRef{Id: "project_id"}, |
| 33 | + } |
| 34 | + tmpDir := t.TempDir() |
| 35 | + conf.WorkDir = tmpDir |
| 36 | + f, err := os.Create(filepath.Join(tmpDir, "test.json")) |
| 37 | + require.NoError(t, err) |
| 38 | + _ = f.Close() |
| 39 | + |
| 40 | + ctx, cancel := context.WithCancel(t.Context()) |
| 41 | + logger, err := comm.GetLoggerProducer(ctx, &conf.Task, nil) |
| 42 | + require.NoError(t, err) |
| 43 | + |
| 44 | + conf.Task.TestSelectionEnabled = true |
| 45 | + conf.ProjectRef.TestSelection.Allowed = utility.TruePtr() |
| 46 | + return ctx, cancel, conf, comm, logger |
| 47 | +} |
| 48 | + |
| 49 | +func TestTestSelectionGetParseFailsWithMissingOutputFile(t *testing.T) { |
| 50 | + cmd := &testSelectionGet{} |
| 51 | + params := map[string]any{} |
| 52 | + require.Error(t, cmd.ParseParams(params)) |
| 53 | +} |
| 54 | + |
| 55 | +func TestParseSucceedsWithValidParams(t *testing.T) { |
| 56 | + cmd := &testSelectionGet{} |
| 57 | + params := map[string]any{ |
| 58 | + "output_file": "test.json", |
| 59 | + } |
| 60 | + require.NoError(t, cmd.ParseParams(params)) |
| 61 | + assert.Equal(t, "test.json", cmd.OutputFile) |
| 62 | + assert.Empty(t, cmd.Tests) |
| 63 | + |
| 64 | + params["tests"] = []string{"test1", "test2"} |
| 65 | + require.NoError(t, cmd.ParseParams(params)) |
| 66 | + assert.Equal(t, "test.json", cmd.OutputFile) |
| 67 | + assert.Equal(t, []string{"test1", "test2"}, cmd.Tests) |
| 68 | +} |
| 69 | + |
| 70 | +func TestSkipsWhenTestSelectionNotAllowed(t *testing.T) { |
| 71 | + ctx, cancel, conf, comm, logger := setupTestEnv(t) |
| 72 | + defer cancel() |
| 73 | + cmd := &testSelectionGet{OutputFile: "test.json"} |
| 74 | + |
| 75 | + // Test selection not allowed in project settings |
| 76 | + conf.ProjectRef.TestSelection.Allowed = utility.FalsePtr() |
| 77 | + require.NoError(t, cmd.Execute(ctx, comm, logger, conf)) |
| 78 | + |
| 79 | + var output TestSelectionOutput |
| 80 | + require.NoError(t, utility.ReadJSONFile(cmd.OutputFile, &output)) |
| 81 | + assert.Empty(t, output.Tests) |
| 82 | + |
| 83 | + // Test selection allowed in project settings but not enabled for task |
| 84 | + conf.ProjectRef.TestSelection.Allowed = utility.TruePtr() |
| 85 | + conf.Task.TestSelectionEnabled = false |
| 86 | + require.NoError(t, cmd.Execute(ctx, comm, logger, conf)) |
| 87 | + |
| 88 | + output = TestSelectionOutput{} |
| 89 | + require.NoError(t, utility.ReadJSONFile(cmd.OutputFile, &output)) |
| 90 | + assert.Empty(t, output.Tests) |
| 91 | +} |
| 92 | + |
| 93 | +func TestCallsAPIWhenEnabled(t *testing.T) { |
| 94 | + ctx, cancel, conf, comm, logger := setupTestEnv(t) |
| 95 | + defer cancel() |
| 96 | + cmd := &testSelectionGet{OutputFile: "test.json", Tests: []string{"test1", "test3"}} |
| 97 | + |
| 98 | + require.NoError(t, cmd.Execute(ctx, comm, logger, conf)) |
| 99 | + |
| 100 | + // Should return the expected tests from the mock API. |
| 101 | + data, err := os.ReadFile(cmd.OutputFile) |
| 102 | + require.NoError(t, err) |
| 103 | + var output TestSelectionOutput |
| 104 | + require.NoError(t, json.Unmarshal(data, &output)) |
| 105 | + require.Len(t, output.Tests, 2) |
| 106 | + assert.Equal(t, "test1", output.Tests[0].Name) |
| 107 | + assert.Equal(t, "test3", output.Tests[1].Name) |
| 108 | + |
| 109 | + // Verify the API was called with correct parameters from TaskConfig |
| 110 | + assert.True(t, comm.SelectTestsCalled) |
| 111 | + assert.Equal(t, conf.Task.Project, comm.SelectTestsRequest.Project) |
| 112 | + assert.Equal(t, conf.Task.Requester, comm.SelectTestsRequest.Requester) |
| 113 | + assert.Equal(t, conf.Task.BuildVariant, comm.SelectTestsRequest.BuildVariant) |
| 114 | + assert.Equal(t, conf.Task.Id, comm.SelectTestsRequest.TaskID) |
| 115 | + assert.Equal(t, conf.Task.DisplayName, comm.SelectTestsRequest.TaskName) |
| 116 | + assert.Equal(t, []string{"test1", "test3"}, comm.SelectTestsRequest.Tests) |
| 117 | +} |
| 118 | + |
| 119 | +func TestHandlesAPIError(t *testing.T) { |
| 120 | + ctx, cancel, conf, comm, logger := setupTestEnv(t) |
| 121 | + defer cancel() |
| 122 | + cmd := &testSelectionGet{OutputFile: "test.json"} |
| 123 | + |
| 124 | + // Mock API error |
| 125 | + comm.SelectTestsError = errors.New("test error") |
| 126 | + |
| 127 | + err := cmd.Execute(ctx, comm, logger, conf) |
| 128 | + require.Error(t, err) |
| 129 | + assert.Contains(t, err.Error(), "test error") |
| 130 | + assert.Contains(t, err.Error(), "calling test selection API") |
| 131 | +} |
0 commit comments