aboutsummaryrefslogtreecommitdiff
path: root/src/garage/tests/common/mod.rs
blob: eca3e42b0803ae44af4e4df2ae45f0126280c1e8 (plain) (blame)
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
use aws_sdk_s3::{Client, Region};
use ext::*;

#[macro_use]
pub mod macros;

pub mod client;
pub mod custom_requester;
pub mod ext;
pub mod garage;

use custom_requester::CustomRequester;

const REGION: Region = Region::from_static("garage-integ-test");

#[derive(Clone)]
pub struct Context {
	pub garage: &'static garage::Instance,
	pub key: garage::Key,
	pub client: Client,
	pub custom_request: CustomRequester,
	pub k2v: K2VContext,
}

#[derive(Clone)]
pub struct K2VContext {
	pub request: CustomRequester,
}

impl Context {
	fn new() -> Self {
		let garage = garage::instance();
		let key = garage.key(None);
		let client = client::build_client(garage, &key);
		let custom_request = CustomRequester::new_s3(garage, &key);
		let k2v_request = CustomRequester::new_k2v(garage, &key);

		Context {
			garage,
			client,
			key,
			custom_request,
			k2v: K2VContext {
				request: k2v_request,
			},
		}
	}

	/// Create an unique bucket with a random suffix.
	///
	/// Return the created bucket full name.
	pub fn create_bucket(&self, name: &str) -> String {
		let bucket_name = name.to_owned();

		self.garage
			.command()
			.args(["bucket", "create", &bucket_name])
			.quiet()
			.expect_success_status("Could not create bucket");
		self.garage
			.command()
			.args(["bucket", "allow"])
			.args(["--owner", "--read", "--write"])
			.arg(&bucket_name)
			.args(["--key", &self.key.id])
			.quiet()
			.expect_success_status("Could not allow key for bucket");

		bucket_name
	}
}

pub fn context() -> Context {
	Context::new()
}