def test_ls_when_integration_is_empty_string(accounts, s3_client, mocker): s3_client.create_bucket(Bucket='some-bucket') s3_client.put_object(Bucket='some-bucket', Key='state/integration-name-1/some-file-1', Body='test') s3_client.put_object(Bucket='some-bucket', Key='state/integration-name-2/some-file-2', Body='test') s3_client.put_object(Bucket='some-bucket', Key='state/integration-name-3/nested/some-file-2', Body='test') mock_aws_api = mocker.patch('reconcile.utils.state.AWSApi', autospec=True) mock_aws_api.return_value \ .get_session.return_value \ .client.return_value = s3_client state = State('', accounts) keys = state.ls() expected = [ '/integration-name-1/some-file-1', '/integration-name-2/some-file-2', '/integration-name-3/nested/some-file-2', ] assert keys == expected
def test_ls_when_that_are_more_than_1000_keys(accounts, s3_client, mocker): s3_client.create_bucket(Bucket='some-bucket') expected = [] # Putting more than 1000 keys for i in range(0, 1010): key = f'/some-file-{i}' expected.append(key) s3_client.put_object(Bucket='some-bucket', Key=f'state/integration{key}', Body=f'{i}') # S3 response is sorted expected.sort() mock_aws_api = mocker.patch('reconcile.utils.state.AWSApi', autospec=True) mock_aws_api.return_value \ .get_session.return_value \ .client.return_value = s3_client state = State('integration', accounts) keys = state.ls() assert keys == expected
def test_ls_when_integration_is_empty_string(accounts, s3_client, mocker): s3_client.create_bucket(Bucket="some-bucket") s3_client.put_object(Bucket="some-bucket", Key="state/integration-name-1/some-file-1", Body="test") s3_client.put_object(Bucket="some-bucket", Key="state/integration-name-2/some-file-2", Body="test") s3_client.put_object( Bucket="some-bucket", Key="state/integration-name-3/nested/some-file-2", Body="test", ) mock_aws_api = mocker.patch("reconcile.utils.state.AWSApi", autospec=True) mock_aws_api.return_value.get_session.return_value.client.return_value = s3_client state = State("", accounts) keys = state.ls() expected = [ "/integration-name-1/some-file-1", "/integration-name-2/some-file-2", "/integration-name-3/nested/some-file-2", ] assert keys == expected
def ls(ctx, integration): settings = queries.get_app_interface_settings() accounts = queries.get_aws_accounts() state = State(integration, accounts, settings=settings) keys = state.ls() # if 'integration' is defined, the 0th token is empty table_content = [ {'integration': k.split('/')[0] or integration, 'key': '/'.join(k.split('/')[1:])} for k in keys] print_output('table', table_content, ['integration', 'key'])
def test_ls_when_state_is_empty(accounts, s3_client, mocker): s3_client.create_bucket(Bucket="some-bucket") mock_aws_api = mocker.patch("reconcile.utils.state.AWSApi", autospec=True) mock_aws_api.return_value.get_session.return_value.client.return_value = s3_client state = State("integration-name", accounts) keys = state.ls() assert keys == []
def ls(ctx, integration): settings = queries.get_app_interface_settings() accounts = queries.get_state_aws_accounts() state = State(integration, accounts, settings=settings) keys = state.ls() # if integration in not defined the 2th token will be the integration name key_index = 1 if integration else 2 table_content = [ {'integration': integration or k.split('/')[1], 'key': '/'.join(k.split('/')[key_index:])} for k in keys] print_output({'output': 'table', 'sort': False}, table_content, ['integration', 'key'])
def get_managed(inventory: LabelInventory, state: State) -> None: """ Fill the label inventory with the list of currently managed labels for each cluster & namespace. This information is retrieved from the state store provided in input """ keys = state.ls() # We could run threaded here: probably faster but more parallel requests. for cluster, ns_name, types in inventory: if types.get(DESIRED) is None: continue # cluster, ns_name = get_names_for_namespace(namespace) key = state_key(cluster, ns_name) if f"/{key}" not in keys: continue managed = state.get(key, []) inventory.set(cluster, ns_name, MANAGED, managed)
def test_ls_returns_correct_file(accounts, s3_client, mocker): s3_client.create_bucket(Bucket="some-bucket") s3_client.put_object(Bucket="some-bucket", Key="state/integration-name/some-file-1", Body="test") # Creating some-file-2 to identify when two or more integrations have # similar names s3_client.put_object(Bucket="some-bucket", Key="state/integration-name-2/some-file-2", Body="test") mock_aws_api = mocker.patch("reconcile.utils.state.AWSApi", autospec=True) mock_aws_api.return_value.get_session.return_value.client.return_value = s3_client state = State("integration-name", accounts) keys = state.ls() expected = ["/some-file-1"] assert keys == expected
def test_ls_returns_correct_file(accounts, s3_client, mocker): s3_client.create_bucket(Bucket='some-bucket') s3_client.put_object(Bucket='some-bucket', Key='state/integration-name/some-file-1', Body='test') # Creating some-file-2 to identify when two or more integrations have # similar names s3_client.put_object(Bucket='some-bucket', Key='state/integration-name-2/some-file-2', Body='test') mock_aws_api = mocker.patch('reconcile.utils.state.AWSApi', autospec=True) mock_aws_api.return_value \ .get_session.return_value \ .client.return_value = s3_client state = State('integration-name', accounts) keys = state.ls() expected = ['/some-file-1'] assert keys == expected