Exemple #1
0
def test_pid_file():
    try:
        import psutil
    except:
        warnings.warn('No psutil module present for this test')
        return
    wrapper = PlatformWrapper()

    address = "tcp://127.0.0.1:{}".format(get_rand_port())
    wrapper.startup_platform(address)

    assert wrapper is not None
    assert wrapper.is_running()
    pid_file = os.path.join(wrapper.volttron_home, "VOLTTRON_PID")
    assert os.path.exists(pid_file)
    with open(pid_file, 'r') as pf:
        assert psutil.pid_exists(int(pf.read().strip()))
    wrapper.skip_cleanup = True
    wrapper.shutdown_platform()
    # give operating system enough time to update pids.
    gevent.sleep(0.1)
    assert not os.path.exists(pid_file)

    # Check overwrite of pid file. In case last shutdown was not clean
    with open(pid_file, 'w') as pf:
        pf.write('abcd')

    wrapper = PlatformWrapper()

    address = "tcp://127.0.0.1:{}".format(get_rand_port())
    wrapper.startup_platform(address)

    assert wrapper is not None
    assert wrapper.is_running()
    pid_file = os.path.join(wrapper.volttron_home, "VOLTTRON_PID")
    assert os.path.exists(pid_file)
    with open(pid_file, 'r') as pf:
        pid_str = pf.read().strip()
        assert psutil.pid_exists(int(pid_str))

    # test start-volttron script we don't start a second volttron process if one
    # is already running
    env = os.environ.copy()
    env["VOLTTRON_HOME"] = wrapper.volttron_home
    vsource_home = os.path.dirname(
        os.path.dirname(os.path.dirname(os.path.realpath(__file__))))
    process = Popen(["./start-volttron"],
                    cwd=vsource_home,
                    env=env,
                    stderr=subprocess.PIPE,
                    stdout=subprocess.PIPE)
    (output, error) = process.communicate()
    assert process.returncode == 1
    assert "VOLTTRON with process id " + pid_str + " is already running" in \
           output
    def get_n_volttron_instances(n, should_start=True, address_file=True):
        get_n_volttron_instances.count = n
        instances = []
        vip_addresses = []
        web_addresses = []
        instances = []
        addr_config = dict()
        names = []

        for i in range(0, n):
            address = get_rand_vip()
            web_address = "http://{}".format(get_rand_ip_and_port())
            vip_addresses.append(address)
            web_addresses.append(web_address)
            nm = 'platform{}'.format(i + 1)
            names.append(nm)

        for i in range(0, n):
            address = vip_addresses[i]
            web_address = web_addresses[i]
            wrapper = PlatformWrapper()

            addr_file = os.path.join(wrapper.volttron_home,
                                     'external_address.json')
            if address_file:
                with open(addr_file, 'w') as f:
                    json.dump(web_addresses, f)
                    gevent.sleep(.1)
            wrapper.startup_platform(address,
                                     bind_web_address=web_address,
                                     instance_name=names[i],
                                     setupmode=True)
            wrapper.skip_cleanup = True
            instances.append(wrapper)

        gevent.sleep(11)
        for i in range(0, n):
            instances[i].shutdown_platform()

        gevent.sleep(1)
        # del instances[:]
        for i in range(0, n):
            address = vip_addresses.pop(0)
            web_address = web_addresses.pop(0)
            print address, web_address
            instances[i].startup_platform(address,
                                          bind_web_address=web_address,
                                          instance_name=names[i])
            instances[i].allow_all_connections()
        gevent.sleep(11)
        instances = instances if n > 1 else instances[0]

        get_n_volttron_instances.instances = instances
        return instances
    def build_n_volttron_instances(n, bad_config=False, add_my_address=True):
        build_n_volttron_instances.count = n
        instances = []
        vip_addresses = []
        instances = []
        addr_config = dict()
        names = []

        for i in range(0, n):
            address = get_rand_vip()
            vip_addresses.append(address)
            nm = 'platform{}'.format(i + 1)
            names.append(nm)

        for i in range(0, n):
            address = vip_addresses[i]
            wrapper = PlatformWrapper()
            wrapper.startup_platform(address, instance_name=names[i])
            wrapper.skip_cleanup = True
            instances.append(wrapper)

        gevent.sleep(1)
        for i in range(0, n):
            instances[i].shutdown_platform()

        for i in range(0, n):
            addr_config.clear()
            for j in range(0, n):
                if j != i or (j == i and add_my_address):
                    name = names[j]
                    addr_config[name] = dict()
                    addr_config[name]['instance-name'] = names[j]
                    if bad_config:
                        addr_config[name]['vip-address123'] = vip_addresses[j]
                    else:
                        addr_config[name]['vip-address'] = vip_addresses[j]
                    addr_config[name]['serverkey'] = instances[j].serverkey

            address_file = os.path.join(instances[i].volttron_home,
                                        'external_platform_discovery.json')
            if address_file:
                with open(address_file, 'w') as f:
                    json.dump(addr_config, f)

        gevent.sleep(1)
        for i in range(0, n):
            address = vip_addresses.pop(0)
            instances[i].startup_platform(address, instance_name=names[i])
            instances[i].allow_all_connections()
        gevent.sleep(11)
        instances = instances if n > 1 else instances[0]

        build_n_volttron_instances.instances = instances
        return instances