def test_JelasticEnvironment_list_with_nodes(): """ JelasticEnvironment can be instantiated with nodes """ # With an empty node_groups, that'll fail. jelapic()._ = Mock(return_value={ "infos": [ { "env": get_standard_env(), "nodeGroups": [], "nodes": [get_standard_node()], "envGroups": [], }, ] }, ) with pytest.raises(JelasticObjectException): JelasticEnvironment.dict.cache_clear() jelenvs = JelasticEnvironment.dict() jelapic()._ = Mock(return_value={ "infos": [ { "env": get_standard_env(), "nodeGroups": get_standard_node_groups(), "nodes": [get_standard_node()], "envGroups": [], }, ] }, ) JelasticEnvironment.dict.cache_clear() jelenvs = JelasticEnvironment.dict() assert isinstance(jelenvs, dict) first_jelenvname = list(jelenvs)[0] assert isinstance(jelenvs[first_jelenvname], JelasticEnvironment) jelapic()._.assert_called_once()
def test_JelasticEnvironment_sleep_via_status(): """ JelasticEnvironment can be put to sleep if running, by setting the status to SLEEPING, and saving """ jelapic()._ = Mock( return_value={ "env": get_standard_env( status=JelasticEnvironment.Status.SLEEPING.value ), # After the sleep, the API returns that it was sleeping "envGroups": [], }) # Test these two starting statuses for status in [ JelasticEnvironment.Status.STOPPED, JelasticEnvironment.Status.SLEEPING, ]: jelapic()._.reset_mock() jelenv = JelasticEnvironment() jelenv.update_from_env_dict(get_standard_env(status.value)) jelenv.status = JelasticEnvironment.Status.RUNNING jelenv.save() assert jelenv.status == JelasticEnvironment.Status.SLEEPING jelapic()._.assert_called() # A second save should not call the API jelapic()._.reset_mock() jelenv.save() jelapic()._.assert_not_called()
def test_JelasticEnvironment_can_only_be_stopped_from_running(): """ JelasticEnvironment cannot (yet) be put to certain states """ jelenv = JelasticEnvironment() jelenv.update_from_env_dict(get_standard_env()) JelStatus = JelasticEnvironment.Status for status in [ JelStatus.UNKNOWN, JelStatus.LAUNCHING, JelStatus.SUSPENDED, JelStatus.CREATING, JelStatus.CLONING, JelStatus.UPDATING, ]: jelenv._status = jelenv._from_api["status"] = status with pytest.raises(JelasticObjectException): jelenv.stop() # But it works from running jelapic()._ = Mock() jelenv._from_api["status"] = JelStatus.RUNNING jelenv._status = jelenv._from_api["status"] = JelStatus.RUNNING jelenv.stop() jelapic()._.assert_called_once()
def test_JelasticEnvironment_stop_via_status(): """ JelasticEnvironment can be started if started, by setting the status to STOPPED, and saving """ jelapic()._ = Mock( return_value={ "env": get_standard_env( status=JelasticEnvironment.Status.STOPPED.value ), # After the stop, the API returns that it was stopped "envGroups": [], }) jelenv = JelasticEnvironment() jelenv.update_from_env_dict(get_standard_env()) jelenv.status = JelasticEnvironment.Status.STOPPED jelenv.save() assert jelenv.status == JelasticEnvironment.Status.STOPPED jelapic()._.assert_called() # A second save should not call the API jelapic()._.reset_mock() jelenv.save() jelapic()._.assert_not_called()
def test_JelasticEnvironment_differs_from_api_if_envGroups_is_changed(): """ JelasticEnvironment can be instantiated, but some read-only attributes can be read, but not written """ jelenv = JelasticEnvironment() jelenv.update_from_env_dict(get_standard_env()) jelenv.update_env_groups_from_info(["A", "B"]) jelenv.envGroups.append("C") assert jelenv.differs_from_api() jelenv.envGroups = ["A", "B"] assert not jelenv.differs_from_api() jelenv.envGroups.remove("A") assert jelenv.differs_from_api()
def test_JelasticEnvironment_sleep_via_method(): """ JelasticEnvironment can be started if stopped or sleeping, with the start() method """ jelapic()._ = Mock( return_value={ "env": get_standard_env( status=JelasticEnvironment.Status.SLEEPING.value ), # After the stop, the API returns that it was stopped "envGroups": [], }) jelenv = JelasticEnvironment() jelenv.update_from_env_dict( get_standard_env(JelasticEnvironment.Status.RUNNING)) jelenv.sleep() assert jelenv.status == JelasticEnvironment.Status.SLEEPING jelapic()._.assert_called() # A second save should not call the API jelapic()._.reset_mock() jelenv.save() jelapic()._.assert_not_called()
def test_JelasticEnvironment_with_missing_data(): """ JelasticEnvironment cannot be instantiated with partial envInfo """ env_truncated = get_standard_env() del env_truncated["domain"] # Deprecated format with warnings.catch_warnings(record=True): with pytest.raises(KeyError): JelasticEnvironment(jelastic_env=env_truncated) j = JelasticEnvironment() with pytest.raises(KeyError): j.update_from_env_dict(env_truncated)
def test_JelasticEnvironment_getter_by_name(): """ JelasticEnvironment.get() works, and does one call to api """ jelapic()._ = Mock(return_value={ "env": get_standard_env(), "envGroups": [] }, ) assert isinstance(JelasticEnvironment.get("envName"), JelasticEnvironment) jelapic()._.assert_called_once()
def test_JelasticEnvironment_deprecated_init_calls(): """ JelasticEnvironment has deprecated init calls """ with warnings.catch_warnings(record=True) as warns: JelasticEnvironment(jelastic_env=get_standard_env()) assert len(warns) == 1 with warnings.catch_warnings(record=True) as warns: JelasticEnvironment(env_groups=["A"]) assert len(warns) == 1 with pytest.raises(JelasticObjectException): # Cannot be given just node_groups (it still warns) with warnings.catch_warnings(record=True): JelasticEnvironment(node_groups=get_standard_node_groups()) with warnings.catch_warnings(record=True) as warns: JelasticEnvironment(jelastic_env=get_standard_env(), node_groups=get_standard_node_groups()) assert len(warns) == 2 with pytest.raises(JelasticObjectException): # Cannot be given just nodes (it still warns) with warnings.catch_warnings(record=True): JelasticEnvironment(nodes=[get_standard_node()]) with warnings.catch_warnings(record=True) as warns: # It needs to be given all 3, otherwise the node falls outside the nodegroups JelasticEnvironment( jelastic_env=get_standard_env(), node_groups=get_standard_node_groups(), nodes=[get_standard_node()], ) assert len(warns) == 3
def test_JelasticEnvironment_envGroups_change_and_save_will_talk_to_API(): """ JelasticEnvironment can be instantiated, but some read-only attributes can be read, but not written """ jelapic()._ = Mock(return_value={ "env": get_standard_env(), "envGroups": [] }, ) jelenv = JelasticEnvironment() jelenv.update_from_env_dict(get_standard_env()) jelenv.update_env_groups_from_info(["A", "B"]) jelenv.envGroups.append("C") jelenv.save() jelapic()._.assert_called() jelapic()._.reset_mock() # A second save should not call the API jelenv.save() jelapic()._.assert_not_called()
def test_JelasticEnvironment_stranger_nodes(): """ JelasticEnvironment cannot be instantiated with nodes outside of the nodeGroups """ nodes = [ get_standard_node(), ] # Get a node groups' list without above nodes' nodegroup node_groups = [ ngdict for ngdict in get_standard_node_groups() if ngdict["name"] != nodes[0]["nodeGroup"] ] jelenv = JelasticEnvironment() jelenv.update_from_env_dict(get_standard_env()) jelenv.update_node_groups_from_info(node_groups) with pytest.raises(JelasticObjectException): jelenv.update_nodes_from_info(nodes)
def test_JelasticEnvironment_unsupported_statuses(): """ JelasticEnvironment cannot (yet) be put to certain states """ jelenv = JelasticEnvironment() jelenv.update_from_env_dict(get_standard_env()) JelStatus = JelasticEnvironment.Status for status in [ JelStatus.UNKNOWN, JelStatus.LAUNCHING, JelStatus.SUSPENDED, JelStatus.CREATING, JelStatus.CLONING, JelStatus.UPDATING, ]: jelenv.status = status with pytest.raises(JelasticObjectException): jelenv.save()
def test_JelasticEnvironment_dict_all(): """ JelasticEnvironment.get() works, and does one call to api """ jelapic()._ = Mock(return_value={ "infos": [ { "env": get_standard_env(), "envGroups": [] }, ] }, ) jelapic()._.reset_mock() JelasticEnvironment.dict.cache_clear() with warnings.catch_warnings(record=True) as warns: # .list() is deprecated, use dict() now JelasticEnvironment.list() assert len(warns) == 1 jelapic()._.assert_called_once() jelapic()._.reset_mock() JelasticEnvironment.dict.cache_clear() jelenvs = JelasticEnvironment.dict() assert isinstance(jelenvs, dict) first_jelenvname = list(jelenvs)[0] assert isinstance(jelenvs[first_jelenvname], JelasticEnvironment) jelapic()._.assert_called_once() # If we gather the list again, it will not get called more, thanks to the lru_cache: jelapic()._.reset_mock() JelasticEnvironment.dict() jelapic()._.assert_not_called() # Let's clear the lru_cache JelasticEnvironment.dict.cache_clear() JelasticEnvironment.dict() jelapic()._.assert_called_once()
def test_JelasticEnvironment_ordering(): """ JelasticEnvironment can be instantiated, but the updates cannot be called in any order """ j = JelasticEnvironment() with pytest.raises(JelasticObjectException): j.update_node_groups_from_info([]) with pytest.raises(JelasticObjectException): j.update_nodes_from_info([]) # Calling the update_from_env_dict first solves this j.update_from_env_dict(get_standard_env()) j.update_node_groups_from_info([]) j.update_nodes_from_info([])
def test_JelasticEnvironment_node_fetcher(): """ Test the convennience node fetcher """ nodes = [] for i in range(2): node = get_standard_node() node["id"] = i nodes.append(node) nodes[0]["nodeGroup"] = "cp" nodes[1]["nodeGroup"] = "sqldb" jelenv = JelasticEnvironment() jelenv.update_from_env_dict(get_standard_env()) jelenv.update_node_groups_from_info(get_standard_node_groups()) jelenv.update_nodes_from_info(nodes) from jelapi.classes import JelasticNodeGroup # Do not look by full NodeGroup object with pytest.raises(JelasticObjectException): jelenv.node_by_node_group(JelasticNodeGroup.NodeGroupType.CACHE) # If the node's not around, exception with pytest.raises(JelasticObjectException): jelenv.node_by_node_group("nosqldb") assert isinstance(jelenv.node_by_node_group("cp"), JelasticNode)
def test_JelasticEnvironment_nodes(): """ JelasticEnvironment can be instantiated with nodes """ nodes = [] for i in range(3): node = get_standard_node() node["id"] = i nodes.append(node) jelenv = JelasticEnvironment() jelenv.update_from_env_dict(get_standard_env()) jelenv.update_node_groups_from_info(get_standard_node_groups()) jelenv.update_nodes_from_info(nodes) assert not jelenv.differs_from_api() jelenv.nodeGroups["cp"].nodes[0].fixedCloudlets = 8 assert jelenv.differs_from_api() jelapic()._ = Mock(return_value={ "env": get_standard_env(), "envGroups": [], "nodes": [get_standard_node()], }, ) jelenv.save() assert not jelenv.differs_from_api()
def test_JelasticEnvironment_extdomains_change_and_save_will_talk_to_API(): """ JelasticEnvironment can be instantiated, but some read-only attributes can be read, but not written """ twodomains = ["test.example.com", "test.example.org"] jelapic()._ = Mock(return_value={ "env": get_standard_env(extdomains=twodomains), "envGroups": [] }, ) jelenv = JelasticEnvironment() jelenv.update_from_env_dict(get_standard_env()) jelenv.extdomains = twodomains jelenv.save() jelapic()._.assert_called() jelapic()._.reset_mock() # Removing a domain also calls jelenv.extdomains.remove("test.example.com") jelenv.save() jelapic()._.assert_called() jelapic()._.reset_mock() # A second save should not call the API jelenv.save() jelapic()._.assert_not_called()
def test_JelasticEnvironment_simple(): """ JelasticEnvironment can be instantiated as is """ JelasticEnvironment()