def testProcessArtifacts(self, mock_upload, mock_download):
        """test ProcessArtifacts."""
        # Test image source type is local.
        args = mock.MagicMock()
        args.config_file = ""
        args.avd_type = constants.TYPE_CF
        args.flavor = "phone"
        args.local_image = None
        avd_spec_local_img = avd_spec.AVDSpec(args)
        fake_image_name = "/fake/aosp_cf_x86_phone-img-eng.username.zip"
        fake_host_package_name = "/fake/host_package.tar.gz"
        factory_local_img = remote_instance_cf_device_factory.RemoteInstanceDeviceFactory(
            avd_spec_local_img,
            fake_image_name,
            fake_host_package_name)
        factory_local_img._ProcessArtifacts(constants.IMAGE_SRC_LOCAL)
        self.assertEqual(mock_upload.call_count, 1)

        # Test image source type is remote.
        args.local_image = ""
        args.build_id = "1234"
        args.branch = "fake_branch"
        args.build_target = "fake_target"
        args.system_build_id = "2345"
        args.system_branch = "sys_branch"
        args.system_build_target = "sys_target"
        args.kernel_build_id = "3456"
        args.kernel_branch = "kernel_branch"
        args.kernel_build_target = "kernel_target"
        avd_spec_remote_img = avd_spec.AVDSpec(args)
        self.Patch(cvd_compute_client_multi_stage.CvdComputeClient, "UpdateFetchCvd")
        factory_remote_img = remote_instance_cf_device_factory.RemoteInstanceDeviceFactory(
            avd_spec_remote_img)
        factory_remote_img._ProcessArtifacts(constants.IMAGE_SRC_REMOTE)
        self.assertEqual(mock_download.call_count, 1)
    def testRemoteHostInstanceName(self):
        """Test Remote host instance name."""
        args = mock.MagicMock()
        args.config_file = ""
        args.avd_type = constants.TYPE_CF
        args.flavor = "phone"
        args.remote_host = "1.1.1.1"
        args.local_image = None
        args.adb_port = None
        fake_avd_spec = avd_spec.AVDSpec(args)
        fake_avd_spec.cfg.enable_multi_stage = True
        fake_avd_spec._instance_name_to_reuse = None
        fake_uuid = mock.MagicMock(hex="1234")
        self.Patch(uuid, "uuid4", return_value=fake_uuid)
        self.Patch(cvd_compute_client_multi_stage.CvdComputeClient, "CreateInstance")
        self.Patch(cvd_compute_client_multi_stage.CvdComputeClient, "InitRemoteHost")
        fake_host_package_name = "/fake/host_package.tar.gz"

        fake_image_name = "/fake/aosp_cf_x86_phone-img-eng.username.zip"
        factory = remote_instance_cf_device_factory.RemoteInstanceDeviceFactory(
            fake_avd_spec,
            fake_image_name,
            fake_host_package_name)
        self.assertEqual(factory._InitRemotehost(), "host-1.1.1.1-userbuild-aosp_cf_x86_phone")

        # No image zip path, it uses local build images.
        fake_image_name = ""
        factory = remote_instance_cf_device_factory.RemoteInstanceDeviceFactory(
            fake_avd_spec,
            fake_image_name,
            fake_host_package_name)
        self.assertEqual(factory._InitRemotehost(), "host-1.1.1.1-userbuild-fake-target")
    def testProcessRemoteHostArtifacts(self, mock_upload, mock_download):
        """Test process remote host artifacts."""
        self.Patch(
            cvd_compute_client_multi_stage,
            "CvdComputeClient",
            return_value=mock.MagicMock())
        fake_avd_spec = mock.MagicMock()

        # Test process remote host artifacts with local images.
        fake_avd_spec.instance_type = constants.INSTANCE_TYPE_HOST
        fake_avd_spec.image_source = constants.IMAGE_SRC_LOCAL
        fake_avd_spec._instance_name_to_reuse = None
        fake_host_package_name = "/fake/host_package.tar.gz"
        fake_image_name = ""
        factory = remote_instance_cf_device_factory.RemoteInstanceDeviceFactory(
            fake_avd_spec,
            fake_image_name,
            fake_host_package_name)
        factory._ProcessRemoteHostArtifacts()
        self.assertEqual(mock_upload.call_count, 1)

        # Test process remote host artifacts with remote images.
        fake_tmp_folder = "/tmp/1111/"
        mock_upload.call_count = 0
        self.Patch(tempfile, "mkdtemp", return_value=fake_tmp_folder)
        self.Patch(shutil, "rmtree")
        fake_avd_spec.instance_type = constants.INSTANCE_TYPE_HOST
        fake_avd_spec.image_source = constants.IMAGE_SRC_REMOTE
        fake_avd_spec._instance_name_to_reuse = None
        factory = remote_instance_cf_device_factory.RemoteInstanceDeviceFactory(
            fake_avd_spec)
        factory._ProcessRemoteHostArtifacts()
        self.assertEqual(mock_upload.call_count, 1)
        self.assertEqual(mock_download.call_count, 2)
        shutil.rmtree.assert_called_once_with(fake_tmp_folder)
    def testDownloadArtifacts(self, mock_download):
        """Test process remote cuttlefish image."""
        extract_path = "/tmp/1111/"
        fake_remote_image = {"build_target" : "aosp_cf_x86_phone-userdebug",
                             "build_id": "1234"}
        self.Patch(
            cvd_compute_client_multi_stage,
            "CvdComputeClient",
            return_value=mock.MagicMock())
        self.Patch(tempfile, "mkdtemp", return_value="/tmp/1111/")
        self.Patch(shutil, "rmtree")
        fake_avd_spec = mock.MagicMock()
        fake_avd_spec.cfg = mock.MagicMock()
        fake_avd_spec.remote_image = fake_remote_image
        fake_avd_spec.image_download_dir = "/tmp"
        self.Patch(os.path, "exists", return_value=False)
        self.Patch(os, "makedirs")
        factory = remote_instance_cf_device_factory.RemoteInstanceDeviceFactory(
            fake_avd_spec)
        factory._DownloadArtifacts(extract_path)
        build_id = "1234"
        build_target = "aosp_cf_x86_phone-userdebug"
        checkfile1 = "aosp_cf_x86_phone-img-1234.zip"
        checkfile2 = "cvd-host_package.tar.gz"

        # To validate DownloadArtifact runs twice.
        self.assertEqual(mock_download.call_count, 2)

        # To validate DownloadArtifact arguments correct.
        mock_download.assert_has_calls([
            mock.call(fake_avd_spec.cfg, build_target, build_id, checkfile1,
                      extract_path, decompress=True),
            mock.call(fake_avd_spec.cfg, build_target, build_id, checkfile2,
                      extract_path)], any_order=True)
示例#5
0
    def _CreateAVD(self, avd_spec, no_prompts):
        """Create the AVD.

        Args:
            avd_spec: AVDSpec object that tells us what we're going to create.
            no_prompts: Boolean, True to skip all prompts.

        Returns:
            A Report instance.
        """
        device_factory = remote_instance_cf_device_factory.RemoteInstanceDeviceFactory(
            avd_spec,
            avd_spec.local_image_artifact,
            create_common.GetCvdHostPackage())
        report = common_operations.CreateDevices(
            "create_cf", avd_spec.cfg, device_factory, num=1,
            report_internal_ip=avd_spec.report_internal_ip,
            autoconnect=avd_spec.autoconnect,
            avd_type=constants.TYPE_CF,
            boot_timeout_secs=avd_spec.boot_timeout_secs,
            unlock_screen=avd_spec.unlock_screen,
            wait_for_boot=False,
            connect_webrtc=avd_spec.connect_webrtc)
        # Launch vnc client if we're auto-connecting.
        if avd_spec.connect_vnc:
            utils.LaunchVNCFromReport(report, avd_spec, no_prompts)
        return report
    def testGetBuildInfoDict(self):
        """Test GetBuildInfoDict."""
        fake_host_package_name = "/fake/host_package.tar.gz"
        fake_image_name = "/fake/aosp_cf_x86_phone-img-eng.username.zip"
        args = mock.MagicMock()
        # Test image source type is local.
        args.config_file = ""
        args.avd_type = constants.TYPE_CF
        args.flavor = "phone"
        args.local_image = "fake_local_image"
        args.adb_port = None
        args.cheeps_betty_image = None
        avd_spec_local_image = avd_spec.AVDSpec(args)
        factory = remote_instance_cf_device_factory.RemoteInstanceDeviceFactory(
            avd_spec_local_image,
            fake_image_name,
            fake_host_package_name)
        self.assertEqual(factory.GetBuildInfoDict(), None)

        # Test image source type is remote.
        args.local_image = ""
        args.build_id = "123"
        args.branch = "fake_branch"
        args.build_target = "fake_target"
        args.system_build_id = "234"
        args.system_branch = "sys_branch"
        args.system_build_target = "sys_target"
        args.kernel_build_id = "345"
        args.kernel_branch = "kernel_branch"
        args.kernel_build_target = "kernel_target"
        avd_spec_remote_image = avd_spec.AVDSpec(args)
        factory = remote_instance_cf_device_factory.RemoteInstanceDeviceFactory(
            avd_spec_remote_image,
            fake_image_name,
            fake_host_package_name)
        expected_build_info = {
            "build_id": "123",
            "branch": "fake_branch",
            "build_target": "fake_target",
            "system_build_id": "234",
            "system_branch": "sys_branch",
            "system_build_target": "sys_target",
            "kernel_build_id": "345",
            "kernel_branch": "kernel_branch",
            "kernel_build_target": "kernel_target"
        }
        self.assertEqual(factory.GetBuildInfoDict(), expected_build_info)
    def testCreateGceInstanceNameMultiStage(self):
        """test create gce instance."""
        # Mock uuid
        args = mock.MagicMock()
        args.config_file = ""
        args.avd_type = constants.TYPE_CF
        args.flavor = "phone"
        args.local_image = None
        args.adb_port = None
        fake_avd_spec = avd_spec.AVDSpec(args)
        fake_avd_spec.cfg.enable_multi_stage = True
        fake_avd_spec._instance_name_to_reuse = None

        fake_uuid = mock.MagicMock(hex="1234")
        self.Patch(uuid, "uuid4", return_value=fake_uuid)
        self.Patch(cvd_compute_client_multi_stage.CvdComputeClient, "CreateInstance")
        self.Patch(cvd_compute_client_multi_stage.CvdComputeClient,
                   "GetHostImageName", return_value="fake_image")
        fake_host_package_name = "/fake/host_package.tar.gz"
        fake_image_name = "/fake/aosp_cf_x86_phone-img-eng.username.zip"

        factory = remote_instance_cf_device_factory.RemoteInstanceDeviceFactory(
            fake_avd_spec,
            fake_image_name,
            fake_host_package_name)
        self.assertEqual(factory._CreateGceInstance(), "ins-1234-userbuild-aosp-cf-x86-phone")

        # Can't get target name from zip file name.
        fake_image_name = "/fake/aosp_cf_x86_phone.username.zip"
        factory = remote_instance_cf_device_factory.RemoteInstanceDeviceFactory(
            fake_avd_spec,
            fake_image_name,
            fake_host_package_name)
        self.assertEqual(factory._CreateGceInstance(), "ins-1234-userbuild-fake-target")

        # No image zip path, it uses local build images.
        fake_image_name = ""
        factory = remote_instance_cf_device_factory.RemoteInstanceDeviceFactory(
            fake_avd_spec,
            fake_image_name,
            fake_host_package_name)
        self.assertEqual(factory._CreateGceInstance(), "ins-1234-userbuild-fake-target")
    def testUploadArtifacts(self, mock_ssh_run, mock_shell):
        """Test UploadArtifacts."""
        fake_host_package = "/fake/host_package.tar.gz"
        fake_image = "/fake/aosp_cf_x86_phone-img-eng.username.zip"
        fake_local_image_dir = "/fake_image"
        fake_ip = ssh.IP(external="1.1.1.1", internal="10.1.1.1")
        args = mock.MagicMock()
        # Test local image extract from image zip case.
        args.config_file = ""
        args.avd_type = constants.TYPE_CF
        args.flavor = "phone"
        args.local_image = "fake_local_image"
        args.adb_port = None
        avd_spec_local_image = avd_spec.AVDSpec(args)
        factory = remote_instance_cf_device_factory.RemoteInstanceDeviceFactory(
            avd_spec_local_image,
            fake_image,
            fake_host_package)
        factory._ssh = ssh.Ssh(ip=fake_ip,
                               user=constants.GCE_USER,
                               ssh_private_key_path="/fake/acloud_rea")
        factory._UploadArtifacts(fake_image, fake_host_package, fake_local_image_dir)
        expected_cmd1 = ("/usr/bin/install_zip.sh . < %s" % fake_image)
        expected_cmd2 = ("tar -x -z -f - < %s" % fake_host_package)
        mock_ssh_run.assert_has_calls([
            mock.call(expected_cmd1),
            mock.call(expected_cmd2)])

        # Test local image get from local folder case.
        fake_image = None
        self.Patch(glob, "glob", return_value=["fake.img"])
        factory._UploadArtifacts(fake_image, fake_host_package, fake_local_image_dir)
        expected_cmd = (
            "tar -cf - --lzop -S -C %s fake.img | "
            "%s -- tar -xf - --lzop -S" %
            (fake_local_image_dir, factory._ssh.GetBaseCmd(constants.SSH_BIN)))
        mock_shell.assert_called_once_with(expected_cmd)

        mock_shell.reset_mock()
        m = mock.mock_open(read_data = (
            "boot.img\n"
            "cache.img\n"
            "super.img\n"
            "userdata.img\n"
            "vendor_boot.img\n"))
        with mock.patch.object(six.moves.builtins, "open", m):
            factory._UploadArtifacts(fake_image, fake_host_package, fake_local_image_dir)
            expected_cmd = (
                "tar -cf - --lzop -S -C %s boot.img cache.img super.img userdata.img vendor_boot.img | "
                "%s -- tar -xf - --lzop -S" %
                (fake_local_image_dir, factory._ssh.GetBaseCmd(constants.SSH_BIN)))
            mock_shell.assert_called_once_with(expected_cmd)
 def testLocalImageCreateInstance(self, mock_launchcvd, mock_upload, mock_create_gce_instance):
     """Test local image with create instance."""
     self.Patch(
         cvd_compute_client_multi_stage,
         "CvdComputeClient",
         return_value=mock.MagicMock())
     fake_avd_spec = mock.MagicMock()
     fake_avd_spec.instance_type = constants.INSTANCE_TYPE_REMOTE
     fake_avd_spec.image_source = constants.IMAGE_SRC_LOCAL
     fake_avd_spec._instance_name_to_reuse = None
     fake_host_package_name = "/fake/host_package.tar.gz"
     fake_image_name = ""
     factory = remote_instance_cf_device_factory.RemoteInstanceDeviceFactory(
         fake_avd_spec,
         fake_image_name,
         fake_host_package_name)
     factory.CreateInstance()
     self.assertEqual(mock_create_gce_instance.call_count, 1)
     self.assertEqual(mock_upload.call_count, 1)
     self.assertEqual(mock_launchcvd.call_count, 1)