Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
slapos
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Titouan Soulard
slapos
Commits
f4ab205f
Commit
f4ab205f
authored
Oct 11, 2022
by
Cédric Le Ninivin
Committed by
Titouan Soulard
Mar 11, 2024
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
request: Update Transaction file when using jIOAPI
parent
77733217
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
83 additions
and
2 deletions
+83
-2
slapos/recipe/request.py
slapos/recipe/request.py
+26
-1
slapos/test/recipe/test_request.py
slapos/test/recipe/test_request.py
+55
-0
slapos/test/recipe/test_slaposconfiguration.py
slapos/test/recipe/test_slaposconfiguration.py
+2
-1
No files found.
slapos/recipe/request.py
View file @
f4ab205f
...
@@ -28,9 +28,10 @@ import logging
...
@@ -28,9 +28,10 @@ import logging
from
zc.buildout
import
UserError
from
zc.buildout
import
UserError
from
slapos.recipe.librecipe
import
wrap
,
JSON_SERIALISED_MAGIC_KEY
from
slapos.recipe.librecipe
import
wrap
,
JSON_SERIALISED_MAGIC_KEY
import
json
import
json
import
os
from
slapos
import
slap
as
slapmodule
from
slapos
import
slap
as
slapmodule
from
slapos.slap
import
SoftwareProductCollection
from
slapos.slap
import
SoftwareProductCollection
from
slapos.slap.slap
import
json_loads_byteified
from
slapos.slap.slap
import
json_loads_byteified
,
COMPUTER_PARTITION_REQUEST_LIST_TEMPLATE_FILENAME
import
slapos.recipe.librecipe.generic
as
librecipe
import
slapos.recipe.librecipe.generic
as
librecipe
import
traceback
import
traceback
...
@@ -178,6 +179,7 @@ class Recipe(object):
...
@@ -178,6 +179,7 @@ class Recipe(object):
if
requested_state
:
if
requested_state
:
request_dict
[
"state"
]
=
requested_state
request_dict
[
"state"
]
=
requested_state
self
.
_updateTransactionFile
(
options
[
'partition-id'
],
name
)
partition_dict
=
slap
.
jio_api_connector
.
post
(
request_dict
)
partition_dict
=
slap
.
jio_api_connector
.
post
(
request_dict
)
if
"$schema"
in
partition_dict
and
"error-response-schema.json"
in
partition_dict
[
"$schema"
]:
if
"$schema"
in
partition_dict
and
"error-response-schema.json"
in
partition_dict
[
"$schema"
]:
self
.
logger
.
warning
(
self
.
logger
.
warning
(
...
@@ -307,6 +309,29 @@ class Recipe(object):
...
@@ -307,6 +309,29 @@ class Recipe(object):
update
=
install
update
=
install
def
_updateTransactionFile
(
self
,
partition_id
,
name
):
"""
Store reference to all Instances requested by this Computer Parition
"""
# Environ variable set by Slapgrid while processing this partition
instance_root
=
os
.
environ
.
get
(
'SLAPGRID_INSTANCE_ROOT'
,
''
)
if
not
instance_root
or
not
partition_id
:
return
transaction_file_name
=
COMPUTER_PARTITION_REQUEST_LIST_TEMPLATE_FILENAME
%
partition_id
transaction_file_path
=
os
.
path
.
join
(
instance_root
,
partition_id
,
transaction_file_name
)
try
:
if
os
.
access
(
os
.
path
.
join
(
instance_root
,
partition_id
),
os
.
W_OK
):
if
not
os
.
path
.
exists
(
transaction_file_path
):
transac_file
=
open
(
transaction_file_path
,
'w'
)
transac_file
.
close
()
with
open
(
transaction_file_path
,
'a'
)
as
transac_file
:
transac_file
.
write
(
'%s
\
n
'
%
name
)
except
OSError
:
return
class
RequestOptional
(
Recipe
):
class
RequestOptional
(
Recipe
):
"""
"""
...
...
slapos/test/recipe/test_request.py
View file @
f4ab205f
import
httmock
import
httmock
import
json
import
json
import
mock
import
mock
import
os
import
unittest
import
unittest
import
tempfile
from
collections
import
defaultdict
from
collections
import
defaultdict
from
slapos.recipe
import
request
from
slapos.recipe
import
request
from
slapos.slap.slap
import
COMPUTER_PARTITION_REQUEST_LIST_TEMPLATE_FILENAME
from
test_slaposconfiguration
import
APIRequestHandler
from
test_slaposconfiguration
import
APIRequestHandler
from
testfixtures
import
LogCapture
from
testfixtures
import
LogCapture
...
@@ -195,6 +198,13 @@ class RecipejIOTestMixin:
...
@@ -195,6 +198,13 @@ class RecipejIOTestMixin:
"partition-id"
:
self
.
instance_data
[
"compute_partition_id"
],
"partition-id"
:
self
.
instance_data
[
"compute_partition_id"
],
"software-url"
:
self
.
instance_data
[
"software_release_uri"
],
"software-url"
:
self
.
instance_data
[
"software_release_uri"
],
}
}
instance_root
=
tempfile
.
mkdtemp
()
partition_root
=
os
.
path
.
join
(
instance_root
,
self
.
instance_data
[
"compute_partition_id"
])
os
.
mkdir
(
partition_root
)
os
.
environ
[
'SLAPGRID_INSTANCE_ROOT'
]
=
instance_root
transaction_file_name
=
COMPUTER_PARTITION_REQUEST_LIST_TEMPLATE_FILENAME
%
self
.
instance_data
[
"compute_partition_id"
]
self
.
transaction_file_path
=
os
.
path
.
join
(
partition_root
,
transaction_file_name
)
def
test_no_return_in_options_logs
(
self
):
def
test_no_return_in_options_logs
(
self
):
api_handler
=
APIRequestHandler
([
api_handler
=
APIRequestHandler
([
...
@@ -220,6 +230,11 @@ class RecipejIOTestMixin:
...
@@ -220,6 +230,11 @@ class RecipejIOTestMixin:
expected_request_body
[
"parameters"
]
=
json
.
dumps
(
self
.
called_partition_parameter_kw
)
expected_request_body
[
"parameters"
]
=
json
.
dumps
(
self
.
called_partition_parameter_kw
)
self
.
assertEqual
(
self
.
assertEqual
(
api_handler
.
request_payload_list
[
0
],
json
.
dumps
(
expected_request_body
))
api_handler
.
request_payload_list
[
0
],
json
.
dumps
(
expected_request_body
))
self
.
assertEqual
(
api_handler
.
sequence_list
,
[
"/api/post/"
])
self
.
assertTrue
(
os
.
path
.
exists
(
self
.
transaction_file_path
))
with
open
(
self
.
transaction_file_path
,
'r'
)
as
f
:
content_list
=
f
.
read
().
splitlines
()
self
.
assertEqual
(
sorted
(
content_list
),
[
'MyInstance'
])
def
test_return_in_options_logs
(
self
):
def
test_return_in_options_logs
(
self
):
api_handler
=
APIRequestHandler
([
api_handler
=
APIRequestHandler
([
...
@@ -244,6 +259,12 @@ class RecipejIOTestMixin:
...
@@ -244,6 +259,12 @@ class RecipejIOTestMixin:
self
.
assertEqual
(
self
.
assertEqual
(
api_handler
.
request_payload_list
[
0
],
json
.
dumps
(
expected_request_body
))
api_handler
.
request_payload_list
[
0
],
json
.
dumps
(
expected_request_body
))
self
.
assertEqual
(
self
.
options
[
"connection-anything"
],
"done"
)
self
.
assertEqual
(
self
.
options
[
"connection-anything"
],
"done"
)
self
.
assertEqual
(
api_handler
.
sequence_list
,
[
"/api/post/"
])
self
.
assertTrue
(
os
.
path
.
exists
(
self
.
transaction_file_path
))
with
open
(
self
.
transaction_file_path
,
'r'
)
as
f
:
content_list
=
f
.
read
().
splitlines
()
self
.
assertEqual
(
sorted
(
content_list
),
[
'MyInstance'
])
def
test_return_not_ready
(
self
):
def
test_return_not_ready
(
self
):
self
.
instance_data
[
"connection_parameters"
]
=
self
.
connection_parameter_dict_empty
self
.
instance_data
[
"connection_parameters"
]
=
self
.
connection_parameter_dict_empty
...
@@ -272,6 +293,11 @@ class RecipejIOTestMixin:
...
@@ -272,6 +293,11 @@ class RecipejIOTestMixin:
self
.
assertEqual
(
self
.
assertEqual
(
api_handler
.
request_payload_list
[
0
],
json
.
dumps
(
expected_request_body
))
api_handler
.
request_payload_list
[
0
],
json
.
dumps
(
expected_request_body
))
self
.
assertEqual
(
self
.
options
[
"connection-anything"
],
""
)
self
.
assertEqual
(
self
.
options
[
"connection-anything"
],
""
)
self
.
assertEqual
(
api_handler
.
sequence_list
,
[
"/api/post/"
])
self
.
assertTrue
(
os
.
path
.
exists
(
self
.
transaction_file_path
))
with
open
(
self
.
transaction_file_path
,
'r'
)
as
f
:
content_list
=
f
.
read
().
splitlines
()
self
.
assertEqual
(
sorted
(
content_list
),
[
'MyInstance'
])
def
test_return_ready
(
self
):
def
test_return_ready
(
self
):
api_handler
=
APIRequestHandler
([
api_handler
=
APIRequestHandler
([
...
@@ -297,6 +323,35 @@ class RecipejIOTestMixin:
...
@@ -297,6 +323,35 @@ class RecipejIOTestMixin:
api_handler
.
request_payload_list
[
0
],
json
.
dumps
(
expected_request_body
))
api_handler
.
request_payload_list
[
0
],
json
.
dumps
(
expected_request_body
))
self
.
assertEqual
(
self
.
options
[
"connection-anything"
],
"done"
)
self
.
assertEqual
(
self
.
options
[
"connection-anything"
],
"done"
)
self
.
assertIsInstance
(
self
.
options
[
'connection-anything'
],
str
)
self
.
assertIsInstance
(
self
.
options
[
'connection-anything'
],
str
)
self
.
assertEqual
(
api_handler
.
sequence_list
,
[
"/api/post/"
])
self
.
assertTrue
(
os
.
path
.
exists
(
self
.
transaction_file_path
))
with
open
(
self
.
transaction_file_path
,
'r'
)
as
f
:
content_list
=
f
.
read
().
splitlines
()
self
.
assertEqual
(
sorted
(
content_list
),
[
'MyInstance'
])
def
test_two_requests_return_ready
(
self
):
# Request first instance
api_handler
=
APIRequestHandler
([
(
"/api/get"
,
json
.
dumps
(
self
.
instance_data
)),
])
self
.
options
[
'return'
]
=
'anything'
with
httmock
.
HTTMock
(
api_handler
.
request_handler
):
recipe
=
self
.
recipe
(
self
.
buildout
,
"request"
,
self
.
options
)
result
=
recipe
.
install
()
# Request Second Instance
self
.
options
[
"name"
]
=
self
.
instance_data
[
"title"
]
=
'MyInstance2'
self
.
instance_data
[
"reference"
]
=
"SOFTINST-13"
api_handler
=
APIRequestHandler
([
(
"/api/get"
,
json
.
dumps
(
self
.
instance_data
)),
])
self
.
options
[
'return'
]
=
'anything'
with
httmock
.
HTTMock
(
api_handler
.
request_handler
):
recipe
=
self
.
recipe
(
self
.
buildout
,
"request"
,
self
.
options
)
result
=
recipe
.
install
()
self
.
assertTrue
(
os
.
path
.
exists
(
self
.
transaction_file_path
))
with
open
(
self
.
transaction_file_path
,
'r'
)
as
f
:
content_list
=
f
.
read
().
splitlines
()
self
.
assertEqual
(
sorted
(
content_list
),
[
'MyInstance'
,
'MyInstance2'
])
class
RequestjIOTest
(
RecipejIOTestMixin
,
unittest
.
TestCase
):
class
RequestjIOTest
(
RecipejIOTestMixin
,
unittest
.
TestCase
):
recipe
=
request
.
Recipe
recipe
=
request
.
Recipe
...
...
slapos/test/recipe/test_slaposconfiguration.py
View file @
f4ab205f
...
@@ -18,12 +18,13 @@ class APIRequestHandler(object):
...
@@ -18,12 +18,13 @@ class APIRequestHandler(object):
self
.
sequence_list
=
[]
self
.
sequence_list
=
[]
def
request_handler
(
self
,
url
,
req
):
def
request_handler
(
self
,
url
,
req
):
self
.
sequence_list
.
append
(
url
.
path
)
if
url
.
path
==
"/getHateoasUrl"
:
if
url
.
path
==
"/getHateoasUrl"
:
return
""
return
""
elif
url
.
path
==
"/getJIOAPIUrl"
:
elif
url
.
path
==
"/getJIOAPIUrl"
:
return
"https://127.0.0.1/api/"
return
"https://127.0.0.1/api/"
self
.
sequence_list
.
append
(
url
.
path
)
if
not
self
.
response_list
and
self
.
response_list
[
0
][
0
]
!=
url
.
path
:
if
not
self
.
response_list
and
self
.
response_list
[
0
][
0
]
!=
url
.
path
:
raise
ValueError
(
"Unexcpected call: %s %s"
%
(
url
.
path
,
req
.
body
))
raise
ValueError
(
"Unexcpected call: %s %s"
%
(
url
.
path
,
req
.
body
))
...
...
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