blob: 73efad1f842d12ac2022060e157059bfac93c5f7 [file] [log] [blame]
Jan Tattermusch62d6c202015-12-14 10:55:30 -08001#region Copyright notice and license
2
Jan Tattermusch7897ae92017-06-07 22:57:36 +02003// Copyright 2015 gRPC authors.
Jan Tattermusch62d6c202015-12-14 10:55:30 -08004//
Jan Tattermusch7897ae92017-06-07 22:57:36 +02005// 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 Tattermusch62d6c202015-12-14 10:55:30 -08008//
Jan Tattermusch7897ae92017-06-07 22:57:36 +02009// http://www.apache.org/licenses/LICENSE-2.0
Jan Tattermusch62d6c202015-12-14 10:55:30 -080010//
Jan Tattermusch7897ae92017-06-07 22:57:36 +020011// 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 Tattermusch62d6c202015-12-14 10:55:30 -080016
17#endregion
18
19using System;
20using System.Collections.Generic;
21using System.IO;
22using System.Reflection;
23using Grpc.Core;
24using Grpc.Core.Internal;
25using Grpc.Core.Utils;
Jan Tattermusch9c3f15c2016-04-09 16:25:01 -070026using Newtonsoft.Json;
Jan Tattermusch62d6c202015-12-14 10:55:30 -080027using NUnit.Framework;
28
29namespace Grpc.Core.Tests
30{
31 public class SanityTest
32 {
Jan Tattermusch317b8ac2016-06-16 09:36:11 -070033 // TODO: make sanity test work for CoreCLR as well
Jan Tattermusch1c0f30c2016-07-30 03:59:16 +080034#if !NETCOREAPP1_0
Jan Tattermusch62d6c202015-12-14 10:55:30 -080035 /// <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 Polcyn7f8456e2016-08-14 20:24:35 -070046 var discoveredTests = DiscoverAllTestClasses();
47 var testsFromFile
48 = JsonConvert.DeserializeObject<Dictionary<string, List<string>>>(ReadTestsJson());
Jan Tattermusch62d6c202015-12-14 10:55:30 -080049
Alex Polcynb4280fa2016-07-28 23:48:50 -070050 Assert.AreEqual(discoveredTests, testsFromFile);
Jan Tattermusch62d6c202015-12-14 10:55:30 -080051 }
52
53 /// <summary>
54 /// Gets list of all test classes obtained by inspecting all the test assemblies.
55 /// </summary>
Jan Tattermusch9c3f15c2016-04-09 16:25:01 -070056 private Dictionary<string, List<string>> DiscoverAllTestClasses()
Jan Tattermusch62d6c202015-12-14 10:55:30 -080057 {
58 var assemblies = GetTestAssemblies();
59
Jan Tattermusch9c3f15c2016-04-09 16:25:01 -070060 var testsByAssembly = new Dictionary<string, List<string>>();
Jan Tattermusch62d6c202015-12-14 10:55:30 -080061 foreach (var assembly in assemblies)
62 {
Jan Tattermusch9c3f15c2016-04-09 16:25:01 -070063 var testClasses = new List<string>();
Jan Tattermusch62d6c202015-12-14 10:55:30 -080064 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 Tattermusch9c3f15c2016-04-09 16:25:01 -070077 testClasses.Sort();
78 testsByAssembly.Add(assembly.GetName().Name, testClasses);
Jan Tattermusch62d6c202015-12-14 10:55:30 -080079 }
Jan Tattermusch9c3f15c2016-04-09 16:25:01 -070080 return testsByAssembly;
Jan Tattermusch62d6c202015-12-14 10:55:30 -080081 }
82
Jan Tattermusch9c3f15c2016-04-09 16:25:01 -070083 /// <summary>
84 /// Reads contents of tests.json file.
85 /// </summary>
86 private string ReadTestsJson()
Jan Tattermusch62d6c202015-12-14 10:55:30 -080087 {
88 var assemblyDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
Jan Tattermusch3c344d22017-04-04 16:49:06 +020089 var testsJsonFile = Path.Combine(assemblyDir, "..", "..", "..", "..", "tests.json");
Jan Tattermusch62d6c202015-12-14 10:55:30 -080090 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 Tattermusch02017762016-11-23 15:39:56 +0100103 "Grpc.IntegrationTesting",
104 "Grpc.Reflection.Tests",
Jan Tattermusch62d6c202015-12-14 10:55:30 -0800105 };
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 Tattermuschec4359d2016-04-11 20:55:44 -0700113#endif
Jan Tattermusch62d6c202015-12-14 10:55:30 -0800114 }
115}