Skip to content

Commit b39252c

Browse files
hellspawn679mehul gautamyurishkuro
authored
Increase codecov of pkg/kafka (jaegertracing#5682)
## Which problem is this PR solving? - jaegertracing#5068 ## Description of the changes - added test case for ` pkg/kafka/auth/config.go` and updated `internal/tracegen/worker_test.go` ## How was this change tested? - ## Checklist - [ ] I have read https://github.com/jaegertracing/jaeger/blob/master/CONTRIBUTING_GUIDELINES.md - [ ] I have signed all commits - [ ] I have added unit tests for the new functionality - [ ] I have run lint and test steps successfully - for `jaeger`: `make lint test` - for `jaeger-ui`: `yarn lint` and `yarn test` --------- Signed-off-by: mehul gautam <[email protected]> Signed-off-by: mehul gautam <[email protected]> Co-authored-by: mehul gautam <[email protected]> Co-authored-by: Yuri Shkuro <[email protected]>
1 parent 42bffdf commit b39252c

File tree

7 files changed

+450
-23
lines changed

7 files changed

+450
-23
lines changed

internal/tracegen/worker_test.go

+40-23
Original file line numberDiff line numberDiff line change
@@ -16,30 +16,47 @@ import (
1616
)
1717

1818
func Test_SimulateTraces(t *testing.T) {
19-
logger, buf := testutils.NewLogger()
20-
tp := sdktrace.NewTracerProvider()
21-
tracers := []trace.Tracer{tp.Tracer("stdout")}
22-
wg := sync.WaitGroup{}
23-
wg.Add(1)
24-
var running uint32 = 1
25-
26-
worker := &worker{
27-
logger: logger,
28-
tracers: tracers,
29-
wg: &wg,
30-
id: 7,
31-
running: &running,
32-
Config: Config{
33-
Traces: 7,
34-
Duration: time.Second,
35-
Pause: time.Second,
36-
Service: "stdout",
37-
Debug: true,
38-
Firehose: true,
19+
tests := []struct {
20+
name string
21+
pause time.Duration
22+
}{
23+
{
24+
name: "no pause",
25+
pause: 0,
26+
},
27+
{
28+
name: "with pause",
29+
pause: time.Second,
3930
},
4031
}
41-
expectedOutput := `{"level":"info","msg":"Worker 7 generated 7 traces"}` + "\n"
4232

43-
worker.simulateTraces()
44-
assert.Equal(t, expectedOutput, buf.String())
33+
for _, tt := range tests {
34+
t.Run(tt.name, func(t *testing.T) {
35+
logger, buf := testutils.NewLogger()
36+
tp := sdktrace.NewTracerProvider()
37+
tracers := []trace.Tracer{tp.Tracer("stdout")}
38+
wg := sync.WaitGroup{}
39+
wg.Add(1)
40+
var running uint32 = 1
41+
worker := &worker{
42+
logger: logger,
43+
tracers: tracers,
44+
wg: &wg,
45+
id: 7,
46+
running: &running,
47+
Config: Config{
48+
Traces: 7,
49+
Duration: time.Second,
50+
Pause: tt.pause,
51+
Service: "stdout",
52+
Debug: true,
53+
Firehose: true,
54+
ChildSpans: 1,
55+
},
56+
}
57+
expectedOutput := `{"level":"info","msg":"Worker 7 generated 7 traces"}` + "\n"
58+
worker.simulateTraces()
59+
assert.Equal(t, expectedOutput, buf.String())
60+
})
61+
}
4562
}

pkg/kafka/auth/config_test.go

+174
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
// Copyright (c) 2024 The Jaeger Authors.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package auth
5+
6+
import (
7+
"flag"
8+
"testing"
9+
10+
"github.com/Shopify/sarama"
11+
"github.com/spf13/viper"
12+
"github.com/stretchr/testify/assert"
13+
"github.com/stretchr/testify/require"
14+
"go.uber.org/zap"
15+
"go.uber.org/zap/zaptest"
16+
17+
"github.com/jaegertracing/jaeger/pkg/config"
18+
"github.com/jaegertracing/jaeger/pkg/config/tlscfg"
19+
)
20+
21+
func addFlags(flags *flag.FlagSet) {
22+
configPrefix := "kafka.auth"
23+
AddFlags(configPrefix, flags)
24+
}
25+
26+
func Test_InitFromViper(t *testing.T) {
27+
configPrefix := "kafka.auth"
28+
v, command := config.Viperize(addFlags)
29+
command.ParseFlags([]string{
30+
"--kafka.auth.authentication=tls",
31+
"--kafka.auth.kerberos.service-name=kafka",
32+
"--kafka.auth.kerberos.realm=EXAMPLE.COM",
33+
"--kafka.auth.kerberos.use-keytab=true",
34+
"--kafka.auth.kerberos.username=user",
35+
"--kafka.auth.kerberos.password=password",
36+
"--kafka.auth.kerberos.config-file=/path/to/krb5.conf",
37+
"--kafka.auth.kerberos.keytab-file=/path/to/keytab",
38+
"--kafka.auth.kerberos.disable-fast-negotiation=true",
39+
"--kafka.auth.tls.enabled=false",
40+
"--kafka.auth.plaintext.username=user",
41+
"--kafka.auth.plaintext.password=password",
42+
"--kafka.auth.plaintext.mechanism=SCRAM-SHA-256",
43+
"--kafka.auth.tls.ca=failing",
44+
})
45+
46+
authConfig := &AuthenticationConfig{}
47+
err := authConfig.InitFromViper(configPrefix, v)
48+
require.EqualError(t, err, "failed to process Kafka TLS options: kafka.auth.tls.* options cannot be used when kafka.auth.tls.enabled is false")
49+
50+
command.ParseFlags([]string{"--kafka.auth.tls.ca="})
51+
v.BindPFlags(command.Flags())
52+
err = authConfig.InitFromViper(configPrefix, v)
53+
require.NoError(t, err)
54+
55+
expectedConfig := &AuthenticationConfig{
56+
Authentication: "tls",
57+
Kerberos: KerberosConfig{
58+
ServiceName: "kafka",
59+
Realm: "EXAMPLE.COM",
60+
UseKeyTab: true,
61+
Username: "user",
62+
Password: "password",
63+
ConfigPath: "/path/to/krb5.conf",
64+
KeyTabPath: "/path/to/keytab",
65+
DisablePAFXFast: true,
66+
},
67+
TLS: tlscfg.Options{
68+
Enabled: true,
69+
},
70+
PlainText: PlainTextConfig{
71+
Username: "user",
72+
Password: "password",
73+
Mechanism: "SCRAM-SHA-256",
74+
},
75+
}
76+
assert.Equal(t, expectedConfig, authConfig)
77+
}
78+
79+
// Test plaintext with different mechanisms
80+
func testPlaintext(v *viper.Viper, t *testing.T, configPrefix string, logger *zap.Logger, mechanism string, saramaConfig *sarama.Config) {
81+
v.Set(configPrefix+plainTextPrefix+suffixPlainTextMechanism, mechanism)
82+
authConfig := &AuthenticationConfig{}
83+
err := authConfig.InitFromViper(configPrefix, v)
84+
require.NoError(t, err)
85+
require.NoError(t, authConfig.SetConfiguration(saramaConfig, logger))
86+
}
87+
88+
func TestSetConfiguration(t *testing.T) {
89+
logger := zaptest.NewLogger(t)
90+
saramaConfig := sarama.NewConfig()
91+
configPrefix := "kafka.auth"
92+
v, command := config.Viperize(addFlags)
93+
94+
// Table-driven test cases
95+
tests := []struct {
96+
name string
97+
authType string
98+
expectedError string
99+
plainTextMechanisms []string
100+
}{
101+
{
102+
name: "Invalid authentication method",
103+
authType: "fail",
104+
expectedError: "Unknown/Unsupported authentication method fail to kafka cluster",
105+
},
106+
{
107+
name: "Kerberos authentication",
108+
authType: "kerberos",
109+
expectedError: "",
110+
},
111+
{
112+
name: "Plaintext authentication with SCRAM-SHA-256",
113+
authType: "plaintext",
114+
expectedError: "",
115+
plainTextMechanisms: []string{"SCRAM-SHA-256"},
116+
},
117+
{
118+
name: "Plaintext authentication with SCRAM-SHA-512",
119+
authType: "plaintext",
120+
expectedError: "",
121+
plainTextMechanisms: []string{"SCRAM-SHA-512"},
122+
},
123+
{
124+
name: "Plaintext authentication with PLAIN",
125+
authType: "plaintext",
126+
expectedError: "",
127+
plainTextMechanisms: []string{"PLAIN"},
128+
},
129+
{
130+
name: "No authentication",
131+
authType: " ",
132+
expectedError: "",
133+
},
134+
{
135+
name: "TLS authentication",
136+
authType: "tls",
137+
expectedError: "",
138+
},
139+
{
140+
name: "TLS authentication with invalid cipher suite",
141+
authType: "tls",
142+
expectedError: "error loading tls config: failed to get cipher suite ids from cipher suite names: cipher suite fail not supported or doesn't exist",
143+
},
144+
}
145+
146+
for _, tt := range tests {
147+
t.Run(tt.name, func(t *testing.T) {
148+
command.ParseFlags([]string{
149+
"--kafka.auth.authentication=" + tt.authType,
150+
})
151+
authConfig := &AuthenticationConfig{}
152+
defer authConfig.TLS.Close()
153+
err := authConfig.InitFromViper(configPrefix, v)
154+
require.NoError(t, err)
155+
156+
if tt.authType == "tls" && tt.expectedError != "" {
157+
authConfig.TLS.CipherSuites = []string{"fail"}
158+
}
159+
160+
if len(tt.plainTextMechanisms) > 0 {
161+
for _, mechanism := range tt.plainTextMechanisms {
162+
testPlaintext(v, t, configPrefix, logger, mechanism, saramaConfig)
163+
}
164+
} else {
165+
err = authConfig.SetConfiguration(saramaConfig, logger)
166+
if tt.expectedError != "" {
167+
require.EqualError(t, err, tt.expectedError)
168+
} else {
169+
require.NoError(t, err)
170+
}
171+
}
172+
})
173+
}
174+
}

pkg/kafka/auth/kerberos_test.go

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// Copyright (c) 2024 The Jaeger Authors.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package auth
5+
6+
import (
7+
"testing"
8+
9+
"github.com/Shopify/sarama"
10+
"github.com/stretchr/testify/assert"
11+
)
12+
13+
func TestSetKerberosConfiguration(t *testing.T) {
14+
tests := []struct {
15+
name string
16+
config KerberosConfig
17+
}{
18+
{
19+
name: "With KeyTab",
20+
config: KerberosConfig{
21+
ServiceName: "service",
22+
Realm: "realm",
23+
UseKeyTab: true,
24+
Username: "username",
25+
Password: "password",
26+
ConfigPath: "/path/to/config",
27+
KeyTabPath: "/path/to/keytab",
28+
DisablePAFXFast: true,
29+
},
30+
},
31+
{
32+
name: "Without KeyTab",
33+
config: KerberosConfig{
34+
ServiceName: "service",
35+
Realm: "realm",
36+
UseKeyTab: false,
37+
Username: "username",
38+
Password: "password",
39+
ConfigPath: "/path/to/config",
40+
DisablePAFXFast: false,
41+
},
42+
},
43+
}
44+
45+
for _, tt := range tests {
46+
t.Run(tt.name, func(t *testing.T) {
47+
saramaConfig := sarama.NewConfig()
48+
49+
setKerberosConfiguration(&tt.config, saramaConfig)
50+
51+
assert.Equal(t, sarama.SASLMechanism("GSSAPI"), saramaConfig.Net.SASL.Mechanism)
52+
assert.True(t, saramaConfig.Net.SASL.Enable)
53+
assert.Equal(t, tt.config.Username, saramaConfig.Net.SASL.GSSAPI.Username)
54+
assert.Equal(t, tt.config.Realm, saramaConfig.Net.SASL.GSSAPI.Realm)
55+
assert.Equal(t, tt.config.ServiceName, saramaConfig.Net.SASL.GSSAPI.ServiceName)
56+
assert.Equal(t, tt.config.DisablePAFXFast, saramaConfig.Net.SASL.GSSAPI.DisablePAFXFAST)
57+
assert.Equal(t, tt.config.ConfigPath, saramaConfig.Net.SASL.GSSAPI.KerberosConfigPath)
58+
59+
if tt.config.UseKeyTab {
60+
assert.Equal(t, tt.config.KeyTabPath, saramaConfig.Net.SASL.GSSAPI.KeyTabPath)
61+
assert.Equal(t, sarama.KRB5_KEYTAB_AUTH, saramaConfig.Net.SASL.GSSAPI.AuthType)
62+
} else {
63+
assert.Equal(t, tt.config.Password, saramaConfig.Net.SASL.GSSAPI.Password)
64+
assert.Equal(t, sarama.KRB5_USER_AUTH, saramaConfig.Net.SASL.GSSAPI.AuthType)
65+
}
66+
})
67+
}
68+
}

0 commit comments

Comments
 (0)