import boto.emr import pytest from tests.fixtures import emr from octopus.utils import emr_cluster @pytest.fixture(params=emr_cluster.ACTIVE_STATES) def default_emr_cluster(request): def call(): connection = emr.MockEmrConnection() connection.set_clusters([ emr.Cluster(status_state=request.param)]) return connection return call @pytest.fixture(params=emr_cluster.ACTIVE_STATES) def default_emr_cluster_with_tag(request): def call(): connection = emr.MockEmrConnection() connection.set_clusters([ emr.Cluster( id='wrong', tags=[emr.Tag(key='wrongtag', value=None)], status_state=request.param), emr.Cluster( id='found', tags=[emr.Tag(key='tag', value=None)], status_state=request.param)]) return connection return call def test_find_active_emr_cluster_on_empty_list(monkeypatch): """Try to find an emr cluster on an empty list of clusters. """ def emr_connection(): return emr.MockEmrConnection() monkeypatch.setattr( boto.emr.connection, 'EmrConnection', emr_connection) assert not emr_cluster.find_active_emr_cluster_id_by_tag('tag') def test_find_active_emr_cluster_on_list_without_tags( monkeypatch, default_emr_cluster): """Try to find an emr cluster on an empty list of clusters. """ monkeypatch.setattr( boto.emr.connection, 'EmrConnection', default_emr_cluster) assert not emr_cluster.find_active_emr_cluster_id_by_tag('tag') def test_find_active_emr_cluster_on_list_with_tag( monkeypatch, default_emr_cluster_with_tag): """Try to find an emr cluster with the right tag. """ monkeypatch.setattr( boto.emr.connection, 'EmrConnection', default_emr_cluster_with_tag) cluster_id = emr_cluster.find_active_emr_cluster_id_by_tag('tag') assert cluster_id assert cluster_id == 'found' def test_find_no_active_emr_cluster(monkeypatch): """Try to find an emr cluster on an empty list of clusters. """ def emr_connection(): connection = emr.MockEmrConnection() connection.set_clusters([ emr.Cluster( tags=[emr.Tag(key='tag', value=None)], status_state='TERMINATED'), emr.Cluster( tags=[emr.Tag(key='tag', value=None)], status_state='TERMINATED')]) return connection monkeypatch.setattr( boto.emr.connection, 'EmrConnection', emr_connection) assert not emr_cluster.find_active_emr_cluster_id_by_tag('tag')