Skip to content

Commit ba957c3

Browse files
authored
test: add some tests for web operators (#638)
1 parent 1641a10 commit ba957c3

36 files changed

+1947
-1547
lines changed
Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
using System.Reflection;
2-
3-
using KubeOps.Abstractions.Entities;
4-
5-
namespace KubeOps.Cli.Transpilation;
6-
7-
internal abstract record BaseWebhook(TypeInfo Webhook, EntityMetadata Metadata)
8-
{
9-
private bool HasCreate => Webhook.DeclaredMembers.Any(m => m.Name.StartsWith("Create"));
10-
11-
private bool HasUpdate => Webhook.DeclaredMembers.Any(m => m.Name.StartsWith("Update"));
12-
13-
private bool HasDelete => Webhook.DeclaredMembers.Any(m => m.Name.StartsWith("Delete"));
14-
15-
public abstract string WebhookPath { get; }
16-
17-
public string[] GetOperations() =>
18-
new[] { HasCreate ? "CREATE" : null, HasUpdate ? "UPDATE" : null, HasDelete ? "DELETE" : null, }
19-
.Where(o => o is not null).ToArray()!;
20-
}
1+
using System.Reflection;
2+
3+
using KubeOps.Abstractions.Entities;
4+
5+
namespace KubeOps.Cli.Transpilation;
6+
7+
internal abstract record BaseWebhook(TypeInfo Webhook, EntityMetadata Metadata)
8+
{
9+
private bool HasCreate => Webhook.DeclaredMembers.Any(m => m.Name.StartsWith("Create"));
10+
11+
private bool HasUpdate => Webhook.DeclaredMembers.Any(m => m.Name.StartsWith("Update"));
12+
13+
private bool HasDelete => Webhook.DeclaredMembers.Any(m => m.Name.StartsWith("Delete"));
14+
15+
public abstract string WebhookPath { get; }
16+
17+
public string[] GetOperations() =>
18+
new[] { HasCreate ? "CREATE" : null, HasUpdate ? "UPDATE" : null, HasDelete ? "DELETE" : null, }
19+
.Where(o => o is not null).ToArray()!;
20+
}
Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
using System.Reflection;
2-
3-
using KubeOps.Abstractions.Entities;
4-
5-
namespace KubeOps.Cli.Transpilation;
6-
7-
internal record MutationWebhook(TypeInfo Validator, EntityMetadata Metadata) : BaseWebhook(Validator, Metadata)
8-
{
9-
public override string WebhookPath =>
10-
$"/mutate/{Validator.BaseType!.GenericTypeArguments[0].Name.ToLowerInvariant()}";
11-
}
1+
using System.Reflection;
2+
3+
using KubeOps.Abstractions.Entities;
4+
5+
namespace KubeOps.Cli.Transpilation;
6+
7+
internal record MutationWebhook(TypeInfo Validator, EntityMetadata Metadata) : BaseWebhook(Validator, Metadata)
8+
{
9+
public override string WebhookPath =>
10+
$"/mutate/{Validator.BaseType!.GenericTypeArguments[0].Name.ToLowerInvariant()}";
11+
}
Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
using System.Reflection;
2-
3-
using KubeOps.Abstractions.Entities;
4-
5-
namespace KubeOps.Cli.Transpilation;
6-
7-
internal record ValidationWebhook(TypeInfo Validator, EntityMetadata Metadata) : BaseWebhook(Validator, Metadata)
8-
{
9-
public override string WebhookPath =>
10-
$"/validate/{Validator.BaseType!.GenericTypeArguments[0].Name.ToLowerInvariant()}";
11-
}
1+
using System.Reflection;
2+
3+
using KubeOps.Abstractions.Entities;
4+
5+
namespace KubeOps.Cli.Transpilation;
6+
7+
internal record ValidationWebhook(TypeInfo Validator, EntityMetadata Metadata) : BaseWebhook(Validator, Metadata)
8+
{
9+
public override string WebhookPath =>
10+
$"/validate/{Validator.BaseType!.GenericTypeArguments[0].Name.ToLowerInvariant()}";
11+
}
Lines changed: 50 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,50 @@
1-
using KubeOps.Abstractions.Builder;
2-
using KubeOps.Operator.Web.LocalTunnel;
3-
4-
using Microsoft.Extensions.DependencyInjection;
5-
6-
namespace KubeOps.Operator.Web.Builder;
7-
8-
/// <summary>
9-
/// Method extensions for the operator builder to register web specific services.
10-
/// </summary>
11-
public static class OperatorBuilderExtensions
12-
{
13-
/// <summary>
14-
/// Adds a hosted service to the system that creates a "Local Tunnel"
15-
/// (http://localtunnel.github.io/www/) to the running application.
16-
/// The tunnel points to the configured host/port configuration and then
17-
/// registers itself as webhook target within Kubernetes. This
18-
/// enables developers to easily create webhooks without the requirement
19-
/// of registering ngrok / localtunnel urls themselves.
20-
/// </summary>
21-
/// <param name="builder">The operator builder.</param>
22-
/// <param name="port">The desired port that the asp.net application will run on.</param>
23-
/// <param name="hostname">The desired hostname.</param>
24-
/// <returns>The builder for chaining.</returns>
25-
/// <example>
26-
/// Attach the development tunnel to the operator if in debug mode.
27-
/// <code>
28-
/// var builder = WebApplication.CreateBuilder(args);
29-
/// builder.Services
30-
/// .AddKubernetesOperator()
31-
/// .RegisterComponents()
32-
/// #if DEBUG
33-
/// .AddDevelopmentTunnel(5000)
34-
/// #endif
35-
/// ;
36-
/// </code>
37-
/// </example>
38-
public static IOperatorBuilder AddDevelopmentTunnel(
39-
this IOperatorBuilder builder,
40-
ushort port,
41-
string hostname = "localhost")
42-
{
43-
builder.Services.AddHostedService<DevelopmentTunnelService>();
44-
builder.Services.AddSingleton(new TunnelConfig(hostname, port));
45-
return builder;
46-
}
47-
}
1+
using System.Reflection;
2+
3+
using KubeOps.Abstractions.Builder;
4+
using KubeOps.Operator.Web.LocalTunnel;
5+
6+
using Microsoft.Extensions.DependencyInjection;
7+
8+
namespace KubeOps.Operator.Web.Builder;
9+
10+
/// <summary>
11+
/// Method extensions for the operator builder to register web specific services.
12+
/// </summary>
13+
public static class OperatorBuilderExtensions
14+
{
15+
/// <summary>
16+
/// Adds a hosted service to the system that creates a "Local Tunnel"
17+
/// (http://localtunnel.github.io/www/) to the running application.
18+
/// The tunnel points to the configured host/port configuration and then
19+
/// registers itself as webhook target within Kubernetes. This
20+
/// enables developers to easily create webhooks without the requirement
21+
/// of registering ngrok / localtunnel urls themselves.
22+
/// </summary>
23+
/// <param name="builder">The operator builder.</param>
24+
/// <param name="port">The desired port that the asp.net application will run on.</param>
25+
/// <param name="hostname">The desired hostname.</param>
26+
/// <returns>The builder for chaining.</returns>
27+
/// <example>
28+
/// Attach the development tunnel to the operator if in debug mode.
29+
/// <code>
30+
/// var builder = WebApplication.CreateBuilder(args);
31+
/// builder.Services
32+
/// .AddKubernetesOperator()
33+
/// .RegisterComponents()
34+
/// #if DEBUG
35+
/// .AddDevelopmentTunnel(5000)
36+
/// #endif
37+
/// ;
38+
/// </code>
39+
/// </example>
40+
public static IOperatorBuilder AddDevelopmentTunnel(
41+
this IOperatorBuilder builder,
42+
ushort port,
43+
string hostname = "localhost")
44+
{
45+
builder.Services.AddHostedService<DevelopmentTunnelService>();
46+
builder.Services.AddSingleton(new TunnelConfig(hostname, port));
47+
builder.Services.AddSingleton(new WebhookLoader(Assembly.GetEntryAssembly()!));
48+
return builder;
49+
}
50+
}

0 commit comments

Comments
 (0)