def testUploadIntentWithExperimentUrlCallback(self):
        """Test the upload intent with a callback."""
        server_info = server_info_pb2.ServerInfoResponse()
        server_info.url_format.template = "https://tensorboard.dev/x/{}"
        server_info.url_format.id_placeholder = "{}"

        stub = dry_run_stubs.DryRunTensorBoardWriterStub()
        stub.CreateExperiment = (
            lambda req, **__: write_service_pb2.CreateExperimentResponse(
                experiment_id="test_experiment_id", url="this URL is ignored"
            )
        )

        expected_url = "https://tensorboard.dev/x/test_experiment_id"

        with mock.patch.object(
            dry_run_stubs,
            "DryRunTensorBoardWriterStub",
            wraps=lambda: stub,
        ), mock.patch.object(sys.stdout, "write"):
            mock_channel = mock.Mock()
            mock_experiment_url_callback = mock.Mock()
            intent = uploader_subcommand.UploadIntent(
                self.get_temp_dir(),
                dry_run=True,
                one_shot=True,
                experiment_url_callback=mock_experiment_url_callback,
            )
            intent.execute(server_info, mock_channel)
        mock_experiment_url_callback.assert_called_once_with(expected_url)
 def testUploadIntentDryRunNonOneShotInterrupted(self):
     mock_server_info = mock.MagicMock()
     mock_channel = mock.MagicMock()
     mock_stdout_write = mock.MagicMock()
     mock_uploader = mock.MagicMock()
     with mock.patch.object(
             mock_uploader,
             "start_uploading",
             side_effect=KeyboardInterrupt(),
     ), mock.patch.object(uploader_lib,
                          "TensorBoardUploader",
                          return_value=mock_uploader), mock.patch.object(
                              sys.stdout, "write", mock_stdout_write):
         intent = uploader_subcommand.UploadIntent(self.get_temp_dir(),
                                                   dry_run=True,
                                                   one_shot=False)
         intent.execute(mock_server_info, mock_channel)
     self.assertRegex(mock_stdout_write.call_args_list[-1][0][0],
                      ".*Interrupted.*")
 def testUploadIntentOneShot(self):
     """Test the upload intent under the one-shot mode."""
     # Mock three places:
     # 1. The uploader itself, we will inspect invocations of its methods but
     #    do not want to actually uplaod anything.
     # 2. Writing to stdout, so we can inspect messages to the user.
     # 3. The creation of the grpc WriteServiceChannel, which happens in the
     #    non dry_run execution, but we don't want to actually open a network
     #    communication.        mock_uploader = mock.MagicMock()
     mock_uploader = mock.MagicMock()
     mock_uploader.create_experiment = mock.MagicMock(
         return_value="fake_experiment_id"
     )
     mock_stdout_write = mock.MagicMock()
     with mock.patch.object(
         sys.stdout, "write", mock_stdout_write
     ), mock.patch.object(
         uploader_lib, "TensorBoardUploader", return_value=mock_uploader
     ), mock.patch.object(
         write_service_pb2_grpc, "TensorBoardWriterServiceStub"
     ):
         # Set up an UploadIntent configured with one_shot and an empty temp
         # directory.
         intent = uploader_subcommand.UploadIntent(
             self.get_temp_dir(), one_shot=True
         )
         # Execute the intent.execute method.
         intent.execute(server_info_pb2.ServerInfoResponse(), None)
     # Expect that there is one call to create_experiment.
     self.assertEqual(mock_uploader.create_experiment.call_count, 1)
     # Expect that there is one call to start_uploading.
     self.assertEqual(mock_uploader.start_uploading.call_count, 1)
     # Expect that ".*Done scanning logdir.*" is among the things printed.
     stdout_writes = [x[0][0] for x in mock_stdout_write.call_args_list]
     self.assertRegex(
         ",".join(stdout_writes),
         ".*experiment created.*",
     )
     # Expect that the last thing written is the string "Done" and the
     # experiment_id.
     self.assertRegex(stdout_writes[-1], ".*Done.*")
     self.assertRegex(stdout_writes[-1], ".*fake_experiment_id.*")
    def testUploadIntentOneShotEmptyDirectoryFails(self):
        """Test the upload intent under the one-shot mode with missing dir.

        In the case of a non-existent directoy, uploading should not
        create an experiment.
        """
        # Mock three places:
        # 1. The uploader itself, we will inspect invocations of its methods but
        #    do not want to actually uplaod anything.
        # 2. Writing to stdout, so we can inspect messages to the user.
        # 3. The creation of the grpc WriteServiceChannel, which happens in the
        #    non dry_run execution, but we don't want to actually open a network
        #    communication.
        mock_uploader = mock.MagicMock()
        mock_stdout_write = mock.MagicMock()
        with mock.patch.object(
            uploader_lib,
            "TensorBoardUploader",
            return_value=mock_uploader,
        ), mock.patch.object(
            sys.stdout, "write", mock_stdout_write
        ), mock.patch.object(
            write_service_pb2_grpc, "TensorBoardWriterServiceStub"
        ):
            # Set up an UploadIntent configured with one_shot and a
            # non-existent directory.
            intent = uploader_subcommand.UploadIntent(
                "/dev/null/non/existent/directory", one_shot=True
            )
            # Execute the intent.execute method.
            intent.execute(server_info_pb2.ServerInfoResponse(), None)
        # Expect that there is no call to create an experiment.
        self.assertEqual(mock_uploader.create_experiment.call_count, 0)
        # Expect a message to the user indicating no experiment was created.
        stdout_writes = [x[0][0] for x in mock_stdout_write.call_args_list]
        self.assertRegex(
            ",".join(stdout_writes),
            ".*Exiting without creating an experiment.*",
        )
 def testUploadIntentUnderDryRunOneShot(self):
     """Test the upload intent under the dry-run + one-shot mode."""
     mock_server_info = mock.MagicMock()
     mock_channel = mock.MagicMock()
     upload_limits = server_info_pb2.UploadLimits(
         max_scalar_request_size=128000,
         max_tensor_request_size=128000,
         max_tensor_point_size=11111,
         max_blob_request_size=128000,
         max_blob_size=128000,
     )
     mock_stdout_write = mock.MagicMock()
     with mock.patch.object(
         server_info_lib,
         "allowed_plugins",
         return_value=_SCALARS_HISTOGRAMS_AND_GRAPHS,
     ), mock.patch.object(
         server_info_lib, "upload_limits", return_value=upload_limits
     ), mock.patch.object(
         sys.stdout, "write", mock_stdout_write
     ), mock.patch.object(
         dry_run_stubs,
         "DryRunTensorBoardWriterStub",
         side_effect=dry_run_stubs.DryRunTensorBoardWriterStub,
     ) as mock_dry_run_stub:
         intent = uploader_subcommand.UploadIntent(
             self.get_temp_dir(), dry_run=True, one_shot=True
         )
         intent.execute(mock_server_info, mock_channel)
     self.assertEqual(mock_dry_run_stub.call_count, 1)
     self.assertRegex(
         mock_stdout_write.call_args_list[-2][0][0],
         ".*Done scanning logdir.*",
     )
     self.assertEqual(
         mock_stdout_write.call_args_list[-1][0][0], "\nDone.\n"
     )