Skip to content

Commit

Permalink
Add state management to destroy, create, converge
Browse files Browse the repository at this point in the history
The destroy, create, and converge sub commands are logging their state
to the state file.  We will then make decisions off of that data later.
  • Loading branch information
retr0h committed Jan 7, 2017
1 parent 50c6fb3 commit 5d1066f
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 14 deletions.
2 changes: 2 additions & 0 deletions molecule/command/converge.py
Expand Up @@ -57,6 +57,8 @@ def execute(self):
self._config.provisioner.inventory_file,
self._config.scenario.converge)

self._config.state.change_state('converged', True)


@click.command()
@click.pass_context
Expand Down
2 changes: 2 additions & 0 deletions molecule/command/create.py
Expand Up @@ -56,6 +56,8 @@ def execute(self):
self._config.provisioner.inventory_file,
self._config.scenario.setup)

self._config.state.change_state('created', True)


@click.command()
@click.pass_context
Expand Down
2 changes: 2 additions & 0 deletions molecule/command/destroy.py
Expand Up @@ -56,6 +56,8 @@ def execute(self):
self._config.provisioner.inventory_file,
self._config.scenario.teardown)

self._config.state.reset()


@click.command()
@click.pass_context
Expand Down
14 changes: 12 additions & 2 deletions molecule/state.py
Expand Up @@ -24,7 +24,10 @@

from molecule import util

VALID_KEYS = ['created', ]
VALID_KEYS = [
'created',
'converged',
]


class InvalidState(Exception):
Expand Down Expand Up @@ -66,6 +69,10 @@ def wrapper(self, *args, **kwargs):
def state_file(self):
return self._state_file

@property
def converged(self):
return self._data.get('converged')

@property
def created(self):
return self._data.get('created')
Expand Down Expand Up @@ -96,7 +103,10 @@ def _get_data(self):
return self._default_data()

def _default_data(self):
return {"created": None, }
return {
'converged': None,
'created': None,
}

def _load_file(self):
with open(self.state_file) as stream:
Expand Down
2 changes: 2 additions & 0 deletions test/unit/command/test_converge.py
Expand Up @@ -36,3 +36,5 @@ def test_execute(mocker, patched_print_info, patched_ansible_converge,
patched_ansible_converge.assert_called_once_with(
config_instance.provisioner.inventory_file,
config_instance.scenario.converge)

assert config_instance.state.converged
2 changes: 2 additions & 0 deletions test/unit/command/test_create.py
Expand Up @@ -36,3 +36,5 @@ def test_execute(mocker, patched_print_info, patched_ansible_converge,
patched_ansible_converge.assert_called_once_with(
config_instance.provisioner.inventory_file,
config_instance.scenario.setup)

assert config_instance.state.created
3 changes: 3 additions & 0 deletions test/unit/command/test_destroy.py
Expand Up @@ -36,3 +36,6 @@ def test_execute(mocker, patched_print_info, patched_ansible_converge,
patched_ansible_converge.assert_called_once_with(
config_instance.provisioner.inventory_file,
config_instance.scenario.teardown)

assert not config_instance.state.converged
assert not config_instance.state.created
29 changes: 17 additions & 12 deletions test/unit/test_state.py
Expand Up @@ -41,39 +41,43 @@ def test_state_file_property(state_instance):
assert x == state_instance.state_file


def test_converged(state_instance):
assert not state_instance.converged


def test_created(state_instance):
assert not state_instance.created


def test_reset(state_instance):
assert not state_instance.created
assert not state_instance.converged

state_instance.change_state('created', True)
assert state_instance.created
state_instance.change_state('converged', True)
assert state_instance.converged

state_instance.reset()
assert not state_instance.created
assert not state_instance.converged


def test_reset_persists(state_instance):
assert not state_instance.created
assert not state_instance.converged

state_instance.change_state('created', True)
assert state_instance.created
state_instance.change_state('converged', True)
assert state_instance.converged

state_instance.reset()
assert not state_instance.created
assert not state_instance.converged

with open(state_instance.state_file) as stream:
d = yaml.safe_load(stream)

assert not d.get('created')
assert not d.get('converged')


def test_change_state(state_instance):
state_instance.change_state('created', True)
state_instance.change_state('converged', True)

assert state_instance.created
assert state_instance.converged


def test_change_state_raises(state_instance):
Expand All @@ -91,10 +95,11 @@ def test_get_data_loads_existing_state_file(temp_dir):

os.makedirs(ephemeral_directory)

data = {'created': True}
data = {'converged': False, 'created': True}
util.write_file(state_file, yaml.safe_dump(data))

c = config.Config(molecule_file)
s = state.State(c)

assert not s.converged
assert s.created

0 comments on commit 5d1066f

Please sign in to comment.