Commit 84504f38 authored by Caleb Rouleau's avatar Caleb Rouleau Committed by Commit Bot

[ChromeDriver] Delete old DevToolsPort file if it exists.

Bug: chromedriver:2446
Change-Id: I63cfdc367af8cdbfc7f116fb51a3e3148ee0a950
Reviewed-on: https://chromium-review.googlesource.com/1087803
Commit-Queue: Caleb Rouleau <crouleau@chromium.org>
Reviewed-by: default avatarJohn Chen <johnchen@chromium.org>
Cr-Commit-Position: refs/heads/master@{#564803}
parent b9b3a7e8
......@@ -339,8 +339,17 @@ Status LaunchDesktopChrome(URLRequestContextGetter* context_getter,
base::ScopedTempDir user_data_dir_temp_dir;
base::FilePath user_data_dir;
base::ScopedTempDir extension_dir;
Status status = Status(kOk);
std::vector<std::string> extension_bg_pages;
Status status =
if (capabilities.switches.HasSwitch("user-data-dir")) {
status = internal::RemoveOldDevToolsActivePortFile(base::FilePath(
capabilities.switches.GetSwitchValueNative("user-data-dir")));
if (status.IsError()) {
return status;
}
}
status =
PrepareCommandLine(capabilities, &command, &user_data_dir_temp_dir,
&extension_dir, &extension_bg_pages, &user_data_dir);
if (status.IsError())
......@@ -909,4 +918,19 @@ Status ParseDevToolsActivePortFile(const base::FilePath& user_data_dir,
return result;
}
Status RemoveOldDevToolsActivePortFile(const base::FilePath& user_data_dir) {
base::FilePath port_filepath = user_data_dir.Append(kDevToolsActivePort);
// Note that calling DeleteFile on a path that doesn't exist returns True.
if (base::DeleteFile(port_filepath, false)) {
return Status(kOk);
}
return Status(
kUnknownError,
std::string("Could not remove old devtools port file. Perhaps "
"the given user-data-dir at ") +
user_data_dir.AsUTF8Unsafe() +
std::string(" is still attached to a running Chrome or Chromium "
"process."));
}
} // namespace internal
......@@ -47,6 +47,7 @@ Status PrepareUserDataDir(
Status ParseDevToolsActivePortFile(const base::FilePath& user_data_dir,
int* port);
Status ReadInPort(const base::FilePath& port_filepath, int* port);
Status RemoveOldDevToolsActivePortFile(const base::FilePath& user_data_dir);
} // namespace internal
#endif // CHROME_TEST_CHROMEDRIVER_CHROME_LAUNCHER_H_
......@@ -16,6 +16,7 @@
#include "base/path_service.h"
#include "base/strings/string_split.h"
#include "base/values.h"
#include "build/build_config.h"
#include "chrome/common/chrome_constants.h"
#include "chrome/test/chromedriver/chrome/status.h"
#include "testing/gtest/include/gtest/gtest.h"
......@@ -245,3 +246,30 @@ TEST(DesktopLauncher, ReadInPort_NoFile) {
ASSERT_FALSE(internal::ReadInPort(temp_file, &port).IsOk());
ASSERT_EQ(port, 1111);
}
TEST(DesktopLauncher, RemoveOldDevToolsActivePortFile_Success) {
base::ScopedTempDir temp_dir;
ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
base::FilePath temp_file =
temp_dir.GetPath().Append(FILE_PATH_LITERAL("DevToolsActivePort"));
char data[] = "12345asdf\nblahblah";
base::WriteFile(temp_file, data, strlen(data));
ASSERT_TRUE(
internal::RemoveOldDevToolsActivePortFile(temp_dir.GetPath()).IsOk());
ASSERT_FALSE(base::PathExists(temp_file));
ASSERT_TRUE(base::PathExists(temp_dir.GetPath()));
}
#if defined(OS_WIN)
TEST(DesktopLauncher, RemoveOldDevToolsActivePortFile_Failure) {
base::ScopedTempDir temp_dir;
ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
base::FilePath temp_file =
temp_dir.GetPath().Append(FILE_PATH_LITERAL("DevToolsActivePort"));
FILE* fd = base::OpenFile(temp_file, "w");
ASSERT_FALSE(
internal::RemoveOldDevToolsActivePortFile(temp_dir.GetPath()).IsOk());
ASSERT_TRUE(base::PathExists(temp_file));
base::CloseFile(fd);
}
#endif
......@@ -2617,6 +2617,26 @@ class RemoteBrowserTest(ChromeDriverBaseTest):
raise RuntimeError('Cannot find open port')
class ExistingUserDataDirTest(ChromeDriverBaseTest):
def setUp(self):
self.user_data_dir = tempfile.mkdtemp()
def tearDown(self):
shutil.rmtree(self.user_data_dir, ignore_errors=True)
def testStartUpWithExistingDevToolsPortFile(self):
dev_tools_port_file = os.path.join(self.user_data_dir, 'DevToolsActivePort')
with open(dev_tools_port_file, 'w') as fd:
fd.write('34\n/devtools/browser/2dab5fb1-5571-40d8-a6ad-98823bc5ff84')
driver = self.CreateDriver(
chrome_switches=['user-data-dir=' + self.user_data_dir])
with open(dev_tools_port_file, 'r') as fd:
port = int(fd.readlines()[0])
# Ephemeral ports are always high numbers.
self.assertTrue(port > 100)
class PerfTest(ChromeDriverBaseTest):
"""Tests for ChromeDriver perf."""
def setUp(self):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment