|
12 | 12 | using Microsoft.Extensions.Hosting;
|
13 | 13 | using Microsoft.Extensions.Logging;
|
14 | 14 |
|
15 |
| -namespace KubeOps.Testing |
| 15 | +namespace KubeOps.Testing; |
| 16 | + |
| 17 | +/// <summary> |
| 18 | +/// Operator factory for testing an operator created with the asp web server. |
| 19 | +/// </summary> |
| 20 | +/// <typeparam name="TTestStartup">Type of the Startup type (see asp.net).</typeparam> |
| 21 | +public class KubernetesOperatorFactory<TTestStartup> : WebApplicationFactory<TTestStartup> |
| 22 | + where TTestStartup : class |
16 | 23 | {
|
| 24 | + private string? _solutionRelativeContentRoot; |
| 25 | + |
17 | 26 | /// <summary>
|
18 |
| - /// Operator factory for testing an operator created with the asp web server. |
| 27 | + /// Return a mocked kubernetes client. This client defines |
| 28 | + /// no-op for all related methods. |
19 | 29 | /// </summary>
|
20 |
| - /// <typeparam name="TTestStartup">Type of the Startup type (see asp.net).</typeparam> |
21 |
| - public class KubernetesOperatorFactory<TTestStartup> : WebApplicationFactory<TTestStartup> |
22 |
| - where TTestStartup : class |
23 |
| - { |
24 |
| - private string? _solutionRelativeContentRoot; |
| 30 | + public MockKubernetesClient MockedKubernetesClient => |
| 31 | + Services.GetRequiredService<IKubernetesClient>() as MockKubernetesClient ?? |
| 32 | + throw new ArgumentException("Wrong kubernetes client registered."); |
25 | 33 |
|
26 |
| - /// <summary> |
27 |
| - /// Return a mocked kubernetes client. This client defines |
28 |
| - /// no-op for all related methods. |
29 |
| - /// </summary> |
30 |
| - public MockKubernetesClient MockedKubernetesClient => |
31 |
| - Services.GetRequiredService<IKubernetesClient>() as MockKubernetesClient ?? |
32 |
| - throw new ArgumentException("Wrong kubernetes client registered."); |
33 |
| - |
34 |
| - /// <summary> |
35 |
| - /// Set a specific content root path to the given factory. |
36 |
| - /// </summary> |
37 |
| - /// <param name="root">The solution relative content root path to configure.</param> |
38 |
| - /// <returns>The <see cref="KubernetesOperatorFactory{TTestStartup}"/> for chaining.</returns> |
39 |
| - public KubernetesOperatorFactory<TTestStartup> WithSolutionRelativeContentRoot(string root) |
40 |
| - { |
41 |
| - _solutionRelativeContentRoot = root; |
42 |
| - return this; |
43 |
| - } |
44 |
| - |
45 |
| - /// <summary> |
46 |
| - /// Start the server. |
47 |
| - /// </summary> |
48 |
| - public void Run() |
49 |
| - { |
50 |
| - // This triggers "EnsureServer()" in the base class. |
51 |
| - var server = Server; |
52 |
| - } |
| 34 | + /// <summary> |
| 35 | + /// Set a specific content root path to the given factory. |
| 36 | + /// </summary> |
| 37 | + /// <param name="root">The solution relative content root path to configure.</param> |
| 38 | + /// <returns>The <see cref="KubernetesOperatorFactory{TTestStartup}"/> for chaining.</returns> |
| 39 | + public KubernetesOperatorFactory<TTestStartup> WithSolutionRelativeContentRoot(string root) |
| 40 | + { |
| 41 | + _solutionRelativeContentRoot = root; |
| 42 | + return this; |
| 43 | + } |
53 | 44 |
|
54 |
| - public Task EnqueueEvent<TEntity>( |
55 |
| - ResourceEventType type, |
56 |
| - TEntity resource, |
57 |
| - int attempt = 0, |
58 |
| - TimeSpan? delay = null) |
59 |
| - where TEntity : class, IKubernetesObject<V1ObjectMeta> |
60 |
| - { |
61 |
| - var queue = Services.GetService<IEventQueue<TEntity>>(); |
| 45 | + /// <summary> |
| 46 | + /// Start the server. |
| 47 | + /// </summary> |
| 48 | + public void Run() |
| 49 | + { |
| 50 | + // This triggers "EnsureServer()" in the base class. |
| 51 | + var server = Server; |
| 52 | + } |
62 | 53 |
|
63 |
| - queue?.EnqueueLocal(new ResourceEvent<TEntity>(type, resource, attempt, delay)); |
| 54 | + public Task EnqueueEvent<TEntity>( |
| 55 | + ResourceEventType type, |
| 56 | + TEntity resource, |
| 57 | + int attempt = 0, |
| 58 | + TimeSpan? delay = null) |
| 59 | + where TEntity : class, IKubernetesObject<V1ObjectMeta> |
| 60 | + { |
| 61 | + var queue = Services.GetService<IEventQueue<TEntity>>(); |
64 | 62 |
|
65 |
| - return Task.CompletedTask; |
66 |
| - } |
| 63 | + queue?.EnqueueLocal(new ResourceEvent<TEntity>(type, resource, attempt, delay)); |
67 | 64 |
|
68 |
| - public Task EnqueueFinalization<TEntity>(TEntity resource) |
69 |
| - where TEntity : class, IKubernetesObject<V1ObjectMeta> |
70 |
| - { |
71 |
| - var queue = Services.GetService<IEventQueue<TEntity>>(); |
| 65 | + return Task.CompletedTask; |
| 66 | + } |
72 | 67 |
|
73 |
| - queue?.EnqueueLocal(new ResourceEvent<TEntity>(ResourceEventType.Finalizing, resource)); |
| 68 | + public Task EnqueueFinalization<TEntity>(TEntity resource) |
| 69 | + where TEntity : class, IKubernetesObject<V1ObjectMeta> |
| 70 | + { |
| 71 | + var queue = Services.GetService<IEventQueue<TEntity>>(); |
74 | 72 |
|
75 |
| - return Task.CompletedTask; |
76 |
| - } |
| 73 | + queue?.EnqueueLocal(new ResourceEvent<TEntity>(ResourceEventType.Finalizing, resource)); |
77 | 74 |
|
78 |
| - /// <summary> |
79 |
| - /// Create the host builder. Needed for the factory. |
80 |
| - /// </summary> |
81 |
| - /// <returns>The created <see cref="IHostBuilder"/>.</returns> |
82 |
| - protected override IHostBuilder CreateHostBuilder() => |
83 |
| - Host.CreateDefaultBuilder() |
84 |
| - .ConfigureWebHostDefaults( |
85 |
| - webBuilder => webBuilder |
86 |
| - .UseStartup<TTestStartup>()); |
| 75 | + return Task.CompletedTask; |
| 76 | + } |
87 | 77 |
|
88 |
| - /// <summary> |
89 |
| - /// Configure the web-host. |
90 |
| - /// This registers the mocked client as well as mocked |
91 |
| - /// event queues. |
92 |
| - /// </summary> |
93 |
| - /// <param name="builder">The web host builder.</param> |
94 |
| - protected override void ConfigureWebHost(IWebHostBuilder builder) |
95 |
| - { |
96 |
| - base.ConfigureWebHost(builder); |
97 |
| - builder.ConfigureTestServices( |
98 |
| - services => |
99 |
| - { |
100 |
| - var elector = services.First( |
101 |
| - d => d.ServiceType == typeof(IHostedService) && d.ImplementationType == typeof(LeaderElector)); |
102 |
| - services.Remove(elector); |
| 78 | + /// <summary> |
| 79 | + /// Create the host builder. Needed for the factory. |
| 80 | + /// </summary> |
| 81 | + /// <returns>The created <see cref="IHostBuilder"/>.</returns> |
| 82 | + protected override IHostBuilder CreateHostBuilder() => |
| 83 | + Host.CreateDefaultBuilder() |
| 84 | + .ConfigureWebHostDefaults( |
| 85 | + webBuilder => webBuilder |
| 86 | + .UseStartup<TTestStartup>()); |
103 | 87 |
|
104 |
| - services.RemoveAll(typeof(IEventQueue<>)); |
105 |
| - services.AddSingleton(typeof(IEventQueue<>), typeof(MockEventQueue<>)); |
106 |
| - }); |
107 |
| - if (_solutionRelativeContentRoot != null) |
| 88 | + /// <summary> |
| 89 | + /// Configure the web-host. |
| 90 | + /// This registers the mocked client as well as mocked |
| 91 | + /// event queues. |
| 92 | + /// </summary> |
| 93 | + /// <param name="builder">The web host builder.</param> |
| 94 | + protected override void ConfigureWebHost(IWebHostBuilder builder) |
| 95 | + { |
| 96 | + base.ConfigureWebHost(builder); |
| 97 | + builder.ConfigureTestServices( |
| 98 | + services => |
108 | 99 | {
|
109 |
| - builder.UseSolutionRelativeContentRoot(_solutionRelativeContentRoot); |
110 |
| - } |
| 100 | + var elector = services.First( |
| 101 | + d => d.ServiceType == typeof(IHostedService) && d.ImplementationType == typeof(LeaderElector)); |
| 102 | + services.Remove(elector); |
111 | 103 |
|
112 |
| - builder.ConfigureLogging(logging => logging.ClearProviders()); |
| 104 | + services.RemoveAll(typeof(IEventQueue<>)); |
| 105 | + services.AddSingleton(typeof(IEventQueue<>), typeof(MockEventQueue<>)); |
| 106 | + }); |
| 107 | + if (_solutionRelativeContentRoot != null) |
| 108 | + { |
| 109 | + builder.UseSolutionRelativeContentRoot(_solutionRelativeContentRoot); |
113 | 110 | }
|
| 111 | + |
| 112 | + builder.ConfigureLogging(logging => logging.ClearProviders()); |
114 | 113 | }
|
115 | 114 | }
|
0 commit comments