PageRenderTime 65ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/mcs/tools/mono-helix-client/HelixTestBase.cs

https://github.com/grendello/mono
C# | 100 lines | 58 code | 13 blank | 29 comment | 1 complexity | eb847920c6ae855d3ce98677f55090f4 MD5 | raw file
  1. //
  2. // HelixTestBase.cs
  3. //
  4. // Authors:
  5. // Alexander Köplinger <alkpli@microsoft.com>
  6. //
  7. // Copyright (C) 2018 Microsoft
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.IO;
  30. using System.Threading.Tasks;
  31. using Microsoft.DotNet.Helix.Client;
  32. public abstract class HelixTestBase : HelixBase
  33. {
  34. IJobDefinition _job;
  35. protected HelixTestBase (string helixType) : base ()
  36. {
  37. var helixSource = GetEnvironmentVariable ("MONO_HELIX_SOURCE");
  38. if (helixSource.StartsWith ("pr/"))
  39. {
  40. // workaround for https://github.com/dotnet/arcade/issues/1392
  41. var storage = new Storage ((HelixApi)_api);
  42. var anonymousApi = ApiFactory.GetAnonymous ();
  43. typeof (HelixApi).GetProperty ("Storage").SetValue (anonymousApi, storage, null);
  44. _api = anonymousApi;
  45. }
  46. var build = _api.Job.Define ()
  47. .WithSource (helixSource)
  48. .WithType (helixType)
  49. .WithBuild (GetEnvironmentVariable ("MONO_HELIX_BUILD_MONIKER"));
  50. _job = build
  51. .WithTargetQueue (GetEnvironmentVariable ("MONO_HELIX_TARGET_QUEUE"))
  52. .WithCreator (GetEnvironmentVariable ("MONO_HELIX_CREATOR"))
  53. .WithCorrelationPayloadDirectory (GetEnvironmentVariable ("MONO_HELIX_TEST_PAYLOAD_DIRECTORY"))
  54. .WithCorrelationPayloadFiles (GetEnvironmentVariable ("MONO_HELIX_XUNIT_REPORTER_PATH"))
  55. // these are well-known properties used by Mission Control
  56. .WithProperty ("architecture", GetEnvironmentVariable ("MONO_HELIX_ARCHITECTURE"))
  57. .WithProperty ("operatingSystem", GetEnvironmentVariable ("MONO_HELIX_OPERATINGSYSTEM"));
  58. }
  59. protected void CreateWorkItem (string name, string command, int timeoutInSeconds)
  60. {
  61. _job.DefineWorkItem (name)
  62. .WithCommand ($"chmod +x $HELIX_CORRELATION_PAYLOAD/mono-test.sh; $HELIX_CORRELATION_PAYLOAD/mono-test.sh {command}; exit_code=$1; $HELIX_PYTHONPATH $HELIX_CORRELATION_PAYLOAD/xunit-reporter.py; exit $exit_code")
  63. .WithEmptyPayload ()
  64. .WithTimeout (TimeSpan.FromSeconds (timeoutInSeconds))
  65. .AttachToJob ();
  66. }
  67. protected void CreateCustomWorkItem (string suite, int timeoutInSeconds = 900)
  68. {
  69. CreateWorkItem (suite, $"--{suite}", timeoutInSeconds);
  70. }
  71. protected void CreateNunitWorkItem (string assembly, string profile = "net_4_x", int timeoutInSeconds = 900)
  72. {
  73. var flakyTestRetries = Environment.GetEnvironmentVariable ("MONO_FLAKY_TEST_RETRIES") ?? "0";
  74. CreateWorkItem (assembly, $"--nunit {profile}/tests/{assembly} --flaky-test-retries={flakyTestRetries}", timeoutInSeconds);
  75. }
  76. protected void CreateXunitWorkItem (string assembly, string profile = "net_4_x", int timeoutInSeconds = 900)
  77. {
  78. CreateWorkItem (assembly, $"--xunit {profile}/tests/{assembly}", timeoutInSeconds);
  79. }
  80. public async Task<string> SendJob ()
  81. {
  82. Console.WriteLine ($"Sending job to Helix...");
  83. var sentJob = await _job.SendAsync ();
  84. Console.WriteLine ($"Job '{sentJob.CorrelationId}' created.");
  85. return sentJob.CorrelationId;
  86. }
  87. }