Esempio n. 1
0
    def _test_start(self, expected_url, mock_post, mock_get_primary,
                    mock_generate, mock_get_one_cluster):
        mock_generate.return_value = "mock_unique_name"
        response = MagicMock(name="requests.response", autospec=True)
        response.json.return_value = {"name": "mock_unique_name"}
        mock_post.return_value = response
        mock_get_primary.return_value = ("mock_primary_host", 27017)

        mock_http_return_value = {
            "stateName": "IDLE",
            "name": "mock_unique_name",
            "mongoURI": "mongodb://mock_mongo_uri:27017",
            "mongoURIWithOptions":
            "mongodb://mock_mongo_uri:27017/?ssl=true&authSource=admin",
            "mongoURIUpdated": "2018-11-27T12:24:17Z",
        }
        mock_get_one_cluster.return_value = mock_http_return_value
        expected_out = copy.deepcopy(mock_http_return_value)
        expected_out.update({
            "hosts": "mock_mongo_uri:27017",
            "mongodb_url": "mock_mongo_uri:27017/?ssl=true&authSource=admin",
            "hostname": "mock_primary_host",
            "port": 27017,
        })

        atlas = atlas_setup.AtlasSetup(self.config)
        atlas.start()
        mock_post.assert_called_with(expected_url, auth=ANY, json=ANY)
        self.assertDictEqual(
            self.config["mongodb_setup"]["out"]["atlas"]["clusters"]
            [0].as_dict(), expected_out)
        self.assertEqual(
            len(self.config["mongodb_setup"]["out"]["atlas"]["clusters"]), 1)
Esempio n. 2
0
 def test_start_when_cluster_exists(self):
     with in_dir(FIXTURE_FILES.fixture_file_path("atlas-config")):
         config = load_config_dict("mongodb_setup")
         # Inject a fake cluster into mongodb_setup.out
         config["mongodb_setup"]["out"] = {}
         config["mongodb_setup"]["out"]["atlas"] = {}
         config["mongodb_setup"]["out"]["atlas"]["clusters"] = [{
             "name":
             "some_other_unique_name",
             "stateName":
             "IDLE"
         }]
         atlas = atlas_setup.AtlasSetup(config)
         with self.assertRaises(RuntimeError):
             with LogCapture(level=logging.ERROR) as log_error:
                 self.assertFalse(atlas.start())
                 log_error.check(
                     (
                         "common.atlas_setup",
                         "ERROR",
                         "[error    ] Clusters already exist in mongodb_setup.out.atlas.clusters. [common.atlas_setup] ",
                     ),
                     (
                         "common.atlas_setup",
                         "ERROR",
                         "[error    ] Please shutdown existing clusters first with infrastructure_teardown.py. [common.atlas_setup] ",
                     ),
                 )
Esempio n. 3
0
def destroy_atlas_resources():
    """
    Destroys Atlas resources using atlas_client REST call.
    """
    # The following will work if run from a work directory, such as during an Evergreen task.
    # It will cause ImportError when run from an Evergreen teardown hook.
    # This means Atlas clusters must be shut down at the end of the task, they are not reused.
    try:
        # pylint: disable=import-outside-toplevel
        from dsi.common import config
        from dsi.common import atlas_setup
    except ImportError as error:
        LOG.info(error)
        LOG.info(
            "Cannot import ConfigDict or AtlasSetup. Skipping Atlas teardown.")
        LOG.info("(This is benign inside evergreen teardown hook.)")
        return True

    # AtlasSetup.destroy() will write to mongodb_setup.out.yml
    configdict = config.ConfigDict("mongodb_setup")
    configdict.load()

    # start a mongodb configuration using config
    atlas = atlas_setup.AtlasSetup(configdict)
    atlas.destroy()

    return True
Esempio n. 4
0
 def test_unique_name(self):
     atlas_cluster = {
         "clusterType": "REPLSET",
         "providerSettings": {
             "instanceSizeName": "M99"
         }
     }
     with in_dir(FIXTURE_FILES.fixture_file_path("atlas-config")):
         config = load_config_dict("mongodb_setup")
         atlas = atlas_setup.AtlasSetup(config)
         name = atlas._generate_unique_name(atlas_cluster)
     # Generated name looks like: dsi-M99-abcdefg
     # (The last part is random, but fixed length.)
     self.assertRegex(name, "dsi-M99-")
     self.assertEqual(len(name), 15)
Esempio n. 5
0
    def __init__(self, config):
        self.config = config
        self.mongodb_setup = self.config["mongodb_setup"]
        self.clusters = []

        self._downloader = None

        timeouts = self.config["mongodb_setup"].get("timeouts", {})
        self.shutdown_ms = timeouts.get("shutdown_ms",
                                        9 * host_utils.ONE_MINUTE_MILLIS)
        self.sigterm_ms = timeouts.get("sigterm_ms",
                                       host_utils.ONE_MINUTE_MILLIS)

        self.parse_cluster()

        self.atlas = atlas_setup.AtlasSetup(config)
Esempio n. 6
0
 def test_destroy(self, mock_delete, mock_generate, mock_get_one_cluster):
     with in_dir(FIXTURE_FILES.fixture_file_path("atlas-config")):
         config = load_config_dict("mongodb_setup")
         # Inject a fake cluster into mongodb_setup.out
         config["mongodb_setup"]["out"] = {}
         config["mongodb_setup"]["out"]["atlas"] = {}
         config["mongodb_setup"]["out"]["atlas"]["clusters"] = [{
             "name":
             "some_other_unique_name",
             "stateName":
             "IDLE"
         }]
         atlas = atlas_setup.AtlasSetup(config)
         atlas.destroy()
     mock_delete.assert_called_with(
         "https://cloud-dev.mongodb.com/api/atlas/v1.0/MOCK/URL/groups/test_group_id/clusters/some_other_unique_name",
         auth=ANY,
     )
     self.assertEqual(config["mongodb_setup"]["out"]["atlas"]["clusters"],
                      [])
Esempio n. 7
0
 def test_log_collection(self, mock_create, mock_get, mock_download,
                         mock_sleep):
     mock_create.return_value = "12345abcdef"
     mock_get.side_effect = [{"status": "FOO"}, {"status": "SUCCESS"}]
     with in_dir(FIXTURE_FILES.fixture_file_path("atlas-config")):
         config = load_config_dict("mongodb_setup")
         # Inject a fake cluster into mongodb_setup.out
         config["mongodb_setup"]["out"] = {}
         config["mongodb_setup"]["out"]["atlas"] = {}
         config["mongodb_setup"]["out"]["atlas"]["clusters"] = [{
             "name":
             "mock_cluster_name",
             "clusterType":
             "REPLSET",
             "stateName":
             "IDLE"
         }]
         atlas = atlas_setup.AtlasSetup(config)
         atlas.download_logs("post_task")
         mock_download.assert_called_with(
             "12345abcdef", "reports/post_task/mock_cluster_name.tgz")
     mock_sleep.assert_called()
Esempio n. 8
0
def run_atlas_command(target, command, config, prefix):
    """
    Execute on_atlas commands with atlas_setup.

    :param str target: The target to run the command on
    :param dict command: The action to run
    :param ConfigDict config: The system configuration
    :param str prefix: The id for the test related to the current command. If there
    is not a specific test related to the current command, the value of prefix should reflect the
    hook that the command belongs to, such as between_tests, post_task, and so on.
    """
    assert target == "on_atlas"

    keys = list(command.keys())
    LOG.info("Running command(s) %s on %s", keys, target)
    atlas = atlas_setup.AtlasSetup(config)
    for key in keys:
        if key == "retrieve_logs":
            atlas.download_logs(prefix)
        else:
            raise KeyError(
                "Command {} not supported with on_atlas.".format(key))
    LOG.debug("Done running command(s) %s on %s", keys, target)