Skip to content

Commit

Permalink
Merge pull request #528 from nodece/fix_golangci_lint
Browse files Browse the repository at this point in the history
test: fix golangci-lint error
  • Loading branch information
hsluoyz committed Jul 16, 2020
2 parents a2768d0 + ecfadd4 commit aabe6ce
Show file tree
Hide file tree
Showing 19 changed files with 300 additions and 232 deletions.
4 changes: 2 additions & 2 deletions config/config_test.go
Expand Up @@ -60,13 +60,13 @@ func TestGet(t *testing.T) {
t.Fatalf("err: %v", err)
}

config.Set("other::key1", "new test key")
_ = config.Set("other::key1", "new test key")

if v := config.String("other::key1"); v != "new test key" {
t.Errorf("Get failure: expected different value for other::key1 (expected: [%#v] got: [%#v])", "new test key", v)
}

config.Set("other::key1", "test key")
_ = config.Set("other::key1", "test key")

if v := config.String("multi1::name"); v != "r.sub==p.sub && r.obj==p.obj" {
t.Errorf("Get failure: expected different value for multi1::name (expected: [%#v] got: [%#v])", "r.sub==p.sub&&r.obj==p.obj", v)
Expand Down
2 changes: 1 addition & 1 deletion enforcer.go
Expand Up @@ -218,7 +218,7 @@ func (e *Enforcer) SetAdapter(adapter persist.Adapter) {
// SetWatcher sets the current watcher.
func (e *Enforcer) SetWatcher(watcher persist.Watcher) error {
e.watcher = watcher
return watcher.SetUpdateCallback(func(string) { e.LoadPolicy() })
return watcher.SetUpdateCallback(func(string) { _ = e.LoadPolicy() })
}

// GetRoleManager gets the current role manager.
Expand Down
79 changes: 45 additions & 34 deletions enforcer_cached_b_test.go
Expand Up @@ -30,7 +30,7 @@ func BenchmarkCachedBasicModel(b *testing.B) {

b.ResetTimer()
for i := 0; i < b.N; i++ {
e.Enforce("alice", "data1", "read")
_, _ = e.Enforce("alice", "data1", "read")
}
}

Expand All @@ -39,80 +39,89 @@ func BenchmarkCachedRBACModel(b *testing.B) {

b.ResetTimer()
for i := 0; i < b.N; i++ {
e.Enforce("alice", "data2", "read")
_, _ = e.Enforce("alice", "data2", "read")
}
}

func BenchmarkCachedRBACModelSmall(b *testing.B) {
e, _ := NewCachedEnforcer("examples/rbac_model.conf", false)
// Do not rebuild the role inheritance relations for every AddGroupingPolicy() call.
e.EnableAutoBuildRoleLinks(false)
// 100 roles, 10 resources.
for i := 0; i < 100; i++ {
e.AddPolicy(fmt.Sprintf("group%d", i), fmt.Sprintf("data%d", i/10), "read")
_, err := e.AddPolicy(fmt.Sprintf("group%d", i), fmt.Sprintf("data%d", i/10), "read")
if err != nil {
b.Fatal(err)
}
}
// 1000 users.
for i := 0; i < 1000; i++ {
e.AddGroupingPolicy(fmt.Sprintf("user%d", i), fmt.Sprintf("group%d", i/10))
_, err := e.AddGroupingPolicy(fmt.Sprintf("user%d", i), fmt.Sprintf("group%d", i/10))
if err != nil {
b.Fatal(err)
}
}
e.BuildRoleLinks()

b.ResetTimer()
for i := 0; i < b.N; i++ {
e.Enforce("user501", "data9", "read")
_, _ = e.Enforce("user501", "data9", "read")
}
}

func BenchmarkCachedRBACModelMedium(b *testing.B) {
e, _ := NewCachedEnforcer("examples/rbac_model.conf", false)
// Do not rebuild the role inheritance relations for every AddGroupingPolicy() call.
e.EnableAutoBuildRoleLinks(false)
// 1000 roles, 100 resources.
pPolicies := make([][]string, 0)
for i := 0; i < 1000; i++ {
pPolicies = append(pPolicies, []string{fmt.Sprintf("group%d", i), fmt.Sprintf("data%d", i/10), "read"})
}
e.AddPolicies(pPolicies)

_, err := e.AddPolicies(pPolicies)
if err != nil {
b.Fatal(err)
}

// 10000 users.
gPolicies := make([][]string, 0)
for i := 0; i < 10000; i++ {
gPolicies = append(gPolicies, []string{fmt.Sprintf("user%d", i), fmt.Sprintf("group%d", i/10)})
}
e.AddGroupingPolicies(gPolicies)

e.BuildRoleLinks()
_, err = e.AddGroupingPolicies(gPolicies)
if err != nil {
b.Fatal(err)
}

b.ResetTimer()
for i := 0; i < b.N; i++ {
e.Enforce("user5001", "data150", "read")
_, _ = e.Enforce("user5001", "data150", "read")
}
}

func BenchmarkCachedRBACModelLarge(b *testing.B) {
e, _ := NewCachedEnforcer("examples/rbac_model.conf", false)
// Do not rebuild the role inheritance relations for every AddGroupingPolicy() call.
e.EnableAutoBuildRoleLinks(false)

// 10000 roles, 1000 resources.
pPolicies := make([][]string, 0)
for i := 0; i < 10000; i++ {
pPolicies = append(pPolicies, []string{fmt.Sprintf("group%d", i), fmt.Sprintf("data%d", i/10), "read"})
}
e.AddPolicies(pPolicies)
_, err := e.AddPolicies(pPolicies)
if err != nil {
b.Fatal(err)
}

// 100000 users.
gPolicies := make([][]string, 0)
for i := 0; i < 100000; i++ {
gPolicies = append(gPolicies, []string{fmt.Sprintf("user%d", i), fmt.Sprintf("group%d", i/10)})
}
e.AddGroupingPolicies(gPolicies)

e.BuildRoleLinks()
_, err = e.AddGroupingPolicies(gPolicies)
if err != nil {
b.Fatal(err)
}

b.ResetTimer()
for i := 0; i < b.N; i++ {
e.Enforce("user50001", "data1500", "read")
_, _ = e.Enforce("user50001", "data1500", "read")
}
}

Expand All @@ -121,7 +130,7 @@ func BenchmarkCachedRBACModelWithResourceRoles(b *testing.B) {

b.ResetTimer()
for i := 0; i < b.N; i++ {
e.Enforce("alice", "data1", "read")
_, _ = e.Enforce("alice", "data1", "read")
}
}

Expand All @@ -130,7 +139,7 @@ func BenchmarkCachedRBACModelWithDomains(b *testing.B) {

b.ResetTimer()
for i := 0; i < b.N; i++ {
e.Enforce("alice", "domain1", "data1", "read")
_, _ = e.Enforce("alice", "domain1", "data1", "read")
}
}

Expand All @@ -140,7 +149,7 @@ func BenchmarkCachedABACModel(b *testing.B) {

b.ResetTimer()
for i := 0; i < b.N; i++ {
e.Enforce("alice", data1, "read")
_, _ = e.Enforce("alice", data1, "read")
}
}

Expand All @@ -149,7 +158,7 @@ func BenchmarkCachedKeyMatchModel(b *testing.B) {

b.ResetTimer()
for i := 0; i < b.N; i++ {
e.Enforce("alice", "/alice_data/resource1", "GET")
_, _ = e.Enforce("alice", "/alice_data/resource1", "GET")
}
}

Expand All @@ -158,7 +167,7 @@ func BenchmarkCachedRBACModelWithDeny(b *testing.B) {

b.ResetTimer()
for i := 0; i < b.N; i++ {
e.Enforce("alice", "data1", "read")
_, _ = e.Enforce("alice", "data1", "read")
}
}

Expand All @@ -167,35 +176,37 @@ func BenchmarkCachedPriorityModel(b *testing.B) {

b.ResetTimer()
for i := 0; i < b.N; i++ {
e.Enforce("alice", "data1", "read")
_, _ = e.Enforce("alice", "data1", "read")
}
}

func BenchmarkCachedRBACModelMediumParallel(b *testing.B) {
e, _ := NewCachedEnforcer("examples/rbac_model.conf", false)
// Do not rebuild the role inheritance relations for every AddGroupingPolicy() call.
e.EnableAutoBuildRoleLinks(false)

// 10000 roles, 1000 resources.
pPolicies := make([][]string, 0)
for i := 0; i < 10000; i++ {
pPolicies = append(pPolicies, []string{fmt.Sprintf("group%d", i), fmt.Sprintf("data%d", i/10), "read"})
}
e.AddPolicies(pPolicies)
_, err := e.AddPolicies(pPolicies)
if err != nil {
b.Fatal(err)
}

// 100000 users.
gPolicies := make([][]string, 0)
for i := 0; i < 100000; i++ {
gPolicies = append(gPolicies, []string{fmt.Sprintf("user%d", i), fmt.Sprintf("group%d", i/10)})
}
e.AddGroupingPolicies(gPolicies)

e.BuildRoleLinks()
_, err = e.AddGroupingPolicies(gPolicies)
if err != nil {
b.Fatal(err)
}

b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
e.Enforce("user5001", "data150", "read")
_, _ = e.Enforce("user5001", "data150", "read")
}
})
}
2 changes: 1 addition & 1 deletion enforcer_cached_test.go
Expand Up @@ -34,7 +34,7 @@ func TestCache(t *testing.T) {

// The cache is enabled, so even if we remove a policy rule, the decision
// for ("alice", "data1", "read") will still be true, as it uses the cached result.
e.RemovePolicy("alice", "data1", "read")
_, _ = e.RemovePolicy("alice", "data1", "read")

testEnforceCache(t, e, "alice", "data1", "read", true)
testEnforceCache(t, e, "alice", "data1", "write", false)
Expand Down
2 changes: 1 addition & 1 deletion enforcer_synced.go
Expand Up @@ -92,7 +92,7 @@ func (e *SyncedEnforcer) StopAutoLoadPolicy() {
// SetWatcher sets the current watcher.
func (e *SyncedEnforcer) SetWatcher(watcher persist.Watcher) error {
e.watcher = watcher
return watcher.SetUpdateCallback(func(string) { e.LoadPolicy() })
return watcher.SetUpdateCallback(func(string) { _ = e.LoadPolicy() })
}

// ClearPolicy clears all policy.
Expand Down

0 comments on commit aabe6ce

Please sign in to comment.