private static string CreateConfigRequestContentAsJson() { // Create a Json dictionary of name/value pairs from TestProperties StringBuilder sb = new StringBuilder("{ "); string[] propertyNames = TestProperties.PropertyNames.ToArray(); for (int i = 0; i < propertyNames.Length; ++i) { string propertyName = propertyNames[i]; string propertyValue = TestProperties.GetProperty(propertyName); // We do not configure the BridgeResourceFolder because the Bridge // is provided one at startup. Moreover, cross-platform scenarios // would require this folder to be shared across OS boundaries. if (String.Equals(propertyName, TestProperties.BridgeResourceFolder_PropertyName, StringComparison.OrdinalIgnoreCase)) { continue; } sb.Append(String.Format("\"{0}\" : \"{1}\"", propertyName, propertyValue)); if (i < propertyNames.Length - 1) { sb.Append(", "); } } sb.Append(" }"); return(sb.ToString()); }
private static string CreateConfigRequestContentAsJson() { ValidateConfigRequestProperties(); // Create a Json dictionary of name/value pairs from TestProperties StringBuilder sb = new StringBuilder("{ "); string[] propertyNames = TestProperties.PropertyNames.ToArray(); for (int i = 0; i < propertyNames.Length; ++i) { string propertyName = propertyNames[i]; string propertyValue = TestProperties.GetProperty(propertyName); // If the Bridge is remote but the resources folder is local, omit it from the config request. // In remote scenarios, either the Bridge must be started with its own resources folder or // it is placed on a file share that we can reference from this application. if ((String.Equals(propertyName, TestProperties.BridgeResourceFolder_PropertyName)) && (!IsBridgeHostedLocally && !IsPathRemote(propertyValue))) { continue; } sb.Append(String.Format("\"{0}\" : \"{1}\"", propertyName, propertyValue)); if (i < propertyNames.Length - 1) { sb.Append(", "); } } sb.Append(" }"); return(sb.ToString()); }
// Returns true if the Bridge will respond or has responded to localhost. // This is used to determine whether local file paths are acceptable for the BridgeResourceFolder. private static bool DoesBridgeRespondToLocalHost() { // If we have already pinged before and the host is "localhost", no need to repeat it. if (_BridgeStatus == BridgeState.Started && String.Equals("localhost", TestProperties.GetProperty(TestProperties.BridgeHost_PropertyName), StringComparison.OrdinalIgnoreCase)) { return(true); } // This ping to localhost may fail if the Bridge is running on a remote machine. // This is acceptable for the purposes of this teste. string localHostAddress = String.Format("http://localhost:{0}", TestProperties.GetProperty(TestProperties.BridgePort_PropertyName)); using (HttpClient httpClient = new HttpClient()) { try { var response = httpClient.GetAsync(BridgeBaseAddress).GetAwaiter().GetResult(); return(response.IsSuccessStatusCode); } catch { return(false); } } }
// Returns 'true' or 'false' for the given condition name. // There are several levels of precedence to evaluate the condition. // The value is cached after being evaluated the first time. // Precedence order for evaluation is: // 1. Environment variable, if set and is 'true' or 'false'. // 2. TestProperties, if set and is 'true' or 'false' // 3. detection func, if specified // 4. If none of the above, 'false' private static bool GetConditionValue(string conditionName, Func <bool> detectFunc = null) { // Lock to evaluate once only lock (s_evaluationLock) { bool result = false; if (s_evaluatedConditions.TryGetValue(conditionName, out result)) { return(result); } bool evaluatedResult = false; // Highest precedence: environment variable if set and can be parsed string value = Environment.GetEnvironmentVariable(conditionName); if (value != null) { value = value.Trim(); } bool parsedValue = false; if (!String.IsNullOrWhiteSpace(value) && bool.TryParse(value, out parsedValue)) { result = parsedValue; evaluatedResult = true; } // Next precedence: TestProperties if present and can be parsed else if (TestProperties.PropertyNames.Contains(conditionName)) { // GetProperty trims the string value = TestProperties.GetProperty(conditionName); if (!String.IsNullOrWhiteSpace(value) && bool.TryParse(value, out parsedValue)) { result = parsedValue; evaluatedResult = true; } } // Next precedence: optional runtime detection func if (!evaluatedResult && detectFunc != null) { result = detectFunc(); evaluatedResult = true; } // Final precedence: false is default if (!evaluatedResult) { result = false; } s_evaluatedConditions[conditionName] = result; return(result); } }
// Validates some of the name/value pairs that will be sent to the Bridge // via the /config POST request. private static void ValidateConfigRequestProperties() { string bridgeResourceFolder = TestProperties.GetProperty(TestProperties.BridgeResourceFolder_PropertyName); // Validate the Bridge resource folder exists (even if UNC). if (String.IsNullOrEmpty(bridgeResourceFolder) || !Directory.Exists(bridgeResourceFolder)) { throw new Exception(String.Format("BridgeResourceFolder '{0}' does not exist", bridgeResourceFolder)); } }
// Returns the Domain if available. // TestProperties takes precedence, but if it has not been specified // and this is a Windows client, we infer it. public static string GetDomain() { string result = TestProperties.GetProperty(TestProperties.NegotiateTestDomain_PropertyName); if (String.IsNullOrEmpty(result) && IsClientDomainJoined()) { result = Environment.GetEnvironmentVariable("USERDOMAIN"); } return(result); }
private static bool IsConditionalTestEnabled(string testCategoryName) { string propertyValue = TestProperties.GetProperty(testCategoryName); bool isEnabled; if (!String.IsNullOrEmpty(propertyValue) && bool.TryParse(propertyValue, out isEnabled)) { return(isEnabled); } return(false); }
// Returns 'true' if the server is running as localhost. // This test is meant to be used only to detect that our service URI // indicates localhost. It is not intended to be used to detect that // the services are running locally but using an explicit host name. public static bool IsServerLocalHost() { string host = TestProperties.GetProperty(TestProperties.ServiceUri_PropertyName); if (String.IsNullOrWhiteSpace(host)) { return(false); } int index = host.IndexOf("localhost", 0, StringComparison.OrdinalIgnoreCase); return(index == 0); }
// Maps from the $(TestNugetRuntimeId) property to corresponding OSID. // Returns OSID.None if cannot determine the OSID. private static OSID OSIDfromTestRuntime() { string testRuntime = TestProperties.GetProperty(TestProperties.TestNugetRuntimeId_PropertyName); if (string.IsNullOrEmpty(testRuntime)) { return(OSID.None); } foreach (var pair in _runtimeToOSID) { string runtime = pair.Item1; if (testRuntime.IndexOf(runtime, StringComparison.OrdinalIgnoreCase) >= 0) { return(pair.Item2); } } return(OSID.None); }
// Returns a HashSet<int> containing all the test issue numbers to include // during a test run (where "include" means "don't skip"). The meaning of // this HashSet is: // null: [default] don't include any tests with [Issue] (i.e. skip them all) // count == 0: Include all tests with [Issue] (i.e. don't skip any) // count > 0: Include only those tests with an issue in the HashSet private static HashSet <int> DetectIssuesToInclude() { string includeTestsWithIssues = TestProperties.GetProperty(TestProperties.IncludeTestsWithIssues_PropertyName); // Empty string means don't include any tests with issues. // In other words, all tests with [Issue] will be skipped. if (String.IsNullOrEmpty(includeTestsWithIssues)) { return(null); } // The special value 'true' means include all tests with [Issue]. // The special value 'false' means skip all tests with [Issue]. bool propertyAsBool = false; if (bool.TryParse(includeTestsWithIssues, out propertyAsBool)) { return(propertyAsBool ? new HashSet <int>() : null); } // Anything else is interpreted as a semicolon or comma separated list of // issue numbers to include (i.e. not skip). string[] issues = includeTestsWithIssues.Split(new char[] { ';', ',' }, StringSplitOptions.RemoveEmptyEntries); HashSet <int> hashSet = new HashSet <int>(); foreach (string issue in issues) { int issueAsInt = 0; if (!int.TryParse(issue, out issueAsInt)) { Console.WriteLine(String.Format("Warning: The number '{0}' in IncludeTestsWithIssues is not a valid integer and will be ignored.", issue)); continue; } hashSet.Add(issueAsInt); } return(hashSet); }
// Gets the SPN if available internal static string GetSPN() { return(TestProperties.GetProperty(TestProperties.NegotiateTestSpn_PropertyName)); }
// Returns the explicit password if available internal static string GetExplicitPassword() { return(TestProperties.GetProperty(TestProperties.ExplicitPassword_PropertyName)); }
// Returns the explicit user name if available internal static string GetExplicitUserName() { return(TestProperties.GetProperty(TestProperties.ExplicitUserName_PropertyName)); }
// Returns 'true' if explicit credentials are available. // Currently, 'true' means the TestProperties for ExplicitUserName // and ExplicitPassword are non-blank. public static bool AreExplicitCredentialsAvailable() { return(!String.IsNullOrWhiteSpace(TestProperties.GetProperty(TestProperties.ExplicitUserName_PropertyName)) && !String.IsNullOrWhiteSpace(TestProperties.GetProperty(TestProperties.ExplicitPassword_PropertyName))); }