|
| 1 | +import pytest |
| 2 | +from moto import mock_ecs, mock_sqs, mock_s3, mock_ec2 |
| 3 | +import boto3 |
| 4 | + |
| 5 | +import run |
| 6 | +import config |
| 7 | + |
| 8 | + |
| 9 | +# startCluster will: |
| 10 | +# send a spot fleet request to moto-mocked-aws |
| 11 | +# create a APP_NAMESpotFleetRequestId.json file |
| 12 | +# once spot fleet request is ready (instantly), creat log groups for your log streams to go in |
| 13 | +# DS will ask aws moto-mocked-aws to place Docker containers onto the spot fleet instances |
| 14 | +# job will begin instantly |
| 15 | + |
| 16 | +class TestGenerateECSConfig: |
| 17 | + @mock_ecs |
| 18 | + @mock_sqs |
| 19 | + def test_generate_ecs_config(self, s3, run_submitJob, tmp_path): |
| 20 | + if (config.AWS_REGION == "us-east-1"): |
| 21 | + # 'us-east-1' is the default region for S3 buckets |
| 22 | + # and is not a vallid arg for "LocationConstraint" |
| 23 | + s3.create_bucket(Bucket=config.AWS_BUCKET) |
| 24 | + else: |
| 25 | + s3.create_bucket(Bucket=config.AWS_BUCKET, CreateBucketConfiguration={"LocationConstraint": config.AWS_REGION}) |
| 26 | + |
| 27 | + run_submitJob() |
| 28 | + |
| 29 | + res_endpoint = run.generateECSconfig(config.ECS_CLUSTER, config.APP_NAME, config.AWS_BUCKET, s3) |
| 30 | + |
| 31 | + expected_key = f"ecsconfigs/{config.APP_NAME}_ecs.config" |
| 32 | + expected_file_path = tmp_path / "configtemp.config" |
| 33 | + expected_file_endpoint = f"s3://{config.AWS_BUCKET}/{expected_key}" |
| 34 | + |
| 35 | + assert res_endpoint == expected_file_endpoint |
| 36 | + |
| 37 | + # create the file object to write to |
| 38 | + expected_file_path.touch() |
| 39 | + |
| 40 | + with expected_file_path.open('wb') as f: |
| 41 | + s3.download_fileobj(config.AWS_BUCKET, expected_key, f) |
| 42 | + |
| 43 | + res_file = expected_file_path.read_text() |
| 44 | + |
| 45 | + assert res_file == f"ECS_CLUSTER={config.ECS_CLUSTER}\nECS_AVAILABLE_LOGGING_DRIVERS=[\"json-file\",\"awslogs\"]" |
| 46 | + |
| 47 | + |
| 48 | + |
| 49 | +class EarlyTermination(Exception): |
| 50 | + ... |
| 51 | + |
| 52 | +def hijack_ec2(real_client): |
| 53 | + """ |
| 54 | + Patches boto3.client so that an invocation of 'ec2' will cause an |
| 55 | + EarlyTermination exception, allowing inspection and testing of the |
| 56 | + stack frame up until that point. |
| 57 | + """ |
| 58 | + def f(*args, **kwargs): |
| 59 | + if (args[0] == 'ec2'): |
| 60 | + raise EarlyTermination("early termination") |
| 61 | + |
| 62 | + return real_client(*args, **kwargs) |
| 63 | + |
| 64 | + return f |
| 65 | + |
| 66 | + |
| 67 | +class TestSpotFleetConfig: |
| 68 | + @mock_ecs |
| 69 | + @mock_sqs |
| 70 | + @mock_s3 |
| 71 | + def test_spot_fleet_config(self, run_startCluster, monkeypatch): |
| 72 | + monkeypatch.setattr(boto3, "client", hijack_ec2(boto3.client)) |
| 73 | + with pytest.raises(EarlyTermination) as e_info: |
| 74 | + run_startCluster() |
| 75 | + |
| 76 | + spot_fleet_config_res = None |
| 77 | + for tb in e_info.traceback: |
| 78 | + if (tb.name == "startCluster"): |
| 79 | + spot_fleet_config_res = tb.frame.f_locals["spotfleetConfig"] |
| 80 | + |
| 81 | + assert spot_fleet_config_res is not None |
| 82 | + |
| 83 | + # For config file requirements, see: |
| 84 | + # https://distributedscience.github.io/Distributed-Something/step_3_start_cluster.html#configuring-your-spot-fleet-request |
| 85 | + |
| 86 | + assert "IamFleetRole" in spot_fleet_config_res |
| 87 | + assert spot_fleet_config_res["IamFleetRole"].startswith("arn:aws:iam::") |
| 88 | + |
| 89 | + assert "ValidFrom" in spot_fleet_config_res |
| 90 | + assert "ValidUntil" in spot_fleet_config_res |
| 91 | + assert "TargetCapacity" in spot_fleet_config_res |
| 92 | + assert spot_fleet_config_res["TargetCapacity"] == config.CLUSTER_MACHINES |
| 93 | + assert "SpotPrice" in spot_fleet_config_res |
| 94 | + assert float(spot_fleet_config_res["SpotPrice"]) == pytest.approx(config.MACHINE_PRICE, rel=1e-2) |
| 95 | + |
| 96 | + launch_specs = spot_fleet_config_res["LaunchSpecifications"] |
| 97 | + for i in range(len(launch_specs)): |
| 98 | + assert "IamInstanceProfile" in launch_specs[i] |
| 99 | + assert "Arn" in launch_specs[i]["IamInstanceProfile"] |
| 100 | + assert launch_specs[i]["IamInstanceProfile"]["Arn"].startswith("arn:aws:iam::") |
| 101 | + |
| 102 | + assert "KeyName" in launch_specs[i] |
| 103 | + assert launch_specs[i]["KeyName"] == config.SSH_KEY_NAME[:-4] |
| 104 | + |
| 105 | + assert "ImageId" in launch_specs[i] |
| 106 | + assert launch_specs[i]["ImageId"].startswith("ami-") |
| 107 | + |
| 108 | + assert "NetworkInterfaces" in launch_specs[i] |
| 109 | + net_intfcs = launch_specs[i]["NetworkInterfaces"] |
| 110 | + for j in range(len(net_intfcs)): |
| 111 | + assert "SubnetId" in net_intfcs[j] |
| 112 | + assert net_intfcs[j]["SubnetId"].startswith("subnet-") |
| 113 | + |
| 114 | + assert "Groups" in net_intfcs[j] |
| 115 | + grps = net_intfcs[j]["Groups"] |
| 116 | + for k in range(len(grps)): |
| 117 | + assert grps[k].startswith("sg-") |
| 118 | + |
| 119 | + assert "BlockDeviceMappings" in launch_specs[i] |
| 120 | + bdms = launch_specs[i]["BlockDeviceMappings"] |
| 121 | + |
| 122 | + assert "Ebs" in bdms[0] |
| 123 | + assert "SnapshotId" in bdms[0]["Ebs"] |
| 124 | + assert bdms[0]["Ebs"]["SnapshotId"].startswith("snap-") |
| 125 | + |
| 126 | + assert "Ebs" in bdms[1] |
| 127 | + assert "VolumeSize" in bdms[1]["Ebs"] |
| 128 | + assert bdms[1]["Ebs"]["VolumeSize"] == config.EBS_VOL_SIZE |
| 129 | + |
| 130 | + assert "InstanceType" in launch_specs[i] |
| 131 | + assert launch_specs[i]["InstanceType"] == config.MACHINE_TYPE[i] |
| 132 | + |
| 133 | + assert "UserData" in launch_specs[i] |
| 134 | + |
| 135 | + |
| 136 | +class TestStartCluster: |
| 137 | + @mock_ecs |
| 138 | + @mock_sqs |
| 139 | + @mock_s3 |
| 140 | + @mock_ec2 |
| 141 | + @pytest.mark.skip(reason="not implemented yet") |
| 142 | + def test_start_cluster(self, run_startCluster): |
| 143 | + run_startCluster() |
0 commit comments