-
Notifications
You must be signed in to change notification settings - Fork 503
/
test_smoke.py
3360 lines (3055 loc) · 142 KB
/
test_smoke.py
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Smoke tests for SkyPilot
# Default options are set in pyproject.toml
# Example usage:
# Run all tests except for AWS and Lambda Cloud
# > pytest tests/test_smoke.py
#
# Terminate failed clusters after test finishes
# > pytest tests/test_smoke.py --terminate-on-failure
#
# Re-run last failed tests
# > pytest --lf
#
# Run one of the smoke tests
# > pytest tests/test_smoke.py::test_minimal
#
# Only run managed spot tests
# > pytest tests/test_smoke.py --managed-spot
#
# Only run test for AWS + generic tests
# > pytest tests/test_smoke.py --aws
#
# Change cloud for generic tests to aws
# > pytest tests/test_smoke.py --generic-cloud aws
import hashlib
import inspect
import pathlib
import shutil
import subprocess
import sys
import tempfile
import time
from typing import Dict, List, NamedTuple, Optional, Tuple
import urllib.parse
import uuid
import os
import warnings
import colorama
import jinja2
import pytest
import sky
from sky import global_user_state
from sky.adaptors import ibm
from sky.adaptors import cloudflare
from sky.clouds import AWS, GCP, Azure
from sky.data import data_utils
from sky.data import storage as storage_lib
from sky.data.data_utils import Rclone
from sky.skylet import events
from sky.utils import common_utils
from sky.utils import subprocess_utils
# For uniquefying users on shared-account cloud providers. Used as part of the
# cluster names.
_smoke_test_hash = hashlib.md5(
common_utils.user_and_hostname_hash().encode()).hexdigest()[:4]
# To avoid the second smoke test reusing the cluster launched in the first
# smoke test. Also required for test_spot_recovery to make sure the manual
# termination with aws ec2 does not accidentally terminate other spot clusters
# from the different spot launch with the same cluster name but a different job
# id.
test_id = str(uuid.uuid4())[-2:]
LAMBDA_TYPE = '--cloud lambda --gpus A10'
SCP_TYPE = '--cloud scp'
SCP_GPU_V100 = '--gpus V100-32GB'
storage_setup_commands = [
'touch ~/tmpfile', 'mkdir -p ~/tmp-workdir',
'touch ~/tmp-workdir/tmp\ file', 'touch ~/tmp-workdir/tmp\ file2',
'touch ~/tmp-workdir/foo',
'ln -f -s ~/tmp-workdir/ ~/tmp-workdir/circle-link',
'touch ~/.ssh/id_rsa.pub'
]
# Wait until the spot controller is not in INIT state.
# This is a workaround for the issue that when multiple spot tests
# are running in parallel, the spot controller may be in INIT and
# the spot queue/cancel command will return staled table.
_SPOT_QUEUE_WAIT = ('s=$(sky spot queue); '
'until [ `echo "$s" '
'| grep "jobs will not be shown until it becomes UP." '
'| wc -l` -eq 0 ]; '
'do echo "Waiting for spot queue to be ready..."; '
'sleep 5; s=$(sky spot queue); done; echo "$s"; '
'echo; echo; echo "$s"')
_SPOT_CANCEL_WAIT = (
's=$(sky spot cancel -y -n {job_name}); until [ `echo "$s" '
'| grep "Please wait for the controller to be ready." '
'| wc -l` -eq 0 ]; do echo "Waiting for the spot controller '
'to be ready"; sleep 5; s=$(sky spot cancel -y -n {job_name}); '
'done; echo "$s"; echo; echo; echo "$s"')
# TODO(zhwu): make the spot controller on GCP.
class Test(NamedTuple):
name: str
# Each command is executed serially. If any failed, the remaining commands
# are not run and the test is treated as failed.
commands: List[str]
teardown: Optional[str] = None
# Timeout for each command in seconds.
timeout: int = 15 * 60
def echo(self, message: str):
# pytest's xdist plugin captures stdout; print to stderr so that the
# logs are streaming while the tests are running.
prefix = f'[{self.name}]'
message = f'{prefix} {message}'
message = message.replace('\n', f'\n{prefix} ')
print(message, file=sys.stderr, flush=True)
def _get_cluster_name() -> str:
"""Returns a user-unique cluster name for each test_<name>().
Must be called from each test_<name>().
"""
caller_func_name = inspect.stack()[1][3]
test_name = caller_func_name.replace('_', '-').replace('test-', 't-')
if len(test_name) > 16:
test_name = test_name[:16] + hashlib.md5(
test_name.encode()).hexdigest()[:3]
return f'{test_name}-{_smoke_test_hash}-{test_id}'
def run_one_test(test: Test) -> Tuple[int, str, str]:
# Fail fast if `sky` CLI somehow errors out.
subprocess.run(['sky', 'status'], stdout=subprocess.DEVNULL, check=True)
log_file = tempfile.NamedTemporaryFile('a',
prefix=f'{test.name}-',
suffix='.log',
delete=False)
test.echo(f'Test started. Log: less {log_file.name}')
for command in test.commands:
log_file.write(f'+ {command}\n')
log_file.flush()
proc = subprocess.Popen(
command,
stdout=log_file,
stderr=subprocess.STDOUT,
shell=True,
executable='/bin/bash',
)
try:
proc.wait(timeout=test.timeout)
except subprocess.TimeoutExpired as e:
log_file.flush()
test.echo(f'Timeout after {test.timeout} seconds.')
test.echo(str(e))
log_file.write(f'Timeout after {test.timeout} seconds.\n')
log_file.flush()
# Kill the current process.
proc.terminate()
proc.returncode = 1 # None if we don't set it.
break
if proc.returncode:
break
style = colorama.Style
fore = colorama.Fore
outcome = (f'{fore.RED}Failed{style.RESET_ALL}'
if proc.returncode else f'{fore.GREEN}Passed{style.RESET_ALL}')
reason = f'\nReason: {command}' if proc.returncode else ''
msg = (f'{outcome}.'
f'{reason}'
f'\nLog: less {log_file.name}\n')
test.echo(msg)
log_file.write(msg)
if (proc.returncode == 0 or
pytest.terminate_on_failure) and test.teardown is not None:
subprocess_utils.run(
test.teardown,
stdout=log_file,
stderr=subprocess.STDOUT,
timeout=10 * 60, # 10 mins
shell=True,
)
if proc.returncode:
raise Exception(f'test failed: less {log_file.name}')
def get_aws_region_for_quota_failover() -> Optional[str]:
candidate_regions = AWS.regions_with_offering(instance_type='p3.16xlarge',
accelerators=None,
use_spot=True,
region=None,
zone=None)
for region in candidate_regions:
resources = sky.Resources(cloud=sky.AWS(),
instance_type='p3.16xlarge',
region=region.name,
use_spot=True)
if not AWS.check_quota_available(resources):
return region.name
return None
def get_gcp_region_for_quota_failover() -> Optional[str]:
candidate_regions = GCP.regions_with_offering(instance_type=None,
accelerators={'A100-80GB': 1},
use_spot=True,
region=None,
zone=None)
for region in candidate_regions:
if not GCP.check_quota_available(
sky.Resources(cloud=sky.GCP(),
region=region.name,
accelerators={'A100-80GB': 1},
use_spot=True)):
return region.name
return None
# ---------- Dry run: 2 Tasks in a chain. ----------
def test_example_app():
test = Test(
'example_app',
['python examples/example_app.py'],
)
run_one_test(test)
# ---------- A minimal task ----------
def test_minimal(generic_cloud: str):
name = _get_cluster_name()
test = Test(
'minimal',
[
f'sky launch -y -c {name} --cloud {generic_cloud} tests/test_yamls/minimal.yaml',
f'sky logs {name} 1 --status',
f'sky logs {name} --status | grep "Job 1: SUCCEEDED"', # Equivalent.
# Check the logs downloading
f'log_path=$(sky logs {name} 1 --sync-down | grep "Job 1 logs:" | sed -E "s/^.*Job 1 logs: (.*)\\x1b\\[0m/\\1/g") && echo $log_path && test -f $log_path/run.log',
# Ensure the raylet process has the correct file descriptor limit.
f'sky exec {name} "prlimit -n --pid=\$(pgrep -f \'raylet/raylet --raylet_socket_name\') | grep \'"\'1048576 1048576\'"\'"',
f'sky logs {name} 2 --status', # Ensure the job succeeded.
],
f'sky down -y {name}',
)
run_one_test(test)
# ---------- Test region ----------
@pytest.mark.aws
def test_aws_region():
name = _get_cluster_name()
test = Test(
'aws_region',
[
f'sky launch -y -c {name} --region us-west-2 examples/minimal.yaml',
f'sky exec {name} examples/minimal.yaml',
f'sky logs {name} 1 --status', # Ensure the job succeeded.
f'sky status --all | grep {name} | grep us-west-2', # Ensure the region is correct.
],
f'sky down -y {name}',
)
run_one_test(test)
@pytest.mark.gcp
def test_gcp_region():
name = _get_cluster_name()
test = Test(
'gcp_region',
[
f'sky launch -y -c {name} --region us-central1 --cloud gcp tests/test_yamls/minimal.yaml',
f'sky exec {name} tests/test_yamls/minimal.yaml',
f'sky logs {name} 1 --status', # Ensure the job succeeded.
f'sky status --all | grep {name} | grep us-central1', # Ensure the region is correct.
],
f'sky down -y {name}',
)
run_one_test(test)
@pytest.mark.ibm
def test_ibm_region():
name = _get_cluster_name()
region = 'eu-de'
test = Test(
'region',
[
f'sky launch -y -c {name} --cloud ibm --region {region} examples/minimal.yaml',
f'sky exec {name} --cloud ibm examples/minimal.yaml',
f'sky logs {name} 1 --status', # Ensure the job succeeded.
f'sky status --all | grep {name} | grep {region}', # Ensure the region is correct.
],
f'sky down -y {name}',
)
run_one_test(test)
@pytest.mark.azure
def test_azure_region():
name = _get_cluster_name()
test = Test(
'azure_region',
[
f'sky launch -y -c {name} --region eastus2 --cloud azure tests/test_yamls/minimal.yaml',
f'sky exec {name} tests/test_yamls/minimal.yaml',
f'sky logs {name} 1 --status', # Ensure the job succeeded.
f'sky status --all | grep {name} | grep eastus2', # Ensure the region is correct.
],
f'sky down -y {name}',
)
run_one_test(test)
# ---------- Test zone ----------
@pytest.mark.aws
def test_aws_zone():
name = _get_cluster_name()
test = Test(
'aws_zone',
[
f'sky launch -y -c {name} examples/minimal.yaml --zone us-west-2b',
f'sky exec {name} examples/minimal.yaml --zone us-west-2b',
f'sky logs {name} 1 --status', # Ensure the job succeeded.
f'sky status --all | grep {name} | grep us-west-2b', # Ensure the zone is correct.
],
f'sky down -y {name}',
)
run_one_test(test)
@pytest.mark.ibm
def test_ibm_zone():
name = _get_cluster_name()
zone = 'eu-de-2'
test = Test(
'zone',
[
f'sky launch -y -c {name} --cloud ibm examples/minimal.yaml --zone {zone}',
f'sky exec {name} --cloud ibm examples/minimal.yaml --zone {zone}',
f'sky logs {name} 1 --status', # Ensure the job succeeded.
f'sky status --all | grep {name} | grep {zone}', # Ensure the zone is correct.
],
f'sky down -y {name} {name}-2 {name}-3',
)
run_one_test(test)
@pytest.mark.gcp
def test_gcp_zone():
name = _get_cluster_name()
test = Test(
'gcp_zone',
[
f'sky launch -y -c {name} --zone us-central1-a --cloud gcp tests/test_yamls/minimal.yaml',
f'sky exec {name} --zone us-central1-a --cloud gcp tests/test_yamls/minimal.yaml',
f'sky logs {name} 1 --status', # Ensure the job succeeded.
f'sky status --all | grep {name} | grep us-central1-a', # Ensure the zone is correct.
],
f'sky down -y {name}',
)
run_one_test(test)
# ---------- Test the image ----------
@pytest.mark.aws
def test_aws_images():
name = _get_cluster_name()
test = Test(
'aws_images',
[
f'sky launch -y -c {name} --image-id skypilot:gpu-ubuntu-1804 examples/minimal.yaml',
f'sky logs {name} 1 --status', # Ensure the job succeeded.
f'sky launch -c {name} --image-id skypilot:gpu-ubuntu-2004 examples/minimal.yaml && exit 1 || true',
f'sky launch -y -c {name} examples/minimal.yaml',
f'sky logs {name} 2 --status',
f'sky logs {name} --status | grep "Job 2: SUCCEEDED"', # Equivalent.
],
f'sky down -y {name}',
)
run_one_test(test)
@pytest.mark.gcp
def test_gcp_images():
name = _get_cluster_name()
test = Test(
'gcp_images',
[
f'sky launch -y -c {name} --image-id skypilot:gpu-debian-10 --cloud gcp tests/test_yamls/minimal.yaml',
f'sky logs {name} 1 --status', # Ensure the job succeeded.
f'sky launch -c {name} --image-id skypilot:cpu-debian-10 --cloud gcp tests/test_yamls/minimal.yaml && exit 1 || true',
f'sky launch -y -c {name} tests/test_yamls/minimal.yaml',
f'sky logs {name} 2 --status',
f'sky logs {name} --status | grep "Job 2: SUCCEEDED"', # Equivalent.
],
f'sky down -y {name}',
)
run_one_test(test)
@pytest.mark.aws
def test_aws_image_id_dict():
name = _get_cluster_name()
test = Test(
'aws_image_id_dict',
[
# Use image id dict.
f'sky launch -y -c {name} examples/per_region_images.yaml',
f'sky exec {name} examples/per_region_images.yaml',
f'sky exec {name} "ls ~"',
f'sky logs {name} 1 --status',
f'sky logs {name} 2 --status',
f'sky logs {name} 3 --status',
],
f'sky down -y {name}',
)
run_one_test(test)
@pytest.mark.gcp
def test_gcp_image_id_dict():
name = _get_cluster_name()
test = Test(
'gcp_image_id_dict',
[
# Use image id dict.
f'sky launch -y -c {name} tests/test_yamls/gcp_per_region_images.yaml',
f'sky exec {name} tests/test_yamls/gcp_per_region_images.yaml',
f'sky exec {name} "ls ~"',
f'sky logs {name} 1 --status',
f'sky logs {name} 2 --status',
f'sky logs {name} 3 --status',
],
f'sky down -y {name}',
)
run_one_test(test)
@pytest.mark.aws
def test_aws_image_id_dict_region():
name = _get_cluster_name()
test = Test(
'aws_image_id_dict_region',
[
# Use region to filter image_id dict.
f'sky launch -y -c {name} --region us-east-1 examples/per_region_images.yaml && exit 1 || true',
f'sky status | grep {name} && exit 1 || true', # Ensure the cluster is not created.
f'sky launch -y -c {name} --region us-west-2 examples/per_region_images.yaml',
# Should success because the image id match for the region.
f'sky launch -c {name} --image-id skypilot:gpu-ubuntu-1804 examples/minimal.yaml',
f'sky exec {name} --image-id skypilot:gpu-ubuntu-1804 examples/minimal.yaml',
f'sky exec {name} --image-id skypilot:gpu-ubuntu-2004 examples/minimal.yaml && exit 1 || true',
f'sky logs {name} 1 --status',
f'sky logs {name} 2 --status',
f'sky logs {name} 3 --status',
f'sky status --all | grep {name} | grep us-west-2', # Ensure the region is correct.
# Ensure exec works.
f'sky exec {name} --region us-west-2 examples/per_region_images.yaml',
f'sky exec {name} examples/per_region_images.yaml',
f'sky exec {name} --cloud aws --region us-west-2 "ls ~"',
f'sky exec {name} "ls ~"',
f'sky logs {name} 4 --status',
f'sky logs {name} 5 --status',
f'sky logs {name} 6 --status',
f'sky logs {name} 7 --status',
],
f'sky down -y {name}',
)
run_one_test(test)
@pytest.mark.gcp
def test_gcp_image_id_dict_region():
name = _get_cluster_name()
test = Test(
'gcp_image_id_dict_region',
[
# Use region to filter image_id dict.
f'sky launch -y -c {name} --region us-east1 tests/test_yamls/gcp_per_region_images.yaml && exit 1 || true',
f'sky status | grep {name} && exit 1 || true', # Ensure the cluster is not created.
f'sky launch -y -c {name} --region us-west3 tests/test_yamls/gcp_per_region_images.yaml',
# Should success because the image id match for the region.
f'sky launch -c {name} --cloud gcp --image-id projects/ubuntu-os-cloud/global/images/ubuntu-1804-bionic-v20230112 tests/test_yamls/minimal.yaml',
f'sky exec {name} --cloud gcp --image-id projects/ubuntu-os-cloud/global/images/ubuntu-1804-bionic-v20230112 tests/test_yamls/minimal.yaml',
f'sky exec {name} --cloud gcp --image-id skypilot:cpu-debian-10 tests/test_yamls/minimal.yaml && exit 1 || true',
f'sky logs {name} 1 --status',
f'sky logs {name} 2 --status',
f'sky logs {name} 3 --status',
f'sky status --all | grep {name} | grep us-west3', # Ensure the region is correct.
# Ensure exec works.
f'sky exec {name} --region us-west3 tests/test_yamls/gcp_per_region_images.yaml',
f'sky exec {name} tests/test_yamls/gcp_per_region_images.yaml',
f'sky exec {name} --cloud gcp --region us-west3 "ls ~"',
f'sky exec {name} "ls ~"',
f'sky logs {name} 4 --status',
f'sky logs {name} 5 --status',
f'sky logs {name} 6 --status',
f'sky logs {name} 7 --status',
],
f'sky down -y {name}',
)
run_one_test(test)
@pytest.mark.aws
def test_aws_image_id_dict_zone():
name = _get_cluster_name()
test = Test(
'aws_image_id_dict_zone',
[
# Use zone to filter image_id dict.
f'sky launch -y -c {name} --zone us-east-1b examples/per_region_images.yaml && exit 1 || true',
f'sky status | grep {name} && exit 1 || true', # Ensure the cluster is not created.
f'sky launch -y -c {name} --zone us-west-2a examples/per_region_images.yaml',
# Should success because the image id match for the zone.
f'sky launch -y -c {name} --image-id skypilot:gpu-ubuntu-1804 examples/minimal.yaml',
f'sky exec {name} --image-id skypilot:gpu-ubuntu-1804 examples/minimal.yaml',
# Fail due to image id mismatch.
f'sky exec {name} --image-id skypilot:gpu-ubuntu-2004 examples/minimal.yaml && exit 1 || true',
f'sky logs {name} 1 --status',
f'sky logs {name} 2 --status',
f'sky logs {name} 3 --status',
f'sky status --all | grep {name} | grep us-west-2a', # Ensure the zone is correct.
# Ensure exec works.
f'sky exec {name} --zone us-west-2a examples/per_region_images.yaml',
f'sky exec {name} examples/per_region_images.yaml',
f'sky exec {name} --cloud aws --region us-west-2 "ls ~"',
f'sky exec {name} "ls ~"',
f'sky logs {name} 4 --status',
f'sky logs {name} 5 --status',
f'sky logs {name} 6 --status',
f'sky logs {name} 7 --status',
],
f'sky down -y {name}',
)
run_one_test(test)
@pytest.mark.gcp
def test_gcp_image_id_dict_zone():
name = _get_cluster_name()
test = Test(
'gcp_image_id_dict_zone',
[
# Use zone to filter image_id dict.
f'sky launch -y -c {name} --zone us-east1-a tests/test_yamls/gcp_per_region_images.yaml && exit 1 || true',
f'sky status | grep {name} && exit 1 || true', # Ensure the cluster is not created.
f'sky launch -y -c {name} --zone us-central1-a tests/test_yamls/gcp_per_region_images.yaml',
# Should success because the image id match for the zone.
f'sky launch -y -c {name} --cloud gcp --image-id skypilot:cpu-debian-10 tests/test_yamls/minimal.yaml',
f'sky exec {name} --cloud gcp --image-id skypilot:cpu-debian-10 tests/test_yamls/minimal.yaml',
# Fail due to image id mismatch.
f'sky exec {name} --cloud gcp --image-id skypilot:gpu-debian-10 tests/test_yamls/minimal.yaml && exit 1 || true',
f'sky logs {name} 1 --status',
f'sky logs {name} 2 --status',
f'sky logs {name} 3 --status',
f'sky status --all | grep {name} | grep us-central1', # Ensure the zone is correct.
# Ensure exec works.
f'sky exec {name} --cloud gcp --zone us-central1-a tests/test_yamls/gcp_per_region_images.yaml',
f'sky exec {name} tests/test_yamls/gcp_per_region_images.yaml',
f'sky exec {name} --cloud gcp --region us-central1 "ls ~"',
f'sky exec {name} "ls ~"',
f'sky logs {name} 4 --status',
f'sky logs {name} 5 --status',
f'sky logs {name} 6 --status',
f'sky logs {name} 7 --status',
],
f'sky down -y {name}',
)
run_one_test(test)
@pytest.mark.aws
def test_clone_disk_aws():
name = _get_cluster_name()
test = Test(
'clone_disk_aws',
[
f'sky launch -y -c {name} --cloud aws --region us-east-2 --retry-until-up "echo hello > ~/user_file.txt"',
f'sky launch --clone-disk-from {name} -y -c {name}-clone && exit 1 || true',
f'sky stop {name} -y',
'sleep 60',
f'sky launch --clone-disk-from {name} -y -c {name}-clone --cloud aws -d --region us-west-2 "cat ~/user_file.txt | grep hello"',
f'sky launch --clone-disk-from {name} -y -c {name}-clone-2 --cloud aws -d --region us-east-2 "cat ~/user_file.txt | grep hello"',
f'sky logs {name}-clone 1 --status',
f'sky logs {name}-clone-2 1 --status',
],
f'sky down -y {name} {name}-clone {name}-clone-2',
timeout=30 * 60,
)
run_one_test(test)
@pytest.mark.gcp
def test_clone_disk_gcp():
name = _get_cluster_name()
test = Test(
'clone_disk_gcp',
[
f'sky launch -y -c {name} --cloud gcp --zone us-east1-b --retry-until-up "echo hello > ~/user_file.txt"',
f'sky launch --clone-disk-from {name} -y -c {name}-clone && exit 1 || true',
f'sky stop {name} -y',
f'sky launch --clone-disk-from {name} -y -c {name}-clone --cloud gcp --zone us-central1-a "cat ~/user_file.txt | grep hello"',
f'sky launch --clone-disk-from {name} -y -c {name}-clone-2 --cloud gcp --zone us-east1-b "cat ~/user_file.txt | grep hello"',
f'sky logs {name}-clone 1 --status',
f'sky logs {name}-clone-2 1 --status',
],
f'sky down -y {name} {name}-clone {name}-clone-2',
)
run_one_test(test)
@pytest.mark.aws
def test_image_no_conda():
name = _get_cluster_name()
test = Test(
'image_no_conda',
[
# Use image id dict.
f'sky launch -y -c {name} --region us-east-2 examples/per_region_images.yaml',
f'sky logs {name} 1 --status',
f'sky stop {name} -y',
f'sky start {name} -y',
f'sky exec {name} examples/per_region_images.yaml',
f'sky logs {name} 2 --status',
],
f'sky down -y {name}',
)
run_one_test(test)
# ------------ Test stale job ------------
@pytest.mark.no_lambda_cloud # Lambda Cloud does not support stopping instances
@pytest.mark.no_kubernetes # Kubernetes does not support stopping instances
def test_stale_job(generic_cloud: str):
name = _get_cluster_name()
test = Test(
'stale_job',
[
f'sky launch -y -c {name} --cloud {generic_cloud} "echo hi"',
f'sky exec {name} -d "echo start; sleep 10000"',
f'sky stop {name} -y',
'sleep 100', # Ensure this is large enough, else GCP leaks.
f'sky start {name} -y',
f'sky logs {name} 1 --status',
f's=$(sky queue {name}); echo "$s"; echo; echo; echo "$s" | grep FAILED',
],
f'sky down -y {name}',
)
run_one_test(test)
@pytest.mark.aws
def test_aws_stale_job_manual_restart():
name = _get_cluster_name()
region = 'us-west-2'
test = Test(
'aws_stale_job_manual_restart',
[
f'sky launch -y -c {name} --cloud aws --region {region} "echo hi"',
f'sky exec {name} -d "echo start; sleep 10000"',
# Stop the cluster manually.
f'id=`aws ec2 describe-instances --region {region} --filters '
f'Name=tag:ray-cluster-name,Values={name} '
f'--query Reservations[].Instances[].InstanceId '
'--output text`; '
f'aws ec2 stop-instances --region {region} '
'--instance-ids $id',
'sleep 40',
f'sky launch -c {name} -y "echo hi"',
f'sky logs {name} 1 --status',
f'sky logs {name} 3 --status',
# Ensure the skylet updated the stale job status.
f'sleep {events.JobSchedulerEvent.EVENT_INTERVAL_SECONDS}',
f's=$(sky queue {name}); echo "$s"; echo; echo; echo "$s" | grep FAILED',
],
f'sky down -y {name}',
)
run_one_test(test)
@pytest.mark.gcp
def test_gcp_stale_job_manual_restart():
name = _get_cluster_name()
zone = 'us-west2-a'
query_cmd = (f'gcloud compute instances list --filter='
f'"(labels.ray-cluster-name={name})" '
f'--zones={zone} --format="value(name)"')
stop_cmd = (f'gcloud compute instances stop --zone={zone}'
f' --quiet $({query_cmd})')
test = Test(
'gcp_stale_job_manual_restart',
[
f'sky launch -y -c {name} --cloud gcp --zone {zone} "echo hi"',
f'sky exec {name} -d "echo start; sleep 10000"',
# Stop the cluster manually.
stop_cmd,
'sleep 40',
f'sky launch -c {name} -y "echo hi"',
f'sky logs {name} 1 --status',
f'sky logs {name} 3 --status',
# Ensure the skylet updated the stale job status.
f'sleep {events.JobSchedulerEvent.EVENT_INTERVAL_SECONDS}',
f's=$(sky queue {name}); echo "$s"; echo; echo; echo "$s" | grep FAILED',
],
f'sky down -y {name}',
)
run_one_test(test)
# ---------- Check Sky's environment variables; workdir. ----------
@pytest.mark.no_scp # SCP does not support num_nodes > 1 yet
@pytest.mark.no_kubernetes # K8s does not support num_nodes > 1 yet
def test_env_check(generic_cloud: str):
name = _get_cluster_name()
test = Test(
'env_check',
[
f'sky launch -y -c {name} --cloud {generic_cloud} --detach-setup examples/env_check.yaml',
f'sky logs {name} 1 --status', # Ensure the job succeeded.
],
f'sky down -y {name}',
)
run_one_test(test)
# ---------- file_mounts ----------
@pytest.mark.no_scp # SCP does not support num_nodes > 1 yet. Run test_scp_file_mounts instead.
def test_file_mounts(generic_cloud: str):
name = _get_cluster_name()
extra_flags = ''
if generic_cloud in 'kubernetes':
# Kubernetes does not support multi-node
# NOTE: This test will fail if you have a Kubernetes cluster running on
# arm64 (e.g., Apple Silicon) since goofys does not work on arm64.
extra_flags = '--num-nodes 1'
test_commands = [
*storage_setup_commands,
f'sky launch -y -c {name} --cloud {generic_cloud} {extra_flags} examples/using_file_mounts.yaml',
f'sky logs {name} 1 --status', # Ensure the job succeeded.
]
test = Test(
'using_file_mounts',
test_commands,
f'sky down -y {name}',
timeout=20 * 60, # 20 mins
)
run_one_test(test)
@pytest.mark.scp
def test_scp_file_mounts():
name = _get_cluster_name()
test_commands = [
*storage_setup_commands,
f'sky launch -y -c {name} {SCP_TYPE} --num-nodes 1 examples/using_file_mounts.yaml',
f'sky logs {name} 1 --status', # Ensure the job succeeded.
]
test = Test(
'SCP_using_file_mounts',
test_commands,
f'sky down -y {name}',
timeout=20 * 60, # 20 mins
)
run_one_test(test)
def test_using_file_mounts_with_env_vars(generic_cloud: str):
name = _get_cluster_name()
test_commands = [
*storage_setup_commands,
(f'sky launch -y -c {name} --cpus 2+ --cloud {generic_cloud} '
'examples/using_file_mounts_with_env_vars.yaml'),
f'sky logs {name} 1 --status', # Ensure the job succeeded.
# Override with --env:
(f'sky launch -y -c {name}-2 --cpus 2+ --cloud {generic_cloud} '
'examples/using_file_mounts_with_env_vars.yaml '
'--env MY_LOCAL_PATH=tmpfile'),
f'sky logs {name}-2 1 --status', # Ensure the job succeeded.
]
test = Test(
'using_file_mounts_with_env_vars',
test_commands,
f'sky down -y {name} {name}-2',
timeout=20 * 60, # 20 mins
)
run_one_test(test)
# ---------- storage ----------
@pytest.mark.aws
def test_aws_storage_mounts():
name = _get_cluster_name()
storage_name = f'sky-test-{int(time.time())}'
template_str = pathlib.Path(
'tests/test_yamls/test_storage_mounting.yaml.j2').read_text()
template = jinja2.Template(template_str)
content = template.render(storage_name=storage_name)
with tempfile.NamedTemporaryFile(suffix='.yaml', mode='w') as f:
f.write(content)
f.flush()
file_path = f.name
test_commands = [
*storage_setup_commands,
f'sky launch -y -c {name} --cloud aws {file_path}',
f'sky logs {name} 1 --status', # Ensure job succeeded.
f'aws s3 ls {storage_name}/hello.txt',
]
test = Test(
'aws_storage_mounts',
test_commands,
f'sky down -y {name}; sky storage delete {storage_name}',
timeout=20 * 60, # 20 mins
)
run_one_test(test)
@pytest.mark.gcp
def test_gcp_storage_mounts():
name = _get_cluster_name()
storage_name = f'sky-test-{int(time.time())}'
template_str = pathlib.Path(
'tests/test_yamls/test_storage_mounting.yaml.j2').read_text()
template = jinja2.Template(template_str)
content = template.render(storage_name=storage_name)
with tempfile.NamedTemporaryFile(suffix='.yaml', mode='w') as f:
f.write(content)
f.flush()
file_path = f.name
test_commands = [
*storage_setup_commands,
f'sky launch -y -c {name} --cloud gcp {file_path}',
f'sky logs {name} 1 --status', # Ensure job succeeded.
f'gsutil ls gs://{storage_name}/hello.txt',
]
test = Test(
'gcp_storage_mounts',
test_commands,
f'sky down -y {name}; sky storage delete {storage_name}',
timeout=20 * 60, # 20 mins
)
run_one_test(test)
@pytest.mark.kubernetes
def test_kubernetes_storage_mounts():
# Tests bucket mounting on k8s, assuming S3 is configured.
# This test will fail if run on non x86_64 architecture, since goofys is
# built for x86_64 only.
name = _get_cluster_name()
storage_name = f'sky-test-{int(time.time())}'
template_str = pathlib.Path(
'tests/test_yamls/test_storage_mounting.yaml.j2').read_text()
template = jinja2.Template(template_str)
content = template.render(storage_name=storage_name)
with tempfile.NamedTemporaryFile(suffix='.yaml', mode='w') as f:
f.write(content)
f.flush()
file_path = f.name
test_commands = [
*storage_setup_commands,
f'sky launch -y -c {name} --cloud kubernetes {file_path}',
f'sky logs {name} 1 --status', # Ensure job succeeded.
f'aws s3 ls {storage_name}/hello.txt',
]
test = Test(
'kubernetes_storage_mounts',
test_commands,
f'sky down -y {name}; sky storage delete {storage_name}',
timeout=20 * 60, # 20 mins
)
run_one_test(test)
@pytest.mark.cloudflare
def test_cloudflare_storage_mounts(generic_cloud: str):
name = _get_cluster_name()
storage_name = f'sky-test-{int(time.time())}'
template_str = pathlib.Path(
'tests/test_yamls/test_r2_storage_mounting.yaml').read_text()
template = jinja2.Template(template_str)
content = template.render(storage_name=storage_name)
endpoint_url = cloudflare.create_endpoint()
with tempfile.NamedTemporaryFile(suffix='.yaml', mode='w') as f:
f.write(content)
f.flush()
file_path = f.name
test_commands = [
*storage_setup_commands,
f'sky launch -y -c {name} --cloud {generic_cloud} {file_path}',
f'sky logs {name} 1 --status', # Ensure job succeeded.
f'AWS_SHARED_CREDENTIALS_FILE={cloudflare.R2_CREDENTIALS_PATH} aws s3 ls s3://{storage_name}/hello.txt --endpoint {endpoint_url} --profile=r2'
]
test = Test(
'cloudflare_storage_mounts',
test_commands,
f'sky down -y {name}; sky storage delete {storage_name}',
timeout=20 * 60, # 20 mins
)
run_one_test(test)
@pytest.mark.ibm
def test_ibm_storage_mounts():
name = _get_cluster_name()
storage_name = f'sky-test-{int(time.time())}'
bucket_rclone_profile = Rclone.generate_rclone_bucket_profile_name(
storage_name, Rclone.RcloneClouds.IBM)
template_str = pathlib.Path(
'tests/test_yamls/test_ibm_cos_storage_mounting.yaml').read_text()
template = jinja2.Template(template_str)
content = template.render(storage_name=storage_name)
with tempfile.NamedTemporaryFile(suffix='.yaml', mode='w') as f:
f.write(content)
f.flush()
file_path = f.name
test_commands = [
*storage_setup_commands,
f'sky launch -y -c {name} --cloud ibm {file_path}',
f'sky logs {name} 1 --status', # Ensure job succeeded.
f'rclone ls {bucket_rclone_profile}:{storage_name}/hello.txt',
]
test = Test(
'ibm_storage_mounts',
test_commands,
f'sky down -y {name}; sky storage delete {storage_name}',
timeout=20 * 60, # 20 mins
)
run_one_test(test)
# ---------- CLI logs ----------
@pytest.mark.no_scp # SCP does not support num_nodes > 1 yet. Run test_scp_logs instead.
def test_cli_logs(generic_cloud: str):
name = _get_cluster_name()
num_nodes = 2
if generic_cloud == 'kubernetes':
# Kubernetes does not support multi-node
num_nodes = 1
timestamp = time.time()
test = Test(
'cli_logs',
[
f'sky launch -y -c {name} --cloud {generic_cloud} --num-nodes {num_nodes} "echo {timestamp} 1"',
f'sky exec {name} "echo {timestamp} 2"',
f'sky exec {name} "echo {timestamp} 3"',
f'sky exec {name} "echo {timestamp} 4"',
f'sky logs {name} 2 --status',
f'sky logs {name} 3 4 --sync-down',
f'sky logs {name} * --sync-down',
f'sky logs {name} 1 | grep "{timestamp} 1"',
f'sky logs {name} | grep "{timestamp} 4"',
],
f'sky down -y {name}',
)
run_one_test(test)
@pytest.mark.scp
def test_scp_logs():
name = _get_cluster_name()
timestamp = time.time()
test = Test(
'SCP_cli_logs',
[
f'sky launch -y -c {name} {SCP_TYPE} "echo {timestamp} 1"',
f'sky exec {name} "echo {timestamp} 2"',
f'sky exec {name} "echo {timestamp} 3"',
f'sky exec {name} "echo {timestamp} 4"',
f'sky logs {name} 2 --status',
f'sky logs {name} 3 4 --sync-down',
f'sky logs {name} * --sync-down',
f'sky logs {name} 1 | grep "{timestamp} 1"',
f'sky logs {name} | grep "{timestamp} 4"',
],
f'sky down -y {name}',
)
run_one_test(test)
# ---------- Job Queue. ----------
@pytest.mark.no_lambda_cloud # Lambda Cloud does not have K80 gpus
@pytest.mark.no_ibm # IBM Cloud does not have K80 gpus. run test_ibm_job_queue instead
@pytest.mark.no_scp # SCP does not have K80 gpus. Run test_scp_job_queue instead
@pytest.mark.no_oci # OCI does not have K80 gpus
@pytest.mark.no_kubernetes # Kubernetes not have gpus
def test_job_queue(generic_cloud: str):
name = _get_cluster_name()
test = Test(
'job_queue',
[
f'sky launch -y -c {name} --cloud {generic_cloud} examples/job_queue/cluster.yaml',