|
| 1 | +use crate::opamp::instance_id::storer::{InstanceIDStorer, StorerError}; |
| 2 | +use crate::opamp::instance_id::{Identifiers, Storer}; |
| 3 | +use serde::{Deserialize, Serialize}; |
| 4 | +use std::fmt::{Display, Formatter}; |
| 5 | +use tracing::debug; |
| 6 | +use ulid::Ulid; |
| 7 | + |
| 8 | +#[derive(thiserror::Error, Debug)] |
| 9 | +pub enum GetterError { |
| 10 | + #[error("failed to persist Data: `{0}`")] |
| 11 | + Persisting(#[from] StorerError), |
| 12 | +} |
| 13 | + |
| 14 | +// InstanceID holds the to_string of Ulid assigned to a Agent |
| 15 | +#[derive(Default, Debug, Deserialize, Serialize, PartialEq, Clone)] |
| 16 | +pub struct InstanceID(String); |
| 17 | + |
| 18 | +impl InstanceID { |
| 19 | + pub(crate) fn new(id: String) -> InstanceID { |
| 20 | + InstanceID(id) |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +impl Display for InstanceID { |
| 25 | + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { |
| 26 | + write!(f, "{}", self.0) |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +// InstanceIDGetter returns an InstanceID for a specific agentID. |
| 31 | +pub trait InstanceIDGetter { |
| 32 | + fn get(&self, agent_id: &str) -> Result<InstanceID, GetterError>; |
| 33 | +} |
| 34 | + |
| 35 | +pub struct ULIDInstanceIDGetter<S> |
| 36 | +where |
| 37 | + S: InstanceIDStorer, |
| 38 | +{ |
| 39 | + storer: S, |
| 40 | + identifiers: Identifiers, |
| 41 | +} |
| 42 | + |
| 43 | +impl<S> ULIDInstanceIDGetter<S> |
| 44 | +where |
| 45 | + S: InstanceIDStorer, |
| 46 | +{ |
| 47 | + pub fn new(storer: S, identifiers: Identifiers) -> Self { |
| 48 | + Self { |
| 49 | + storer, |
| 50 | + identifiers, |
| 51 | + } |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +impl<S> InstanceIDGetter for ULIDInstanceIDGetter<S> |
| 56 | +where |
| 57 | + S: InstanceIDStorer, |
| 58 | +{ |
| 59 | + fn get(&self, agent_id: &str) -> Result<InstanceID, GetterError> { |
| 60 | + debug!("retrieving ulid"); |
| 61 | + let data = self.storer.get(agent_id)?; |
| 62 | + |
| 63 | + match data { |
| 64 | + None => { |
| 65 | + debug!("storer returned no data"); |
| 66 | + } |
| 67 | + Some(d) if d.identifiers == self.identifiers => return Ok(d.ulid), |
| 68 | + Some(d) => debug!( |
| 69 | + "stored data had different identifiers {:?}!={:?}", |
| 70 | + d.identifiers, self.identifiers |
| 71 | + ), |
| 72 | + } |
| 73 | + |
| 74 | + let new_data = DataStored { |
| 75 | + ulid: InstanceID(Ulid::new().to_string()), |
| 76 | + identifiers: self.identifiers.clone(), |
| 77 | + }; |
| 78 | + |
| 79 | + debug!("persisting ulid {}", new_data.ulid); |
| 80 | + self.storer.set(agent_id, &new_data)?; |
| 81 | + |
| 82 | + Ok(new_data.ulid) |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +impl Default for ULIDInstanceIDGetter<Storer> { |
| 87 | + fn default() -> Self { |
| 88 | + Self::new(Storer {}, Identifiers::default()) |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +#[derive(Deserialize, Serialize)] |
| 93 | +pub struct DataStored { |
| 94 | + pub ulid: InstanceID, |
| 95 | + pub identifiers: Identifiers, |
| 96 | +} |
| 97 | + |
| 98 | +#[cfg(test)] |
| 99 | +pub mod test { |
| 100 | + use super::*; |
| 101 | + use crate::opamp::instance_id::getter::{DataStored, InstanceIDGetter, ULIDInstanceIDGetter}; |
| 102 | + use crate::opamp::instance_id::storer::test::MockInstanceIDStorerMock; |
| 103 | + use crate::opamp::instance_id::storer::StorerError; |
| 104 | + use mockall::{mock, predicate}; |
| 105 | + |
| 106 | + mock! { |
| 107 | + pub InstanceIDGetterMock {} |
| 108 | + |
| 109 | + impl InstanceIDGetter for InstanceIDGetterMock { |
| 110 | + fn get(&self, agent_id: &str) -> Result<InstanceID, GetterError>; |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + impl MockInstanceIDGetterMock { |
| 115 | + pub fn should_get(&mut self, agent_id: String, ulid: String) { |
| 116 | + self.expect_get() |
| 117 | + .once() |
| 118 | + .with(predicate::eq(agent_id.clone())) |
| 119 | + .returning(move |_| Ok(InstanceID(ulid.clone()))); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + const AGENT_NAME: &str = "agent1"; |
| 124 | + |
| 125 | + #[test] |
| 126 | + fn test_not_found() { |
| 127 | + let mut mock = MockInstanceIDStorerMock::new(); |
| 128 | + |
| 129 | + mock.expect_get() |
| 130 | + .once() |
| 131 | + .with(predicate::eq(AGENT_NAME)) |
| 132 | + .returning(|_| Ok(None)); |
| 133 | + mock.expect_set() |
| 134 | + .once() |
| 135 | + .with(predicate::eq(AGENT_NAME), predicate::always()) |
| 136 | + .returning(|_, _| Ok(())); |
| 137 | + let getter = ULIDInstanceIDGetter::new(mock, Identifiers::default()); |
| 138 | + let res = getter.get(AGENT_NAME); |
| 139 | + |
| 140 | + assert!(res.is_ok()); |
| 141 | + } |
| 142 | + |
| 143 | + #[test] |
| 144 | + fn test_error_get() { |
| 145 | + let mut mock = MockInstanceIDStorerMock::new(); |
| 146 | + |
| 147 | + mock.expect_get() |
| 148 | + .once() |
| 149 | + .with(predicate::eq(AGENT_NAME)) |
| 150 | + .returning(|_| Err(StorerError::Generic)); |
| 151 | + let getter = ULIDInstanceIDGetter::new(mock, Identifiers::default()); |
| 152 | + let res = getter.get(AGENT_NAME); |
| 153 | + |
| 154 | + assert!(res.is_err()); |
| 155 | + } |
| 156 | + |
| 157 | + #[test] |
| 158 | + fn test_error_set() { |
| 159 | + let mut mock = MockInstanceIDStorerMock::new(); |
| 160 | + |
| 161 | + mock.expect_get() |
| 162 | + .once() |
| 163 | + .with(predicate::eq(AGENT_NAME)) |
| 164 | + .returning(|_| Ok(None)); |
| 165 | + mock.expect_set() |
| 166 | + .once() |
| 167 | + .with(predicate::eq(AGENT_NAME), predicate::always()) |
| 168 | + .returning(|_, _| Err(StorerError::Generic)); |
| 169 | + |
| 170 | + let getter = ULIDInstanceIDGetter::new(mock, Identifiers::default()); |
| 171 | + let res = getter.get(AGENT_NAME); |
| 172 | + |
| 173 | + assert!(res.is_err()); |
| 174 | + } |
| 175 | + |
| 176 | + #[test] |
| 177 | + fn test_ulid_already_present() { |
| 178 | + let mut mock = MockInstanceIDStorerMock::new(); |
| 179 | + let ulid = Ulid::new(); |
| 180 | + |
| 181 | + mock.expect_get() |
| 182 | + .once() |
| 183 | + .with(predicate::eq(AGENT_NAME)) |
| 184 | + .returning(move |_| { |
| 185 | + Ok(Some(DataStored { |
| 186 | + ulid: InstanceID(ulid.to_string()), |
| 187 | + identifiers: Default::default(), |
| 188 | + })) |
| 189 | + }); |
| 190 | + let getter = ULIDInstanceIDGetter::new(mock, Identifiers::default()); |
| 191 | + let res = getter.get(AGENT_NAME); |
| 192 | + |
| 193 | + assert!(res.is_ok()); |
| 194 | + assert_eq!(InstanceID(ulid.to_string()), res.unwrap()); |
| 195 | + } |
| 196 | + |
| 197 | + #[test] |
| 198 | + fn test_ulid_present_but_different_identifiers() { |
| 199 | + let mut mock = MockInstanceIDStorerMock::new(); |
| 200 | + let ulid = ulid::Ulid::new(); |
| 201 | + |
| 202 | + mock.expect_get() |
| 203 | + .once() |
| 204 | + .with(predicate::eq(AGENT_NAME)) |
| 205 | + .returning(move |_| { |
| 206 | + Ok(Some(DataStored { |
| 207 | + ulid: InstanceID(ulid.to_string()), |
| 208 | + identifiers: get_different_identifier(), |
| 209 | + })) |
| 210 | + }); |
| 211 | + mock.expect_set() |
| 212 | + .once() |
| 213 | + .with(predicate::eq(AGENT_NAME), predicate::always()) |
| 214 | + .returning(|_, _| Ok(())); |
| 215 | + let getter = ULIDInstanceIDGetter::new(mock, Identifiers::default()); |
| 216 | + let res = getter.get(AGENT_NAME); |
| 217 | + |
| 218 | + assert!(res.is_ok()); |
| 219 | + assert_ne!(InstanceID(ulid.to_string()), res.unwrap()); |
| 220 | + } |
| 221 | + |
| 222 | + fn get_different_identifier() -> Identifiers { |
| 223 | + #[cfg(all(not(feature = "onhost"), feature = "k8s"))] |
| 224 | + return Identifiers { |
| 225 | + cluster_name: "test".to_string(), |
| 226 | + }; |
| 227 | + |
| 228 | + #[cfg(feature = "onhost")] |
| 229 | + return Identifiers { |
| 230 | + machine_id: "different".to_string(), |
| 231 | + hostname: "different".to_string(), |
| 232 | + }; |
| 233 | + } |
| 234 | +} |
0 commit comments