|
| 1 | +// Copyright 2026 The NativeLink Authors. All rights reserved. |
| 2 | +// |
| 3 | +// Licensed under the Functional Source License, Version 1.1, Apache 2.0 Future License (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 | +// See LICENSE file for details |
| 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 | +use core::fmt::Write; |
| 16 | +use core::sync::atomic::{AtomicBool, Ordering}; |
| 17 | + |
| 18 | +use either::Either; |
| 19 | +use nativelink_util::background_spawn; |
| 20 | +use redis::Value; |
| 21 | +use redis_protocol::resp2::decode::decode; |
| 22 | +use redis_protocol::resp2::types::OwnedFrame; |
| 23 | +use tokio::net::TcpListener; |
| 24 | +use tracing::info; |
| 25 | + |
| 26 | +use crate::fake_redis::{arg_as_string, fake_redis_internal}; |
| 27 | + |
| 28 | +const FAKE_SCRIPT_SHA: &str = "b22b9926cbce9dd9ba97fa7ba3626f89feea1ed5"; |
| 29 | + |
| 30 | +// The first time we hit SETRANGE, we output a ReadOnly |
| 31 | +static SETRANGE_TRIGGERED: AtomicBool = AtomicBool::new(false); |
| 32 | + |
| 33 | +async fn dynamic_fake_redis(listener: TcpListener) { |
| 34 | + let readonly_err_str = "READONLY You can't write against a read only replica."; |
| 35 | + let readonly_err = format!("!{}\r\n{readonly_err_str}\r\n", readonly_err_str.len()); |
| 36 | + |
| 37 | + let inner = move |buf: &[u8]| -> String { |
| 38 | + let mut output = String::new(); |
| 39 | + let mut buf_index = 0; |
| 40 | + loop { |
| 41 | + let frame = match decode(&buf[buf_index..]).unwrap() { |
| 42 | + Some((frame, amt)) => { |
| 43 | + buf_index += amt; |
| 44 | + frame |
| 45 | + } |
| 46 | + None => { |
| 47 | + panic!("No frame!"); |
| 48 | + } |
| 49 | + }; |
| 50 | + let (cmd, args) = { |
| 51 | + if let OwnedFrame::Array(a) = frame { |
| 52 | + if let OwnedFrame::BulkString(s) = a.first().unwrap() { |
| 53 | + let args: Vec<_> = a[1..].to_vec(); |
| 54 | + (str::from_utf8(s).unwrap().to_string(), args) |
| 55 | + } else { |
| 56 | + panic!("Array not starting with cmd: {a:?}"); |
| 57 | + } |
| 58 | + } else { |
| 59 | + panic!("Non array cmd: {frame:?}"); |
| 60 | + } |
| 61 | + }; |
| 62 | + |
| 63 | + let ret: Either<Value, String> = match cmd.as_str() { |
| 64 | + "HELLO" => Either::Left(Value::Map(vec![( |
| 65 | + Value::SimpleString("server".into()), |
| 66 | + Value::SimpleString("redis".into()), |
| 67 | + )])), |
| 68 | + "CLIENT" => { |
| 69 | + // We can safely ignore these, as it's just setting the library name/version |
| 70 | + Either::Left(Value::Int(0)) |
| 71 | + } |
| 72 | + "SCRIPT" => { |
| 73 | + assert_eq!(args[0], OwnedFrame::BulkString(b"LOAD".to_vec())); |
| 74 | + |
| 75 | + let OwnedFrame::BulkString(ref _script) = args[1] else { |
| 76 | + panic!("Script should be a bulkstring: {args:?}"); |
| 77 | + }; |
| 78 | + Either::Left(Value::SimpleString(FAKE_SCRIPT_SHA.to_string())) |
| 79 | + } |
| 80 | + "ROLE" => Either::Left(Value::Array(vec![ |
| 81 | + Value::BulkString(b"master".to_vec()), |
| 82 | + Value::Int(0), |
| 83 | + Value::Array(vec![]), |
| 84 | + ])), |
| 85 | + "SETRANGE" => { |
| 86 | + let value = SETRANGE_TRIGGERED.load(Ordering::Relaxed); |
| 87 | + if value { |
| 88 | + Either::Left(Value::Int(5)) |
| 89 | + } else { |
| 90 | + SETRANGE_TRIGGERED.store(true, Ordering::Relaxed); |
| 91 | + Either::Right(readonly_err.clone()) |
| 92 | + } |
| 93 | + } |
| 94 | + "STRLEN" => Either::Left(Value::Int(5)), |
| 95 | + "RENAME" => { |
| 96 | + let value = SETRANGE_TRIGGERED.load(Ordering::Relaxed); |
| 97 | + if value { |
| 98 | + Either::Left(Value::Okay) |
| 99 | + } else { |
| 100 | + Either::Right(readonly_err.clone()) |
| 101 | + } |
| 102 | + } |
| 103 | + actual => { |
| 104 | + panic!("Mock command not implemented! {actual:?}"); |
| 105 | + } |
| 106 | + }; |
| 107 | + |
| 108 | + match ret { |
| 109 | + Either::Left(v) => { |
| 110 | + arg_as_string(&mut output, v); |
| 111 | + } |
| 112 | + Either::Right(s) => { |
| 113 | + write!(&mut output, "{s}").unwrap(); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + if buf_index == buf.len() { |
| 118 | + break; |
| 119 | + } |
| 120 | + } |
| 121 | + output |
| 122 | + }; |
| 123 | + fake_redis_internal(listener, vec![inner]).await; |
| 124 | +} |
| 125 | + |
| 126 | +pub async fn run() -> u16 { |
| 127 | + let listener = TcpListener::bind("127.0.0.1:0").await.unwrap(); |
| 128 | + let port = listener.local_addr().unwrap().port(); |
| 129 | + info!("Using port {port}"); |
| 130 | + |
| 131 | + background_spawn!("listener", async move { |
| 132 | + dynamic_fake_redis(listener).await; |
| 133 | + }); |
| 134 | + |
| 135 | + port |
| 136 | +} |
0 commit comments