buehler/dotnet-operator-sdk

[bug]: MockKubernetesClient issue

vvitkovsky opened this issue · 1 comments

Describe the bug

MockKubernetesClient contains this object:
public IList<object>? ListResult { get; set; }

and this code to return result:

public Task<IList<TEntity>> List<TEntity>(string? @namespace = null, string? labelSelector = null)
     where TEntity : IKubernetesObject<V1ObjectMeta>
     => Task.FromResult(ListResult as IList<TEntity> ?? new List<TEntity>());

but the problem is that there is no direct cast between IList<object> and IList<TEntity>, so this code works incorrect.

To reproduce

Example:

var client = new MockKubernetesClient();
client.ListResult = new List<object>() { new V1Secret() };
var result = client.ListResult as IList<V1Secret>;

result will be null

Expected behavior

I would change a List a bit:

public Task<IList<TEntity>> List<TEntity>(string? @namespace = null, string? labelSelector = null)
     where TEntity : IKubernetesObject<V1ObjectMeta>
     => Task.FromResult<IList<TEntity>>(ListResult?.OfType<TEntity>().ToList() ?? new List<TEntity>());

This code works ok

Screenshots

No response

Additional Context

No response

invalid in v8 now :)