Skip to content

Commit 21addda

Browse files
committed
fix: module would not warn on redis 9. Extracted out method and unit tested it
1 parent a14286c commit 21addda

File tree

2 files changed

+86
-7
lines changed

2 files changed

+86
-7
lines changed

provider/pro/resource_rediscloud_pro_database.go

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1048,20 +1048,32 @@ func containsDBModule(modules []map[string]interface{}, moduleName string) bool
10481048
return false
10491049
}
10501050

1051+
// shouldWarnRedis8Modules checks if a warning should be issued for modules in Redis 8.0 or higher
1052+
func shouldWarnRedis8Modules(version string, hasModules bool) bool {
1053+
if !hasModules {
1054+
return false
1055+
}
1056+
// Extract major version (first character before the dot)
1057+
if len(version) > 0 {
1058+
majorVersionStr := strings.Split(version, ".")[0]
1059+
if majorVersion, err := strconv.Atoi(majorVersionStr); err == nil {
1060+
return majorVersion >= 8
1061+
}
1062+
}
1063+
return false
1064+
}
1065+
10511066
func validateModulesForRedis8() schema.CustomizeDiffFunc {
10521067
return func(ctx context.Context, diff *schema.ResourceDiff, meta interface{}) error {
10531068
redisVersion, versionExists := diff.GetOk("redis_version")
10541069
modules, modulesExists := diff.GetOkExists("modules")
10551070

10561071
if versionExists && modulesExists {
10571072
version := redisVersion.(string)
1058-
// Check if version is >= 8.0
1059-
if strings.HasPrefix(version, "8.") {
1060-
moduleSet := modules.(*schema.Set)
1061-
if moduleSet.Len() > 0 {
1062-
log.Printf("[WARN] Modules are bundled by default in Redis %s. You should remove the modules block as it is deprecated for this version.", version)
1063-
return nil
1064-
}
1073+
moduleSet := modules.(*schema.Set)
1074+
1075+
if shouldWarnRedis8Modules(version, moduleSet.Len() > 0) {
1076+
log.Printf("[WARN] Modules are bundled by default in Redis %s. You should remove the modules block as it is deprecated for this version.", version)
10651077
}
10661078
}
10671079
return nil
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package pro
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
// TestUnitShouldWarnRedis8Modules_Redis8WithModules tests that warning is triggered for Redis 8.0 with modules
10+
func TestUnitShouldWarnRedis8Modules_Redis8WithModules(t *testing.T) {
11+
result := shouldWarnRedis8Modules("8.0", true)
12+
assert.True(t, result, "should warn for Redis 8.0 with modules")
13+
}
14+
15+
// TestUnitShouldWarnRedis8Modules_Redis80WithModules tests that warning is triggered for Redis 8.0.0 with modules
16+
func TestUnitShouldWarnRedis8Modules_Redis80WithModules(t *testing.T) {
17+
result := shouldWarnRedis8Modules("8.0.0", true)
18+
assert.True(t, result, "should warn for Redis 8.0.0 with modules")
19+
}
20+
21+
// TestUnitShouldWarnRedis8Modules_Redis81WithModules tests that warning is triggered for Redis 8.1+ with modules
22+
func TestUnitShouldWarnRedis8Modules_Redis81WithModules(t *testing.T) {
23+
result := shouldWarnRedis8Modules("8.1.0", true)
24+
assert.True(t, result, "should warn for Redis 8.1.0 with modules")
25+
}
26+
27+
// TestUnitShouldWarnRedis8Modules_Redis89WithModules tests that warning is triggered for Redis 8.9+ with modules
28+
func TestUnitShouldWarnRedis8Modules_Redis89WithModules(t *testing.T) {
29+
result := shouldWarnRedis8Modules("8.9.9", true)
30+
assert.True(t, result, "should warn for Redis 8.9.9 with modules")
31+
}
32+
33+
// TestUnitShouldWarnRedis8Modules_Redis7WithModules tests that no warning for Redis 7.x with modules
34+
func TestUnitShouldWarnRedis8Modules_Redis7WithModules(t *testing.T) {
35+
result := shouldWarnRedis8Modules("7.4", true)
36+
assert.False(t, result, "should not warn for Redis 7.4 with modules")
37+
}
38+
39+
// TestUnitShouldWarnRedis8Modules_Redis6WithModules tests that no warning for Redis 6.x with modules
40+
func TestUnitShouldWarnRedis8Modules_Redis6WithModules(t *testing.T) {
41+
result := shouldWarnRedis8Modules("6.2", true)
42+
assert.False(t, result, "should not warn for Redis 6.2 with modules")
43+
}
44+
45+
// TestUnitShouldWarnRedis8Modules_Redis8NoModules tests that no warning for Redis 8.0 without modules
46+
func TestUnitShouldWarnRedis8Modules_Redis8NoModules(t *testing.T) {
47+
result := shouldWarnRedis8Modules("8.0", false)
48+
assert.False(t, result, "should not warn for Redis 8.0 without modules")
49+
}
50+
51+
// TestUnitShouldWarnRedis8Modules_Redis7NoModules tests that no warning for Redis 7.x without modules
52+
func TestUnitShouldWarnRedis8Modules_Redis7NoModules(t *testing.T) {
53+
result := shouldWarnRedis8Modules("7.4", false)
54+
assert.False(t, result, "should not warn for Redis 7.4 without modules")
55+
}
56+
57+
// TestUnitShouldWarnRedis8Modules_Redis9WithModules tests that warning is triggered for Redis 9.x with modules (future-proofing)
58+
func TestUnitShouldWarnRedis8Modules_Redis9WithModules(t *testing.T) {
59+
result := shouldWarnRedis8Modules("9.0", true)
60+
assert.True(t, result, "should warn for Redis 9.0 with modules (modules bundled in 8.0+)")
61+
}
62+
63+
// TestUnitShouldWarnRedis8Modules_Redis10WithModules tests that warning is triggered for Redis 10.x with modules
64+
func TestUnitShouldWarnRedis8Modules_Redis10WithModules(t *testing.T) {
65+
result := shouldWarnRedis8Modules("10.0.0", true)
66+
assert.True(t, result, "should warn for Redis 10.0.0 with modules (modules bundled in 8.0+)")
67+
}

0 commit comments

Comments
 (0)