1 import ./make-test-python.nix ({ pkgs, ...} :
3 mkNode = { replicationMode, publicV6Address ? "::1" }: { pkgs, ... }: {
4 networking.interfaces.eth1.ipv6.addresses = [{
5 address = publicV6Address;
9 networking.firewall.allowedTCPPorts = [ 3901 3902 ];
14 replication_mode = replicationMode;
16 rpc_bind_addr = "[::]:3901";
17 rpc_public_addr = "[${publicV6Address}]:3901";
18 rpc_secret = "5c1915fa04d0b6739675c61bf5907eb0fe3d9c69850c83820f51b4d25d13868c";
22 api_bind_addr = "[::]:3900";
23 root_domain = ".s3.garage";
27 bind_addr = "[::]:3902";
28 root_domain = ".web.garage";
33 environment.systemPackages = [ pkgs.minio-client ];
35 # Garage requires at least 1GiB of free disk space to run.
36 virtualisation.diskSize = 2 * 1024;
43 maintainers = with pkgs.lib.maintainers; [ raitobezarius ];
47 single_node = mkNode { replicationMode = "none"; };
48 node1 = mkNode { replicationMode = 3; publicV6Address = "fc00:1::1"; };
49 node2 = mkNode { replicationMode = 3; publicV6Address = "fc00:1::2"; };
50 node3 = mkNode { replicationMode = 3; publicV6Address = "fc00:1::3"; };
51 node4 = mkNode { replicationMode = 3; publicV6Address = "fc00:1::4"; };
55 from typing import List
56 from dataclasses import dataclass
60 cur_version_regex = re.compile('Current cluster layout version: (?P<ver>\d*)')
61 key_creation_regex = re.compile('Key name: (?P<key_name>.*)\nKey ID: (?P<key_id>.*)\nSecret key: (?P<secret_key>.*)')
74 def get_node_fqn(machine: Machine) -> GarageNode:
75 node_id, host = machine.succeed("garage node id").split('@')
76 return GarageNode(node_id=node_id, host=host)
78 def get_node_id(machine: Machine) -> str:
79 return get_node_fqn(machine).node_id
81 def get_layout_version(machine: Machine) -> int:
82 version_data = machine.succeed("garage layout show")
83 m = cur_version_regex.search(version_data)
84 if m and m.group('ver') is not None:
85 return int(m.group('ver')) + 1
87 raise ValueError('Cannot find current layout version')
89 def apply_garage_layout(machine: Machine, layouts: List[str]):
90 for layout in layouts:
91 machine.succeed(f"garage layout assign {layout}")
92 version = get_layout_version(machine)
93 machine.succeed(f"garage layout apply --version {version}")
95 def create_api_key(machine: Machine, key_name: str) -> S3Key:
96 output = machine.succeed(f"garage key new --name {key_name}")
97 m = key_creation_regex.match(output)
98 if not m or not m.group('key_id') or not m.group('secret_key'):
99 raise ValueError('Cannot parse API key data')
100 return S3Key(key_name=key_name, key_id=m.group('key_id'), secret_key=m.group('secret_key'))
102 def get_api_key(machine: Machine, key_pattern: str) -> S3Key:
103 output = machine.succeed(f"garage key info {key_pattern}")
104 m = key_creation_regex.match(output)
105 if not m or not m.group('key_name') or not m.group('key_id') or not m.group('secret_key'):
106 raise ValueError('Cannot parse API key data')
107 return S3Key(key_name=m.group('key_name'), key_id=m.group('key_id'), secret_key=m.group('secret_key'))
109 def test_bucket_writes(node):
110 node.succeed("garage bucket create test-bucket")
111 s3_key = create_api_key(node, "test-api-key")
112 node.succeed("garage bucket allow --read --write test-bucket --key test-api-key")
113 other_s3_key = get_api_key(node, 'test-api-key')
114 assert other_s3_key.secret_key == other_s3_key.secret_key
116 f"mc alias set test-garage http://[::1]:3900 {s3_key.key_id} {s3_key.secret_key} --api S3v4"
118 node.succeed("echo test | mc pipe test-garage/test-bucket/test.txt")
119 assert node.succeed("mc cat test-garage/test-bucket/test.txt").strip() == "test"
121 def test_bucket_over_http(node, bucket='test-bucket', url=None):
123 url = f"{bucket}.web.garage"
125 node.succeed(f'garage bucket website --allow {bucket}')
126 node.succeed(f'echo hello world | mc pipe test-garage/{bucket}/index.html')
127 assert (node.succeed(f"curl -H 'Host: {url}' http://localhost:3902")).strip() == 'hello world'
129 with subtest("Garage works as a single-node S3 storage"):
130 single_node.wait_for_unit("garage.service")
131 single_node.wait_for_open_port(3900)
132 # Now Garage is initialized.
133 single_node_id = get_node_id(single_node)
134 apply_garage_layout(single_node, [f'-z qemutest -c 1 "{single_node_id}"'])
135 # Now Garage is operational.
136 test_bucket_writes(single_node)
137 test_bucket_over_http(single_node)
139 with subtest("Garage works as a multi-node S3 storage"):
140 nodes = ('node1', 'node2', 'node3', 'node4')
141 rev_machines = {m.name: m for m in machines}
142 def get_machine(key): return rev_machines[key]
144 node = get_machine(key)
145 node.wait_for_unit("garage.service")
146 node.wait_for_open_port(3900)
148 # Garage is initialized on all nodes.
149 node_ids = {key: get_node_fqn(get_machine(key)) for key in nodes}
152 for other_key in nodes:
154 other_id = node_ids[other_key]
155 get_machine(key).succeed(f"garage node connect {other_id.node_id}@{other_id.host}")
157 # Provide multiple zones for the nodes.
158 zones = ["nixcon", "nixcon", "paris_meetup", "fosdem"]
159 apply_garage_layout(node1,
161 f'{ndata.node_id} -z {zones[index]} -c 1'
162 for index, ndata in enumerate(node_ids.values())
164 # Now Garage is operational.
165 test_bucket_writes(node1)
167 test_bucket_over_http(get_machine(node))