MCPcopy
hub / github.com/segmentio/kafka-go / TestAwsMskIamMechanism

Function TestAwsMskIamMechanism

sasl/aws_msk_iam_v2/msk_iam_test.go:26–109  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

24var signTime = time.Date(2021, 10, 14, 13, 5, 0, 0, time.UTC)
25
26func TestAwsMskIamMechanism(t *testing.T) {
27 creds := credentials.NewStaticCredentialsProvider(accessKeyId, secretAccessKey, "")
28 ctxWithMetadata := func() context.Context {
29 return sasl.WithMetadata(context.Background(), &sasl.Metadata{
30 Host: "localhost",
31 Port: 9092,
32 })
33 }
34
35 tests := []struct {
36 description string
37 ctx func() context.Context
38 shouldFail bool
39 }{
40 {
41 description: "with metadata",
42 ctx: ctxWithMetadata,
43 },
44 {
45 description: "without metadata",
46 ctx: func() context.Context {
47 return context.Background()
48 },
49 shouldFail: true,
50 },
51 }
52
53 for _, tt := range tests {
54 t.Run(tt.description, func(t *testing.T) {
55 ctx := tt.ctx()
56
57 mskMechanism := &Mechanism{
58 Signer: signer.NewSigner(),
59 Credentials: creds,
60 Region: "us-east-1",
61 SignTime: signTime,
62 }
63 sess, auth, err := mskMechanism.Start(ctx)
64 if tt.shouldFail { // if error is expected
65 if err == nil { // but we don't find one
66 t.Fatal("error expected")
67 } else { // but we do find one
68 return // return early since the remaining assertions are irrelevant
69 }
70 } else { // if error is not expected (typical)
71 if err != nil { // but we do find one
72 t.Fatal(err)
73 }
74 }
75
76 if sess != mskMechanism {
77 t.Error(
78 "Unexpected session",
79 "expected", mskMechanism,
80 "got", sess,
81 )
82 }
83

Callers

nothing calls this directly

Calls 4

StartMethod · 0.95
WithMetadataFunction · 0.92
MarshalMethod · 0.80
ErrorMethod · 0.45

Tested by

no test coverage detected