Beispiel #1
0
 def __init__(self):
     resource.Resource.__init__(self)
     # Each nodes is identified by its peer address
     # Example:
     # { '172.17.0.31' : ['containeridentifier1', 'containeridentifier2'] }
     self.nodes = []
     self.api = CygnusAPI()
Beispiel #2
0
 def setUp(self):
     CygnusAPI.client = mockClient()
     self.api = CygnusAPI()
     self.containers = []
     self.dummy_req = dict()
     self.dummy_req['ClientRequest'] = dict()
     self.dummy_req['ClientRequest']['Request'] = None
     self.dummy_req['ClientRequest']['Method'] = 'POST'
     self.dummy_req['ServerResponse'] = 'dummy'
 def setUp(self):
     CygnusAPI.client = mockClient()
     self.api = CygnusAPI()
     self.containers = []
     self.dummy_req = dict()
     self.dummy_req['ClientRequest'] = dict()
     self.dummy_req['ClientRequest']['Request'] = None
     self.dummy_req['ClientRequest']['Method'] = 'POST'
     self.dummy_req['ServerResponse'] = 'dummy'
Beispiel #4
0
class CygnusNetworkAdapter(resource.Resource):
    isLeaf = True
    client = None

    def __init__(self):
        resource.Resource.__init__(self)
        # Each nodes is identified by its peer address
        # Example:
        # { '172.17.0.31' : ['containeridentifier1', 'containeridentifier2'] }
        self.nodes = []
        self.api = CygnusAPI()

    def render_POST(self, request):
        print(request.method, "In post")
        print('')
        requestJson = json.loads(request.content.read())
        pprint(requestJson)
        if requestJson["Type"] == 'pre-hook':
            return self.customHandlePre(request, requestJson)
        elif requestJson["Type"] == 'post-hook':
            return self.customHandlePost(request, requestJson)

    def customHandlePre(self, request, request_json):
        response = {
            "PowerstripProtocolVersion": 1,
            "ModifiedClientRequest": request_json["ClientRequest"],
        }
        # TODO: Capture the environment variables we use.
        pprint(request_json)
        request.write(json.dumps(response))
        request.finish()
        return server.NOT_DONE_YET

    def customHandlePost(self, request, requestJson):
        # We need to store a container identifier and its peer address
        # print requestJson
        # self.nodes.append(
        #     json.loads(requestJson["ServerResponse"]["Body"])["Id"])
        # print "Node registered"
        # response = {
        #        "PowerstripProtocolVersion": 1,
        #        "ModifiedServerResponse": requestJson["ServerResponse"]}
        response = self.api.getResponse(requestJson, self.nodes)
        request.write(json.dumps(response))
        request.finish()
        return server.NOT_DONE_YET
Beispiel #5
0
class CygnusApiTest(unittest.TestCase):
    def setUp(self):
        CygnusAPI.client = mockClient()
        self.api = CygnusAPI()
        self.containers = []
        self.dummy_req = dict()
        self.dummy_req['ClientRequest'] = dict()
        self.dummy_req['ClientRequest']['Request'] = None
        self.dummy_req['ClientRequest']['Method'] = 'POST'
        self.dummy_req['ServerResponse'] = 'dummy'

    def tearDown(self):
        del self.api

    def mock_createContainer(self, *args):
        randid = str(args[0])
        self.containers.append(randid)
        self.api.client.cluster_state.addContainer(randid)

    def mock_stopContainer(self, *args):
        self.api.client.cluster_state.stopContainer(str(args[0]))

    def mock_startContainer(self, *args):
        if type(args[0]) is dict:
            self.api.client.cluster_state.updateContainer(
                int(args[0]['ClientRequest']['Request'].split('/')[3]),
                'State', 1)
            return
        self.api.client.cluster_state.updateContainer(args[0], 'State', 1)

    def test_getResponse(self):
        reqs = [
            '/version/containers/create', '/version/containers/alterme/stop',
            '/version/containers/alterme/start'
        ]
        self.api.createContainer = self.mock_createContainer
        self.api.stopContainer = self.mock_stopContainer
        self.api.startContainer = self.mock_startContainer
        conts = ['1', '2', '3', '4']

        ## Create
        for cont in conts:
            self.api.createContainer(cont)

        for cont in self.api.client.cluster_state.containers.values():
            self.assertEquals(cont, 1)

        for cont in conts:
            self.api.stopContainer(cont)
        for cont in self.api.client.cluster_state.containers.values():
            self.assertEquals(cont, 0)

        for cont in conts:
            self.api.startContainer(cont)
        for cont in self.api.client.cluster_state.containers.values():
            self.assertEquals(cont, 1)
        for cont in conts:
            self.dummy_req['ClientRequest']['Request'] = (reqs[randint(
                0, 2)]).replace('alterme', cont)
            self.api.getResponse(self.dummy_req, 'foo')

    def test_getEndpoint(self):
        req = '/'
        for i in range(0, randint(3, 4)):
            for i in range(0, randint(1, 10)):
                randnum = chr(randint(48, 57))
                randlow = chr(randint(97, 122))
                randup = chr(randint(65, 90))
                randarr = [randnum, randlow, randup]
                req = req + randarr[randint(0, 2)]
            req = req + '/'
        ## erase the most right slash
        req = req[:len(req) - 1]
        ep = self.api.getEndpoint(req)
        req = req[1:]
        req_dis = req.split("/")
        if len(req_dis) == 4:
            self.assertIsInstance(ep, tuple)
            self.assertEquals(ep[0], req_dis[-1])
            self.assertEquals(ep[1], req_dis[-2])

        elif len(req_dis) == 3:
            self.assertIsInstance(ep, tuple)
            self.assertEquals(ep[0], req_dis[-1])
            self.assertEquals(ep[1], '')
class CygnusApiTest(unittest.TestCase):

    def setUp(self):
        CygnusAPI.client = mockClient()
        self.api = CygnusAPI()
        self.containers = []
        self.dummy_req = dict()
        self.dummy_req['ClientRequest'] = dict()
        self.dummy_req['ClientRequest']['Request'] = None
        self.dummy_req['ClientRequest']['Method'] = 'POST'
        self.dummy_req['ServerResponse'] = 'dummy'

    def tearDown(self):
        del self.api

    def mock_createContainer(self, *args):
        randid = str(args[0])
        self.containers.append(randid)
        self.api.client.cluster_state.addContainer(randid)

    def mock_stopContainer(self, *args):
        self.api.client.cluster_state.stopContainer(str(args[0]))

    def mock_startContainer(self, *args):
        if type(args[0]) is dict:
            self.api.client.cluster_state.updateContainer(int(args[0]['ClientRequest']['Request'].split('/')[3]), 'State', 1)
            return
        self.api.client.cluster_state.updateContainer(args[0],'State',1)

    def test_getResponse(self):
        reqs = [
                '/version/containers/create',
                '/version/containers/alterme/stop',
                '/version/containers/alterme/start'
                ]
        self.api.createContainer    = self.mock_createContainer
        self.api.stopContainer      = self.mock_stopContainer
        self.api.startContainer     = self.mock_startContainer
        conts = ['1','2','3','4']

        ## Create
        for cont in conts:
            self.api.createContainer(cont)

        for cont in self.api.client.cluster_state.containers.values():
            self.assertEquals(cont,1)

        for cont in conts:
            self.api.stopContainer(cont)
        for cont in self.api.client.cluster_state.containers.values():
            self.assertEquals(cont, 0)

        for cont in conts:
            self.api.startContainer(cont)
        for cont in self.api.client.cluster_state.containers.values():
            self.assertEquals(cont, 1)
        for cont in conts:
            self.dummy_req['ClientRequest']['Request'] = (reqs[randint(0,2)]).replace('alterme',cont)
            self.api.getResponse(self.dummy_req,'foo')

    def test_getEndpoint(self):
        req = '/'
        for i in range(0,randint(3,4)):
            for i in range(0,randint(1,10)):
                randnum = chr(randint(48,57))
                randlow = chr(randint(97,122))
                randup  = chr(randint(65,90))
                randarr = [randnum,randlow,randup]
                req = req + randarr[randint(0,2)]
            req = req + '/'
        ## erase the most right slash
        req     = req[:len(req)-1]
        ep      = self.api.getEndpoint(req)
        req     = req[1:]
        req_dis = req.split("/")
        if len(req_dis) == 4:
            self.assertIsInstance(ep, tuple)
            self.assertEquals(ep[0], req_dis[-1])
            self.assertEquals(ep[1], req_dis[-2])

        elif len(req_dis) == 3:
            self.assertIsInstance(ep, tuple)
            self.assertEquals(ep[0], req_dis[-1])
            self.assertEquals(ep[1],'')