Skip to content

Commit 10e79fa

Browse files
committed
More tests
1 parent 286cbac commit 10e79fa

File tree

6 files changed

+361
-32
lines changed

6 files changed

+361
-32
lines changed

Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ clean:
2121
test:
2222
go test -v -cover
2323

24+
coverage:
25+
go test -coverprofile=coverage.out
26+
go tool cover -func=coverage.out
27+
go tool cover -html=coverage.out
28+
2429
build: ${KAFKA_OPS}
2530

2631
${KAFKA_OPS}:

README.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,9 @@ kafka-cluster-example2.json:
112112
{
113113
"resource": {
114114
"type": "cluster",
115-
"pattern": "kafka-cluster",
116-
"patternType": "LITERAL"
117115
},
118116
"allow_operations": [
119-
"ALL:*"
117+
"IDEMPOTENT_WRITE:*"
120118
]
121119
}
122120
]

acl_types.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package main
22

33
import (
4-
"github.com/Shopify/sarama"
4+
"github.com/Shopify/sarama"
55

6-
"strings"
6+
"strings"
77
)
88

99
func aclOperationToString(operation sarama.AclOperation) string {

acl_types_test.go

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
package main
2+
3+
import (
4+
"github.com/Shopify/sarama"
5+
6+
"testing"
7+
)
8+
9+
var aclOperationToStringTests = []struct {
10+
in int
11+
out string
12+
}{
13+
{0, "INVALID"},
14+
{1, "ANY"},
15+
{2, "ALL"},
16+
{3, "READ"},
17+
{4, "WRITE"},
18+
{5, "CREATE"},
19+
{6, "DELETE"},
20+
{7, "ALTER"},
21+
{8, "DESCRIBE"},
22+
{9, "CLUSTER_ACTION"},
23+
{10, "DESCRIBE_CONFIGS"},
24+
{11, "ALTER_CONFIGS"},
25+
{12, "IDEMPOTENT_WRITE"},
26+
}
27+
28+
func TestAclOperationToString(t *testing.T) {
29+
for _, tt := range aclOperationToStringTests {
30+
val := aclOperationToString(sarama.AclOperation(tt.in))
31+
if val != tt.out {
32+
t.Errorf("aclOperationToString failed, expected %s, got %s", tt.out, val)
33+
}
34+
}
35+
}
36+
37+
var aclOperationFromStringTests = []struct {
38+
in string
39+
out int
40+
}{
41+
{"ANY", 1},
42+
{"ALL", 2},
43+
{"READ", 3},
44+
{"WRITE", 4},
45+
{"CREATE", 5},
46+
{"DELETE", 6},
47+
{"ALTER", 7},
48+
{"DESCRIBE", 8},
49+
{"CLUSTER_ACTION", 9},
50+
{"DESCRIBE_CONFIGS", 10},
51+
{"ALTER_CONFIGS", 11},
52+
{"IDEMPOTENT_WRITE", 12},
53+
}
54+
55+
func TestAclOperationFromString(t *testing.T) {
56+
for _, tt := range aclOperationFromStringTests {
57+
val := int(aclOperationFromString(tt.in))
58+
if val != tt.out {
59+
t.Errorf("aclOperationToString failed, expected %v, got %v", tt.out, val)
60+
}
61+
}
62+
}
63+
64+
var aclResourcePatternTypeToStringTests = []struct {
65+
in int
66+
out string
67+
}{
68+
{0, "INVALID"},
69+
{1, "ANY"},
70+
{2, "MATCH"},
71+
{3, "LITERAL"},
72+
{4, "PREFIXED"},
73+
}
74+
75+
func TestAclResourcePatternTypeToString(t *testing.T) {
76+
for _, tt := range aclResourcePatternTypeToStringTests {
77+
val := aclResourcePatternTypeToString(sarama.AclResourcePatternType(tt.in))
78+
if val != tt.out {
79+
t.Errorf("aclResourcePatternTypeToString failed, expected %s, got %s", tt.out, val)
80+
}
81+
}
82+
}
83+
84+
var aclResourcePatternTypeFromStringTests = []struct {
85+
in string
86+
out int
87+
}{
88+
{"ANY", 1},
89+
{"MATCH", 2},
90+
{"LITERAL", 3},
91+
{"PREFIXED", 4},
92+
}
93+
94+
func TestAclResourcePatternTypeFromString(t *testing.T) {
95+
for _, tt := range aclResourcePatternTypeFromStringTests {
96+
val := int(aclResourcePatternTypeFromString(tt.in))
97+
if val != tt.out {
98+
t.Errorf("aclResourcePatternTypeFromString failed, expected %v, got %v", tt.out, val)
99+
}
100+
}
101+
}
102+
103+
var aclResourceTypeToStringTests = []struct {
104+
in int
105+
out string
106+
}{
107+
{0, "INVALID"},
108+
{1, "any"},
109+
{2, "topic"},
110+
{3, "group"},
111+
{4, "cluster"},
112+
{5, "transactional-id"},
113+
}
114+
115+
func TestAclResourceTypeToString(t *testing.T) {
116+
for _, tt := range aclResourceTypeToStringTests {
117+
val := aclResourceTypeToString(sarama.AclResourceType(tt.in))
118+
if val != tt.out {
119+
t.Errorf("aclResourceTypeToString failed, expected %s, got %s", tt.out, val)
120+
}
121+
}
122+
}
123+
124+
var aclResourceTypeFromStringTests = []struct {
125+
in string
126+
out int
127+
}{
128+
{"any", 1},
129+
{"topic", 2},
130+
{"group", 3},
131+
{"cluster", 4},
132+
{"transactional-id", 5},
133+
}
134+
135+
func TestAclResourceTypeFromString(t *testing.T) {
136+
for _, tt := range aclResourceTypeFromStringTests {
137+
val := int(aclResourceTypeFromString(tt.in))
138+
if val != tt.out {
139+
t.Errorf("aclResourceTypeFromString failed, expected %v, got %v", tt.out, val)
140+
}
141+
}
142+
}
143+
144+
var aclPermissionTypeToStringTests = []struct {
145+
in int
146+
out string
147+
}{
148+
{0, "INVALID"},
149+
{1, "ANY"},
150+
{2, "DENY"},
151+
{3, "ALLOW"},
152+
}
153+
154+
func TestAclPermissionTypeToString(t *testing.T) {
155+
for _, tt := range aclPermissionTypeToStringTests {
156+
val := aclPermissionTypeToString(sarama.AclPermissionType(tt.in))
157+
if val != tt.out {
158+
t.Errorf("aclPermissionTypeToString failed, expected %s, got %s", tt.out, val)
159+
}
160+
}
161+
}
162+
163+
var aclPermissionTypeFromStringTests = []struct {
164+
in string
165+
out int
166+
}{
167+
{"ANY", 1},
168+
{"DENY", 2},
169+
{"ALLOW", 3},
170+
}
171+
172+
func TestAclPermissionTypeFromString(t *testing.T) {
173+
for _, tt := range aclPermissionTypeFromStringTests {
174+
val := int(aclPermissionTypeFromString(tt.in))
175+
if val != tt.out {
176+
t.Errorf("aclPermissionTypeFromString failed, expected %v, got %v", tt.out, val)
177+
}
178+
}
179+
}

main.go

Lines changed: 38 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -523,13 +523,27 @@ func alignAcl(admin *sarama.ClusterAdmin, acls *[]sarama.ResourceAcls, acl Singl
523523
} else {
524524
action = "Remove"
525525
}
526+
527+
if acl.Resource.Type == "cluster" {
528+
if acl.Resource.Pattern == "" {
529+
acl.Resource.Pattern = "kafka-cluster"
530+
}
531+
}
532+
if acl.Resource.PatternType == "" {
533+
acl.Resource.PatternType = "LITERAL"
534+
}
535+
526536
fmt.Printf("TASK [ACL : %s ACL (%s %s@%s to %s %s:%s:%s)] %s\n", action, acl.PermissionType, acl.Principal,
527537
acl.Host, acl.Operation, acl.Resource.Type, acl.Resource.PatternType, acl.Resource.Pattern, strings.Repeat("*", 25))
528538

529539
if acl.Principal == "" {
530540
return Error, errors.New("Principal not defined")
531541
}
532542

543+
if aclResourceTypeFromString(acl.Resource.Type) == sarama.AclResourceUnknown {
544+
return Error, errors.New("Wrong resource type: " + acl.Resource.Type)
545+
}
546+
533547
if acl.State == "absent" {
534548
// Won't check the presence. We'll just try do delete and see the length of MatchingAcl in response
535549
filter := sarama.AclFilter{
@@ -561,6 +575,7 @@ func alignAcl(admin *sarama.ClusterAdmin, acls *[]sarama.ResourceAcls, acl Singl
561575
} else {
562576
r := sarama.Resource{
563577
ResourceType: aclResourceTypeFromString(acl.Resource.Type),
578+
ResourceName: acl.Resource.Pattern,
564579
ResourcePatternType: aclResourcePatternTypeFromString(acl.Resource.PatternType),
565580
}
566581
a := sarama.Acl{
@@ -740,29 +755,29 @@ func (f *arrFlags) String() string {
740755
}
741756

742757
func validateFlags() {
743-
flag.StringVar(&broker, "broker", "", "Bootstrap-brokers, default is localhost:9092 (can be also set by Env variable KAFKA_BROKER)")
744-
flag.StringVar(&specfile, "spec", "", "Spec-file (can be set by Env variable KAFKA_SPEC_FILE)")
745-
flag.StringVar(&protocol, "protocol", "plaintext", "Security protocol. Available options: plaintext, sasl_ssl, sasl_plaintext (default: plaintext)")
746-
flag.StringVar(&mechanism, "mechanism", "scram-sha-256", "SASL mechanism. Available options: scram-sha-256, scram-sha-512 (default: scram-sha-256)")
747-
flag.StringVar(&username, "username", "", "Username for authentication (can be also set by Env variable KAFKA_USERNAME")
748-
flag.StringVar(&password, "password", "", "Password for authentication (can be also set by Env variable KAFKA_PASSWORD")
749-
flag.BoolVar(&actionApply, "apply", false, "Apply spec-file to the broker, create all entities that do not exist there; this is the default action")
750-
flag.BoolVar(&actionDump, "dump", false, "Dump broker entities in YAML (default) or JSON format to stdout or to a file if --spec option is defined")
751-
flag.BoolVar(&actionHelp, "help", false, "Print usage")
752-
flag.BoolVar(&isYAML, "yaml", false, "Spec-file is in YAML format (will try to detect format if none of --yaml or --json is set)")
753-
flag.BoolVar(&isJSON, "json", false, "Spec-file is in JSON format (will try to detect format if none of --yaml or --json is set)")
754-
flag.BoolVar(&errorStop, "stop-on-error", false, "Exit on first occurred error")
755-
flag.BoolVar(&isTemplate, "template", false, "Spec-file is a template")
756-
flag.BoolVar(&missingOk, "missingok", false, "Ignore missing template keys")
757-
flag.BoolVar(&verbose, "verbose", false, "Verbose output")
758-
flag.Var(&varFlags, "var", "Variable for templating")
759-
flag.Usage = func() {
760-
usage()
761-
}
762-
flag.Parse()
763-
764-
protocol = strings.ToLower(protocol)
765-
mechanism = strings.ToLower(mechanism)
758+
flag.StringVar(&broker, "broker", "", "Bootstrap-brokers, default is localhost:9092 (can be also set by Env variable KAFKA_BROKER)")
759+
flag.StringVar(&specfile, "spec", "", "Spec-file (can be set by Env variable KAFKA_SPEC_FILE)")
760+
flag.StringVar(&protocol, "protocol", "plaintext", "Security protocol. Available options: plaintext, sasl_ssl, sasl_plaintext (default: plaintext)")
761+
flag.StringVar(&mechanism, "mechanism", "scram-sha-256", "SASL mechanism. Available options: scram-sha-256, scram-sha-512 (default: scram-sha-256)")
762+
flag.StringVar(&username, "username", "", "Username for authentication (can be also set by Env variable KAFKA_USERNAME")
763+
flag.StringVar(&password, "password", "", "Password for authentication (can be also set by Env variable KAFKA_PASSWORD")
764+
flag.BoolVar(&actionApply, "apply", false, "Apply spec-file to the broker, create all entities that do not exist there; this is the default action")
765+
flag.BoolVar(&actionDump, "dump", false, "Dump broker entities in YAML (default) or JSON format to stdout or to a file if --spec option is defined")
766+
flag.BoolVar(&actionHelp, "help", false, "Print usage")
767+
flag.BoolVar(&isYAML, "yaml", false, "Spec-file is in YAML format (will try to detect format if none of --yaml or --json is set)")
768+
flag.BoolVar(&isJSON, "json", false, "Spec-file is in JSON format (will try to detect format if none of --yaml or --json is set)")
769+
flag.BoolVar(&errorStop, "stop-on-error", false, "Exit on first occurred error")
770+
flag.BoolVar(&isTemplate, "template", false, "Spec-file is a template")
771+
flag.BoolVar(&missingOk, "missingok", false, "Ignore missing template keys")
772+
flag.BoolVar(&verbose, "verbose", false, "Verbose output")
773+
flag.Var(&varFlags, "var", "Variable for templating")
774+
flag.Usage = func() {
775+
usage()
776+
}
777+
flag.Parse()
778+
779+
protocol = strings.ToLower(protocol)
780+
mechanism = strings.ToLower(mechanism)
766781

767782
if !actionApply && !actionDump && !actionHelp {
768783
fmt.Println("Please define one of the actions: --dump, --apply, --help")

0 commit comments

Comments
 (0)