|
| 1 | +// Copyright (c) 2026 Uber Technologies, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package handler |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "errors" |
| 20 | + "testing" |
| 21 | + |
| 22 | + "github.com/stretchr/testify/assert" |
| 23 | + "github.com/stretchr/testify/require" |
| 24 | + pb "github.com/uber/submitqueue/api/stovepipe/protopb" |
| 25 | + "github.com/uber/submitqueue/stovepipe/controller" |
| 26 | + "github.com/uber/submitqueue/stovepipe/entity" |
| 27 | +) |
| 28 | + |
| 29 | +type fakeRequestHistoryController struct { |
| 30 | + getByID func(context.Context, entity.GetRequestHistoryByIDRequest) ([]entity.RequestLog, error) |
| 31 | + getByURI func(context.Context, entity.GetRequestHistoryByURIRequest) ([]entity.RequestHistory, error) |
| 32 | +} |
| 33 | + |
| 34 | +var _ controller.RequestHistoryController = (*fakeRequestHistoryController)(nil) |
| 35 | + |
| 36 | +func (f *fakeRequestHistoryController) GetRequestHistoryByID(ctx context.Context, req entity.GetRequestHistoryByIDRequest) ([]entity.RequestLog, error) { |
| 37 | + return f.getByID(ctx, req) |
| 38 | +} |
| 39 | + |
| 40 | +func (f *fakeRequestHistoryController) GetRequestHistoryByURI(ctx context.Context, req entity.GetRequestHistoryByURIRequest) ([]entity.RequestHistory, error) { |
| 41 | + return f.getByURI(ctx, req) |
| 42 | +} |
| 43 | + |
| 44 | +func TestGetRequestHistoryByID(t *testing.T) { |
| 45 | + controllerErr := errors.New("controller failed") |
| 46 | + logs := []entity.RequestLog{ |
| 47 | + {ID: "occurrence/1", State: entity.RequestStateAccepted, TimestampMs: 1000}, |
| 48 | + {ID: "occurrence/2", Event: entity.RequestEventBuildTriggered, TimestampMs: 2000}, |
| 49 | + } |
| 50 | + tests := []struct { |
| 51 | + name string |
| 52 | + logs []entity.RequestLog |
| 53 | + err error |
| 54 | + wantLogs int |
| 55 | + }{ |
| 56 | + {name: "maps successful result", logs: logs, wantLogs: 2}, |
| 57 | + {name: "maps empty result", logs: nil, wantLogs: 0}, |
| 58 | + {name: "returns controller error unchanged", err: controllerErr}, |
| 59 | + } |
| 60 | + |
| 61 | + for _, tt := range tests { |
| 62 | + t.Run(tt.name, func(t *testing.T) { |
| 63 | + var gotReq entity.GetRequestHistoryByIDRequest |
| 64 | + fake := &fakeRequestHistoryController{ |
| 65 | + getByID: func(_ context.Context, req entity.GetRequestHistoryByIDRequest) ([]entity.RequestLog, error) { |
| 66 | + gotReq = req |
| 67 | + return tt.logs, tt.err |
| 68 | + }, |
| 69 | + } |
| 70 | + srv := &StovepipeServer{requestHistoryController: fake} |
| 71 | + |
| 72 | + resp, err := srv.GetRequestHistoryByID(context.Background(), &pb.GetRequestHistoryByIDRequest{ |
| 73 | + Queue: "monorepo/main", RequestId: "request/1", |
| 74 | + }) |
| 75 | + |
| 76 | + assert.Equal(t, entity.GetRequestHistoryByIDRequest{Queue: "monorepo/main", ID: "request/1"}, gotReq) |
| 77 | + if tt.err != nil { |
| 78 | + require.ErrorIs(t, err, tt.err) |
| 79 | + assert.Nil(t, resp) |
| 80 | + return |
| 81 | + } |
| 82 | + require.NoError(t, err) |
| 83 | + require.Len(t, resp.Events, tt.wantLogs) |
| 84 | + if tt.wantLogs > 0 { |
| 85 | + assert.Equal(t, "accepted", resp.Events[0].GetRequestState()) |
| 86 | + assert.Equal(t, "build_triggered", resp.Events[1].GetEvent()) |
| 87 | + } |
| 88 | + }) |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +func TestGetRequestHistoryByURI(t *testing.T) { |
| 93 | + controllerErr := errors.New("controller failed") |
| 94 | + histories := []entity.RequestHistory{{ |
| 95 | + RequestID: "request/1", |
| 96 | + Events: []entity.RequestLog{{ID: "occurrence/1", State: entity.RequestStateAccepted}}, |
| 97 | + }} |
| 98 | + tests := []struct { |
| 99 | + name string |
| 100 | + histories []entity.RequestHistory |
| 101 | + err error |
| 102 | + wantHistories int |
| 103 | + }{ |
| 104 | + {name: "maps successful result", histories: histories, wantHistories: 1}, |
| 105 | + {name: "maps empty result", histories: nil, wantHistories: 0}, |
| 106 | + {name: "returns controller error unchanged", err: controllerErr}, |
| 107 | + } |
| 108 | + |
| 109 | + for _, tt := range tests { |
| 110 | + t.Run(tt.name, func(t *testing.T) { |
| 111 | + var gotReq entity.GetRequestHistoryByURIRequest |
| 112 | + fake := &fakeRequestHistoryController{ |
| 113 | + getByURI: func(_ context.Context, req entity.GetRequestHistoryByURIRequest) ([]entity.RequestHistory, error) { |
| 114 | + gotReq = req |
| 115 | + return tt.histories, tt.err |
| 116 | + }, |
| 117 | + } |
| 118 | + srv := &StovepipeServer{requestHistoryController: fake} |
| 119 | + |
| 120 | + resp, err := srv.GetRequestHistoryByURI(context.Background(), &pb.GetRequestHistoryByURIRequest{ |
| 121 | + Queue: "monorepo/main", Uri: "git://monorepo/abc", |
| 122 | + }) |
| 123 | + |
| 124 | + assert.Equal(t, entity.GetRequestHistoryByURIRequest{Queue: "monorepo/main", URI: "git://monorepo/abc"}, gotReq) |
| 125 | + if tt.err != nil { |
| 126 | + require.ErrorIs(t, err, tt.err) |
| 127 | + assert.Nil(t, resp) |
| 128 | + return |
| 129 | + } |
| 130 | + require.NoError(t, err) |
| 131 | + require.Len(t, resp.Histories, tt.wantHistories) |
| 132 | + if tt.wantHistories > 0 { |
| 133 | + assert.Equal(t, "request/1", resp.Histories[0].RequestId) |
| 134 | + require.Len(t, resp.Histories[0].Events, 1) |
| 135 | + assert.Equal(t, "accepted", resp.Histories[0].Events[0].GetRequestState()) |
| 136 | + } |
| 137 | + }) |
| 138 | + } |
| 139 | +} |
0 commit comments