Jan Tattermusch | 62d6c20 | 2015-12-14 10:55:30 -0800 | [diff] [blame] | 1 | #region Copyright notice and license |
| 2 | |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 3 | // Copyright 2015 gRPC authors. |
Jan Tattermusch | 62d6c20 | 2015-12-14 10:55:30 -0800 | [diff] [blame] | 4 | // |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 5 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | // you may not use this file except in compliance with the License. |
| 7 | // You may obtain a copy of the License at |
Jan Tattermusch | 62d6c20 | 2015-12-14 10:55:30 -0800 | [diff] [blame] | 8 | // |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 9 | // http://www.apache.org/licenses/LICENSE-2.0 |
Jan Tattermusch | 62d6c20 | 2015-12-14 10:55:30 -0800 | [diff] [blame] | 10 | // |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 11 | // Unless required by applicable law or agreed to in writing, software |
| 12 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | // See the License for the specific language governing permissions and |
| 15 | // limitations under the License. |
Jan Tattermusch | 62d6c20 | 2015-12-14 10:55:30 -0800 | [diff] [blame] | 16 | |
| 17 | #endregion |
| 18 | |
| 19 | using System; |
| 20 | using System.Collections.Generic; |
| 21 | using System.IO; |
| 22 | using System.Reflection; |
| 23 | using Grpc.Core; |
| 24 | using Grpc.Core.Internal; |
| 25 | using Grpc.Core.Utils; |
Jan Tattermusch | 9c3f15c | 2016-04-09 16:25:01 -0700 | [diff] [blame] | 26 | using Newtonsoft.Json; |
Jan Tattermusch | 62d6c20 | 2015-12-14 10:55:30 -0800 | [diff] [blame] | 27 | using NUnit.Framework; |
| 28 | |
| 29 | namespace Grpc.Core.Tests |
| 30 | { |
| 31 | public class SanityTest |
| 32 | { |
Jan Tattermusch | 317b8ac | 2016-06-16 09:36:11 -0700 | [diff] [blame] | 33 | // TODO: make sanity test work for CoreCLR as well |
Jan Tattermusch | 1c0f30c | 2016-07-30 03:59:16 +0800 | [diff] [blame] | 34 | #if !NETCOREAPP1_0 |
Jan Tattermusch | 62d6c20 | 2015-12-14 10:55:30 -0800 | [diff] [blame] | 35 | /// <summary> |
| 36 | /// Because we depend on a native library, sometimes when things go wrong, the |
| 37 | /// entire NUnit test process crashes. To be able to track down problems better, |
| 38 | /// the NUnit tests are run by run_tests.py script in a separate process per test class. |
| 39 | /// The list of tests to run is stored in src/csharp/tests.json. |
| 40 | /// This test checks that the tests.json file is up to date by discovering all the |
| 41 | /// existing NUnit tests in all test assemblies and comparing to contents of tests.json. |
| 42 | /// </summary> |
| 43 | [Test] |
| 44 | public void TestsJsonUpToDate() |
| 45 | { |
Alex Polcyn | 7f8456e | 2016-08-14 20:24:35 -0700 | [diff] [blame] | 46 | var discoveredTests = DiscoverAllTestClasses(); |
| 47 | var testsFromFile |
| 48 | = JsonConvert.DeserializeObject<Dictionary<string, List<string>>>(ReadTestsJson()); |
Jan Tattermusch | 62d6c20 | 2015-12-14 10:55:30 -0800 | [diff] [blame] | 49 | |
Alex Polcyn | b4280fa | 2016-07-28 23:48:50 -0700 | [diff] [blame] | 50 | Assert.AreEqual(discoveredTests, testsFromFile); |
Jan Tattermusch | 62d6c20 | 2015-12-14 10:55:30 -0800 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | /// <summary> |
| 54 | /// Gets list of all test classes obtained by inspecting all the test assemblies. |
| 55 | /// </summary> |
Jan Tattermusch | 9c3f15c | 2016-04-09 16:25:01 -0700 | [diff] [blame] | 56 | private Dictionary<string, List<string>> DiscoverAllTestClasses() |
Jan Tattermusch | 62d6c20 | 2015-12-14 10:55:30 -0800 | [diff] [blame] | 57 | { |
| 58 | var assemblies = GetTestAssemblies(); |
| 59 | |
Jan Tattermusch | 9c3f15c | 2016-04-09 16:25:01 -0700 | [diff] [blame] | 60 | var testsByAssembly = new Dictionary<string, List<string>>(); |
Jan Tattermusch | 62d6c20 | 2015-12-14 10:55:30 -0800 | [diff] [blame] | 61 | foreach (var assembly in assemblies) |
| 62 | { |
Jan Tattermusch | 9c3f15c | 2016-04-09 16:25:01 -0700 | [diff] [blame] | 63 | var testClasses = new List<string>(); |
Jan Tattermusch | 62d6c20 | 2015-12-14 10:55:30 -0800 | [diff] [blame] | 64 | foreach (var t in assembly.GetTypes()) |
| 65 | { |
| 66 | foreach (var m in t.GetMethods()) |
| 67 | { |
| 68 | var attributes = m.GetCustomAttributes(typeof(NUnit.Framework.TestAttribute), true); |
| 69 | if (attributes.Length > 0) |
| 70 | { |
| 71 | testClasses.Add(t.FullName); |
| 72 | break; |
| 73 | } |
| 74 | |
| 75 | } |
| 76 | } |
Jan Tattermusch | 9c3f15c | 2016-04-09 16:25:01 -0700 | [diff] [blame] | 77 | testClasses.Sort(); |
| 78 | testsByAssembly.Add(assembly.GetName().Name, testClasses); |
Jan Tattermusch | 62d6c20 | 2015-12-14 10:55:30 -0800 | [diff] [blame] | 79 | } |
Jan Tattermusch | 9c3f15c | 2016-04-09 16:25:01 -0700 | [diff] [blame] | 80 | return testsByAssembly; |
Jan Tattermusch | 62d6c20 | 2015-12-14 10:55:30 -0800 | [diff] [blame] | 81 | } |
| 82 | |
Jan Tattermusch | 9c3f15c | 2016-04-09 16:25:01 -0700 | [diff] [blame] | 83 | /// <summary> |
| 84 | /// Reads contents of tests.json file. |
| 85 | /// </summary> |
| 86 | private string ReadTestsJson() |
Jan Tattermusch | 62d6c20 | 2015-12-14 10:55:30 -0800 | [diff] [blame] | 87 | { |
| 88 | var assemblyDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); |
Jan Tattermusch | 3c344d2 | 2017-04-04 16:49:06 +0200 | [diff] [blame] | 89 | var testsJsonFile = Path.Combine(assemblyDir, "..", "..", "..", "..", "tests.json"); |
Jan Tattermusch | 62d6c20 | 2015-12-14 10:55:30 -0800 | [diff] [blame] | 90 | return File.ReadAllText(testsJsonFile); |
| 91 | } |
| 92 | |
| 93 | private List<Assembly> GetTestAssemblies() |
| 94 | { |
| 95 | var result = new List<Assembly>(); |
| 96 | var executingAssembly = Assembly.GetExecutingAssembly(); |
| 97 | |
| 98 | result.Add(executingAssembly); |
| 99 | |
| 100 | var otherAssemblies = new[] { |
| 101 | "Grpc.Examples.Tests", |
| 102 | "Grpc.HealthCheck.Tests", |
Jan Tattermusch | 0201776 | 2016-11-23 15:39:56 +0100 | [diff] [blame] | 103 | "Grpc.IntegrationTesting", |
| 104 | "Grpc.Reflection.Tests", |
Jan Tattermusch | 62d6c20 | 2015-12-14 10:55:30 -0800 | [diff] [blame] | 105 | }; |
| 106 | foreach (var assemblyName in otherAssemblies) |
| 107 | { |
| 108 | var location = executingAssembly.Location.Replace("Grpc.Core.Tests", assemblyName); |
| 109 | result.Add(Assembly.LoadFrom(location)); |
| 110 | } |
| 111 | return result; |
| 112 | } |
Jan Tattermusch | ec4359d | 2016-04-11 20:55:44 -0700 | [diff] [blame] | 113 | #endif |
Jan Tattermusch | 62d6c20 | 2015-12-14 10:55:30 -0800 | [diff] [blame] | 114 | } |
| 115 | } |