File: HelmExtensionsTests.cs
Web Access
Project: src\tests\Aspire.Hosting.Kubernetes.Tests\Aspire.Hosting.Kubernetes.Tests.csproj (Aspire.Hosting.Kubernetes.Tests)
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
 
using Aspire.Hosting.Kubernetes.Extensions;
using YamlDotNet.Core;
 
namespace Aspire.Hosting.Kubernetes.Tests;
 
public class HelmExtensionsTests
{
    [Theory]
    [InlineData("\U0001F680{{ .Values.message }}", "{{ tpl \"\U0001F680{{ .Values.message }}\" . | quote }}")]
    [InlineData("\\uD83D\\uDE80{{ .Values.message }}", "{{ tpl \"\\\\uD83D\\\\uDE80{{ .Values.message }}\" . | quote }}")]
    [InlineData("\"\n\r\t\\\0", "{{ tpl \"\\\"\\n\\r\\t\\\\\\u0000\" . | quote }}")]
    public void ToQuotedHelmTemplateExpressionUsesGoStringEscaping(string value, string expected)
    {
        Assert.Equal(expected, value.ToQuotedHelmTemplateExpression());
    }
 
    [Fact]
    public void ToQuotedHelmTemplateExpressionDoesNotWrapAnAlreadyQuotedTemplate()
    {
        var template = "{{ .Values.config.myapp.key }}".ToQuotedHelmTemplateExpression();
 
        Assert.Equal(template, template.ToQuotedHelmTemplateExpression());
        Assert.Equal((false, ScalarStyle.ForcePlain), HelmExtensions.ShouldDoubleQuoteString(template));
    }
 
    [Theory]
    [InlineData("plain string", true, null)]
    [InlineData("{{ .Values.config.myapp.key }}", true, null)]
    [InlineData("{{ .Values.config.myapp.port | int }}", false, ScalarStyle.ForcePlain)]
    [InlineData("{{ .Values.config.myapp.count | int64 }}", false, ScalarStyle.ForcePlain)]
    [InlineData("{{ .Values.config.myapp.rate | float64 }}", false, ScalarStyle.ForcePlain)]
    [InlineData("{{ .Values.config.myapp.port | int | toString }}", true, null)]
    [InlineData("{{ if eq (.Values.parameters.myapp.enable_tls | lower) \"true\" }},ssl=true{{ else }},ssl=false{{ end }}", false, ScalarStyle.ForcePlain)]
    public void ShouldDoubleQuoteString_ReturnsExpectedResult(string value, bool expectedShouldApply, ScalarStyle? expectedStyle)
    {
        var (shouldApply, style) = HelmExtensions.ShouldDoubleQuoteString(value);
 
        Assert.Equal(expectedShouldApply, shouldApply);
        Assert.Equal(expectedStyle, style);
    }
 
    [Theory]
    [InlineData("{{ if eq (.Values.parameters.myapp.flag | lower) \"true\" }}valA{{ else }}valB{{ end }}")]
    [InlineData("{{ if eq (.Values.parameters.myapp.enable_tls | lower) \"true\" }}{{ .Values.config.myapp.suffix }}{{ else }}fallback{{ end }}")]
    public void HelmFlowControlPattern_MatchesFlowControlExpressions(string value)
    {
        Assert.Matches(HelmExtensions.HelmFlowControlPattern(), value);
    }
 
    [Theory]
    [InlineData("{{ .Values.config.myapp.key }}")]
    [InlineData("plain text")]
    public void HelmFlowControlPattern_DoesNotMatchNonFlowControlExpressions(string value)
    {
        Assert.DoesNotMatch(HelmExtensions.HelmFlowControlPattern(), value);
    }
 
    [Theory]
    [InlineData("{{ .Values.config.myapp.port | int }}", "{{ .Values.config.myapp.port | int | toString }}")]
    [InlineData("{{ .Values.config.myapp.count | int64 }}", "{{ .Values.config.myapp.count | int64 | toString }}")]
    [InlineData("{{ .Values.config.myapp.rate | float64 }}", "{{ .Values.config.myapp.rate | float64 | toString }}")]
    [InlineData("{{ .Values.config.myapp.key }}", "{{ .Values.config.myapp.key }}")]
    public void EnsureStringOutput_PreservesNumericConversion(string value, string expected)
    {
        Assert.Equal(expected, HelmExtensions.EnsureStringOutput(value));
    }
}