Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/bind.h" | 5 #include "base/bind.h" |
| 6 #include "base/command_line.h" | 6 #include "base/command_line.h" |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/strings/stringprintf.h" | 8 #include "base/strings/stringprintf.h" |
| 9 #include "base/test/launcher/unit_test_launcher.h" | 9 #include "base/test/launcher/unit_test_launcher.h" |
| 10 #include "base/test/test_suite.h" | 10 #include "base/test/test_suite.h" |
| 11 #include "base/test/test_switches.h" | 11 #include "base/test/test_switches.h" |
| 12 #include "google_apis/google_api_keys.h" | 12 #include "google_apis/google_api_keys.h" |
| 13 #include "net/base/escape.h" | 13 #include "net/base/escape.h" |
| 14 #include "remoting/test/app_remoting_test_driver_environment.h" | |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 15 | 16 |
| 16 namespace switches { | 17 namespace switches { |
| 18 const char kAuthCodeSwitchName[] = "authcode"; | |
| 19 const char kHelpSwitchName[] = "help"; | |
| 20 const char kLoggingLevelSwitchName[] = "verbosity"; | |
|
Wez
2015/02/13 03:01:52
Is this the name that other Chromium code uses for
joedow
2015/02/14 02:31:27
I wanted a separate switch which only turned on lo
Wez
2015/02/19 22:00:22
Is --vmodule format really that complex?
| |
| 21 const char kServiceEnvironmentSwitchName[] = "environment"; | |
| 22 const char kSingleProcessTestsSwitchName[] = "single-process-tests"; | |
| 17 const char kUserNameSwitchName[] = "username"; | 23 const char kUserNameSwitchName[] = "username"; |
|
Wez
2015/02/13 03:01:52
Indentation
joedow
2015/02/14 02:31:27
Done.
| |
| 18 const char kAuthCodeSwitchName[] = "authcode"; | |
| 19 const char kServiceEnvironmentSwitchName[] = "environment"; | |
| 20 const char kHelpSwitchName[] = "help"; | |
| 21 const char kSingleProcessTestsSwitchName[] = "single-process-tests"; | |
| 22 } | 24 } |
| 23 | 25 |
| 24 namespace { | 26 namespace { |
| 25 | 27 |
| 26 // Requested permissions needed for App Remoting tests. The spaces in between | 28 // Requested permissions needed for App Remoting tests. The spaces in between |
| 27 // scope fragments are necessary and will be escaped properly before use. | 29 // scope fragments are necessary and will be escaped properly before use. |
| 28 const char kAppRemotingAuthScopeValues[] = | 30 const char kAppRemotingAuthScopeValues[] = |
| 29 "https://www.googleapis.com/auth/appremoting.runapplication" | 31 "https://www.googleapis.com/auth/appremoting.runapplication" |
| 30 " https://www.googleapis.com/auth/googletalk" | 32 " https://www.googleapis.com/auth/googletalk" |
| 31 " https://www.googleapis.com/auth/userinfo.email" | 33 " https://www.googleapis.com/auth/userinfo.email" |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 59 printf("\nRequired Parameters:\n"); | 61 printf("\nRequired Parameters:\n"); |
| 60 printf(" %s: Specifies which account to use when running tests\n", | 62 printf(" %s: Specifies which account to use when running tests\n", |
| 61 switches::kUserNameSwitchName); | 63 switches::kUserNameSwitchName); |
| 62 printf("\nOptional Parameters:\n"); | 64 printf("\nOptional Parameters:\n"); |
| 63 printf(" %s: Exchanged for a refresh and access token for authentication\n", | 65 printf(" %s: Exchanged for a refresh and access token for authentication\n", |
| 64 switches::kAuthCodeSwitchName); | 66 switches::kAuthCodeSwitchName); |
| 65 printf(" %s: Displays additional usage information\n", | 67 printf(" %s: Displays additional usage information\n", |
| 66 switches::kHelpSwitchName); | 68 switches::kHelpSwitchName); |
| 67 printf(" %s: Specifies the service api to use (dev|test) [default: dev]\n", | 69 printf(" %s: Specifies the service api to use (dev|test) [default: dev]\n", |
| 68 switches::kServiceEnvironmentSwitchName); | 70 switches::kServiceEnvironmentSwitchName); |
| 71 printf(" %s: Specifies the optional logging level of the tool (0-3)." | |
| 72 " [default: off]\n", | |
| 73 switches::kLoggingLevelSwitchName); | |
| 69 } | 74 } |
| 70 | 75 |
| 71 void PrintAuthCodeInfo() { | 76 void PrintAuthCodeInfo() { |
| 72 printf("\n*******************************\n"); | 77 printf("\n*******************************\n"); |
| 73 printf("*** Auth Code Example Usage ***\n"); | 78 printf("*** Auth Code Example Usage ***\n"); |
| 74 printf("*******************************\n\n"); | 79 printf("*******************************\n\n"); |
| 75 | 80 |
| 76 printf("If this is the first time you are running the tool,\n"); | 81 printf("If this is the first time you are running the tool,\n"); |
| 77 printf("you will need to provide an authorization code.\n"); | 82 printf("you will need to provide an authorization code.\n"); |
| 78 printf("This code will be exchanged for a long term refresh token which\n"); | 83 printf("This code will be exchanged for a long term refresh token which\n"); |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 98 "chrome-remote-desktop/dev?code=4/AKtf...\n"); | 103 "chrome-remote-desktop/dev?code=4/AKtf...\n"); |
| 99 | 104 |
| 100 printf("\nTool usage example with the newly created auth code:\n"); | 105 printf("\nTool usage example with the newly created auth code:\n"); |
| 101 printf("ar_test_driver --%s=example@gmail.com --%s=4/AKtf...\n\n", | 106 printf("ar_test_driver --%s=example@gmail.com --%s=4/AKtf...\n\n", |
| 102 switches::kUserNameSwitchName, | 107 switches::kUserNameSwitchName, |
| 103 switches::kAuthCodeSwitchName); | 108 switches::kAuthCodeSwitchName); |
| 104 } | 109 } |
| 105 | 110 |
| 106 } // namespace | 111 } // namespace |
| 107 | 112 |
| 113 using remoting::test::AppRemotingSharedData; | |
|
Wez
2015/02/13 03:01:52
nit: You only refer to this a few times; suggest n
joedow
2015/02/14 02:31:27
Done.
| |
| 114 | |
| 108 int main(int argc, char** argv) { | 115 int main(int argc, char** argv) { |
| 109 testing::InitGoogleTest(&argc, argv); | 116 testing::InitGoogleTest(&argc, argv); |
| 110 TestSuite test_suite(argc, argv); | 117 TestSuite test_suite(argc, argv); |
| 111 | 118 |
| 119 // In some configurations, GTest will run main twice. This should not happen | |
| 120 // since we inject the single-process flag, but this is a safeguard in case | |
|
Wez
2015/02/13 03:01:52
Why would the single-process flag have any effect
joedow
2015/02/14 02:31:27
It seemed like main was run twice when GTest was i
| |
| 121 // the flag is changed later. If main is run twice, we don't want to double | |
| 122 // initialize the global data as that will result in a leak or crash. If we | |
|
Wez
2015/02/13 03:01:52
Why do we not tear down the global data when main
joedow
2015/02/14 02:31:27
The GTest framework handles it, there is a comment
| |
| 123 // decide to run in multiple processes, we will want to move the global init | |
| 124 // code into a base::TestSuite derivation and use it instead of the base | |
| 125 // class we use now. | |
| 126 DCHECK(!AppRemotingSharedData); | |
| 127 | |
| 112 // The pointer returned here refers to a singleton, since we don't own the | 128 // The pointer returned here refers to a singleton, since we don't own the |
| 113 // lifetime of the object, don't wrap in a scoped_ptr construct or release it. | 129 // lifetime of the object, don't wrap in a scoped_ptr construct or release it. |
| 114 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); | 130 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); |
| 115 DCHECK(command_line); | 131 DCHECK(command_line); |
| 116 | 132 |
| 117 // We do not want to retry failures as a failed test should signify an error | 133 // We do not want to retry failures as a failed test should signify an error |
| 118 // to be investigated. | 134 // to be investigated. |
| 119 command_line->AppendSwitchASCII(switches::kTestLauncherRetryLimit, "0"); | 135 command_line->AppendSwitchASCII(switches::kTestLauncherRetryLimit, "0"); |
| 120 | 136 |
| 121 // We do not want to run the tests in parallel and we do not want to retry | 137 // We do not want to run the tests in parallel and we do not want to retry |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 138 base::Bind(&base::TestSuite::Run, base::Unretained(&test_suite))); | 154 base::Bind(&base::TestSuite::Run, base::Unretained(&test_suite))); |
| 139 } | 155 } |
| 140 | 156 |
| 141 // Verify we received the required input from the command line. | 157 // Verify we received the required input from the command line. |
| 142 if (!command_line->HasSwitch(switches::kUserNameSwitchName)) { | 158 if (!command_line->HasSwitch(switches::kUserNameSwitchName)) { |
| 143 LOG(ERROR) << "No user name passed in, can't authenticate without that!"; | 159 LOG(ERROR) << "No user name passed in, can't authenticate without that!"; |
| 144 PrintUsage(); | 160 PrintUsage(); |
| 145 return -1; | 161 return -1; |
| 146 } | 162 } |
| 147 | 163 |
| 164 std::string user_name; | |
| 165 user_name = command_line->GetSwitchValueASCII(switches::kUserNameSwitchName); | |
| 166 DVLOG(1) << "Running tests as: " << user_name; | |
| 167 | |
| 168 std::string auth_code; | |
| 169 // Check to see if the user passed in a one time use auth_code for | |
| 170 // refreshing their credentials. | |
| 171 if (command_line->HasSwitch(switches::kAuthCodeSwitchName)) { | |
| 172 auth_code = command_line->GetSwitchValueASCII( | |
| 173 switches::kAuthCodeSwitchName); | |
|
Wez
2015/02/13 03:01:52
Note that you can just call GetSwitchValueASCII an
joedow
2015/02/14 02:31:27
Done.
| |
| 174 } | |
| 175 | |
| 176 std::string service_environment; | |
| 177 // If the user passed in a service environment, use it, otherwise set a | |
| 178 // default value. | |
| 179 if (command_line->HasSwitch(switches::kServiceEnvironmentSwitchName)) { | |
| 180 service_environment = command_line->GetSwitchValueASCII( | |
| 181 switches::kServiceEnvironmentSwitchName); | |
| 182 } else { | |
| 183 // Default to the development service environment. | |
| 184 service_environment = "dev"; | |
| 185 } | |
| 186 // Only two values are allowed, so validate them before proceeding. | |
| 187 if (service_environment != "test" && service_environment != "dev") { | |
| 188 LOG(ERROR) << "Invalid " << switches::kServiceEnvironmentSwitchName | |
| 189 << " argument passed in."; | |
| 190 PrintUsage(); | |
| 191 return -1; | |
| 192 } | |
| 193 | |
| 194 // Update the logging verbosity level is user specified one. | |
| 195 std::string verbosity_level; | |
| 196 if (command_line->HasSwitch(switches::kLoggingLevelSwitchName)) { | |
| 197 verbosity_level = command_line->GetSwitchValueASCII( | |
| 198 switches::kLoggingLevelSwitchName); | |
| 199 | |
| 200 // Turn on logging for the test_driver and remoting components. | |
| 201 // This switch is parsed during logging::InitLogging. | |
| 202 command_line->AppendSwitchASCII("vmodule", | |
| 203 "*/remoting/*=" + verbosity_level); | |
| 204 logging::LoggingSettings logging_settings; | |
| 205 logging::InitLogging(logging_settings); | |
| 206 } | |
| 207 | |
| 208 // Create and register our global test data object. It will handle | |
| 209 // retrieving an access token for the user and spinning up VMs. | |
| 210 // The GTest framework will own the lifetime of this object once | |
| 211 // it is registered below. | |
| 212 scoped_ptr<remoting::test::AppRemotingTestDriverEnvironment> shared_data; | |
| 213 | |
| 214 shared_data.reset( | |
| 215 new remoting::test::AppRemotingTestDriverEnvironment( | |
| 216 user_name, | |
| 217 service_environment)); | |
| 218 | |
| 219 if (!shared_data->Initialize(auth_code)) { | |
| 220 // If we failed to initialize our shared data object, then bail. | |
| 221 return -1; | |
| 222 } | |
| 223 | |
| 224 // Since we've successfully set up our shared_data object, we'll assign the | |
| 225 // value to our global* and transfer ownership to the framework. | |
| 226 AppRemotingSharedData = shared_data.release(); | |
| 227 testing::AddGlobalTestEnvironment(AppRemotingSharedData); | |
| 228 | |
| 148 // Because many tests may access the same remoting host(s), we need to run | 229 // Because many tests may access the same remoting host(s), we need to run |
| 149 // the tests sequentially so they do not interfere with each other. | 230 // the tests sequentially so they do not interfere with each other. |
| 150 return base::LaunchUnitTestsSerially( | 231 return base::LaunchUnitTestsSerially( |
| 151 argc, | 232 argc, |
| 152 argv, | 233 argv, |
| 153 base::Bind(&base::TestSuite::Run, base::Unretained(&test_suite))); | 234 base::Bind(&base::TestSuite::Run, base::Unretained(&test_suite))); |
| 154 } | 235 } |
| OLD | NEW |