|
| 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 | + "database/sql" |
| 20 | + "errors" |
| 21 | + "testing" |
| 22 | + |
| 23 | + "github.com/DATA-DOG/go-sqlmock" |
| 24 | + driver "github.com/go-sql-driver/mysql" |
| 25 | + "github.com/stretchr/testify/require" |
| 26 | + "github.com/uber/submitqueue/stovepipe/entity" |
| 27 | + "github.com/uber/submitqueue/stovepipe/extension/storage" |
| 28 | +) |
| 29 | + |
| 30 | +func TestFixStoresErrors(t *testing.T) { |
| 31 | + ctx := context.Background() |
| 32 | + dbError := errors.New("database unavailable") |
| 33 | + for _, scenario := range []string{"create duplicate", "create error", "get missing", "get error", "update error", "update stale", "rows affected error"} { |
| 34 | + t.Run(scenario, func(t *testing.T) { |
| 35 | + db, mock, err := sqlmock.New() |
| 36 | + require.NoError(t, err) |
| 37 | + defer db.Close() |
| 38 | + backend, err := NewStorage(db, testMetrics()) |
| 39 | + require.NoError(t, err) |
| 40 | + bound, err := backend.For("queue") |
| 41 | + require.NoError(t, err) |
| 42 | + store := bound.GetFixStore() |
| 43 | + fix := entity.Fix{ID: "fix", Queue: "queue"} |
| 44 | + var want error |
| 45 | + switch scenario { |
| 46 | + case "create duplicate": |
| 47 | + mock.ExpectExec("INSERT INTO fix").WillReturnError(&driver.MySQLError{Number: mysqlErrDuplicateEntry}) |
| 48 | + want = storage.ErrAlreadyExists |
| 49 | + err = store.Create(ctx, fix) |
| 50 | + case "create error": |
| 51 | + mock.ExpectExec("INSERT INTO fix").WillReturnError(dbError) |
| 52 | + want = dbError |
| 53 | + err = store.Create(ctx, fix) |
| 54 | + case "get missing": |
| 55 | + mock.ExpectQuery("SELECT").WillReturnError(sql.ErrNoRows) |
| 56 | + want = storage.ErrNotFound |
| 57 | + _, err = store.Get(ctx, fix.ID) |
| 58 | + case "get error": |
| 59 | + mock.ExpectQuery("SELECT").WillReturnError(dbError) |
| 60 | + want = dbError |
| 61 | + _, err = store.Get(ctx, fix.ID) |
| 62 | + case "update error": |
| 63 | + mock.ExpectExec("UPDATE fix").WillReturnError(dbError) |
| 64 | + want = dbError |
| 65 | + err = store.Update(ctx, fix, 1, 2) |
| 66 | + case "update stale": |
| 67 | + mock.ExpectExec("UPDATE fix").WillReturnResult(sqlmock.NewResult(0, 0)) |
| 68 | + want = storage.ErrVersionMismatch |
| 69 | + err = store.Update(ctx, fix, 1, 2) |
| 70 | + case "rows affected error": |
| 71 | + mock.ExpectExec("UPDATE fix").WillReturnResult(sqlmock.NewErrorResult(dbError)) |
| 72 | + want = dbError |
| 73 | + err = store.Update(ctx, fix, 1, 2) |
| 74 | + } |
| 75 | + require.ErrorIs(t, err, want) |
| 76 | + require.NoError(t, mock.ExpectationsWereMet()) |
| 77 | + }) |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +func TestFixStoresRejectInvalidWrites(t *testing.T) { |
| 82 | + ctx := context.Background() |
| 83 | + for _, operation := range []string{"create", "update"} { |
| 84 | + t.Run(operation, func(t *testing.T) { |
| 85 | + db, mock, err := sqlmock.New() |
| 86 | + require.NoError(t, err) |
| 87 | + defer db.Close() |
| 88 | + fixStore := NewFixStore(db, testMetrics(), "queue") |
| 89 | + for _, fix := range []entity.Fix{{Queue: "queue"}, {ID: "fix", Queue: "other"}} { |
| 90 | + if operation == "create" { |
| 91 | + err = fixStore.Create(ctx, fix) |
| 92 | + } else { |
| 93 | + err = fixStore.Update(ctx, fix, 1, 2) |
| 94 | + } |
| 95 | + require.Error(t, err) |
| 96 | + } |
| 97 | + require.NoError(t, mock.ExpectationsWereMet()) |
| 98 | + }) |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +func TestFixStoresRejectCorruptCollections(t *testing.T) { |
| 103 | + ctx := context.Background() |
| 104 | + for _, tt := range []struct{ name, breakages, metadata string }{ |
| 105 | + {"repair episodes", `{}`, `{}`}, |
| 106 | + {"repair metadata", `[]`, `[]`}, |
| 107 | + } { |
| 108 | + t.Run(tt.name, func(t *testing.T) { |
| 109 | + db, mock, err := sqlmock.New() |
| 110 | + require.NoError(t, err) |
| 111 | + defer db.Close() |
| 112 | + mock.ExpectQuery("SELECT").WillReturnRows(sqlmock.NewRows([]string{"id", "state", "breakages", "execution_ref", "metadata", "created_at", "updated_at", "version"}). |
| 113 | + AddRow("fix", "queued", tt.breakages, "", tt.metadata, 1, 1, 1)) |
| 114 | + _, err = NewFixStore(db, testMetrics(), "queue").Get(ctx, "fix") |
| 115 | + require.Error(t, err) |
| 116 | + require.NoError(t, mock.ExpectationsWereMet()) |
| 117 | + }) |
| 118 | + } |
| 119 | +} |
0 commit comments