-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
44 additions
and
173 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,66 +6,14 @@ | |
|
||
from mock import Mock, call, patch | ||
|
||
from rapyuta_io.clients.secret import SecretConfigSourceBasicAuth, SecretConfigSourceSSHAuth, SecretType, \ | ||
from rapyuta_io.clients.secret import SecretType, \ | ||
SecretConfigDocker, DOCKER_HUB_REGISTRY, Secret | ||
from rapyuta_io.utils import InvalidParameterException, InternalServerError, ResourceNotFoundError | ||
from tests.utils.client import get_client, headers, AUTH_TOKEN | ||
from tests.utils.secrets_responses import SECRET_CREATE_SUCCESS, SECRET_LIST_SUCCESS, SECRET_UPDATE_SUCCESS | ||
|
||
|
||
class SecretConfigTests(unittest.TestCase): | ||
def test_bad_secret_config_ssh_auth(self): | ||
expected_err_msg = 'ssh_key cannot be empty' | ||
with self.assertRaises(InvalidParameterException) as e: | ||
SecretConfigSourceSSHAuth(ssh_key='') | ||
self.assertEqual(expected_err_msg, str(e.exception)) | ||
|
||
def test_secret_config_ssh_auth(self): | ||
secret_config = SecretConfigSourceSSHAuth(ssh_key='ssh-key') | ||
expected_serialize = {'ssh-privatekey': base64.b64encode('ssh-key'.encode()).decode()} | ||
self.assertEqual('ssh-key', secret_config.ssh_key) | ||
self.assertEqual(SecretType.SOURCE_SSH_AUTH, secret_config.get_type()) | ||
self.assertEqual(expected_serialize, secret_config.serialize()) | ||
|
||
def test_bad_secret_config_basic_auth_empty_username(self): | ||
expected_err_msg = 'username cannot be empty' | ||
with self.assertRaises(InvalidParameterException) as e: | ||
SecretConfigSourceBasicAuth(username='', password='password', ca_cert='ca-cert') | ||
self.assertEqual(expected_err_msg, str(e.exception)) | ||
|
||
def test_bad_secret_config_basic_auth_empty_password(self): | ||
expected_err_msg = 'password cannot be empty' | ||
with self.assertRaises(InvalidParameterException) as e: | ||
SecretConfigSourceBasicAuth(username='username', password='', ca_cert='ca-cert') | ||
self.assertEqual(expected_err_msg, str(e.exception)) | ||
|
||
def test_bad_secret_config_basic_auth_empty_ca_cert(self): | ||
expected_err_msg = 'ca_cert cannot be empty' | ||
with self.assertRaises(InvalidParameterException) as e: | ||
SecretConfigSourceBasicAuth(username='username', password='password', ca_cert='') | ||
self.assertEqual(expected_err_msg, str(e.exception)) | ||
|
||
def test_secret_config_basic_auth_empty_ca_cert(self): | ||
secret_config = SecretConfigSourceBasicAuth(username='username', password='password') | ||
expected_serialize = {'username': base64.b64encode('username'.encode()).decode(), | ||
'password': base64.b64encode('password'.encode()).decode()} | ||
self.assertEqual('username', secret_config.username) | ||
self.assertEqual('password', secret_config.password) | ||
self.assertIsNone(secret_config.ca_cert) | ||
self.assertEqual(SecretType.SOURCE_BASIC_AUTH, secret_config.get_type()) | ||
self.assertEqual(expected_serialize, secret_config.serialize()) | ||
|
||
def test_secret_config_basic_auth_with_ca_cert(self): | ||
secret_config = SecretConfigSourceBasicAuth(username='username', password='password', ca_cert='ca-cert') | ||
expected_serialize = {'username': base64.b64encode('username'.encode()).decode(), | ||
'password': base64.b64encode('password'.encode()).decode(), | ||
'ca.crt': base64.b64encode('ca-cert'.encode()).decode()} | ||
self.assertEqual('username', secret_config.username) | ||
self.assertEqual('password', secret_config.password) | ||
self.assertEqual('ca-cert', secret_config.ca_cert) | ||
self.assertEqual(SecretType.SOURCE_BASIC_AUTH, secret_config.get_type()) | ||
self.assertEqual(expected_serialize, secret_config.serialize()) | ||
|
||
def test_bad_secret_config_docker_empty_username(self): | ||
expected_err_msg = 'username cannot be empty' | ||
with self.assertRaises(InvalidParameterException) as e: | ||
|
@@ -118,42 +66,36 @@ class SecretTests(unittest.TestCase): | |
def test_bad_secret_name_length(self): | ||
expected_err_msg = 'length of name must be between 3 and 253 characters' | ||
with self.assertRaises(InvalidParameterException) as e: | ||
Secret(name='a' * 300, secret_config=SecretConfigSourceSSHAuth('ssh-key')) | ||
Secret(name='a' * 300, secret_config=SecretConfigDocker(username='username', password='password', email='[email protected]', | ||
registry='quay.io')) | ||
self.assertEqual(expected_err_msg, str(e.exception)) | ||
|
||
def test_bad_secret_name_pattern(self): | ||
expected_err_msg = 'name must consist of lower case alphanumeric characters or - and must start and end with ' \ | ||
'an alphanumeric character' | ||
with self.assertRaises(InvalidParameterException) as e: | ||
Secret(name='-SECRET-', secret_config=SecretConfigSourceSSHAuth('ssh-key')) | ||
Secret(name='-SECRET-', secret_config=SecretConfigDocker(username='username', password='password', email='[email protected]', | ||
registry='quay.io')) | ||
self.assertEqual(expected_err_msg, str(e.exception)) | ||
|
||
def test_bad_secret_name_type(self): | ||
expected_err_msg = 'name must be a string' | ||
with self.assertRaises(InvalidParameterException) as e: | ||
Secret(name=123, secret_config=SecretConfigSourceSSHAuth('ssh-key')) | ||
self.assertEqual(expected_err_msg, str(e.exception)) | ||
|
||
def test_bad_secret_config(self): | ||
expected_err_msg = 'secret_config must be of type SourceSecretBasicConfig, SourceSecretSSHConfig or ' \ | ||
'DockerSecretConfig' | ||
with self.assertRaises(InvalidParameterException) as e: | ||
Secret(name='bad', secret_config='invalid-secret') | ||
Secret(name=123, secret_config=SecretConfigDocker(username='username', password='password', email='[email protected]', | ||
registry='quay.io')) | ||
self.assertEqual(expected_err_msg, str(e.exception)) | ||
|
||
def test_create_secret_invalid_secret_type(self): | ||
expected_err_msg = 'secret must be non-empty and of type rapyuta_io.clients.secret.Secret' | ||
client = get_client() | ||
with self.assertRaises(InvalidParameterException) as e: | ||
client.create_secret('invalid-secret-type') | ||
|
||
self.assertEqual(str(e.exception), expected_err_msg) | ||
|
||
@patch('requests.request') | ||
def test_create_secret_internal_server_error(self, mock_request): | ||
secret = Secret('test-secret', SecretConfigSourceSSHAuth('ssh-key')) | ||
secret = Secret('test-secret', SecretConfigDocker(username='username', password='password', email='[email protected]', | ||
registry='quay.io')) | ||
docker_config = '{"quay.io": {"username": "username", "password": "password", "email": "[email protected]", "auth": "dXNlcm5hbWU6cGFzc3dvcmQ="}}' | ||
client = get_client() | ||
expected_payload = {'type': SecretType.SOURCE_SSH_AUTH, 'data': {'ssh-privatekey': base64.b64encode('ssh-key'.encode()).decode()}, 'name': 'test-secret'} | ||
expected_payload = { | ||
'type': str(SecretType.DOCKER), | ||
'data': {'.dockercfg': base64.b64encode(docker_config.encode()).decode()}, | ||
'name': 'test-secret' | ||
} | ||
expected_url = 'https://gaapiserver.apps.okd4v2.prod.rapyuta.io/api/secret/create' | ||
mock_secret = Mock() | ||
mock_secret.status_code = requests.codes.INTERNAL_SERVER_ERROR | ||
|
@@ -166,9 +108,15 @@ def test_create_secret_internal_server_error(self, mock_request): | |
|
||
@patch('requests.request') | ||
def test_create_secret_success(self, mock_request): | ||
secret = Secret('test-secret', SecretConfigSourceSSHAuth('ssh-key')) | ||
secret = Secret('test-secret', SecretConfigDocker(username='username', password='password', email='[email protected]', | ||
registry='quay.io')) | ||
docker_config = '{"quay.io": {"username": "username", "password": "password", "email": "[email protected]", "auth": "dXNlcm5hbWU6cGFzc3dvcmQ="}}' | ||
client = get_client() | ||
expected_payload = {'type': SecretType.SOURCE_SSH_AUTH, 'data': {'ssh-privatekey': base64.b64encode('ssh-key'.encode()).decode()}, 'name': 'test-secret'} | ||
expected_payload = { | ||
'type': str(SecretType.DOCKER), | ||
'data': {'.dockercfg': base64.b64encode(docker_config.encode()).decode()}, | ||
'name': 'test-secret' | ||
} | ||
expected_url = 'https://gaapiserver.apps.okd4v2.prod.rapyuta.io/api/secret/create' | ||
mock_secret = Mock() | ||
mock_secret.text = SECRET_CREATE_SUCCESS | ||
|
@@ -296,7 +244,7 @@ def test_delete_secret_success(self, mock_request): | |
|
||
@patch('requests.request') | ||
def test_delete_method_internal_server_error(self, mock_request): | ||
secret = Secret('test-secret', SecretConfigSourceSSHAuth('ssh-key')) | ||
secret = Secret('test-secret', SecretConfigDocker(username='username', password='password', email='[email protected]')) | ||
setattr(secret, '_core_api_host', 'https://gaapiserver.apps.okd4v2.prod.rapyuta.io') | ||
setattr(secret, '_auth_token', 'Bearer ' + AUTH_TOKEN) | ||
setattr(secret, '_project', 'test_project') | ||
|
@@ -313,7 +261,7 @@ def test_delete_method_internal_server_error(self, mock_request): | |
]) | ||
|
||
def test_delete_method_invalid_parameter(self): | ||
secret = Secret('test-secret', SecretConfigSourceSSHAuth('ssh-key')) | ||
secret = Secret('test-secret', SecretConfigDocker(username='username', password='password', email='[email protected]')) | ||
expected_err_msg = 'Secret must be created first' | ||
setattr(secret, 'guid', 'secret-guid') | ||
with self.assertRaises(InvalidParameterException) as e: | ||
|
@@ -322,7 +270,8 @@ def test_delete_method_invalid_parameter(self): | |
|
||
@patch('requests.request') | ||
def test_delete_method_success(self, mock_request): | ||
secret = Secret('test-secret', SecretConfigSourceSSHAuth('ssh-key')) | ||
secret = Secret('test-secret', SecretConfigDocker(username='username', password='password', email='[email protected]', | ||
registry='quay.io')) | ||
setattr(secret, '_core_api_host', 'https://gaapiserver.apps.okd4v2.prod.rapyuta.io') | ||
setattr(secret, '_auth_token', 'Bearer ' + AUTH_TOKEN) | ||
setattr(secret, '_project', 'test_project') | ||
|
@@ -340,14 +289,13 @@ def test_delete_method_success(self, mock_request): | |
|
||
@patch('requests.request') | ||
def test_update_method_success(self, mock_request): | ||
secret = Secret('test-secret', SecretConfigSourceBasicAuth(username="testuser", password='testpassword')) | ||
secret = Secret('test-secret', SecretConfigDocker(username='username', password='password', email='[email protected]', | ||
registry='quay.io')) | ||
docker_config = '{"quay.io": {"username": "username", "password": "password", "email": "[email protected]", "auth": "dXNlcm5hbWU6cGFzc3dvcmQ="}}' | ||
client = get_client() | ||
expected_payload = { | ||
'type': SecretType.SOURCE_BASIC_AUTH, | ||
'data': { | ||
"username": base64.b64encode('testuser'.encode()).decode(), | ||
"password": base64.b64encode('testpassword'.encode()).decode() | ||
}, | ||
'type': str(SecretType.DOCKER), | ||
'data': {'.dockercfg': base64.b64encode(docker_config.encode()).decode()}, | ||
'name': 'test-secret' | ||
} | ||
expected_url = 'https://gaapiserver.apps.okd4v2.prod.rapyuta.io/api/secret/secret-guid/update' | ||
|
@@ -363,14 +311,13 @@ def test_update_method_success(self, mock_request): | |
|
||
@patch('requests.request') | ||
def test_update_method_internal_server_error(self, mock_request): | ||
secret = Secret('test-secret', SecretConfigSourceBasicAuth(username="testuser", password='testpassword')) | ||
secret = Secret('test-secret', SecretConfigDocker(username='username', password='password', email='[email protected]', | ||
registry='quay.io')) | ||
docker_config = '{"quay.io": {"username": "username", "password": "password", "email": "[email protected]", "auth": "dXNlcm5hbWU6cGFzc3dvcmQ="}}' | ||
client = get_client() | ||
expected_payload = { | ||
'type': SecretType.SOURCE_BASIC_AUTH, | ||
'data': { | ||
"username": base64.b64encode('testuser'.encode()).decode(), | ||
"password": base64.b64encode('testpassword'.encode()).decode() | ||
}, | ||
'type': str(SecretType.DOCKER), | ||
'data': {'.dockercfg': base64.b64encode(docker_config.encode()).decode()}, | ||
'name': 'test-secret' | ||
} | ||
expected_url = 'https://gaapiserver.apps.okd4v2.prod.rapyuta.io/api/secret/secret-guid/update' | ||
|
@@ -385,14 +332,13 @@ def test_update_method_internal_server_error(self, mock_request): | |
|
||
@patch('requests.request') | ||
def test_update_method_not_found_error(self, mock_request): | ||
secret = Secret('test-secret', SecretConfigSourceBasicAuth(username="testuser", password='testpassword')) | ||
secret = Secret('test-secret', SecretConfigDocker(username='username', password='password', email='[email protected]', | ||
registry='quay.io')) | ||
docker_config = '{"quay.io": {"username": "username", "password": "password", "email": "[email protected]", "auth": "dXNlcm5hbWU6cGFzc3dvcmQ="}}' | ||
client = get_client() | ||
expected_payload = { | ||
'type': SecretType.SOURCE_BASIC_AUTH, | ||
'data': { | ||
"username": base64.b64encode('testuser'.encode()).decode(), | ||
"password": base64.b64encode('testpassword'.encode()).decode() | ||
}, | ||
'type': str(SecretType.DOCKER), | ||
'data': {'.dockercfg': base64.b64encode(docker_config.encode()).decode()}, | ||
'name': 'test-secret' | ||
} | ||
expected_url = 'https://gaapiserver.apps.okd4v2.prod.rapyuta.io/api/secret/secret-guid/update' | ||
|