|
| 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 file |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "os" |
| 20 | + "path/filepath" |
| 21 | + "testing" |
| 22 | + |
| 23 | + "github.com/stretchr/testify/assert" |
| 24 | + "github.com/stretchr/testify/require" |
| 25 | + "github.com/uber/submitqueue/submitqueue/entity" |
| 26 | +) |
| 27 | + |
| 28 | +func writeFile(t *testing.T, contents string) string { |
| 29 | + t.Helper() |
| 30 | + path := filepath.Join(t.TempDir(), "pause.yaml") |
| 31 | + require.NoError(t, os.WriteFile(path, []byte(contents), 0o600)) |
| 32 | + return path |
| 33 | +} |
| 34 | + |
| 35 | +func TestGet(t *testing.T) { |
| 36 | + const contents = ` |
| 37 | +queues: |
| 38 | + - name: rejecting |
| 39 | + reject_lands: true |
| 40 | + - name: freezing |
| 41 | + freeze_processing: true |
| 42 | + - name: both |
| 43 | + reject_lands: true |
| 44 | + freeze_processing: true |
| 45 | + - name: listed-but-running |
| 46 | +` |
| 47 | + tests := []struct { |
| 48 | + name string |
| 49 | + queue string |
| 50 | + want entity.QueuePauseState |
| 51 | + }{ |
| 52 | + {name: "reject only", queue: "rejecting", want: entity.QueuePauseState{RejectLands: true}}, |
| 53 | + {name: "freeze only", queue: "freezing", want: entity.QueuePauseState{FreezeProcessing: true}}, |
| 54 | + {name: "both switches", queue: "both", want: entity.QueuePauseState{RejectLands: true, FreezeProcessing: true}}, |
| 55 | + {name: "listed with no switches set", queue: "listed-but-running", want: entity.QueuePauseState{}}, |
| 56 | + {name: "unlisted queue is not paused", queue: "other", want: entity.QueuePauseState{}}, |
| 57 | + } |
| 58 | + store, err := NewStore(writeFile(t, contents)) |
| 59 | + require.NoError(t, err) |
| 60 | + |
| 61 | + for _, tt := range tests { |
| 62 | + t.Run(tt.name, func(t *testing.T) { |
| 63 | + got, err := store.Get(context.Background(), tt.queue) |
| 64 | + require.NoError(t, err) |
| 65 | + assert.Equal(t, tt.want, got) |
| 66 | + }) |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +func TestNewStoreEmptyFile(t *testing.T) { |
| 71 | + store, err := NewStore(writeFile(t, "")) |
| 72 | + require.NoError(t, err) |
| 73 | + |
| 74 | + got, err := store.Get(context.Background(), "any") |
| 75 | + require.NoError(t, err) |
| 76 | + assert.Equal(t, entity.QueuePauseState{}, got) |
| 77 | +} |
| 78 | + |
| 79 | +func TestNewStoreRejectsInvalidFile(t *testing.T) { |
| 80 | + tests := []struct { |
| 81 | + name string |
| 82 | + contents string |
| 83 | + }{ |
| 84 | + {name: "malformed yaml", contents: "queues: [unterminated"}, |
| 85 | + {name: "empty queue name", contents: "queues:\n - reject_lands: true\n"}, |
| 86 | + {name: "duplicate queue name", contents: "queues:\n - name: a\n - name: a\n"}, |
| 87 | + {name: "misspelled switch", contents: "queues:\n - name: a\n reject_land: true\n"}, |
| 88 | + } |
| 89 | + for _, tt := range tests { |
| 90 | + t.Run(tt.name, func(t *testing.T) { |
| 91 | + _, err := NewStore(writeFile(t, tt.contents)) |
| 92 | + require.Error(t, err) |
| 93 | + }) |
| 94 | + } |
| 95 | + |
| 96 | + t.Run("missing file", func(t *testing.T) { |
| 97 | + _, err := NewStore(filepath.Join(t.TempDir(), "absent.yaml")) |
| 98 | + require.Error(t, err) |
| 99 | + }) |
| 100 | +} |
| 101 | + |
| 102 | +func TestGetReflectsFileChanges(t *testing.T) { |
| 103 | + path := writeFile(t, "queues:\n - name: q\n") |
| 104 | + store, err := NewStore(path) |
| 105 | + require.NoError(t, err) |
| 106 | + |
| 107 | + got, err := store.Get(context.Background(), "q") |
| 108 | + require.NoError(t, err) |
| 109 | + assert.Equal(t, entity.QueuePauseState{}, got) |
| 110 | + |
| 111 | + require.NoError(t, os.WriteFile(path, []byte("queues:\n - name: q\n reject_lands: true\n"), 0o600)) |
| 112 | + got, err = store.Get(context.Background(), "q") |
| 113 | + require.NoError(t, err) |
| 114 | + assert.Equal(t, entity.QueuePauseState{RejectLands: true}, got) |
| 115 | +} |
| 116 | + |
| 117 | +func TestGetFailsWhenFileBecomesUnreadable(t *testing.T) { |
| 118 | + tests := []struct { |
| 119 | + name string |
| 120 | + mutate func(t *testing.T, path string) |
| 121 | + }{ |
| 122 | + {name: "file removed", mutate: func(t *testing.T, path string) { require.NoError(t, os.Remove(path)) }}, |
| 123 | + {name: "file corrupted", mutate: func(t *testing.T, path string) { |
| 124 | + require.NoError(t, os.WriteFile(path, []byte("queues: [unterminated"), 0o600)) |
| 125 | + }}, |
| 126 | + } |
| 127 | + for _, tt := range tests { |
| 128 | + t.Run(tt.name, func(t *testing.T) { |
| 129 | + path := writeFile(t, "queues:\n - name: q\n reject_lands: true\n") |
| 130 | + store, err := NewStore(path) |
| 131 | + require.NoError(t, err) |
| 132 | + |
| 133 | + tt.mutate(t, path) |
| 134 | + |
| 135 | + _, err = store.Get(context.Background(), "q") |
| 136 | + require.Error(t, err) |
| 137 | + }) |
| 138 | + } |
| 139 | +} |
0 commit comments