Exemplo n.º 1
0
    def test_get_env_short_invalid_envs(self):
        """
    Tests if get_env_short raises exceptions when given invalid environments

    Returns:
      None

    Raises:
      AssertionError if any of the assert checks fail
    """
        # Create junk environment values by attaching numbers to non-ephemeral environments and not attaching numbers
        # to ephemeral environments
        for env in EFConfig.ENV_ACCOUNT_MAP:
            if env not in EFConfig.EPHEMERAL_ENVS:
                env += '0'
            with self.assertRaises(ValueError) as exception:
                ef_utils.get_env_short(env)
            self.assertTrue("unknown env" in exception.exception.message)

        # Hard coded junk values
        with self.assertRaises(ValueError) as exception:
            ef_utils.get_env_short("non-existent-env")
        self.assertTrue("unknown env" in exception.exception.message)
        with self.assertRaises(ValueError) as exception:
            ef_utils.get_env_short("")
        self.assertTrue("unknown env" in exception.exception.message)
        with self.assertRaises(ValueError) as exception:
            ef_utils.get_env_short(None)
        self.assertTrue("unknown env" in exception.exception.message)
Exemplo n.º 2
0
    def __init__(self, env, service, logging, clients):
        """
    Args:
      env: the environment to resolve values for
      service: "all" or "<service">
      logging: a logger
      clients: a dict of AWS clients, with at least "s3"
    Raises:
      botocore.exceptions.ClientError if config
    """
        self.clients = clients
        self.env = env
        self.env_short = get_env_short(env)
        self.logging = logging
        self.parameters = None
        self.service = service

        # Load from S3
        parameters_key = self.service + "/parameters/" + self.service + ".parameters.json"
        try:
            parameters_object = self.clients["s3"].get_object(
                Bucket=EFConfig.S3_CONFIG_BUCKET, Key=parameters_key)
        except botocore.exceptions.ClientError:
            raise RuntimeError(
                "Error getting parameters from key: {}".format(parameters_key))
        self.parameters = json.loads(
            parameters_object["Body"].read().decode("utf-8"))["params"]
Exemplo n.º 3
0
    def test_get_env_short(self):
        """
    Checks if get_env_short returns the correct environment shortname when given valid environments

    Returns:
      None

    Raises:
      AssertionError if any of the assert checks fail
    """
        for env in EFConfig.ENV_ACCOUNT_MAP:
            expected_env_value = env
            # Attach a numeric value to environments that are ephemeral
            if env in EFConfig.EPHEMERAL_ENVS:
                env += '0'
            self.assertEquals(ef_utils.get_env_short(env), expected_env_value)
Exemplo n.º 4
0
 def env(self, value):
     """
 Sets context.env, context.env_short, and context.account_alias if env is valid
 For envs of the form "global.<account>" and "mgmt.<account_alias>",
 env is captured as "global" or "mgmt" and account_alias is parsed out of the full env rather than looked up
 Args:
   value: the fully-qualified env value
 Raises:
   ValueError if env is not valid
 """
     env_valid(value)
     self._env_full = value
     if value.find(".") == -1:
         # plain environment, e.g. prod, staging, proto<n>
         self._env = value
         self._account_alias = get_account_alias(value)
     else:
         # "<env>.<account_alias>" form, e.g. global.ellationeng or mgmt.ellationeng
         self._env, self._account_alias = value.split(".")
         # since we extracted an env, must reconfirm that it's legit
         global_env_valid(self._env)
     self._env_short = get_env_short(value)
Exemplo n.º 5
0
 def test_get_env_short(self):
     """Does get_env_short resolve correctly"""
     self.assertEquals(get_env_short("prod"), "prod")
     self.assertEquals(get_env_short("staging"), "staging")
     self.assertEquals(get_env_short("proto0"), "proto")
     self.assertEquals(get_env_short("global.ellation"), "global")