|
| 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 mysql |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "crypto/sha256" |
| 20 | + "database/sql" |
| 21 | + "errors" |
| 22 | + "regexp" |
| 23 | + "strings" |
| 24 | + "testing" |
| 25 | + |
| 26 | + "github.com/DATA-DOG/go-sqlmock" |
| 27 | + "github.com/go-sql-driver/mysql" |
| 28 | + "github.com/stretchr/testify/require" |
| 29 | + |
| 30 | + "github.com/uber/submitqueue/stovepipe/entity" |
| 31 | + "github.com/uber/submitqueue/stovepipe/extension/storage" |
| 32 | +) |
| 33 | + |
| 34 | +func TestBuildIssueStoreCreate(t *testing.T) { |
| 35 | + dbErr := errors.New("database unavailable") |
| 36 | + for _, tt := range []struct { |
| 37 | + name string |
| 38 | + unitID string |
| 39 | + dbErr error |
| 40 | + wantErr error |
| 41 | + }{ |
| 42 | + {name: "unit observation", unitID: "unit"}, |
| 43 | + {name: "task-wide observation"}, |
| 44 | + {name: "long unit identifier", unitID: strings.Repeat("unit/", 1000)}, |
| 45 | + {name: "duplicate observation", unitID: "unit", dbErr: &mysql.MySQLError{Number: mysqlErrDuplicateEntry}, wantErr: storage.ErrAlreadyExists}, |
| 46 | + {name: "dependency error", unitID: "unit", dbErr: dbErr, wantErr: dbErr}, |
| 47 | + } { |
| 48 | + t.Run(tt.name, func(t *testing.T) { |
| 49 | + db, mock, err := sqlmock.New() |
| 50 | + require.NoError(t, err) |
| 51 | + defer db.Close() |
| 52 | + store := NewBuildIssueStore(db, testMetrics(), "queue") |
| 53 | + issue := entity.BuildIssue{TaskID: "task", UnitID: tt.unitID} |
| 54 | + taskHash := sha256.Sum256([]byte(issue.TaskID)) |
| 55 | + unitHash := sha256.Sum256([]byte(issue.UnitID)) |
| 56 | + expectation := mock.ExpectExec(regexp.QuoteMeta(`INSERT INTO build_issue (queue, build_id, task_hash, unit_hash, task_id, unit_id) VALUES (?, ?, ?, ?, ?, ?)`)). |
| 57 | + WithArgs("queue", "build", taskHash[:], unitHash[:], issue.TaskID, issue.UnitID) |
| 58 | + if tt.dbErr != nil { |
| 59 | + expectation.WillReturnError(tt.dbErr) |
| 60 | + } else { |
| 61 | + expectation.WillReturnResult(sqlmock.NewResult(0, 1)) |
| 62 | + } |
| 63 | + |
| 64 | + err = store.Create(context.Background(), "build", issue) |
| 65 | + if tt.wantErr != nil { |
| 66 | + require.ErrorIs(t, err, tt.wantErr) |
| 67 | + } else { |
| 68 | + require.NoError(t, err) |
| 69 | + } |
| 70 | + require.NoError(t, mock.ExpectationsWereMet()) |
| 71 | + }) |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +func TestBuildIssueStoreGet(t *testing.T) { |
| 76 | + dbErr := errors.New("database unavailable") |
| 77 | + for _, tt := range []struct { |
| 78 | + name string |
| 79 | + unitID string |
| 80 | + queryErr error |
| 81 | + scanErr bool |
| 82 | + wantErr error |
| 83 | + }{ |
| 84 | + {name: "unit observation", unitID: "unit"}, |
| 85 | + {name: "task-wide observation"}, |
| 86 | + {name: "missing observation", unitID: "unit", queryErr: sql.ErrNoRows, wantErr: storage.ErrNotFound}, |
| 87 | + {name: "dependency error", unitID: "unit", queryErr: dbErr, wantErr: dbErr}, |
| 88 | + {name: "scan error", unitID: "unit", scanErr: true}, |
| 89 | + } { |
| 90 | + t.Run(tt.name, func(t *testing.T) { |
| 91 | + db, mock, err := sqlmock.New() |
| 92 | + require.NoError(t, err) |
| 93 | + defer db.Close() |
| 94 | + store := NewBuildIssueStore(db, testMetrics(), "queue") |
| 95 | + issue := entity.BuildIssue{TaskID: "task", UnitID: tt.unitID} |
| 96 | + taskHash := sha256.Sum256([]byte(issue.TaskID)) |
| 97 | + unitHash := sha256.Sum256([]byte(issue.UnitID)) |
| 98 | + expectation := mock.ExpectQuery(regexp.QuoteMeta(`SELECT task_id, unit_id FROM build_issue WHERE queue = ? AND build_id = ? AND task_hash = ? AND unit_hash = ?`)). |
| 99 | + WithArgs("queue", "build", taskHash[:], unitHash[:]) |
| 100 | + if tt.queryErr != nil { |
| 101 | + expectation.WillReturnError(tt.queryErr) |
| 102 | + } else if tt.scanErr { |
| 103 | + expectation.WillReturnRows(sqlmock.NewRows([]string{"task_id", "unit_id"}).AddRow(nil, issue.UnitID)) |
| 104 | + } else { |
| 105 | + expectation.WillReturnRows(sqlmock.NewRows([]string{"task_id", "unit_id"}).AddRow(issue.TaskID, issue.UnitID)) |
| 106 | + } |
| 107 | + |
| 108 | + got, err := store.Get(context.Background(), "build", issue.TaskID, issue.UnitID) |
| 109 | + if tt.wantErr != nil { |
| 110 | + require.ErrorIs(t, err, tt.wantErr) |
| 111 | + } else if tt.scanErr { |
| 112 | + require.Error(t, err) |
| 113 | + } else { |
| 114 | + require.NoError(t, err) |
| 115 | + require.Equal(t, issue, got) |
| 116 | + } |
| 117 | + if tt.wantErr != nil || tt.scanErr { |
| 118 | + require.Equal(t, entity.BuildIssue{}, got) |
| 119 | + } |
| 120 | + require.NoError(t, mock.ExpectationsWereMet()) |
| 121 | + }) |
| 122 | + } |
| 123 | +} |
0 commit comments