@@ -2,9 +2,7 @@ package template
22
33import (
44 "encoding/base64"
5- "fmt"
65 "testing"
7- "time"
86
97 "github.com/replicatedhq/embedded-cluster/api/types"
108 ecv1beta1 "github.com/replicatedhq/embedded-cluster/kinds/apis/v1beta1"
@@ -993,147 +991,6 @@ func TestEngine_RecordDependency(t *testing.T) {
993991 assert .ElementsMatch (t , []string {"dependency1" , "dependency2" }, engine .depsTree ["item1" ]) // item1 unchanged
994992}
995993
996- func TestEngine_ConfigMode_TLSGeneration (t * testing.T ) {
997- // Helper function to create config values for a hostname
998- configValuesFor := func (hostname string ) types.AppConfigValues {
999- return types.AppConfigValues {
1000- "ingress_hostname" : {Value : hostname },
1001- }
1002- }
1003-
1004- // Helper function to time an execution
1005- timeExecution := func (name string , fn func () (string , error )) (time.Duration , string ) {
1006- start := time .Now ()
1007- result , err := fn ()
1008- duration := time .Since (start )
1009- require .NoError (t , err , "execution %s failed" , name )
1010- return duration , result
1011- }
1012-
1013- // Create config with TLS certificate generation templates
1014- config := & kotsv1beta1.Config {
1015- TypeMeta : metav1.TypeMeta {
1016- APIVersion : "kots.io/v1beta1" ,
1017- Kind : "Config" ,
1018- },
1019- Spec : kotsv1beta1.ConfigSpec {
1020- Groups : []kotsv1beta1.ConfigGroup {
1021- {
1022- Name : "tls_settings" ,
1023- Title : "TLS Configuration" ,
1024- Items : []kotsv1beta1.ConfigItem {
1025- {
1026- Name : "ingress_hostname" ,
1027- Title : "Ingress Hostname" ,
1028- HelpText : "Enter a DNS hostname to use as the cert's CN." ,
1029- Type : "text" ,
1030- },
1031- {
1032- Name : "tls_json" ,
1033- Title : "TLS JSON" ,
1034- Type : "textarea" ,
1035- Hidden : true ,
1036- Default : multitype .FromString (`repl{{ $ca := genCA (ConfigOption "ingress_hostname") 365 }}
1037- repl{{ $tls := dict "ca" $ca }}
1038- repl{{ $cert := genSignedCert (ConfigOption "ingress_hostname") (list ) (list (ConfigOption "ingress_hostname")) 365 $ca }}
1039- repl{{ $_ := set $tls "cert" $cert }}
1040- repl{{ toJson $tls }}` ),
1041- },
1042- {
1043- Name : "tls_ca" ,
1044- Title : "Signing Authority" ,
1045- Type : "textarea" ,
1046- Default : multitype .FromString (`repl{{ fromJson (ConfigOption "tls_json") | dig "ca" "Cert" "" }}` ),
1047- },
1048- {
1049- Name : "tls_cert" ,
1050- Title : "TLS Cert" ,
1051- Type : "textarea" ,
1052- Default : multitype .FromString (`repl{{ fromJson (ConfigOption "tls_json") | dig "cert" "Cert" "" }}` ),
1053- },
1054- {
1055- Name : "tls_key" ,
1056- Title : "TLS Key" ,
1057- Type : "textarea" ,
1058- Default : multitype .FromString (`repl{{ fromJson (ConfigOption "tls_json") | dig "cert" "Key" "" }}` ),
1059- },
1060- },
1061- },
1062- },
1063- },
1064- }
1065-
1066- engine := NewEngine (config , WithMode (ModeConfig ))
1067-
1068- // Test 1: First execution with hostname - should be slow (certificate generation)
1069- firstHostname := "example.com"
1070- firstDuration , firstResult := timeExecution ("first" , func () (string , error ) {
1071- return engine .Execute (configValuesFor (firstHostname ))
1072- })
1073-
1074- // Verify basic YAML structure
1075- assert .Contains (t , firstResult , "apiVersion: kots.io/v1beta1" )
1076- assert .Contains (t , firstResult , "kind: Config" )
1077-
1078- // Verify TLS config items are present
1079- expectedTLSItems := []string {"tls_json" , "tls_ca" , "tls_cert" , "tls_key" }
1080- for _ , item := range expectedTLSItems {
1081- assert .Contains (t , firstResult , fmt .Sprintf ("name: %s" , item ))
1082- }
1083-
1084- // Test 2: First cached execution - should be fast
1085- firstCachedDuration , firstCachedResult := timeExecution ("first cached" , func () (string , error ) {
1086- return engine .Execute (configValuesFor (firstHostname ))
1087- })
1088-
1089- // Verify performance characteristics: non-cached should be in ms, cached much faster
1090- assert .Greater (t , firstDuration , time .Millisecond * 50 , "First execution should take at least 50ms (cert generation)" )
1091- assert .Less (t , firstCachedDuration , time .Millisecond * 20 , "First cached execution should be under 20ms" )
1092-
1093- // Verify caching provides significant speedup
1094- assert .True (t , firstCachedDuration < firstDuration / 5 ,
1095- "Cached execution should be at least 5x faster. First: %v, Cached: %v" ,
1096- firstDuration , firstCachedDuration )
1097-
1098- // Verify cached result is identical to first execution
1099- assert .Equal (t , firstResult , firstCachedResult , "Cached execution should return identical result" )
1100-
1101- // Test 3: Second execution with different hostname - should be slow again (new certificate generation)
1102- secondHostname := "test.example.com"
1103- secondDuration , secondResult := timeExecution ("second" , func () (string , error ) {
1104- return engine .Execute (configValuesFor (secondHostname ))
1105- })
1106-
1107- // Verify different certificates are generated for different hostnames
1108- assert .NotEqual (t , firstResult , secondResult , "Different hostnames should generate different certificates" )
1109-
1110- // Test 4: Second cached execution - should be fast again
1111- secondCachedDuration , secondCachedResult := timeExecution ("second cached" , func () (string , error ) {
1112- return engine .Execute (configValuesFor (secondHostname ))
1113- })
1114-
1115- // Verify performance characteristics for second hostname
1116- assert .Greater (t , secondDuration , time .Millisecond * 50 , "Second execution should take at least 50ms (cert generation)" )
1117- assert .Less (t , secondCachedDuration , time .Millisecond * 20 , "Second cached execution should be under 20ms" )
1118-
1119- // Verify caching provides significant speedup
1120- assert .True (t , secondCachedDuration < secondDuration / 5 ,
1121- "Cached execution should be at least 5x faster. Second: %v, Cached: %v" ,
1122- secondDuration , secondCachedDuration )
1123-
1124- // Verify second cached result is identical to second execution
1125- assert .Equal (t , secondResult , secondCachedResult , "Second cached execution should return identical result" )
1126-
1127- // Log performance metrics
1128- t .Logf ("TLS Generation Cache Performance:" )
1129- t .Logf (" First execution (%s): %.1fms" , firstHostname , float64 (firstDuration )/ float64 (time .Millisecond ))
1130- t .Logf (" First cached execution (%s): %.1fµs (%.1fx speedup)" , firstHostname , float64 (firstCachedDuration )/ float64 (time .Microsecond ),
1131- float64 (firstDuration )/ float64 (firstCachedDuration ))
1132- t .Logf (" Second execution (%s): %.1fms" , secondHostname , float64 (secondDuration )/ float64 (time .Millisecond ))
1133- t .Logf (" Second cached execution (%s): %.1fµs (%.1fx speedup)" , secondHostname , float64 (secondCachedDuration )/ float64 (time .Microsecond ),
1134- float64 (secondDuration )/ float64 (secondCachedDuration ))
1135- }
1136-
1137994func TestEngine_ConfigMode_BasicTemplating (t * testing.T ) {
1138995 config := & kotsv1beta1.Config {
1139996 TypeMeta : metav1.TypeMeta {
0 commit comments