Commit b7ddce31 authored by Bryton Lacquement's avatar Bryton Lacquement 🚪

wip

parent b78ac34d
...@@ -184,8 +184,8 @@ class PromiseProcess(Process): ...@@ -184,8 +184,8 @@ class PromiseProcess(Process):
raise AttributeError("Class RunPromise not found in promise" \ raise AttributeError("Class RunPromise not found in promise" \
"%s" % self.name) "%s" % self.name)
if not interface.IPromise.implementedBy(promise_module.RunPromise): if not interface.IPromise.implementedBy(promise_module.RunPromise):
raise RuntimeError("RunPromise class in %s must implements 'IPromise'" \ raise RuntimeError("RunPromise class in %s must implement 'IPromise'" \
" interface. zope_interface.implements(interface.IPromise) is" \ " interface. @implementer(interface.IPromise) is" \
" missing ?" % self.name) " missing ?" % self.name)
from slapos.grid.promise.generic import GenericPromise from slapos.grid.promise.generic import GenericPromise
......
...@@ -353,7 +353,7 @@ def updateFile(file_path, content, mode=0o600): ...@@ -353,7 +353,7 @@ def updateFile(file_path, content, mode=0o600):
"""Creates or updates a file with "content" as content.""" """Creates or updates a file with "content" as content."""
altered = False altered = False
if not (os.path.isfile(file_path)) or \ if not (os.path.isfile(file_path)) or \
not (hashlib.md5(open(file_path).read()).digest() == not (hashlib.md5(open(file_path, 'rb').read()).digest() ==
hashlib.md5(content).digest()): hashlib.md5(content).digest()):
with open(file_path, 'w') as fout: with open(file_path, 'w') as fout:
fout.write(content) fout.write(content)
......
...@@ -5,7 +5,7 @@ import netaddr ...@@ -5,7 +5,7 @@ import netaddr
import os import os
from .interface import IManager from .interface import IManager
from itertools import ifilter from six.moves import filter
from zope import interface from zope import interface
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
...@@ -88,7 +88,7 @@ class Manager(object): ...@@ -88,7 +88,7 @@ class Manager(object):
'full_ip_list', []) 'full_ip_list', [])
partition_ip_list = [tup[1] for tup in partition_ip_list] partition_ip_list = [tup[1] for tup in partition_ip_list]
partition_ipv6 = next(ifilter(lambda ip_addr: ':' in ip_addr, partition_ipv6 = next(filter(lambda ip_addr: ':' in ip_addr,
partition_ip_list), partition_ip_list),
None) None)
......
...@@ -4,17 +4,16 @@ import os ...@@ -4,17 +4,16 @@ import os
import sys import sys
import subprocess import subprocess
from zope import interface as zope_interface from zope.interface import implementer
from slapos.manager import interface from slapos.manager import interface
from slapos.grid.slapgrid import COMPUTER_PARTITION_WAIT_LIST_FILENAME from slapos.grid.slapgrid import COMPUTER_PARTITION_WAIT_LIST_FILENAME
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@implementer(interface.IManager)
class Manager(object): class Manager(object):
"""Manager is called in every step of preparation of the computer.""" """Manager is called in every step of preparation of the computer."""
zope_interface.implements(interface.IManager)
def __init__(self, config): def __init__(self, config):
"""Manager needs to know config for its functioning. """Manager needs to know config for its functioning.
""" """
......
...@@ -359,7 +359,7 @@ def requestComputerPartition(): ...@@ -359,7 +359,7 @@ def requestComputerPartition():
if slave: if slave:
# XXX: change schema to include a simple "partition_reference" which # XXX: change schema to include a simple "partition_reference" which
# is name of the instance. Then, no need to do complex search here. # is name of the instance. Then, no need to do complex search here.
slave_reference = parsed_request_dict['partition_id'] + '_' + parsed_request_dict['partition_reference'] slave_reference = parsed_request_dict['partition_id'] + b'_' + parsed_request_dict['partition_reference']
requested_computer_id = parsed_request_dict['filter_kw'].get('computer_guid', app.config['computer_id']) requested_computer_id = parsed_request_dict['filter_kw'].get('computer_guid', app.config['computer_id'])
matching_partition = getAllocatedSlaveInstance(slave_reference, requested_computer_id) matching_partition = getAllocatedSlaveInstance(slave_reference, requested_computer_id)
else: else:
......
...@@ -36,7 +36,7 @@ import psutil ...@@ -36,7 +36,7 @@ import psutil
from time import strftime from time import strftime
from slapos.collect import entity, snapshot, db, reporter from slapos.collect import entity, snapshot, db, reporter
from slapos.cli.entry import SlapOSApp from slapos.cli.entry import SlapOSApp
from ConfigParser import ConfigParser from six.moves.configparser import ConfigParser
class FakeDatabase(object): class FakeDatabase(object):
def __init__(self): def __init__(self):
......
...@@ -34,8 +34,8 @@ import json ...@@ -34,8 +34,8 @@ import json
import random import random
import logging import logging
from datetime import datetime, timedelta from datetime import datetime, timedelta
import Queue import six
from zope import interface as zope_interface from six.moves import queue
from slapos.grid.promise import interface, PromiseLauncher, PromiseProcess, PromiseError from slapos.grid.promise import interface, PromiseLauncher, PromiseProcess, PromiseError
from slapos.grid.promise.generic import (GenericPromise, TestResult, AnomalyResult, from slapos.grid.promise.generic import (GenericPromise, TestResult, AnomalyResult,
PromiseQueueResult, PROMISE_STATE_FOLDER_NAME, PromiseQueueResult, PROMISE_STATE_FOLDER_NAME,
...@@ -111,7 +111,7 @@ class TestSlapOSPromiseMixin(unittest.TestCase): ...@@ -111,7 +111,7 @@ class TestSlapOSPromiseMixin(unittest.TestCase):
'debug': False, 'debug': False,
'name': promise_name, 'name': promise_name,
'path': os.path.join(self.plugin_dir, promise_name), 'path': os.path.join(self.plugin_dir, promise_name),
'queue': Queue.Queue(), 'queue': queue.Queue(),
} }
def createPromiseProcess(self, promise_name, check_anomaly=False, wrap=False): def createPromiseProcess(self, promise_name, check_anomaly=False, wrap=False):
...@@ -135,14 +135,13 @@ class TestSlapOSPromiseMixin(unittest.TestCase): ...@@ -135,14 +135,13 @@ class TestSlapOSPromiseMixin(unittest.TestCase):
def generatePromiseScript(self, name, success=True, failure_count=1, content="", def generatePromiseScript(self, name, success=True, failure_count=1, content="",
periodicity=0.03): periodicity=0.03):
promise_content = """from zope import interface as zope_interface promise_content = """from zope.interface import implementer
from slapos.grid.promise import interface from slapos.grid.promise import interface
from slapos.grid.promise import GenericPromise from slapos.grid.promise import GenericPromise
@implementer(interface.IPromise)
class RunPromise(GenericPromise): class RunPromise(GenericPromise):
zope_interface.implements(interface.IPromise)
def __init__(self, config): def __init__(self, config):
GenericPromise.__init__(self, config) GenericPromise.__init__(self, config)
self.setPeriodicity(minute=%(periodicity)s) self.setPeriodicity(minute=%(periodicity)s)
...@@ -186,7 +185,9 @@ class TestSlapOSPromiseLauncher(TestSlapOSPromiseMixin): ...@@ -186,7 +185,9 @@ class TestSlapOSPromiseLauncher(TestSlapOSPromiseMixin):
with self.assertRaises(ImportError) as exc: with self.assertRaises(ImportError) as exc:
promise_module = promise_process._loadPromiseModule() promise_module = promise_process._loadPromiseModule()
self.assertEquals(exc.exception.message, 'No module named my_promise_no_py') self.assertEquals(str(exc.exception), 'No module named %s' %
("'%s'" % promise_name if six.PY3 else
promise_name))
def test_promise_match_interface_no_implement(self): def test_promise_match_interface_no_implement(self):
promise_name = 'my_promise_noimplement.py' promise_name = 'my_promise_noimplement.py'
...@@ -208,19 +209,18 @@ class RunPromise(GenericPromise): ...@@ -208,19 +209,18 @@ class RunPromise(GenericPromise):
with self.assertRaises(RuntimeError) as exc: with self.assertRaises(RuntimeError) as exc:
promise_module = promise_process._loadPromiseModule() promise_module = promise_process._loadPromiseModule()
message = "RunPromise class in my_promise_noimplement.py must implements" \ message = "RunPromise class in my_promise_noimplement.py must implement" \
" 'IPromise' interface. zope_interface.implements(interface.IPromise) is missing ?" " 'IPromise' interface. @implementer(interface.IPromise) is missing ?"
self.assertEquals(exc.exception.message, message) self.assertEquals(str(exc.exception), message)
def test_promise_match_interface_no_generic(self): def test_promise_match_interface_no_generic(self):
promise_name = 'my_promise_nogeneric.py' promise_name = 'my_promise_nogeneric.py'
promise_content = """from zope import interface as zope_interface promise_content = """from zope.interface import implementer
from slapos.grid.promise import interface from slapos.grid.promise import interface
@implementer(interface.IPromise)
class RunPromise(object): class RunPromise(object):
zope_interface.implements(interface.IPromise)
def __init__(self, config): def __init__(self, config):
pass pass
...@@ -236,18 +236,17 @@ class RunPromise(object): ...@@ -236,18 +236,17 @@ class RunPromise(object):
with self.assertRaises(RuntimeError) as exc: with self.assertRaises(RuntimeError) as exc:
promise_module = promise_process._loadPromiseModule() promise_module = promise_process._loadPromiseModule()
self.assertEquals(exc.exception.message, 'RunPromise class is not a subclass of GenericPromise class.') self.assertEquals(str(exc.exception), 'RunPromise class is not a subclass of GenericPromise class.')
def test_promise_match_interface_no_sense(self): def test_promise_match_interface_no_sense(self):
promise_name = 'my_promise_nosense.py' promise_name = 'my_promise_nosense.py'
promise_content = """from zope import interface as zope_interface promise_content = """from zope.interface import implementer
from slapos.grid.promise import interface from slapos.grid.promise import interface
from slapos.grid.promise import GenericPromise from slapos.grid.promise import GenericPromise
@implementer(interface.IPromise)
class RunPromise(GenericPromise): class RunPromise(GenericPromise):
zope_interface.implements(interface.IPromise)
def __init__(self, config): def __init__(self, config):
pass pass
...@@ -263,23 +262,22 @@ class RunPromise(GenericPromise): ...@@ -263,23 +262,22 @@ class RunPromise(GenericPromise):
with self.assertRaises(TypeError) as exc: with self.assertRaises(TypeError) as exc:
promise_module = promise_process._loadPromiseModule() promise_module = promise_process._loadPromiseModule()
promise = promise_module.RunPromise({}) promise = promise_module.RunPromise({})
self.assertEquals(exc.exception.message, self.assertEquals(str(exc.exception),
"Can't instantiate abstract class RunPromise with abstract methods sense") "Can't instantiate abstract class RunPromise with abstract methods sense")
def test_promise_extra_config(self): def test_promise_extra_config(self):
promise_name = 'my_promise_extra.py' promise_name = 'my_promise_extra.py'
config_dict = {'foo': 'bar', 'my-config': 4522111, config_dict = {'foo': 'bar', 'my-config': 4522111,
'text': 'developers \ninformation, sample\n\ner'} 'text': 'developers \ninformation, sample\n\ner'}
promise_content = """from zope import interface as zope_interface promise_content = """from zope.interface import implementer
from slapos.grid.promise import interface from slapos.grid.promise import interface
from slapos.grid.promise import GenericPromise from slapos.grid.promise import GenericPromise
%(config_name)s = %(config_content)s %(config_name)s = %(config_content)s
@implementer(interface.IPromise)
class RunPromise(GenericPromise): class RunPromise(GenericPromise):
zope_interface.implements(interface.IPromise)
def sense(self): def sense(self):
pass pass
...@@ -299,16 +297,15 @@ class RunPromise(GenericPromise): ...@@ -299,16 +297,15 @@ class RunPromise(GenericPromise):
def test_promise_extra_config_reserved_name(self): def test_promise_extra_config_reserved_name(self):
promise_name = 'my_promise_extra.py' promise_name = 'my_promise_extra.py'
config_dict = {'name': 'bar', 'my-config': 4522111} config_dict = {'name': 'bar', 'my-config': 4522111}
promise_content = """from zope import interface as zope_interface promise_content = """from zope.interface import implementer
from slapos.grid.promise import interface from slapos.grid.promise import interface
from slapos.grid.promise import GenericPromise from slapos.grid.promise import GenericPromise
%(config_name)s = %(config_content)s %(config_name)s = %(config_content)s
@implementer(interface.IPromise)
class RunPromise(GenericPromise): class RunPromise(GenericPromise):
zope_interface.implements(interface.IPromise)
def sense(self): def sense(self):
pass pass
...@@ -321,7 +318,7 @@ class RunPromise(GenericPromise): ...@@ -321,7 +318,7 @@ class RunPromise(GenericPromise):
with self.assertRaises(ValueError) as exc: with self.assertRaises(ValueError) as exc:
promise_module = promise_process._loadPromiseModule() promise_module = promise_process._loadPromiseModule()
self.assertEquals(exc.exception.message, "Extra parameter name 'name' cannot be used.\n%s" % config_dict) self.assertEquals(str(exc.exception), "Extra parameter name 'name' cannot be used.\n%s" % config_dict)
def test_runpromise(self): def test_runpromise(self):
promise_name = 'my_promise.py' promise_name = 'my_promise.py'
...@@ -693,7 +690,7 @@ class RunPromise(GenericPromise): ...@@ -693,7 +690,7 @@ class RunPromise(GenericPromise):
with self.assertRaises(PromiseError) as exc: with self.assertRaises(PromiseError) as exc:
self.launcher.run() self.launcher.run()
self.assertEquals(exc.exception.message, 'Promise %r failed.' % second_promise) self.assertEquals(str(exc.exception), 'Promise %r failed.' % second_promise)
self.assertTrue(os.path.exists(first_state_file)) self.assertTrue(os.path.exists(first_state_file))
self.assertTrue(os.path.exists(second_state_file)) self.assertTrue(os.path.exists(second_state_file))
...@@ -708,7 +705,7 @@ class RunPromise(GenericPromise): ...@@ -708,7 +705,7 @@ class RunPromise(GenericPromise):
time.sleep(2) time.sleep(2)
with self.assertRaises(PromiseError) as exc: with self.assertRaises(PromiseError) as exc:
self.launcher.run() # only my_first_promise will run but second_promise still failing self.launcher.run() # only my_first_promise will run but second_promise still failing
self.assertEquals(exc.exception.message, 'Promise %r failed.' % second_promise) self.assertEquals(str(exc.exception), 'Promise %r failed.' % second_promise)
first_result = json.load(open(first_state_file)) first_result = json.load(open(first_state_file))
second_result = json.load(open(second_state_file)) second_result = json.load(open(second_state_file))
...@@ -816,7 +813,7 @@ exit 1 ...@@ -816,7 +813,7 @@ exit 1
state_file = os.path.join(self.partition_dir, PROMISE_STATE_FOLDER_NAME) state_file = os.path.join(self.partition_dir, PROMISE_STATE_FOLDER_NAME)
with self.assertRaises(PromiseError) as exc: with self.assertRaises(PromiseError) as exc:
self.launcher.run() self.launcher.run()
self.assertEquals(exc.exception.message, 'Promise %r failed.' % promise_name) self.assertEquals(str(exc.exception), 'Promise %r failed.' % promise_name)
def test_runpromise_wrapped_mixed(self): def test_runpromise_wrapped_mixed(self):
self.called = 0 self.called = 0
...@@ -1073,7 +1070,7 @@ class TestSlapOSGenericPromise(TestSlapOSPromiseMixin): ...@@ -1073,7 +1070,7 @@ class TestSlapOSGenericPromise(TestSlapOSPromiseMixin):
self.configureLauncher() self.configureLauncher()
self.generatePromiseScript(self.promise_name, periodicity=1, content=promise_content, success=success) self.generatePromiseScript(self.promise_name, periodicity=1, content=promise_content, success=success)
self.writeInit() self.writeInit()
self.queue = Queue.Queue() self.queue = queue.Queue()
self.promise_config = { self.promise_config = {
'log-folder': self.log_dir, 'log-folder': self.log_dir,
'partition-folder': self.partition_dir, 'partition-folder': self.partition_dir,
......
...@@ -31,6 +31,7 @@ import errno ...@@ -31,6 +31,7 @@ import errno
import os import os
import subprocess import subprocess
import sqlite3 import sqlite3
import six
def mkdir_p(path, mode=0o700): def mkdir_p(path, mode=0o700):
...@@ -84,7 +85,7 @@ def string_to_boolean(string): ...@@ -84,7 +85,7 @@ def string_to_boolean(string):
The parser is completely arbitrary, see code for actual implementation. The parser is completely arbitrary, see code for actual implementation.
""" """
if not isinstance(string, str) and not isinstance(string, unicode): if not isinstance(string, six.binary_type) and not isinstance(string, six.text_type):
raise ValueError('Given value is not a string.') raise ValueError('Given value is not a string.')
acceptable_true_values = ['true'] acceptable_true_values = ['true']
acceptable_false_values = ['false'] acceptable_false_values = ['false']
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment