|
| 1 | +// Copyright 2018 Percona LLC |
| 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 healthcheck |
| 16 | + |
| 17 | +import ( |
| 18 | + "testing" |
| 19 | + |
| 20 | + "github.com/percona/percona-server-mongodb-operator/pkg/psmdb/mongo" |
| 21 | + "github.com/stretchr/testify/require" |
| 22 | +) |
| 23 | + |
| 24 | +func statusWithSelf(state mongo.MemberState, uptime int64, initialSync any) mongo.Status { |
| 25 | + return mongo.Status{ |
| 26 | + MyState: state, |
| 27 | + InitialSyncStatus: initialSync, |
| 28 | + Members: []*mongo.Member{ |
| 29 | + {Id: 0, Name: "other:27017", State: state, Uptime: uptime}, |
| 30 | + {Id: 1, Name: "self:27017", State: state, Uptime: uptime, Self: true}, |
| 31 | + }, |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +func TestCheckStateForLiveness(t *testing.T) { |
| 36 | + tests := []struct { |
| 37 | + name string |
| 38 | + rs mongo.Status |
| 39 | + oplogSize int64 |
| 40 | + wantErr bool |
| 41 | + }{ |
| 42 | + { |
| 43 | + name: "no self member", |
| 44 | + rs: mongo.Status{MyState: mongo.MemberStatePrimary}, |
| 45 | + wantErr: true, |
| 46 | + }, |
| 47 | + { |
| 48 | + name: "primary", |
| 49 | + rs: statusWithSelf(mongo.MemberStatePrimary, 1000, nil), |
| 50 | + }, |
| 51 | + { |
| 52 | + name: "secondary", |
| 53 | + rs: statusWithSelf(mongo.MemberStateSecondary, 1000, nil), |
| 54 | + }, |
| 55 | + { |
| 56 | + name: "arbiter", |
| 57 | + rs: statusWithSelf(mongo.MemberStateArbiter, 1000, nil), |
| 58 | + }, |
| 59 | + { |
| 60 | + name: "startup, no initial sync, uptime within budget", |
| 61 | + rs: statusWithSelf(mongo.MemberStateStartup, 30, nil), |
| 62 | + }, |
| 63 | + { |
| 64 | + name: "startup, no initial sync, uptime exceeds budget", |
| 65 | + rs: statusWithSelf(mongo.MemberStateStartup, 31, nil), |
| 66 | + wantErr: true, |
| 67 | + }, |
| 68 | + { |
| 69 | + name: "startup2, no initial sync, uptime within oplog-scaled budget", |
| 70 | + rs: statusWithSelf(mongo.MemberStateStartup2, 150, nil), |
| 71 | + oplogSize: 2, // budget = 30 + 2*60 = 150 |
| 72 | + }, |
| 73 | + { |
| 74 | + name: "startup2, no initial sync, uptime exceeds oplog-scaled budget", |
| 75 | + rs: statusWithSelf(mongo.MemberStateStartup2, 151, nil), |
| 76 | + oplogSize: 2, |
| 77 | + wantErr: true, |
| 78 | + }, |
| 79 | + { |
| 80 | + name: "startup with initial sync ignores uptime", |
| 81 | + rs: statusWithSelf(mongo.MemberStateStartup, 99999, map[string]any{"syncSourceHost": "src:27017"}), |
| 82 | + }, |
| 83 | + { |
| 84 | + name: "startup2 with initial sync ignores uptime", |
| 85 | + rs: statusWithSelf(mongo.MemberStateStartup2, 99999, map[string]any{"syncSourceHost": "src:27017"}), |
| 86 | + }, |
| 87 | + { |
| 88 | + name: "recovering does not error", |
| 89 | + rs: statusWithSelf(mongo.MemberStateRecovering, 99999, nil), |
| 90 | + }, |
| 91 | + { |
| 92 | + name: "rollback does not error", |
| 93 | + rs: statusWithSelf(mongo.MemberStateRollback, 99999, nil), |
| 94 | + }, |
| 95 | + { |
| 96 | + name: "unknown state", |
| 97 | + rs: statusWithSelf(mongo.MemberStateUnknown, 1, nil), |
| 98 | + wantErr: true, |
| 99 | + }, |
| 100 | + { |
| 101 | + name: "down state", |
| 102 | + rs: statusWithSelf(mongo.MemberStateDown, 1, nil), |
| 103 | + wantErr: true, |
| 104 | + }, |
| 105 | + { |
| 106 | + name: "removed state", |
| 107 | + rs: statusWithSelf(mongo.MemberStateRemoved, 1, nil), |
| 108 | + wantErr: true, |
| 109 | + }, |
| 110 | + { |
| 111 | + name: "out-of-range state", |
| 112 | + rs: statusWithSelf(mongo.MemberState(99), 1, nil), |
| 113 | + wantErr: true, |
| 114 | + }, |
| 115 | + } |
| 116 | + |
| 117 | + for _, tt := range tests { |
| 118 | + t.Run(tt.name, func(t *testing.T) { |
| 119 | + err := CheckStateForLiveness(t.Context(), tt.rs, tt.oplogSize) |
| 120 | + if tt.wantErr { |
| 121 | + require.Error(t, err) |
| 122 | + } else { |
| 123 | + require.NoError(t, err) |
| 124 | + } |
| 125 | + }) |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +func TestCheckStateForReadiness(t *testing.T) { |
| 130 | + tests := []struct { |
| 131 | + name string |
| 132 | + rs mongo.Status |
| 133 | + wantErr bool |
| 134 | + }{ |
| 135 | + { |
| 136 | + name: "no self member", |
| 137 | + rs: mongo.Status{MyState: mongo.MemberStatePrimary}, |
| 138 | + wantErr: true, |
| 139 | + }, |
| 140 | + { |
| 141 | + name: "primary is ready", |
| 142 | + rs: statusWithSelf(mongo.MemberStatePrimary, 100, nil), |
| 143 | + }, |
| 144 | + { |
| 145 | + name: "secondary is ready", |
| 146 | + rs: statusWithSelf(mongo.MemberStateSecondary, 100, nil), |
| 147 | + }, |
| 148 | + { |
| 149 | + name: "arbiter is ready", |
| 150 | + rs: statusWithSelf(mongo.MemberStateArbiter, 100, nil), |
| 151 | + }, |
| 152 | + { |
| 153 | + name: "startup is not ready", |
| 154 | + rs: statusWithSelf(mongo.MemberStateStartup, 1, nil), |
| 155 | + wantErr: true, |
| 156 | + }, |
| 157 | + { |
| 158 | + name: "startup2 is not ready", |
| 159 | + rs: statusWithSelf(mongo.MemberStateStartup2, 1, nil), |
| 160 | + wantErr: true, |
| 161 | + }, |
| 162 | + { |
| 163 | + name: "recovering is not ready", |
| 164 | + rs: statusWithSelf(mongo.MemberStateRecovering, 1, nil), |
| 165 | + wantErr: true, |
| 166 | + }, |
| 167 | + { |
| 168 | + name: "rollback is not ready", |
| 169 | + rs: statusWithSelf(mongo.MemberStateRollback, 1, nil), |
| 170 | + wantErr: true, |
| 171 | + }, |
| 172 | + { |
| 173 | + name: "unknown state", |
| 174 | + rs: statusWithSelf(mongo.MemberStateUnknown, 1, nil), |
| 175 | + wantErr: true, |
| 176 | + }, |
| 177 | + { |
| 178 | + name: "down state", |
| 179 | + rs: statusWithSelf(mongo.MemberStateDown, 1, nil), |
| 180 | + wantErr: true, |
| 181 | + }, |
| 182 | + { |
| 183 | + name: "removed state", |
| 184 | + rs: statusWithSelf(mongo.MemberStateRemoved, 1, nil), |
| 185 | + wantErr: true, |
| 186 | + }, |
| 187 | + { |
| 188 | + name: "out-of-range state", |
| 189 | + rs: statusWithSelf(mongo.MemberState(99), 1, nil), |
| 190 | + wantErr: true, |
| 191 | + }, |
| 192 | + } |
| 193 | + |
| 194 | + for _, tt := range tests { |
| 195 | + t.Run(tt.name, func(t *testing.T) { |
| 196 | + err := CheckStateForReadiness(tt.rs) |
| 197 | + if tt.wantErr { |
| 198 | + require.Error(t, err) |
| 199 | + } else { |
| 200 | + require.NoError(t, err) |
| 201 | + } |
| 202 | + }) |
| 203 | + } |
| 204 | +} |
0 commit comments