Example #1
0
def mount_disks(config):
    target_ip = config['main']['target_ip']
    time.sleep(10)
    cl = client.Client(target_ip, timeout=300)
    cl.timeout = 100
    cl.btrfs.create('storage', ['/dev/sda'])
    cl.disk.mount('/dev/sda', '/var/cache', options=[""])
Example #2
0
 def __init__(self, *args, **kwargs):
     config = configparser.ConfigParser()
     config.read('config.ini')
     self.target_ip = config['main']['target_ip']
     self.zt_access_token = config['main']['zt_access_token']
     self.client = client.Client(self.target_ip)
     self.session = requests.Session()
     self.session.headers['Authorization'] = 'Bearer {}'.format(self.zt_access_token)
     self.root_url = 'https://hub.gig.tech/maxux/ubuntu1604.flist'
     self.ovs_flist = 'https://hub.gig.tech/gig-official-apps/ovs.flist'
     self.storage = 'ardb://hub.gig.tech:16379'
     self.client.timeout = 80
     super(BaseTest, self).__init__(*args, **kwargs)
Example #3
0
 def __init__(self, *args, **kwargs):
     config = configparser.ConfigParser()
     config.read('config.ini')
     self.target_ip = config['main']['target_ip']
     self.zt_access_token = config['main']['zt_access_token'] or os.environ[
         "zt_access_token"]
     self.client = client.Client(self.target_ip)
     self.session = requests.Session()
     self.session.headers['Authorization'] = 'Bearer {}'.format(
         self.zt_access_token)
     self.root_url = 'https://hub.grid.tf/tf-bootable/ubuntu:16.04.flist'
     self.smallsize_img = 'https://hub.grid.tf/tf-official-apps/minio.flist'
     self.ovs_flist = 'https://hub.grid.tf/tf-official-apps/ovs.flist'
     self.storage = 'zdb://hub.grid.tf:9900'
     self.client.timeout = 80
     super(BaseTest, self).__init__(*args, **kwargs)
Example #4
0
    def test001_update_zos_versoin(self):
        """ zos-052
        *Test case for updating zeroos version

        **Test Scenario:**
        #. Create a vm, should succeed.
        #. Update zos vm version.
        #. Wait till the node is back, then check if the version has been updated.
        """
        self.lg('Create a vm, should succeed')
        vm_name = self.rand_str()
        nics = [{'id': 'None', 'type': 'default'}]
        pub_port = randint(4000, 5000)
        ports = {pub_port: 6379}
        vm_uuid = self.create_vm(name=vm_name,
                                 flist=self.zos_flist,
                                 memory=2048,
                                 port=ports,
                                 nics=nics)
        time.sleep(20)
        vm_cl = client.Client(self.target_ip, port=pub_port)

        self.lg('Update zos node')
        rs = vm_cl.ping()
        self.assertEqual(rs[:4], 'PONG')
        cur_version = rs.split()[2]
        r = Repo('../.')
        for branch in r.remotes.origin.refs:
            if cur_version != branch.remote_head:
                new_version = branch.remote_head
                break
        vm_cl.power.update('zero-os-{}.efi'.format(new_version))
        time.sleep(10)

        self.lg(
            'Wait till the node is back, then check if the version has been updated.'
        )
        res = self.ping_zos(vm_cl, timeout=300)
        self.assertTrue(res, "Can't ping zos node")
        rs = vm_cl.ping()
        self.assertEqual(rs.split()[2], new_version)

        self.lg('Destroy the vm')
        self.client.kvm.destroy(vm_uuid)
Example #5
0
def teardown(options):
    zos_vm_name = os.environ['vm_zos_name']
    # get zeroos host client
    params = {
        'grant_type': 'client_credentials',
        'client_id': options.client_id,
        'client_secret': options.client_secret,
        'response_type': 'id_token',
        'scope': 'user:memberof:threefold.sysadmin,offline_access'
    }
    jwt = requests.post('https://itsyou.online/v1/oauth/access_token',
                        params=params).text
    zos_client = client.Client(options.zos_ip, password=jwt)

    # remove vms and bridge
    print('removing vms and bridge')
    vms = zos_client.kvm.list()
    vm_uuid = [
        vm['uuid'] for vm in vms if vm['name'] == os.environ['vm_ubuntu_name']
    ]
    if vm_uuid:
        zos_client.kvm.destroy(vm_uuid[0])
    vm_uuid = [vm['uuid'] for vm in vms if vm['name'] == zos_vm_name]
    if vm_uuid:
        zos_client.kvm.destroy(vm_uuid[0])
    bridge = os.environ['bridge']
    if bridge in zos_client.bridge.list():
        zos_client.bridge.delete(bridge)

    # remove the zos_vm disk from the host
    print('removing zos_vm disk')
    zos_client.bash('rm -rf /var/cache/{}.qcow2'.format(
        os.environ['ubuntu_port']))

    # leave the zt-network
    os.system('zerotier-cli leave {}'.format(os.environ['ZT_NET_ID']))
Example #6
0
 def get(self, host, port=6379, password=''):
     return g8core.Client(host=host, port=port, password=password)
Example #7
0
def main(options):
    utils = Utils(options)

    # get zeroos host client
    params = {
        'grant_type': 'client_credentials',
        'client_id': options.client_id,
        'client_secret': options.client_secret,
        'response_type': 'id_token',
        'scope': 'user:memberof:threefold.sysadmin,offline_access'
    }
    jwt = requests.post('https://itsyou.online/v1/oauth/access_token',
                        params=params).text
    zos_client = client.Client(options.zos_ip, password=jwt)
    vm_zos_name = os.environ['vm_zos_name']
    vm_ubuntu_name = os.environ['vm_ubuntu_name']
    rand_num = random.randint(3, 125)
    vm_zos_ip = '10.100.{}.{}'.format(rand_num, random.randint(3, 125))
    vm_ubuntu_ip = '10.100.{}.{}'.format(rand_num, random.randint(126, 253))
    zos_flist = 'https://hub.grid.tf/tf-autobuilder/zero-os-{}.flist'.format(
        options.branch)
    ubuntu_flist = 'https://hub.grid.tf/tf-bootable/ubuntu:lts.flist'

    script = """
apt-get install git python3-pip -y
git clone https://github.com/threefoldtech/0-core.git
cd 0-core; git checkout %s; pip3 install client/py-client/.
cd tests
pip3 install -r requirements.txt
sed -i -e"s/^target_ip=.*/target_ip=%s/" config.ini
sed -i -e"s/^zt_access_token=.*/zt_access_token=%s/" config.ini
interface=$(ip a | grep 3: | awk '{print $2}')
dhclient $interface
    """ % (options.branch, vm_zos_ip, options.zt_token)

    # create a bridge and assign specific ips for the vms
    bridge = os.environ['bridge']
    vm_zos_mac = utils.random_mac()
    vm_ubuntu_mac = utils.random_mac()
    ubuntu_port = int(os.environ['ubuntu_port'])
    cidr = '10.100.{}.1/24'.format(rand_num)
    start = '10.100.{}.2'.format(rand_num)
    end = '10.100.{}.254'.format(rand_num)
    zos_client.bridge.create(bridge,
                             network='dnsmasq',
                             nat=True,
                             settings={
                                 'cidr': cidr,
                                 'start': start,
                                 'end': end
                             })
    zos_client.json('bridge.host-add', {
        'bridge': bridge,
        'ip': vm_zos_ip,
        'mac': vm_zos_mac
    })
    zos_client.json('bridge.host-add', {
        'bridge': bridge,
        'ip': vm_ubuntu_ip,
        'mac': vm_ubuntu_mac
    })

    # create a zeroos vm
    print('* Creating zero-os vm')
    print('zos_vm ip: ' + vm_zos_ip)
    zos_client.bash(
        'qemu-img create -f qcow2 /var/cache/{}.qcow2 30G'.format(ubuntu_port))
    nic = [{'type': 'bridge', 'id': bridge, 'hwaddr': vm_zos_mac}]
    zos_client.kvm.create(name=vm_zos_name,
                          flist=zos_flist,
                          cpu=4,
                          memory=8192,
                          nics=nic,
                          kvm=True,
                          media=[{
                              'url':
                              '/var/cache/{}.qcow2'.format(ubuntu_port)
                          }],
                          cmdline='development')

    # create sshkey and provide the public key
    keypath = os.path.expanduser('~/.ssh/id_rsa.pub')
    if not os.path.isfile(keypath):
        os.system("echo  | ssh-keygen -P ''")
    with open(keypath, "r") as key:
        pub_key = key.read()
    pub_key.replace('\n', '')

    # create an ubuntu vm to run testcases from
    print('* Creating ubuntu vm to fire the testsuite from')
    print('ubuntu_vm ip: ' + vm_ubuntu_ip)
    nics = [{
        'type': 'default'
    }, {
        'type': 'bridge',
        'id': bridge,
        'hwaddr': vm_ubuntu_mac
    }]
    zos_client.kvm.create(name=vm_ubuntu_name,
                          flist=ubuntu_flist,
                          cpu=4,
                          memory=8192,
                          nics=nics,
                          port={ubuntu_port: 22},
                          config={'/root/.ssh/authorized_keys': pub_key})

    # access the ubuntu vm and start ur testsuite
    time.sleep(10)
    script_path = '/tmp/setup_env.sh'
    with open(script_path, "w") as f:
        f.write(script)
    utils.send_script_to_remote_machine(script_path, options.zos_ip,
                                        ubuntu_port)

    print('* Setup the environment')
    cmd = 'bash setup_env.sh'
    utils.run_cmd_on_remote_machine(cmd, options.zos_ip, ubuntu_port)
    time.sleep(30)

    # Make sure zrobot server is not running
    zrobot_kill = """
from zeroos.core0 import client
cl = client.Client('%s')
cont = cl.container.find('zrobot')
if cont:
    cont_cl = cl.container.client(1)
    cl.bash("echo $'import time; time.sleep(1000000000)' > /mnt/containers/1/.startup.py").get()
    out = cont_cl.bash('ps aux | grep server').get().stdout
    cont_cl.bash('kill -9 {}'.format(out.split()[1])).get()
    """ % (vm_zos_ip)

    time.sleep(5)
    script_path = '/tmp/zrobot_kill.py'
    with open(script_path, "w") as f:
        f.write(zrobot_kill)
    utils.send_script_to_remote_machine(script_path, options.zos_ip,
                                        ubuntu_port)

    print('* killing zrobot server')
    cmd = 'python3 zrobot_kill.py'
    utils.run_cmd_on_remote_machine(cmd, options.zos_ip, ubuntu_port)
Example #8
0
                        default=4,
                        help="Time between creating containers")
    parser.add_argument("--zos_ip",
                        dest="zos_ip",
                        help="IP for zos machine",
                        required=True)
    parser.add_argument("--jwt",
                        dest="jwt",
                        default='',
                        help="JWT for authentiicating ZOS client")
    parser.add_argument("--teardown",
                        dest="teardown",
                        action='store_true',
                        help="Deleting the containers used for this test")

    options = parser.parse_args()
    cl = client.Client(options.zos_ip,
                       password=options.jwt,
                       testConnectionAttempts=0)
    cl.ping()
    tag = 'test_cont'

    if options.teardown:
        delete_containers()
    else:
        cont_list, mem_used = create_containers()
        plt.plot(cont_list, mem_used)
        plt.xlabel('Number of Containers')
        plt.ylabel('Memory Usage')
        plt.savefig('ZOS_Memory_Usage.png')