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_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_add_node_group(): """ Test saving of nodeGroups' updates, adding one """ j = JelasticEnvironmentFactory() ng = JelasticNodeGroup( nodeGroupType=JelasticNodeGroup.NodeGroupType.NOSQL_DATABASE) assert not ng.is_from_api assert ng._envVars == {} ng.attach_to_environment(j) ng.raise_unless_can_call_api() n = JelasticNode(nodeType=JelasticNode.NodeType.DOCKER) assert not n.is_from_api n.attach_to_node_group(ng) assert j.differs_from_api() jelapic()._ = Mock(return_value={ "response": { "env": get_standard_env(), "envGroups": [], "nodes": [get_standard_node()], } }, ) j._save_topology_and_node_groups() # Called twice, once for saving, once for refresh jelapic()._.assert_called()
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_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_JelEnv_can_clone(): j = JelasticEnvironmentFactory() # 33 chars is OK jelapic()._ = Mock(return_value={ "env": get_standard_env(), "envGroups": [], "nodes": [get_standard_node()], "nodeGroups": get_standard_node_groups(), }, ) j.clone("abcdefghijklmnopqrstuvwxyz0123456") # Called twice actually jelapic()._.assert_called()
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_JelasticMountPoint_cannot_add_but_del_if_came_from_api(): """ We can't add a mount point that came from the API """ jmp = JelasticMountPoint() jmp.attach_to_node_group(cp_node_group) jmp.update_from_env_dict( get_standard_mount_point( source_node_id=storage_node_group.nodes[0].id), ) assert jmp.is_from_api with pytest.raises(JelasticObjectException): jmp.add_to_api() jelapic()._ = Mock() jmp.del_from_api() 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_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_JelasticMountPoint_cannot_del_but_add_if_new(): """ We can't add a mount point that came from the API """ jmp = JelasticMountPoint( name="test name", path="/tmp/test1", sourceNode=storage_node_group.nodes[0], sourcePath="/tmp/destination", ) jmp.attach_to_node_group(cp_node_group) assert not jmp.is_from_api with pytest.raises(JelasticObjectException): jmp.del_from_api() jelapic()._ = Mock() jmp.add_to_api() jelapic()._.assert_called_once()
def test_JelasticEnvironment_add_node_group_error_in_api(): """ Test saving of nodeGroups', but it gives an error in API """ j = JelasticEnvironmentFactory() ng = JelasticNodeGroup( nodeGroupType=JelasticNodeGroup.NodeGroupType.NOSQL_DATABASE) ng.attach_to_environment(j) ng.raise_unless_can_call_api() n = JelasticNode(nodeType=JelasticNode.NodeType.DOCKER) n.attach_to_node_group(ng) jelapic()._ = Mock( return_value={"response": { "error": "This is a long error message", }}, ) with pytest.raises(JelasticObjectException): j._save_topology_and_node_groups() # Called once, then rose jelapic()._.assert_called_once()
def test_JelasticEnvironment_sumstats(): """ We can get Environment sumstats """ jelapic()._ = Mock( return_value={"stats": []}, # Of course there is something in that dict. ) jelenv = JelasticEnvironmentFactory() with pytest.raises(TypeError): # duration is needed jelenv.get_sumstats() # Fetch 8 hours' jelenv.get_sumstats(8 * 60 * 60)
def test_JelasticEnvironment_displayName_change_and_save_will_talk_to_API_twice( ): """ 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 = JelasticEnvironmentFactory() jelenv.displayName = "different displayName" jelenv._save_displayName() jelapic()._.assert_called() jelapic()._.reset_mock() # A second save should not call the API jelenv._save_displayName() jelapic()._.assert_not_called()
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_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_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()