Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
Gelencsér Szabolcs
/
cloud
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
b69df995
authored
Nov 21, 2013
by
Őry Máté
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
vm: emit {pre,post}_state_changed signals
parent
1df0e6ed
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
57 additions
and
3 deletions
+57
-3
circle/vm/models.py
+13
-2
circle/vm/tests/test_models.py
+44
-1
No files found.
circle/vm/models.py
View file @
b69df995
...
...
@@ -10,6 +10,7 @@ from django.core import signing
from
django.db.models
import
(
Model
,
ForeignKey
,
ManyToManyField
,
IntegerField
,
DateTimeField
,
BooleanField
,
TextField
,
CharField
,
permalink
,
Manager
)
from
django.dispatch
import
Signal
from
django.utils
import
timezone
from
django.utils.translation
import
ugettext_lazy
as
_
...
...
@@ -33,6 +34,9 @@ ARCHITECTURES = (('x86_64', 'x86-64 (64 bit)'),
(
'i686'
,
'x86 (32 bit)'
))
VNC_PORT_RANGE
=
(
2000
,
65536
)
# inclusive start, exclusive end
pre_state_changed
=
Signal
(
providing_args
=
[
"new_state"
])
post_state_changed
=
Signal
(
providing_args
=
[
"new_state"
])
class
InstanceActiveManager
(
Manager
):
def
get_query_set
(
self
):
...
...
@@ -858,8 +862,15 @@ class Instance(AclBase, VirtualMachineDescModel, TimeStampedModel):
logger
.
debug
(
'Instance
%
s state changed '
'(db:
%
s, new:
%
s)'
,
self
,
self
.
state
,
new_state
)
self
.
state
=
new_state
self
.
save
()
try
:
pre_state_changed
.
send
(
sender
=
self
,
new_state
=
new_state
)
except
Exception
as
e
:
logger
.
info
(
'Instance
%
s state change ignored:
%
s'
,
unicode
(
self
),
unicode
(
e
))
else
:
self
.
state
=
new_state
self
.
save
()
post_state_changed
.
send
(
sender
=
self
,
new_state
=
new_state
)
class
InstanceActivity
(
ActivityModel
):
...
...
circle/vm/tests/test_models.py
View file @
b69df995
from
django.test
import
TestCase
from
..models
import
InstanceTemplate
from
mock
import
Mock
from
..models
import
(
InstanceTemplate
,
Instance
,
pre_state_changed
,
post_state_changed
)
class
TemplateTestCase
(
TestCase
):
def
test_template_creation
(
self
):
template
=
InstanceTemplate
(
name
=
'My first template'
,
access_method
=
'ssh'
,
)
template
.
clean
()
# TODO add images & net
class
InstanceTestCase
(
TestCase
):
def
test_pre_state_changed_w_exception
(
self
):
"""Signal handler of pre_state_changed prevents save with Exception."""
def
callback
(
sender
,
new_state
,
**
kwargs
):
if
new_state
==
'invalid value'
:
raise
Exception
()
pre_state_changed
.
connect
(
callback
)
i
=
Instance
(
state
=
'NOSTATE'
)
i
.
save
=
Mock
()
i
.
state_changed
(
'invalid value'
)
assert
i
.
state
==
'NOSTATE'
assert
not
i
.
save
.
called
def
test_pre_state_changed_wo_exception
(
self
):
"""Signal handler of pre_state_changed allows save."""
mock
=
Mock
()
pre_state_changed
.
connect
(
mock
)
i
=
Instance
(
state
=
'NOSTATE'
)
i
.
save
=
Mock
()
i
.
state_changed
(
'RUNNING'
)
assert
i
.
state
==
'RUNNING'
assert
mock
.
called
assert
i
.
save
.
called
def
test_post_state_changed
(
self
):
"""Signal handler of post_state_changed runs."""
mock
=
Mock
()
post_state_changed
.
connect
(
mock
)
i
=
Instance
(
state
=
'NOSTATE'
)
i
.
save
=
Mock
()
i
.
state_changed
(
'RUNNING'
)
assert
mock
.
called
assert
i
.
save
.
called
assert
i
.
state
==
'RUNNING'
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment