def test_strict_bool_from_string(self): # None isn"t allowed in strict mode exc = self.assertRaises(ValueError, strutils.bool_from_string, None, strict=True) expected_msg = ("Unrecognized value 'None', acceptable values are:" " '0', '1', 'f', 'false', 'n', 'no', 'off', 'on'," " 't', 'true', 'y', 'yes'") self.assertEqual(expected_msg, str(exc)) # Unrecognized strings aren't allowed self.assertFalse(strutils.bool_from_string("Other", strict=False)) exc = self.assertRaises(ValueError, strutils.bool_from_string, "Other", strict=True) expected_msg = ("Unrecognized value 'Other', acceptable values are:" " '0', '1', 'f', 'false', 'n', 'no', 'off', 'on'," " 't', 'true', 'y', 'yes'") self.assertEqual(expected_msg, str(exc)) # Unrecognized numbers aren't allowed exc = self.assertRaises(ValueError, strutils.bool_from_string, 2, strict=True) expected_msg = ("Unrecognized value '2', acceptable values are:" " '0', '1', 'f', 'false', 'n', 'no', 'off', 'on'," " 't', 'true', 'y', 'yes'") self.assertEqual(expected_msg, str(exc)) # False-like values are allowed self.assertFalse(strutils.bool_from_string("f", strict=True)) self.assertFalse(strutils.bool_from_string("false", strict=True)) self.assertFalse(strutils.bool_from_string("off", strict=True)) self.assertFalse(strutils.bool_from_string("n", strict=True)) self.assertFalse(strutils.bool_from_string("no", strict=True)) self.assertFalse(strutils.bool_from_string("0", strict=True)) self.assertTrue(strutils.bool_from_string("1", strict=True)) # Avoid font-similarity issues (one looks like lowercase-el, zero like # oh, etc...) for char in ("O", "o", "L", "l", "I", "i"): self.assertRaises(ValueError, strutils.bool_from_string, char, strict=True)
def get_creds_from_env_vars(): required_env_vars = ["OS_AUTH_URL", "OS_USERNAME", "OS_PASSWORD"] missing_env_vars = [v for v in required_env_vars if v not in os.environ] if missing_env_vars: msg = ("The following environment variables are " "required but not set: %s" % " ".join(missing_env_vars)) raise exceptions.ValidationError(message=msg) creds = { "auth_url": os.environ["OS_AUTH_URL"], "admin": { "username": os.environ["OS_USERNAME"], "password": os.environ["OS_PASSWORD"], "tenant_name": get_project_name_from_env() }, "endpoint_type": get_endpoint_type_from_env(), "endpoint": os.environ.get("OS_ENDPOINT"), "region_name": os.environ.get("OS_REGION_NAME", ""), "https_cacert": os.environ.get("OS_CACERT", ""), "https_insecure": strutils.bool_from_string( os.environ.get("OS_INSECURE")), "profiler_hmac_key": os.environ.get("OSPROFILER_HMAC_KEY"), "profiler_conn_str": os.environ.get("OSPROFILER_CONN_STR") } user_domain_name = os.environ.get("OS_USER_DOMAIN_NAME") project_domain_name = os.environ.get("OS_PROJECT_DOMAIN_NAME") identity_api_version = os.environ.get( "OS_IDENTITY_API_VERSION", os.environ.get("IDENTITY_API_VERSION")) if (identity_api_version == "3" or (identity_api_version is None and (user_domain_name or project_domain_name))): # it is Keystone v3 and it has another config scheme creds["admin"]["project_name"] = creds["admin"].pop("tenant_name") creds["admin"]["user_domain_name"] = user_domain_name or "Default" project_domain_name = project_domain_name or "Default" creds["admin"]["project_domain_name"] = project_domain_name return creds
def _test_bool_from_string(self, c): self.assertTrue(strutils.bool_from_string(c("true"))) self.assertTrue(strutils.bool_from_string(c("TRUE"))) self.assertTrue(strutils.bool_from_string(c("on"))) self.assertTrue(strutils.bool_from_string(c("On"))) self.assertTrue(strutils.bool_from_string(c("yes"))) self.assertTrue(strutils.bool_from_string(c("YES"))) self.assertTrue(strutils.bool_from_string(c("yEs"))) self.assertTrue(strutils.bool_from_string(c("1"))) self.assertTrue(strutils.bool_from_string(c("T"))) self.assertTrue(strutils.bool_from_string(c("t"))) self.assertTrue(strutils.bool_from_string(c("Y"))) self.assertTrue(strutils.bool_from_string(c("y"))) self.assertFalse(strutils.bool_from_string(c("false"))) self.assertFalse(strutils.bool_from_string(c("FALSE"))) self.assertFalse(strutils.bool_from_string(c("off"))) self.assertFalse(strutils.bool_from_string(c("OFF"))) self.assertFalse(strutils.bool_from_string(c("no"))) self.assertFalse(strutils.bool_from_string(c("0"))) self.assertFalse(strutils.bool_from_string(c("42"))) self.assertFalse( strutils.bool_from_string(c("This should not be True"))) self.assertFalse(strutils.bool_from_string(c("F"))) self.assertFalse(strutils.bool_from_string(c("f"))) self.assertFalse(strutils.bool_from_string(c("N"))) self.assertFalse(strutils.bool_from_string(c("n"))) # Whitespace should be stripped self.assertTrue(strutils.bool_from_string(c(" 1 "))) self.assertTrue(strutils.bool_from_string(c(" true "))) self.assertFalse(strutils.bool_from_string(c(" 0 "))) self.assertFalse(strutils.bool_from_string(c(" false ")))
def test_bool_bool_from_string_default(self): self.assertTrue(strutils.bool_from_string("", default=True)) self.assertFalse(strutils.bool_from_string("wibble", default=False))
def test_bool_bool_from_string(self): self.assertTrue(strutils.bool_from_string(True)) self.assertFalse(strutils.bool_from_string(False))
def test_bool_bool_from_string_no_text(self, mock_str): self.assertTrue(strutils.bool_from_string(True)) self.assertFalse(strutils.bool_from_string(False)) self.assertEqual(0, mock_str.call_count)
def test_int_bool_from_string(self): self.assertTrue(strutils.bool_from_string(1)) self.assertFalse(strutils.bool_from_string(-1)) self.assertFalse(strutils.bool_from_string(0)) self.assertFalse(strutils.bool_from_string(2))
def test_other_bool_from_string(self): self.assertFalse(strutils.bool_from_string(None)) self.assertFalse(strutils.bool_from_string(mock.Mock()))
def _test_bool_from_string(self, c): self.assertTrue(strutils.bool_from_string(c("true"))) self.assertTrue(strutils.bool_from_string(c("TRUE"))) self.assertTrue(strutils.bool_from_string(c("on"))) self.assertTrue(strutils.bool_from_string(c("On"))) self.assertTrue(strutils.bool_from_string(c("yes"))) self.assertTrue(strutils.bool_from_string(c("YES"))) self.assertTrue(strutils.bool_from_string(c("yEs"))) self.assertTrue(strutils.bool_from_string(c("1"))) self.assertTrue(strutils.bool_from_string(c("T"))) self.assertTrue(strutils.bool_from_string(c("t"))) self.assertTrue(strutils.bool_from_string(c("Y"))) self.assertTrue(strutils.bool_from_string(c("y"))) self.assertFalse(strutils.bool_from_string(c("false"))) self.assertFalse(strutils.bool_from_string(c("FALSE"))) self.assertFalse(strutils.bool_from_string(c("off"))) self.assertFalse(strutils.bool_from_string(c("OFF"))) self.assertFalse(strutils.bool_from_string(c("no"))) self.assertFalse(strutils.bool_from_string(c("0"))) self.assertFalse(strutils.bool_from_string(c("42"))) self.assertFalse(strutils.bool_from_string(c( "This should not be True"))) self.assertFalse(strutils.bool_from_string(c("F"))) self.assertFalse(strutils.bool_from_string(c("f"))) self.assertFalse(strutils.bool_from_string(c("N"))) self.assertFalse(strutils.bool_from_string(c("n"))) # Whitespace should be stripped self.assertTrue(strutils.bool_from_string(c(" 1 "))) self.assertTrue(strutils.bool_from_string(c(" true "))) self.assertFalse(strutils.bool_from_string(c(" 0 "))) self.assertFalse(strutils.bool_from_string(c(" false ")))
def test_bool_bool_from_string_no_text(self, mock_text_type): self.assertTrue(strutils.bool_from_string(True)) self.assertFalse(strutils.bool_from_string(False)) self.assertEqual(0, mock_text_type.call_count)