-
-
Notifications
You must be signed in to change notification settings - Fork 193
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
kadm: add func to decode AuthorizedOperations
The authorized operations is an int32 bitfield that represents ACL operations. This commit adds a func that allows to decode the bitfield into a slice of kmsg.ACLOperation values. This bitfield is used in the metadata, describe clusters and describe groups API responses.
- Loading branch information
Showing
2 changed files
with
162 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
package kadm | ||
|
||
import ( | ||
"math" | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/twmb/franz-go/pkg/kmsg" | ||
) | ||
|
||
func TestDecodeACLOperations(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
bitfield int32 | ||
expected []kmsg.ACLOperation | ||
}{ | ||
{ | ||
name: "Example 264", | ||
bitfield: 264, | ||
expected: []kmsg.ACLOperation{ | ||
kmsg.ACLOperationRead, | ||
kmsg.ACLOperationDescribe, | ||
}, | ||
}, | ||
{ | ||
name: "Example 3400", | ||
bitfield: 3400, | ||
expected: []kmsg.ACLOperation{ | ||
kmsg.ACLOperationRead, | ||
kmsg.ACLOperationDescribe, | ||
kmsg.ACLOperationAlterConfigs, | ||
kmsg.ACLOperationDescribeConfigs, | ||
kmsg.ACLOperationDelete, | ||
}, | ||
}, | ||
{ | ||
name: "Example 3968", | ||
bitfield: 3968, | ||
expected: []kmsg.ACLOperation{ | ||
kmsg.ACLOperationAlter, | ||
kmsg.ACLOperationAlterConfigs, | ||
kmsg.ACLOperationClusterAction, | ||
kmsg.ACLOperationDescribe, | ||
kmsg.ACLOperationDescribeConfigs, | ||
}, | ||
}, | ||
{ | ||
name: "Example 4000", | ||
bitfield: 4000, | ||
expected: []kmsg.ACLOperation{ | ||
kmsg.ACLOperationAlter, | ||
kmsg.ACLOperationAlterConfigs, | ||
kmsg.ACLOperationClusterAction, | ||
kmsg.ACLOperationCreate, | ||
kmsg.ACLOperationDescribe, | ||
kmsg.ACLOperationDescribeConfigs, | ||
}, | ||
}, | ||
{ | ||
name: "All Operations", | ||
bitfield: math.MaxInt32, // All bits set | ||
expected: []kmsg.ACLOperation{ | ||
kmsg.ACLOperationRead, | ||
kmsg.ACLOperationWrite, | ||
kmsg.ACLOperationCreate, | ||
kmsg.ACLOperationDelete, | ||
kmsg.ACLOperationAlter, | ||
kmsg.ACLOperationDescribe, | ||
kmsg.ACLOperationClusterAction, | ||
kmsg.ACLOperationDescribeConfigs, | ||
kmsg.ACLOperationAlterConfigs, | ||
kmsg.ACLOperationIdempotentWrite, | ||
kmsg.ACLOperationCreateTokens, | ||
kmsg.ACLOperationDescribeTokens, | ||
}, | ||
}, | ||
{ | ||
name: "Invalid Operations Excluded", | ||
bitfield: 1<<15 | 1<<16, // Bits beyond known operations | ||
expected: []kmsg.ACLOperation{}, | ||
}, | ||
{ | ||
name: "Empty Bitfield", | ||
bitfield: math.MinInt32, | ||
expected: []kmsg.ACLOperation{}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
result := DecodeACLOperations(tt.bitfield) | ||
|
||
// Compare slices ignoring order | ||
expectedMap := make(map[kmsg.ACLOperation]bool) | ||
for _, op := range tt.expected { | ||
expectedMap[op] = true | ||
} | ||
|
||
resultMap := make(map[kmsg.ACLOperation]bool) | ||
for _, op := range result { | ||
resultMap[op] = true | ||
} | ||
|
||
if !reflect.DeepEqual(expectedMap, resultMap) { | ||
t.Errorf("DecodeACLOperations(%d) = %v, expected %v", tt.bitfield, result, tt.expected) | ||
} | ||
}) | ||
} | ||
} |