|
| 1 | +package modular |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + "regexp" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/GoCodeAlone/modular/feeders" |
| 11 | +) |
| 12 | + |
| 13 | +// TestTenantConfigAffixedEnvBug tests the specific bug where tenant config loading |
| 14 | +// fails with "env: prefix or suffix cannot be empty" when using TenantAffixedEnvFeeder |
| 15 | +func TestTenantConfigAffixedEnvBug(t *testing.T) { |
| 16 | + // Create temp directory for tenant configs |
| 17 | + tempDir, err := os.MkdirTemp("", "tenant-affixed-env-bug-test") |
| 18 | + if err != nil { |
| 19 | + t.Fatalf("Failed to create temp directory: %v", err) |
| 20 | + } |
| 21 | + defer func() { |
| 22 | + if err := os.RemoveAll(tempDir); err != nil { |
| 23 | + t.Logf("Failed to remove temp directory: %v", err) |
| 24 | + } |
| 25 | + }() |
| 26 | + |
| 27 | + // Create tenant config files |
| 28 | + createTenantConfigFiles(t, tempDir) |
| 29 | + |
| 30 | + // Create application with required services |
| 31 | + app, tenantService := setupAppWithTenantServices(t) |
| 32 | + |
| 33 | + // Create loader with TenantAffixedEnvFeeder (reproduces the bug) |
| 34 | + loader := NewFileBasedTenantConfigLoader(TenantConfigParams{ |
| 35 | + ConfigNameRegex: regexp.MustCompile(`^\w+\.yaml$`), |
| 36 | + ConfigDir: tempDir, |
| 37 | + ConfigFeeders: []Feeder{ |
| 38 | + feeders.NewTenantAffixedEnvFeeder(func(tenantId string) string { |
| 39 | + return fmt.Sprintf("%s_", tenantId) |
| 40 | + }, func(s string) string { return "" }), |
| 41 | + }, |
| 42 | + }) |
| 43 | + |
| 44 | + // This should NOT fail with "env: prefix or suffix cannot be empty" |
| 45 | + err = loader.LoadTenantConfigurations(app, tenantService) |
| 46 | + if err != nil { |
| 47 | + t.Fatalf("Expected tenant config loading to succeed, but got error: %v", err) |
| 48 | + } |
| 49 | + |
| 50 | + // Verify tenants were loaded |
| 51 | + tenants := tenantService.GetTenants() |
| 52 | + if len(tenants) == 0 { |
| 53 | + t.Error("Expected at least one tenant to be loaded") |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +// TestTenantConfigAffixedEnvBugReproduction confirms the bug has been fixed |
| 58 | +func TestTenantConfigAffixedEnvBugReproduction(t *testing.T) { |
| 59 | + // Create temp directory for tenant configs |
| 60 | + tempDir, err := os.MkdirTemp("", "tenant-affixed-env-reproduction-test") |
| 61 | + if err != nil { |
| 62 | + t.Fatalf("Failed to create temp directory: %v", err) |
| 63 | + } |
| 64 | + defer func() { |
| 65 | + if err := os.RemoveAll(tempDir); err != nil { |
| 66 | + t.Logf("Failed to remove temp directory: %v", err) |
| 67 | + } |
| 68 | + }() |
| 69 | + |
| 70 | + // Create tenant config files |
| 71 | + createTenantConfigFiles(t, tempDir) |
| 72 | + |
| 73 | + // Create application with required services |
| 74 | + app, tenantService := setupAppWithTenantServices(t) |
| 75 | + |
| 76 | + // Create loader with TenantAffixedEnvFeeder |
| 77 | + loader := NewFileBasedTenantConfigLoader(TenantConfigParams{ |
| 78 | + ConfigNameRegex: regexp.MustCompile(`^\w+\.yaml$`), |
| 79 | + ConfigDir: tempDir, |
| 80 | + ConfigFeeders: []Feeder{ |
| 81 | + feeders.NewTenantAffixedEnvFeeder(func(tenantId string) string { |
| 82 | + return fmt.Sprintf("%s_", tenantId) |
| 83 | + }, func(s string) string { return "" }), |
| 84 | + }, |
| 85 | + }) |
| 86 | + |
| 87 | + // Try to load tenant configurations - this should now succeed |
| 88 | + err = loader.LoadTenantConfigurations(app, tenantService) |
| 89 | + |
| 90 | + if err != nil { |
| 91 | + t.Errorf("Expected tenant config loading to succeed after fix, but got error: %v", err) |
| 92 | + } else { |
| 93 | + t.Log("Bug has been fixed - tenant config loading succeeded") |
| 94 | + |
| 95 | + // Verify tenants were loaded |
| 96 | + tenants := tenantService.GetTenants() |
| 97 | + if len(tenants) == 0 { |
| 98 | + t.Error("Expected at least one tenant to be loaded") |
| 99 | + } else { |
| 100 | + t.Logf("Successfully loaded %d tenants", len(tenants)) |
| 101 | + } |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +// createTenantConfigFiles creates sample tenant config files |
| 106 | +func createTenantConfigFiles(t *testing.T, tempDir string) { |
| 107 | + ctlConfig := ` |
| 108 | +content: |
| 109 | + defaultTemplate: ctl-branded |
| 110 | + cacheTTL: 600 |
| 111 | +
|
| 112 | +notifications: |
| 113 | + provider: email |
| 114 | + fromAddress: support@ctl.example.com |
| 115 | + maxRetries: 5 |
| 116 | +` |
| 117 | + |
| 118 | + sampleAff1Config := ` |
| 119 | +content: |
| 120 | + defaultTemplate: sampleaff1-branded |
| 121 | + cacheTTL: 300 |
| 122 | +
|
| 123 | +notifications: |
| 124 | + provider: sms |
| 125 | + fromAddress: support@sampleaff1.example.com |
| 126 | + maxRetries: 3 |
| 127 | +` |
| 128 | + |
| 129 | + // Write config files |
| 130 | + err := os.WriteFile(filepath.Join(tempDir, "ctl.yaml"), []byte(ctlConfig), 0600) |
| 131 | + if err != nil { |
| 132 | + t.Fatalf("Failed to write ctl.yaml: %v", err) |
| 133 | + } |
| 134 | + |
| 135 | + err = os.WriteFile(filepath.Join(tempDir, "sampleaff1.yaml"), []byte(sampleAff1Config), 0600) |
| 136 | + if err != nil { |
| 137 | + t.Fatalf("Failed to write sampleaff1.yaml: %v", err) |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +// setupAppWithTenantServices creates app with required config sections and services |
| 142 | +func setupAppWithTenantServices(t *testing.T) (Application, TenantService) { |
| 143 | + // Content config |
| 144 | + type ContentConfig struct { |
| 145 | + DefaultTemplate string `yaml:"defaultTemplate"` |
| 146 | + CacheTTL int `yaml:"cacheTTL"` |
| 147 | + } |
| 148 | + |
| 149 | + // Notifications config |
| 150 | + type NotificationsConfig struct { |
| 151 | + Provider string `yaml:"provider"` |
| 152 | + FromAddress string `yaml:"fromAddress"` |
| 153 | + MaxRetries int `yaml:"maxRetries"` |
| 154 | + } |
| 155 | + |
| 156 | + log := &logger{t} |
| 157 | + app := NewStdApplication(NewStdConfigProvider(nil), log) |
| 158 | + |
| 159 | + // Register config sections that match the tenant YAML files |
| 160 | + app.RegisterConfigSection("content", NewStdConfigProvider(&ContentConfig{})) |
| 161 | + app.RegisterConfigSection("notifications", NewStdConfigProvider(&NotificationsConfig{})) |
| 162 | + |
| 163 | + // Create tenant service |
| 164 | + tenantService := NewStandardTenantService(log) |
| 165 | + |
| 166 | + return app, tenantService |
| 167 | +} |
| 168 | + |
| 169 | +// TestTenantAffixedEnvFeederDirectUsage tests the TenantAffixedEnvFeeder directly |
| 170 | +func TestTenantAffixedEnvFeederDirectUsage(t *testing.T) { |
| 171 | + type TestConfig struct { |
| 172 | + Name string `env:"NAME"` |
| 173 | + Port int `env:"PORT"` |
| 174 | + } |
| 175 | + |
| 176 | + config := &TestConfig{} |
| 177 | + |
| 178 | + // Create a TenantAffixedEnvFeeder |
| 179 | + feeder := feeders.NewTenantAffixedEnvFeeder(func(tenantId string) string { |
| 180 | + return fmt.Sprintf("%s_", tenantId) |
| 181 | + }, func(s string) string { return "" }) |
| 182 | + |
| 183 | + // With the fix, Feed should now work (it will look for unprefixed env vars when no tenant is set) |
| 184 | + err := feeder.Feed(config) |
| 185 | + if err != nil { |
| 186 | + t.Errorf("Expected no error when calling Feed directly, but got: %v", err) |
| 187 | + } else { |
| 188 | + t.Log("Direct Feed call succeeded - fix is working") |
| 189 | + } |
| 190 | + |
| 191 | + // Test FeedKey with a tenant ID |
| 192 | + config2 := &TestConfig{} |
| 193 | + err = feeder.FeedKey("ctl", config2) |
| 194 | + if err != nil { |
| 195 | + t.Errorf("Expected no error when calling FeedKey with tenant ID, but got: %v", err) |
| 196 | + } else { |
| 197 | + t.Log("FeedKey call with tenant ID succeeded") |
| 198 | + } |
| 199 | +} |
0 commit comments