Skip to content

Commit

Permalink
Merge pull request #20 from frankenmichl/fix_issue_19
Browse files Browse the repository at this point in the history
Fix github issue #19
  • Loading branch information
frankenmichl authored Jan 20, 2021
2 parents 5934fd3 + ff5d340 commit 986470c
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 24 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ test:
pycodestyle tests/test_host_lock.py
pycodestyle tests/test_bootscript.py
pycodestyle tests/test_jobid.py
py.test --cov-report=term --cov=baremetal_support tests/
py.test -s --cov-report=term --cov=baremetal_support tests/

.PHONY: init test
35 changes: 16 additions & 19 deletions baremetal_support/bootscript.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def __init__(self, app):

def set(self, ip, script):
""" set the bootscript in the dict """
self.bootscript.update({ip: script})
self.bootscript[ip] = script

def get(self, ip):
""" return specific bootscript """
Expand All @@ -35,40 +35,37 @@ def get(self, ip):
except KeyError:
raise BootscriptNotFound("no script found for requested ip")

def _to_ip(self, addr):
""" convert an address/IP to an IP """
def _is_ip(self, addr):
try:
addr = socket.inet_aton(addr)
except Exception as e:
raise
socket.inet_aton(addr)
return True
except socket.error:
return False

def http_get_bootscript_for_peer(self):
addr = bottle.request.environ.get('REMOTE_ADDR')
return self.http_get_bootscript(addr)

def http_get_bootscript(self, addr):
try:
ip = self._to_ip(addr)
bottle.response.content_type = 'text/text; charset=utf-8'
return self.get(ip)

except socket.error:
# invalid address specified
bottle.response.status = 400
if self._is_ip(addr):
bottle.response.content_type = 'text/text; charset=utf-8'
return self.get(addr)
else:
# invalid address specified
bottle.response.status = 400
except BootscriptNotFound:
# no script found for this IP
bottle.response.body = 'not found'
bottle.response.status = '404 Not Found'
return bottle.response

def http_set_bootscript(self, addr):
try:
ip = self._to_ip(addr)
print("GET: " + addr)
if self._is_ip(addr):
postdata = bottle.request.body.read()
script = postdata.decode('utf-8')
self.set(ip, script)
self.set(addr, script)
bottle.response.status = 200

except socket.error:
# invalid address specified
else:
bottle.response.status = 400
2 changes: 1 addition & 1 deletion baremetal_support/jobid.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,4 @@ def http_get_latest_job(self, arch, distri, flavor, version, test):
return str(result)
except LatestJobNotFound:
bottle.response.body = 'not found'
bootle.respons.status = '404 Not Found'
bottle.response.status = '404 Not Found'
32 changes: 29 additions & 3 deletions tests/test_baremetal_Support.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ def test_baremetal_support():
p.start()
sleep(1)

with pytest.raises(socket.error):
inval = server._bootscript._to_ip("foobar")
assert not server._bootscript._is_ip("foobar")


# request bootscript api
r1 = requests.post(err_url, data='illegal')
Expand Down Expand Up @@ -142,9 +142,35 @@ def test_baremetal_support():
r22 = requests.put(url_unlock3)
assert r22.status_code == 412


# this test verifies issue #19
url_bootscript1 = url + 'bootscript/script.ipxe/10.0.0.1'
url_bootscript2 = url + 'bootscript/script.ipxe/10.0.0.2'
count = 0
bootscript1 = "bootscript1"
bootscript2 = "bootscript2"
while (count < 1000):
print("count: " + str(count));

r30 = requests.post(url_bootscript1, data=bootscript1)
assert r30.status_code == 200

r31 = requests.get(url_bootscript1)
assert r31.status_code == 200
assert r31.text == bootscript1, "after "

r32 = requests.post(url_bootscript2, data=bootscript2)
assert r32.status_code == 200

r33 = requests.get(url_bootscript1)
assert r33.status_code == 200
assert r33.text == bootscript1, "after " + str(count)

r34 = requests.get(url_bootscript2)
assert r34.status_code == 200
assert r34.text == bootscript2

count = count + 1

p.terminate()
p.join()

Expand Down
8 changes: 8 additions & 0 deletions tests/test_bootscript.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ def test_set():
bs.set("10.0.0.1", "bar")
assert bs.get("10.0.0.1") == "bar"

count = 0
while (count < 1000000):
bs.set("10.0.0.1", "bar")
bs.set("10.0.0.2", "foobar")
assert bs.get("10.0.0.1") == "bar"
assert bs.get("10.0.0.2") == "foobar"
count = count + 1


def test_get():
app = Bottle()
Expand Down

0 comments on commit 986470c

Please sign in to comment.