Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
Simon János
/
orchestrator
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
ce7daa24
authored
Nov 18, 2016
by
Simon János
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
validating resource id; checking for duplicate id when adding to resource group
parent
71b45c61
Pipeline
#287
passed with stage
in 32 seconds
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
42 additions
and
14 deletions
+42
-14
orchestrator/model/resources.py
+13
-10
tests/test_model.py
+29
-4
No files found.
orchestrator/model/resources.py
View file @
ce7daa24
import
copy
import
copy
import
json
import
json
import
uuid
import
uuid
from
enum
import
Enum
from
enum
import
Enum
from
voluptuous
import
Schema
,
Invalid
,
MultipleInvalid
,
Any
,
ALLOW_EXTRA
,
PREVENT_EXTRA
from
voluptuous
import
Schema
,
Invalid
,
MultipleInvalid
,
Any
,
ALLOW_EXTRA
,
PREVENT_EXTRA
,
re
class
Resource
(
object
):
class
Resource
(
object
):
...
@@ -37,17 +37,17 @@ class Resource(object):
...
@@ -37,17 +37,17 @@ class Resource(object):
@classmethod
@classmethod
def
__schema
(
cls
,
resource_type
=
None
,
extend
=
False
):
def
__schema
(
cls
,
resource_type
=
None
,
extend
=
False
):
try
:
schema
=
Schema
(
schema
=
{
'id'
:
cls
.
__id_validator
,
'type'
:
ResourceType
.
validate
},
extra
=
ALLOW_EXTRA
)
string_validator
=
Any
(
str
,
unicode
)
except
NameError
:
string_validator
=
Any
(
str
)
schema
=
Schema
(
schema
=
{
'id'
:
string_validator
,
'type'
:
ResourceType
.
validate
},
extra
=
ALLOW_EXTRA
)
if
extend
:
if
extend
:
additional
=
resource_type
.
ADDITIONAL_SCHEMA
if
resource_type
else
cls
.
ADDITIONAL_SCHEMA
additional
=
resource_type
.
ADDITIONAL_SCHEMA
if
resource_type
else
cls
.
ADDITIONAL_SCHEMA
schema
=
schema
.
extend
(
additional
,
extra
=
PREVENT_EXTRA
)
schema
=
schema
.
extend
(
additional
,
extra
=
PREVENT_EXTRA
)
return
schema
return
schema
@staticmethod
def
__id_validator
(
resource_id
):
if
not
re
.
compile
(
'^[a-zA-Z0-9_-]{2,42}$'
)
.
match
(
resource_id
):
raise
Invalid
(
"'
%
s' is invalid"
%
resource_id
)
@property
@property
def
type
(
self
):
def
type
(
self
):
return
str
(
self
.
_attributes
[
'type'
])
return
str
(
self
.
_attributes
[
'type'
])
...
@@ -122,7 +122,10 @@ class ResourceGroup(Resource):
...
@@ -122,7 +122,10 @@ class ResourceGroup(Resource):
resource
=
Resource
(
resource
)
resource
=
Resource
(
resource
)
except
InvalidResourceException
:
except
InvalidResourceException
:
raise
InvalidResourceException
(
'ResourceGroup can only store resources'
)
raise
InvalidResourceException
(
'ResourceGroup can only store resources'
)
self
.
__resources
[
resource
.
id
]
=
resource
if
resource
.
id
not
in
self
.
__resources
.
keys
():
self
.
__resources
[
resource
.
id
]
=
resource
else
:
raise
KeyError
(
"ResourceGroup '
%
s' already contains a resource with id '
%
s'"
%
(
self
.
id
,
resource
.
id
))
def
remove
(
self
,
resource
):
def
remove
(
self
,
resource
):
try
:
try
:
...
@@ -167,7 +170,7 @@ class ResourceType(Enum):
...
@@ -167,7 +170,7 @@ class ResourceType(Enum):
except
KeyError
:
except
KeyError
:
return
cls
(
value
)
return
cls
(
value
)
except
ValueError
:
except
ValueError
:
raise
Invalid
(
'The given resource type (
%
s) is not valid'
%
value
)
raise
Invalid
(
"'
%
s' is invalid"
%
value
)
def
__str__
(
self
):
def
__str__
(
self
):
return
str
(
self
.
name
)
return
str
(
self
.
name
)
...
...
tests/test_model.py
View file @
ce7daa24
...
@@ -45,6 +45,24 @@ class ResourceTest(TestCase):
...
@@ -45,6 +45,24 @@ class ResourceTest(TestCase):
with
self
.
assertRaises
(
InvalidResourceException
):
with
self
.
assertRaises
(
InvalidResourceException
):
Resource
(
invalid_attributes
)
Resource
(
invalid_attributes
)
def
test_resource_id_validation
(
self
):
invalid_ids
=
[
'resource_id_which_is_way_too_long_to_be_valid'
,
'some.resource.id'
,
'some resource id'
,
'a'
]
for
invalid_id
in
invalid_ids
:
with
self
.
assertRaises
(
InvalidResourceException
):
Instance
(
id
=
invalid_id
)
self
.
assertIsInstance
(
Instance
(
id
=
'valid_id'
),
Instance
)
self
.
assertIsInstance
(
Instance
(
id
=
'valid-id'
),
Instance
)
self
.
assertIsInstance
(
Instance
(
id
=
'VALID'
),
Instance
)
self
.
assertIsInstance
(
Instance
(
id
=
'123456'
),
Instance
)
self
.
assertIsInstance
(
Instance
(
id
=
'_id-42'
),
Instance
)
def
test_resource_type_validation
(
self
):
def
test_resource_type_validation
(
self
):
self
.
assertTrue
(
ResourceType
.
validate
(
'instance'
))
self
.
assertTrue
(
ResourceType
.
validate
(
'instance'
))
self
.
assertTrue
(
ResourceType
.
validate
(
Instance
))
self
.
assertTrue
(
ResourceType
.
validate
(
Instance
))
...
@@ -108,7 +126,7 @@ class ResourceTest(TestCase):
...
@@ -108,7 +126,7 @@ class ResourceTest(TestCase):
# then
# then
self
.
assertEqual
(
expected_dict
,
resource_dict
)
self
.
assertEqual
(
expected_dict
,
resource_dict
)
def
test_
dynamic_resource
_type
(
self
):
def
test_
resource_dynamic
_type
(
self
):
# when
# when
resource
=
Resource
(
type
=
'instance'
)
resource
=
Resource
(
type
=
'instance'
)
...
@@ -139,7 +157,6 @@ class ResourceGroupTest(TestCase):
...
@@ -139,7 +157,6 @@ class ResourceGroupTest(TestCase):
# then
# then
self
.
assertEqual
([
resource1
],
ResourceGroup
(
resources
=
[
resource1
])
.
resources
)
self
.
assertEqual
([
resource1
],
ResourceGroup
(
resources
=
[
resource1
])
.
resources
)
self
.
assertEqual
([
resource1
],
ResourceGroup
(
resources
=
[
resource1
,
resource1
])
.
resources
)
for
resources
in
[[
resource1
,
resource2
],
[
resource2
,
resource1
]]:
for
resources
in
[[
resource1
,
resource2
],
[
resource2
,
resource1
]]:
self
.
assertEqual
(
2
,
len
(
ResourceGroup
(
resources
=
resources
)
.
resources
))
self
.
assertEqual
(
2
,
len
(
ResourceGroup
(
resources
=
resources
)
.
resources
))
self
.
assertIn
(
resource1
,
ResourceGroup
(
resources
=
resources
)
.
resources
)
self
.
assertIn
(
resource1
,
ResourceGroup
(
resources
=
resources
)
.
resources
)
...
@@ -158,7 +175,6 @@ class ResourceGroupTest(TestCase):
...
@@ -158,7 +175,6 @@ class ResourceGroupTest(TestCase):
# then
# then
self
.
assertEqual
([
expected1
],
ResourceGroup
(
resources
=
[
dict1
])
.
resources
)
self
.
assertEqual
([
expected1
],
ResourceGroup
(
resources
=
[
dict1
])
.
resources
)
self
.
assertEqual
([
expected1
],
ResourceGroup
(
resources
=
[
dict1
,
dict1
])
.
resources
)
for
resources
in
[[
expected1
,
expected2
],
[
dict2
,
dict1
]]:
for
resources
in
[[
expected1
,
expected2
],
[
dict2
,
dict1
]]:
self
.
assertEqual
(
2
,
len
(
ResourceGroup
(
resources
=
resources
)
.
resources
))
self
.
assertEqual
(
2
,
len
(
ResourceGroup
(
resources
=
resources
)
.
resources
))
self
.
assertIn
(
expected1
,
ResourceGroup
(
resources
=
resources
)
.
resources
)
self
.
assertIn
(
expected1
,
ResourceGroup
(
resources
=
resources
)
.
resources
)
...
@@ -208,7 +224,16 @@ class ResourceGroupTest(TestCase):
...
@@ -208,7 +224,16 @@ class ResourceGroupTest(TestCase):
# then
# then
self
.
assertEqual
([],
group
.
resources
)
self
.
assertEqual
([],
group
.
resources
)
# @skip('works same way as resource addition')
def
test_resource_group_duplicate_resource_id
(
self
):
# given
group
=
ResourceGroup
()
resource
=
Instance
()
# then
with
self
.
assertRaises
(
KeyError
):
group
.
add
(
resource
)
group
.
add
(
resource
)
def
test_resource_group_addition
(
self
):
def
test_resource_group_addition
(
self
):
# given
# given
group1
=
ResourceGroup
()
group1
=
ResourceGroup
()
...
...
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