Esempio n. 1
0
 def deserialize_info(str_data):
     newinf = InfrastructureInfo()
     dic = json.loads(str_data)
     vm_list = dic['vm_list']
     vm_master_id = dic['vm_master']
     dic['vm_master'] = None
     dic['vm_list'] = []
     if dic['auth']:
         dic['auth'] = Authentication.deserialize(dic['auth'])
     if dic['radl']:
         dic['radl'] = parse_radl_json(dic['radl'])
     if 'extra_info' in dic and dic['extra_info'] and "TOSCA" in dic['extra_info']:
         dic['extra_info']['TOSCA'] = Tosca.deserialize(dic['extra_info']['TOSCA'])
     newinf.__dict__.update(dic)
     newinf.cloud_connector = None
     # Set the ConfManager object and the lock to the data loaded
     newinf.cm = None
     newinf.conf_threads = []
     for vm_data in vm_list:
         vm = DB150to151.deserialize_vm(vm_data)
         vm.inf = newinf
         if vm.im_id == vm_master_id:
             newinf.vm_master = vm
         newinf.vm_list.append(vm)
     return newinf
Esempio n. 2
0
    def test_db(self):
        """ Test DB data access """
        inf = InfrastructureInfo()
        inf.id = "1"
        inf.auth = self.getAuth([0], [], [("Dummy", 0)])
        cloud = CloudInfo()
        cloud.type = "Dummy"
        radl = RADL()
        radl.add(
            system(
                "s0",
                [Feature("disk.0.image.url", "=", "mock0://linux.for.ev.er")]))
        radl.add(deploy("s0", 1))
        vm1 = VirtualMachine(inf, "1", cloud, radl, radl)
        vm2 = VirtualMachine(inf, "2", cloud, radl, radl)
        inf.vm_list = [vm1, vm2]
        inf.vm_master = vm1
        # first create the DB table
        Config.DATA_DB = "sqlite:///tmp/ind.dat"
        InfrastructureList.load_data()

        success = InfrastructureList._save_data_to_db(Config.DATA_DB,
                                                      {"1": inf})
        self.assertTrue(success)

        res = InfrastructureList._get_data_from_db(Config.DATA_DB)
        self.assertEqual(len(res), 1)
        self.assertEqual(len(res['1'].vm_list), 2)
        self.assertEqual(res['1'].vm_list[0], res['1'].vm_master)
        self.assertEqual(
            res['1'].vm_master.info.systems[0].getValue("disk.0.image.url"),
            "mock0://linux.for.ev.er")
        self.assertTrue(res['1'].auth.compare(inf.auth,
                                              "InfrastructureManager"))
Esempio n. 3
0
    def test_commands(self, bottle_request, check_auth_data,
                      get_infrastructure, SSH):
        """Test REST StopInfrastructure."""
        bottle_request.return_value = MagicMock()
        bottle_request.headers = {
            "AUTHORIZATION":
            ("type = InfrastructureManager; username = user; password = pass\n"
             "id = one; type = OpenNebula; host = onedock.i3m.upv.es:2633; "
             "username = user; password = pass")
        }
        bottle_request.environ = {'HTTP_HOST': 'imserver.com'}

        inf = InfrastructureInfo()
        inf.id = "1"
        inf.auth = Authentication([{
            'type': 'InfrastructureManager',
            'username': '******',
            'password': '******'
        }])
        get_infrastructure.return_value = inf

        bottle_request.params = {'step': '1'}
        res = RESTGetVMProperty("1", "1", "command")
        auth_str = "Authorization: type = InfrastructureManager; username = user; password = pass"
        url = "http://imserver.com/infrastructures/1/vms/1/command?step=2"
        expected_res = """
                res="wait"
                while [ "$res" == "wait" ]
                do
                  res=`curl -s -H "%s" -H "Accept: text/plain" %s`
                  if [ "$res" != "wait" ]
                  then
                    eval "$res"
                  else
                    sleep 20
                  fi
                done""" % (auth_str, url)
        self.assertEqual(res, expected_res)

        radl_master = parse_radl("""
            network publica (outbound = 'yes')
            network privada ()

            system front (
            cpu.arch='x86_64' and
            cpu.count>=1 and
            memory.size>=512m and
            net_interface.0.ip = '8.8.8.8' and
            net_interface.0.connection = 'publica' and
            net_interface.1.connection = 'privada' and
            disk.0.image.url = 'mock0://linux.for.ev.er' and
            disk.0.os.credentials.username = '******' and
            disk.0.os.credentials.password = '******' and
            disk.0.os.name = 'linux'
            )
        """)

        radl_vm1 = parse_radl("""
            network privada ()

            system wn (
            cpu.arch='x86_64' and
            cpu.count>=1 and
            memory.size>=512m and
            net_interface.0.connection = 'privada' and
            disk.0.image.url = 'mock0://linux.for.ev.er' and
            disk.0.os.credentials.username = '******' and
            disk.0.os.credentials.password = '******' and
            disk.0.os.name = 'linux'
            )
        """)

        radl_vm2 = parse_radl("""
            network privada2 ()

            system wn2 (
            cpu.arch='x86_64' and
            cpu.count>=1 and
            memory.size>=512m and
            net_interface.0.connection = 'privada2' and
            disk.0.image.url = 'mock0://linux.for.ev.er' and
            disk.0.os.credentials.username = '******' and
            disk.0.os.credentials.password = '******' and
            disk.0.os.name = 'linux'
            )
        """)

        # in the Master VM
        bottle_request.params = {'step': '2'}
        inf.vm_master = VirtualMachine(inf, None, None, radl_master,
                                       radl_master)
        inf.vm_master.creation_im_id = 0
        ssh = MagicMock()
        ssh.test_connectivity.return_value = True
        ssh.port = 22
        ssh.private_key = None
        ssh.password = "******"
        ssh.username = "******"
        ssh.host = "8.8.8.8"
        SSH.return_value = ssh
        vm1 = VirtualMachine(inf, None, None, radl_vm1, radl_vm1)
        vm1.creation_im_id = 1
        vm1.destroy = False
        vm2 = VirtualMachine(inf, None, None, radl_vm2, radl_vm2)
        vm2.creation_im_id = 2
        vm2.destroy = False
        inf.vm_list = [inf.vm_master, vm1, vm2]

        res = RESTGetVMProperty("1", "0", "command")
        expected_res = "true"
        self.assertEqual(res, expected_res)

        bottle_request.params = {'step': '2'}
        res = RESTGetVMProperty("1", "1", "command")
        expected_res = "true"
        self.assertEqual(res, expected_res)

        # in VM not connected to the Master VM
        res = RESTGetVMProperty("1", "2", "command")
        expected_res = (
            'sshpass -pyoyoyo ssh -N -R 20002:localhost:22 -p 22 -o "UserKnownHostsFile=/dev/null"'
            ' -o "StrictHostKeyChecking=no" [email protected] &')
        self.assertEqual(res, expected_res)