# -*- coding: utf-8; -*-
#
# Licensed to CRATE Technology GmbH ("Crate") under one or more contributor
# license agreements.  See the NOTICE file distributed with this work for
# additional information regarding copyright ownership.  Crate licenses
# this file to you under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.  You may
# obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
# License for the specific language governing permissions and limitations
# under the License.
#
# However, if you have executed another commercial license agreement
# with Crate these terms will supersede the license and you may use the
# software solely pursuant to the terms of the relevant commercial agreement.

import unittest
from crate.client import connect
from crate.testing.layer import CrateLayer

from testutils.ports import GLOBAL_PORT_POOL
from testutils.paths import crate_path

CRATE_HTTP_PORT = GLOBAL_PORT_POOL.get()
CRATE_TRANSPORT_PORT = GLOBAL_PORT_POOL.get()


class SigarIntegrationTest(unittest.TestCase):

    def test_multiple_probe_selects(self):
        conn = connect('localhost:{}'.format(CRATE_HTTP_PORT))
        cur = conn.cursor()
        cur.execute("SELECT os, load, os FROM sys.nodes")
        res = cur.fetchall()
        for row in res:
            self.assertEqual(row[0], row[2])


def test_suite():
    suite = unittest.TestSuite(unittest.makeSuite(SigarIntegrationTest))
    suite.layer = CrateLayer(
        'crate',
        crate_home=crate_path(),
        port=CRATE_HTTP_PORT,
        transport_port=CRATE_TRANSPORT_PORT
    )
    return suite

@@ -1030,16 +1030,28 @@
 nittest%0A
+import time%0A
 from cra
@@ -1632,16 +1632,566 @@
 ow%5B2%5D)%0A%0A
+    def test_sigar_gets_loaded(self):%0A        conn = connect('localhost:%7B%7D'.format(CRATE_HTTP_PORT))%0A        cur = conn.cursor()%0A        to_sleep = 0.2%0A        while True:%0A            cur.execute(%22select process%5B'cpu'%5D%5B'percent'%5D from sys.nodes%22)%0A            percent = int(cur.fetchone()%5B0%5D)%0A            if percent %3E -1:%0A                break%0A            if to_sleep %3E 30:%0A                raise AssertionError(%0A                    'cpu percent was -1 for too long. Sigar probably missing')%0A            time.sleep(to_sleep)%0A            to_sleep *= 2%0A%0A
 %0Adef tes

Add a test to verify sigar is working if Crate is started from tarball