Skip to content

Commit 38ca7c1

Browse files
Add support for MODULE LOADEX command (#2490)
* Added support for MODULE LOADEX command Co-authored-by: Anurag Bandyopadhyay <[email protected]>
1 parent d7c6c35 commit 38ca7c1

File tree

4 files changed

+85
-2
lines changed

4 files changed

+85
-2
lines changed

commands.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,8 @@ type Cmdable interface {
497497
GeoHash(ctx context.Context, key string, members ...string) *StringSliceCmd
498498

499499
ACLDryRun(ctx context.Context, username string, command ...interface{}) *StringCmd
500+
501+
ModuleLoadex(ctx context.Context, conf *ModuleLoadexConfig) *StringCmd
500502
}
501503

502504
type StatefulCmdable interface {
@@ -3888,3 +3890,32 @@ func (c cmdable) ACLDryRun(ctx context.Context, username string, command ...inte
38883890
_ = c(ctx, cmd)
38893891
return cmd
38903892
}
3893+
3894+
// ModuleLoadexConfig struct is used to specify the arguments for the MODULE LOADEX command of redis.
3895+
// `MODULE LOADEX path [CONFIG name value [CONFIG name value ...]] [ARGS args [args ...]]`
3896+
type ModuleLoadexConfig struct {
3897+
Path string
3898+
Conf map[string]interface{}
3899+
Args []interface{}
3900+
}
3901+
3902+
func (c *ModuleLoadexConfig) toArgs() []interface{} {
3903+
args := make([]interface{}, 3, 3+len(c.Conf)*3+len(c.Args)*2)
3904+
args[0] = "MODULE"
3905+
args[1] = "LOADEX"
3906+
args[2] = c.Path
3907+
for k, v := range c.Conf {
3908+
args = append(args, "CONFIG", k, v)
3909+
}
3910+
for _, arg := range c.Args {
3911+
args = append(args, "ARGS", arg)
3912+
}
3913+
return args
3914+
}
3915+
3916+
// ModuleLoadex Redis `MODULE LOADEX path [CONFIG name value [CONFIG name value ...]] [ARGS args [args ...]]` command.
3917+
func (c cmdable) ModuleLoadex(ctx context.Context, conf *ModuleLoadexConfig) *StringCmd {
3918+
cmd := NewStringCmd(ctx, conf.toArgs()...)
3919+
_ = c(ctx, cmd)
3920+
return cmd
3921+
}

commands_test.go

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1926,11 +1926,59 @@ var _ = Describe("Commands", func() {
19261926
Expect(replace.Val()).To(Equal(int64(1)))
19271927
})
19281928

1929-
It("should acl dryryn", func() {
1929+
It("should acl dryrun", func() {
19301930
dryRun := client.ACLDryRun(ctx, "default", "get", "randomKey")
19311931
Expect(dryRun.Err()).NotTo(HaveOccurred())
19321932
Expect(dryRun.Val()).To(Equal("OK"))
19331933
})
1934+
1935+
It("should fail module loadex", func() {
1936+
dryRun := client.ModuleLoadex(ctx, &redis.ModuleLoadexConfig{
1937+
Path: "/path/to/non-existent-library.so",
1938+
Conf: map[string]interface{}{
1939+
"param1": "value1",
1940+
},
1941+
Args: []interface{}{
1942+
"arg1",
1943+
},
1944+
})
1945+
Expect(dryRun.Err()).To(HaveOccurred())
1946+
Expect(dryRun.Err().Error()).To(Equal("ERR Error loading the extension. Please check the server logs."))
1947+
})
1948+
1949+
It("converts the module loadex configuration to a slice of arguments correctly", func() {
1950+
conf := &redis.ModuleLoadexConfig{
1951+
Path: "/path/to/your/module.so",
1952+
Conf: map[string]interface{}{
1953+
"param1": "value1",
1954+
},
1955+
Args: []interface{}{
1956+
"arg1",
1957+
"arg2",
1958+
3,
1959+
},
1960+
}
1961+
1962+
args := conf.ToArgs()
1963+
1964+
// Test if the arguments are in the correct order
1965+
expectedArgs := []interface{}{
1966+
"MODULE",
1967+
"LOADEX",
1968+
"/path/to/your/module.so",
1969+
"CONFIG",
1970+
"param1",
1971+
"value1",
1972+
"ARGS",
1973+
"arg1",
1974+
"ARGS",
1975+
"arg2",
1976+
"ARGS",
1977+
3,
1978+
}
1979+
1980+
Expect(args).To(Equal(expectedArgs))
1981+
})
19341982
})
19351983

19361984
Describe("hashes", func() {

export_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,7 @@ func (c *Ring) ShardByName(name string) *ringShard {
9898
shard, _ := c.sharding.GetByName(name)
9999
return shard
100100
}
101+
102+
func (c *ModuleLoadexConfig) ToArgs() []interface{} {
103+
return c.toArgs()
104+
}

main_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ func startRedis(port string, args ...string) (*redisProcess, error) {
314314
return nil, err
315315
}
316316

317-
baseArgs := []string{filepath.Join(dir, "redis.conf"), "--port", port, "--dir", dir}
317+
baseArgs := []string{filepath.Join(dir, "redis.conf"), "--port", port, "--dir", dir, "--enable-module-command", "yes"}
318318
process, err := execCmd(redisServerBin, append(baseArgs, args...)...)
319319
if err != nil {
320320
return nil, err

0 commit comments

Comments
 (0)