-
Notifications
You must be signed in to change notification settings - Fork 11
/
provider.rs
232 lines (196 loc) · 7.75 KB
/
provider.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
use std::{
collections::HashMap,
path::PathBuf,
sync::{Arc, Weak},
};
use async_trait::async_trait;
use support::{fs::FileSystem, process::ProcessManager};
use tokio::sync::RwLock;
use uuid::Uuid;
use super::namespace::{NativeNamespace, NativeNamespaceInner};
use crate::{types::ProviderCapabilities, DynNamespace, Provider, ProviderError};
#[derive(Clone)]
pub struct NativeProvider<FS, PM>
where
FS: FileSystem + Send + Sync + Clone,
PM: ProcessManager + Send + Sync + Clone,
{
capabilities: ProviderCapabilities,
tmp_dir: PathBuf,
filesystem: FS,
process_manager: PM,
inner: Arc<RwLock<NativeProviderInner<FS, PM>>>,
}
pub(super) struct NativeProviderInner<FS, PM>
where
FS: FileSystem + Send + Sync + Clone,
PM: ProcessManager + Send + Sync + Clone,
{
pub(super) namespaces: HashMap<String, NativeNamespace<FS, PM>>,
}
#[derive(Clone)]
pub(super) struct WeakNativeProvider<FS, PM>
where
FS: FileSystem + Send + Sync + Clone,
PM: ProcessManager + Send + Sync + Clone,
{
pub(super) inner: Weak<RwLock<NativeProviderInner<FS, PM>>>,
}
impl<FS, PM> NativeProvider<FS, PM>
where
FS: FileSystem + Send + Sync + Clone,
PM: ProcessManager + Send + Sync + Clone,
{
pub fn new(filesystem: FS, process_manager: PM) -> Self {
Self {
capabilities: ProviderCapabilities::new(),
// NOTE: temp_dir in linux return `/tmp` but on mac something like
// `/var/folders/rz/1cyx7hfj31qgb98d8_cg7jwh0000gn/T/`, having
// one `trailing slash` and the other no can cause issues if
// you try to build a fullpath by concatenate. Use Pathbuf to prevent the issue.
tmp_dir: std::env::temp_dir(),
filesystem,
process_manager,
inner: Arc::new(RwLock::new(NativeProviderInner {
namespaces: Default::default(),
})),
}
}
pub fn tmp_dir(mut self, tmp_dir: impl Into<PathBuf>) -> Self {
self.tmp_dir = tmp_dir.into();
self
}
}
#[async_trait]
impl<FS, PM> Provider for NativeProvider<FS, PM>
where
FS: FileSystem + Send + Sync + Clone + 'static,
PM: ProcessManager + Send + Sync + Clone + 'static,
{
fn capabilities(&self) -> &ProviderCapabilities {
&self.capabilities
}
async fn namespaces(&self) -> HashMap<String, DynNamespace> {
self.inner
.read()
.await
.namespaces
.clone()
.into_iter()
.map(|(id, namespace)| (id, Arc::new(namespace) as DynNamespace))
.collect()
}
async fn create_namespace(&self) -> Result<DynNamespace, ProviderError> {
let name = format!("zombie_{}", Uuid::new_v4());
let mut inner = self.inner.write().await;
let base_dir = PathBuf::from_iter([&self.tmp_dir, &PathBuf::from(&name)]);
self.filesystem.create_dir(&base_dir).await?;
let namespace = NativeNamespace {
name: name.clone(),
base_dir,
filesystem: self.filesystem.clone(),
process_manager: self.process_manager.clone(),
provider: WeakNativeProvider {
inner: Arc::downgrade(&self.inner),
},
inner: Arc::new(RwLock::new(NativeNamespaceInner {
nodes: Default::default(),
})),
};
inner.namespaces.insert(name, namespace.clone());
Ok(Arc::new(namespace))
}
}
#[cfg(test)]
mod tests {
use std::{ffi::OsString, str::FromStr};
use support::{
fs::in_memory::{InMemoryFile, InMemoryFileSystem},
process::fake::FakeProcessManager,
};
use super::*;
#[test]
fn capabilities_should_return_provider_capabilities() {
let fs = InMemoryFileSystem::default();
let pm = FakeProcessManager::new(HashMap::new());
let provider = NativeProvider::new(fs, pm);
let capabilities = provider.capabilities();
assert_eq!(
capabilities,
&ProviderCapabilities {
requires_image: false,
}
);
}
#[tokio::test]
async fn tmp_dir_should_set_the_temporary_for_provider() {
let fs = InMemoryFileSystem::new(HashMap::from([
(OsString::from_str("/").unwrap(), InMemoryFile::dir()),
(
OsString::from_str("/someotherdir").unwrap(),
InMemoryFile::dir(),
),
]));
let pm = FakeProcessManager::new(HashMap::new());
let provider = NativeProvider::new(fs.clone(), pm.clone()).tmp_dir("/someotherdir");
// we create a namespace to ensure tmp dir will be used to store namespace
let namespace = provider.create_namespace().await.unwrap();
assert!(namespace.base_dir().starts_with("/someotherdir"))
}
#[tokio::test]
async fn create_namespace_should_create_a_new_namespace_and_returns_it() {
let fs = InMemoryFileSystem::new(HashMap::from([
(OsString::from_str("/").unwrap(), InMemoryFile::dir()),
(OsString::from_str("/tmp").unwrap(), InMemoryFile::dir()),
]));
let pm = FakeProcessManager::new(HashMap::new());
let provider = NativeProvider::new(fs.clone(), pm.clone()).tmp_dir("/tmp");
let namespace = provider.create_namespace().await.unwrap();
// ensure namespace directory is created
assert!(fs
.files
.read()
.await
.contains_key(namespace.base_dir().as_os_str()));
// ensure namespace is added to provider namespaces
assert_eq!(provider.namespaces().await.len(), 1);
// ensure the only provider namespace is the same one as the one we just created
assert!(provider.namespaces().await.get(namespace.name()).is_some());
}
#[tokio::test]
async fn namespaces_should_return_empty_namespaces_map_if_the_provider_has_no_namespaces() {
let fs = InMemoryFileSystem::new(HashMap::from([
(OsString::from_str("/").unwrap(), InMemoryFile::dir()),
(OsString::from_str("/tmp").unwrap(), InMemoryFile::dir()),
]));
let pm = FakeProcessManager::new(HashMap::new());
let provider = NativeProvider::new(fs.clone(), pm.clone()).tmp_dir("/tmp");
assert_eq!(provider.namespaces().await.len(), 0);
}
#[tokio::test]
async fn namespaces_should_return_filled_namespaces_map_if_the_provider_has_one_namespace() {
let fs = InMemoryFileSystem::new(HashMap::from([
(OsString::from_str("/").unwrap(), InMemoryFile::dir()),
(OsString::from_str("/tmp").unwrap(), InMemoryFile::dir()),
]));
let pm = FakeProcessManager::new(HashMap::new());
let provider = NativeProvider::new(fs.clone(), pm.clone()).tmp_dir("/tmp");
let namespace = provider.create_namespace().await.unwrap();
assert_eq!(provider.namespaces().await.len(), 1);
assert!(provider.namespaces().await.get(namespace.name()).is_some());
}
#[tokio::test]
async fn namespaces_should_return_filled_namespaces_map_if_the_provider_has_two_namespaces() {
let fs = InMemoryFileSystem::new(HashMap::from([
(OsString::from_str("/").unwrap(), InMemoryFile::dir()),
(OsString::from_str("/tmp").unwrap(), InMemoryFile::dir()),
]));
let pm = FakeProcessManager::new(HashMap::new());
let provider = NativeProvider::new(fs.clone(), pm).tmp_dir("/tmp");
let namespace1 = provider.create_namespace().await.unwrap();
let namespace2 = provider.create_namespace().await.unwrap();
assert_eq!(provider.namespaces().await.len(), 2);
assert!(provider.namespaces().await.get(namespace1.name()).is_some());
assert!(provider.namespaces().await.get(namespace2.name()).is_some());
}
}