def __init__(self, endpoint_type, region_name): if endpoint_type not in [ "iot:Data", "iot:Data-ATS", "iot:CredentialProvider", "iot:Jobs", ]: raise InvalidRequestException( " An error occurred (InvalidRequestException) when calling the DescribeEndpoint " "operation: Endpoint type %s not recognized." % endpoint_type) self.region_name = region_name data_identifier = random_string(14) if endpoint_type == "iot:Data": self.endpoint = "{i}.iot.{r}.amazonaws.com".format( i=data_identifier, r=self.region_name) elif "iot:Data-ATS" in endpoint_type: self.endpoint = "{i}-ats.iot.{r}.amazonaws.com".format( i=data_identifier, r=self.region_name) elif "iot:CredentialProvider" in endpoint_type: identifier = random_string(14) self.endpoint = "{i}.credentials.iot.{r}.amazonaws.com".format( i=identifier, r=self.region_name) elif "iot:Jobs" in endpoint_type: identifier = random_string(14) self.endpoint = "{i}.jobs.iot.{r}.amazonaws.com".format( i=identifier, r=self.region_name) self.endpoint_type = endpoint_type
def test_create_nodegroup_throws_exception_when_cluster_not_found(): client = boto3.client(SERVICE, region_name=REGION) non_existent_cluster_name = random_string() expected_exception = ResourceNotFoundException expected_msg = CLUSTER_NOT_FOUND_MSG.format(clusterName=non_existent_cluster_name,) with pytest.raises(ClientError) as raised_exception: client.create_nodegroup( clusterName=non_existent_cluster_name, nodegroupName=random_string(), **dict(NodegroupInputs.REQUIRED) ) assert_expected_exception(raised_exception, expected_exception, expected_msg)
def test_create_nodegroup_throws_exception_when_cluster_not_active( NodegroupBuilder, monkeypatch ): if settings.TEST_SERVER_MODE: raise SkipTest("Cant patch Cluster attributes in server mode.") client, generated_test_data = NodegroupBuilder(BatchCountSize.SMALL) expected_exception = InvalidRequestException expected_msg = CLUSTER_NOT_READY_MSG.format( clusterName=generated_test_data.cluster_name, ) with mock.patch("moto.eks.models.Cluster.isActive", return_value=False): with pytest.raises(ClientError) as raised_exception: client.create_nodegroup( clusterName=generated_test_data.cluster_name, nodegroupName=random_string(), **dict(NodegroupInputs.REQUIRED) ) count_nodegroups_after_test = len( client.list_nodegroups(clusterName=generated_test_data.cluster_name)[ ResponseAttributes.NODEGROUPS ] ) count_nodegroups_after_test.should.equal(BatchCountSize.SMALL) assert_expected_exception(raised_exception, expected_exception, expected_msg)
def assert_valid_selectors(ClusterBuilder, expected_msg, expected_result, selectors): client, generated_test_data = ClusterBuilder() cluster_name = generated_test_data.existing_cluster_name fargate_profile_name = random_string() expected_exception = InvalidParameterException test_inputs = dict( deepcopy( # Required Constants [POD_EXECUTION_ROLE_ARN] # Required Variables + [ (ClusterAttributes.CLUSTER_NAME, cluster_name), (FargateProfileAttributes.FARGATE_PROFILE_NAME, fargate_profile_name), ] # Test Case Values + [(FargateProfileAttributes.SELECTORS, selectors)] ) ) if expected_result == PossibleTestResults.SUCCESS: result = client.create_fargate_profile(**test_inputs)[ ResponseAttributes.FARGATE_PROFILE ] for key, expected_value in test_inputs.items(): result[key].should.equal(expected_value) else: with pytest.raises(ClientError) as raised_exception: client.create_fargate_profile(**test_inputs) assert_expected_exception(raised_exception, expected_exception, expected_msg)
def test_create_nodegroup_handles_launch_template_combinations( ClusterBuilder, launch_template, instance_types, disk_size, remote_access, expected_result, ): client, generated_test_data = ClusterBuilder() nodegroup_name = random_string() expected_exception = InvalidParameterException expected_msg = None test_inputs = dict( deepcopy( # Required Constants NodegroupInputs.REQUIRED # Required Variables + [ ( ClusterAttributes.CLUSTER_NAME, generated_test_data.existing_cluster_name, ), (NodegroupAttributes.NODEGROUP_NAME, nodegroup_name), ] # Test Case Values + [ _ for _ in [launch_template, instance_types, disk_size, remote_access] if _ ] ) ) if expected_result == PossibleTestResults.SUCCESS: result = client.create_nodegroup(**test_inputs)[ResponseAttributes.NODEGROUP] for key, expected_value in test_inputs.items(): result[key].should.equal(expected_value) else: if launch_template and disk_size: expected_msg = LAUNCH_TEMPLATE_WITH_DISK_SIZE_MSG elif launch_template and remote_access: expected_msg = LAUNCH_TEMPLATE_WITH_REMOTE_ACCESS_MSG # Docs say this combination throws an exception but testing shows that # instanceTypes overrides the launchTemplate instance values instead. # Leaving here for easier correction if/when that gets fixed. elif launch_template and instance_types: pass if expected_msg: with pytest.raises(ClientError) as raised_exception: client.create_nodegroup(**test_inputs) assert_expected_exception(raised_exception, expected_exception, expected_msg)
def get_cluster_credentials(self, cluster_identifier, db_user, auto_create, duration_seconds): if duration_seconds < 900 or duration_seconds > 3600: raise InvalidParameterValueError( "Token duration must be between 900 and 3600 seconds") expiration = datetime.datetime.now() + datetime.timedelta( 0, duration_seconds) if cluster_identifier in self.clusters: user_prefix = "IAM:" if auto_create is False else "IAMA:" db_user = user_prefix + db_user return { "DbUser": db_user, "DbPassword": random_string(32), "Expiration": expiration, } else: raise ClusterNotFoundError(cluster_identifier)
def test_create_fargate_throws_exception_when_no_selectors_provided(ClusterBuilder): client, generated_test_data = ClusterBuilder() cluster_name = generated_test_data.existing_cluster_name fargate_profile_name = random_string() expected_exception = InvalidParameterException expected_msg = FARGATE_PROFILE_NEEDS_SELECTOR_MSG test_inputs = dict( deepcopy( # Required Constants [POD_EXECUTION_ROLE_ARN] # Required Variables + [ (ClusterAttributes.CLUSTER_NAME, cluster_name), (FargateProfileAttributes.FARGATE_PROFILE_NAME, fargate_profile_name), ] ) ) with pytest.raises(ClientError) as raised_exception: client.create_fargate_profile(**test_inputs) assert_expected_exception(raised_exception, expected_exception, expected_msg)