Esempio n. 1
0
 def test_get_headers(self):
     client = Client(None, None, None, None, 'trial', False)
     eq_(
         {
             'X-Rapidci-Version': version.__version__,
             'Content-Type': 'application/json',
             'X-Rapidci-Api-Key': "trial"
         }, client.get_headers())
Esempio n. 2
0
    def register_request(self, in_request):
        if 'Content-Type' in in_request.headers and in_request.headers['Content-Type'] == 'application/json':
            if 'X-Rapidci-Register-Key' in in_request.headers and in_request.headers['X-Rapidci-Register-Key'] == self.flask_app.rapid_config.register_api_key:
                remote_addr = in_request.remote_addr
                remote_port = in_request.headers['X-Rapidci-port'] if 'X-Rapidci-port' in in_request.headers else None
                time_elapse = max((time.time() * 1000) - float(in_request.headers['X-Rapidci-time']), 1) if 'X-Rapidci-time' in in_request.headers else None

                grains = in_request.json['grains'] if 'grains' in in_request.json else ''
                hostname = in_request.json['hostname'] if 'hostname' in in_request.json else 'unknown'
                grain_restrict = in_request.json['grain_restrict'] if 'grain_restrict' in in_request.json else False

                api_key = in_request.headers['X-Rapidci-Client-Key'] if 'X-Rapidci-Client-Key' in in_request.headers else False
                is_ssl = in_request.headers['X-Is-Ssl'].lower() == 'true' if 'X-is_ssl' in in_request.headers else False

                if remote_port == 443:
                    is_ssl = True

                if not api_key:
                    raise Exception("NO API KEY!")
                client = Client(remote_addr, int(remote_port), grains, grain_restrict, api_key, is_ssl, hostname, time_elapse)

                if HeaderConstants.SINGLE_USE not in in_request.headers:
                    self.store_client(remote_addr, client)

                return Response(jsonpickle.encode(client),
                                content_type='application/json', headers={'Content-Type': 'application/json',
                                                                          'X-Rapidci-Master-Key': self.flask_app.rapid_config.api_key,
                                                                          Version.HEADER: Version.get_version()})
        raise Exception('Not Allowed')
Esempio n. 3
0
    def test_get_state(self):
        client = Client('127.0.0.1',
                        9000,
                        'one;to;three',
                        False,
                        'ApiKey',
                        hostname='bogus',
                        time_elapse=1000)

        eq_(
            {
                'grain_restrict': False,
                'is_ssl': False,
                'sleep': False,
                'grains': ['one', 'to', 'three'],
                'api_key': 'ApiKey',
                'ip_address': '127.0.0.1',
                'port': 9000,
                'time_elapse': 1.5,
                'hostname': 'bogus'
            }, client.__getstate__())
Esempio n. 4
0
    def test_send_work(self, requests):
        client = Client(None, None, None, False)
        work_request = WorkRequest()

        client.send_work(work_request)
        eq_(1, requests.post.call_count)
        requests.post.assert_called_with(client.get_work_uri(),
                                         json=work_request.__dict__,
                                         headers=client.get_headers(),
                                         verify=True,
                                         timeout=4)
Esempio n. 5
0
    def test_can_handle_complex_grain(self):
        client = Client('127.0.0.1', 9000, 'one;two;three', None)

        ok_(client.can_handle('one;two'))
Esempio n. 6
0
    def test_can_handle_multiple_grain(self):
        client = Client('127.0.0.1', 9000, 'one;two;three', False)

        ok_(client.can_handle("one;two"))
Esempio n. 7
0
    def test_can_handle_while_asleep(self):
        client = Client('127.0.0.1', 9000, 'one;two;three', False)

        client.sleep = True
        ok_(not client.can_handle('one'))
Esempio n. 8
0
    def test_can_handle_not_restricted_empty_string(self):
        client = Client('127.0.0.1', 9000, 'one;two;three', False)

        ok_(not client.can_handle(''))
Esempio n. 9
0
    def test_can_handle_not_restricted_complex(self):
        client = Client('127.0.0.1', 9000, 'one;two;three', False)

        ok_(client.can_handle(None))
Esempio n. 10
0
    def test_can_handle_complex_grain_restricted_approve(self):
        client = Client('127.0.0.1', 9000, 'one;two;three', True)

        ok_(client.can_handle('two;three;one'))
Esempio n. 11
0
    def test_can_handle_complex_grain_restricted(self):
        client = Client('127.0.0.1', 9000, 'one;two;three', True)

        ok_(not client.can_handle('one;two'))
Esempio n. 12
0
 def test_grain_restrict(self):
     client = Client(None, None, None, True)
     eq_(False, client.can_handle(""))
Esempio n. 13
0
    def test_get_availability_url(self):
        client = Client('127.0.0.1', 9000, None, None)

        eq_("http://127.0.0.1:9000/work/request",
            client.get_availability_uri())
Esempio n. 14
0
    def test_can_handle_not_label(self):
        client = Client('127.0.0.1', 9000, 'only', None)

        ok_(not client.can_handle('something'))
Esempio n. 15
0
    def test_can_handle_label(self):
        client = Client('127.0.0.1', 9000, 'something', None)

        ok_(True, client.can_handle('something'))
Esempio n. 16
0
    def test_can_handle(self):
        client = Client('127.0.0.1', 9000, None, None)

        ok_(client.can_handle('something'))
Esempio n. 17
0
 def test_get_status_uri(self):
     client = Client("127.0.0.1", '8097', None, False)
     eq_("http://127.0.0.1:8097/status", client.get_status_uri())
Esempio n. 18
0
 def test_get_work_uri(self):
     client = Client("127.0.0.1", '8097', None, False)
     eq_("http://127.0.0.1:8097/work/execute", client.get_work_uri())