forked from nchammas/flintrock
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconftest.py
More file actions
212 lines (168 loc) · 5.35 KB
/
conftest.py
File metadata and controls
212 lines (168 loc) · 5.35 KB
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
import os
import subprocess
import tempfile
import uuid
from collections import OrderedDict
# Flintrock
from flintrock.core import StorageDirs
# External
import pytest
HADOOP_VERSION = '2.8.5'
SPARK_VERSION = '2.4.4'
SPARK_GIT_COMMIT = '7955b3962ac46b89564e0613db7bea98a1478bf2' # 2.4.4
class Dummy():
pass
aws_credentials_required = (
pytest.mark.skipif(
not bool(os.environ.get('USE_AWS_CREDENTIALS')),
reason="USE_AWS_CREDENTIALS not set"))
@pytest.fixture(scope='session')
def project_root_dir():
return os.path.dirname(
os.path.dirname(
os.path.realpath(__file__)
)
)
@pytest.fixture(scope='session')
def dummy_cluster():
storage_dirs = StorageDirs(
root='/media/root',
ephemeral=['/media/eph1', '/media/eph2'],
persistent=None,
)
cluster = Dummy()
cluster.name = 'test'
cluster.storage_dirs = storage_dirs
cluster.master_ip = '10.0.0.1'
cluster.master_host = 'master.hostname'
cluster.slave_ips = ['10.0.0.2']
cluster.slave_hosts = ['slave1.hostname']
return cluster
def random_string():
return str(uuid.uuid4())[:8]
def launch_cluster(
*,
cluster_name,
instance_type,
spark_version,
spark_git_commit):
p = subprocess.run([
'flintrock', 'launch', cluster_name,
'--num-slaves', '1',
'--install-hdfs',
'--hdfs-version', HADOOP_VERSION,
'--install-spark',
'--spark-version', spark_version,
'--spark-git-commit', spark_git_commit,
'--assume-yes',
'--ec2-instance-type', instance_type])
assert p.returncode == 0
def stop_cluster(cluster_name):
p = subprocess.run([
'flintrock', 'stop', cluster_name, '--assume-yes'])
assert p.returncode == 0
def start_cluster(cluster_name):
p = subprocess.run([
'flintrock', 'start', cluster_name])
assert p.returncode == 0
# TODO: This should reuse FlintrockCluster.
class ClusterConfig:
def __init__(
self,
*,
restarted,
instance_type,
spark_version=SPARK_VERSION,
spark_git_commit=''):
self.restarted = restarted
self.instance_type = instance_type
self.spark_version = spark_version
self.spark_git_commit = spark_git_commit
def __str__(self):
return str(OrderedDict(sorted(vars(self).items())))
cluster_configs = [
ClusterConfig(restarted=False, instance_type='t3.small'),
ClusterConfig(restarted=True, instance_type='t3.small'),
ClusterConfig(restarted=False, instance_type='m5.large'),
ClusterConfig(restarted=True, instance_type='m5.large'),
# We don't test all cluster states when building Spark because
# it takes a very long time.
ClusterConfig(
restarted=True,
instance_type='m5.xlarge',
spark_version='',
spark_git_commit=SPARK_GIT_COMMIT)]
@pytest.fixture(
scope='module',
params=cluster_configs,
ids=[str(cc) for cc in cluster_configs])
def running_cluster(request):
"""
Return the name of a running Flintrock cluster.
"""
cluster_name = 'running-cluster-' + random_string()
launch_cluster(
cluster_name=cluster_name,
instance_type=request.param.instance_type,
spark_version=request.param.spark_version,
spark_git_commit=request.param.spark_git_commit)
if request.param.restarted:
stop_cluster(cluster_name)
start_cluster(cluster_name)
def destroy():
p = subprocess.run([
'flintrock', 'destroy', cluster_name, '--assume-yes'])
assert p.returncode == 0
request.addfinalizer(destroy)
return cluster_name
@pytest.fixture(scope='module')
def stopped_cluster(request):
cluster_name = 'running-cluster-' + random_string()
p = subprocess.run([
'flintrock', 'launch', cluster_name,
'--num-slaves', '1',
'--no-install-hdfs',
'--no-install-spark',
'--assume-yes',
'--ec2-instance-type', 't3.small'])
assert p.returncode == 0
p = subprocess.run([
'flintrock', 'stop', cluster_name, '--assume-yes'])
assert p.returncode == 0
def destroy():
p = subprocess.run([
'flintrock', 'destroy', cluster_name, '--assume-yes'])
assert p.returncode == 0
request.addfinalizer(destroy)
return cluster_name
@pytest.fixture(scope='module')
def remote_file(request, running_cluster):
"""
Return the path to a remote dummy file on a running Flintrock cluster.
"""
file_path = '/tmp/remote_dummy_file_for_testing'
p = subprocess.run([
'flintrock', 'run-command', running_cluster, '--',
'echo -e "{data}" > {path}'.format(
data='test\n' * 3,
path=file_path)])
assert p.returncode == 0
def destroy():
p = subprocess.run([
'flintrock', 'run-command', running_cluster, '--',
'rm', '-f', file_path])
assert p.returncode == 0
request.addfinalizer(destroy)
return file_path
@pytest.fixture(scope='module')
def local_file(request):
"""
Return the path to a local dummy file.
"""
file = tempfile.NamedTemporaryFile(delete=False)
with open(file.name, 'wb') as f:
f.truncate(1024)
def destroy():
os.remove(file.name)
request.addfinalizer(destroy)
return file.name