Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
oliviagolden0 committed May 16, 2024
1 parent baa135e commit 8337f7a
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 49 deletions.
10 changes: 8 additions & 2 deletions rules/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package rules
import (
"fmt"
"os"
"regexp"
"strings"
"time"

Expand Down Expand Up @@ -69,7 +70,7 @@ type V3Engine interface {
AddRule(rule DynamicRule,
lockPattern string,
callback V3RuleTaskCallback,
options ...RuleOption)
options ...RuleOption) error
AddPolling(namespacePattern string,
preconditions DynamicRule,
ttl int,
Expand Down Expand Up @@ -169,8 +170,13 @@ func (e *v3Engine) SetWatcherWrapper(watcherWrapper WrapWatcher) {
func (e *v3Engine) AddRule(rule DynamicRule,
lockPattern string,
callback V3RuleTaskCallback,
options ...RuleOption) {
options ...RuleOption) error {
validPath := regexp.MustCompile(`^[[:alnum:] \:\/\"\'\_\.\,\*\=\-]*$`)
if !validPath.MatchString(lockPattern) {
return fmt.Errorf("Path contains an invalid character")
}
e.addRuleWithIface(rule, lockPattern, callback, options...)
return nil
}

func (e *baseEngine) Stop() {
Expand Down
13 changes: 9 additions & 4 deletions rules/engine_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package rules

import (
"fmt"
"testing"
"time"

Expand Down Expand Up @@ -30,14 +31,18 @@ func TestV3EngineConstructor(t *testing.T) {
eng := NewV3Engine(cfg, getTestLogger())
value := "val"
rule, _ := NewEqualsLiteralRule("/key", &value)
eng.AddRule(rule, "/lock", v3DummyCallback, RuleID("test"))
assert.PanicsWithValue(t, "Rule ID option missing", func() { eng.AddRule(rule, "/lock", v3DummyCallback) })
err := eng.AddPolling("/polling", rule, 30, v3DummyCallback)
err := eng.AddRule(rule, "/lock?@", v3DummyCallback, RuleID("test"))
assert.Equal(t, err, fmt.Errorf("Path contains an invalid character"))
err = eng.AddRule(rule, "/lock", v3DummyCallback, RuleID("test"))
assert.NoError(t, err)
assert.PanicsWithValue(t, "Rule ID option missing", func() { assert.NoError(t, eng.AddRule(rule, "/lock", v3DummyCallback)) })
err = eng.AddPolling("/polling", rule, 30, v3DummyCallback)
assert.NoError(t, err)
assertEngineRunStop(t, eng)

eng = NewV3Engine(cfg, getTestLogger(), KeyExpansion(map[string][]string{"a:": {"b"}}))
eng.AddRule(rule, "/lock", v3DummyCallback, RuleLockTimeout(30), RuleID("test"))
err = eng.AddRule(rule, "/lock", v3DummyCallback, RuleLockTimeout(30), RuleID("test"))
assert.NoError(t, err)
err = eng.AddPolling("/polling", rule, 30, v3DummyCallback)
assert.NoError(t, err)
err = eng.AddPolling("/polling[", rule, 30, v3DummyCallback)
Expand Down
6 changes: 0 additions & 6 deletions rules/lock/lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package lock

import (
"errors"
"fmt"
"regexp"
"time"

"golang.org/x/net/context"
Expand Down Expand Up @@ -56,10 +54,6 @@ type v3Locker struct {
}

func (v3l *v3Locker) Lock(key string, options ...Option) (RuleLock, error) {
validPath := regexp.MustCompile(`^[[:alnum:] \/\"\'\_\.\,\*\=\-]+$`)
if !validPath.MatchString(key) {
return nil, fmt.Errorf("Path variable contains an invalid character")
}
return v3l.lockWithTimeout(key, v3l.lockTimeout)
}
func (v3l *v3Locker) lockWithTimeout(key string, timeout int) (RuleLock, error) {
Expand Down
37 changes: 0 additions & 37 deletions rules/lock/lock_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package lock

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -59,39 +58,3 @@ func Test_V3Locker(t *testing.T) {
})
}
}

func Test_V3LockerRegex(t *testing.T) {
cfg, cl := teststore.InitV3Etcd(t)
_, err := v3.New(cfg)
require.NoError(t, err)
newSession := func(_ context.Context) (*v3c.Session, error) {
return v3c.NewSession(cl, v3c.WithTTL(30))
}

testcases := []struct {
name string
lockKey string
err error
}{
{
name: "bad regex",
lockKey: "/test?/",
err: fmt.Errorf("Path variable contains an invalid character"),
},
{
name: "good regex",
lockKey: "/test/",
},
}

for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
rlckr := v3Locker{
newSession: newSession,
lockTimeout: 5,
}
_, err := rlckr.Lock(tc.lockKey)
assert.Equal(t, err, tc.err)
})
}
}

0 comments on commit 8337f7a

Please sign in to comment.