Skip to content

Commit 7a793ec

Browse files
Adrian Fernandez De La Torreadri1197
authored andcommitted
Add test and fuzz test
Signed-off-by: Adrian Fernandez De La Torre <adri1197@gmail.com>
1 parent 5f46461 commit 7a793ec

3 files changed

Lines changed: 153 additions & 0 deletions

File tree

internal/notifier/otel.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
Copyright 2025 The Flux authors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
117
package notifier
218

319
import (
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
Copyright 2025 The Flux authors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package notifier
18+
19+
import (
20+
"context"
21+
"crypto/tls"
22+
"fmt"
23+
"io"
24+
"net/http"
25+
"net/http/httptest"
26+
"testing"
27+
28+
fuzz "github.com/AdaLogics/go-fuzz-headers"
29+
eventv1 "github.com/fluxcd/pkg/apis/event/v1beta1"
30+
)
31+
32+
func Fuzz_Otel(f *testing.F) {
33+
f.Add("", "error", "", "commit-hash-1", []byte{}, []byte{})
34+
f.Add("", "info", "", "commit-hash-1", []byte{}, []byte{})
35+
36+
f.Fuzz(func(t *testing.T,
37+
urlSuffix, severity, message string, commitHash string, seed, response []byte) {
38+
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
39+
w.Write(response)
40+
io.Copy(io.Discard, r.Body)
41+
r.Body.Close()
42+
}))
43+
defer ts.Close()
44+
45+
var tlsConfig tls.Config
46+
_ = fuzz.NewConsumer(seed).GenerateStruct(&tlsConfig)
47+
48+
otel, err := NewOTELTraceNotifier(fmt.Sprintf("%s/%s", ts.URL, urlSuffix), "", nil, &tlsConfig)
49+
if err != nil {
50+
return
51+
}
52+
53+
event := eventv1.Event{}
54+
_ = fuzz.NewConsumer(seed).GenerateStruct(&event)
55+
56+
if event.Metadata == nil {
57+
event.Metadata = map[string]string{}
58+
}
59+
60+
event.Severity = severity
61+
event.Message = message
62+
event.Metadata["revision"] = commitHash
63+
64+
ctx := context.WithValue(context.TODO(), "alertUID", "test-alert-uid")
65+
ctx = context.WithValue(ctx, "alertName", "test-alert")
66+
ctx = context.WithValue(ctx, "alertNamespace", "test-namespace")
67+
68+
_ = otel.Post(context.TODO(), event)
69+
})
70+
}

internal/notifier/otel_test.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
Copyright 2025 The Flux authors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package notifier
18+
19+
import (
20+
"context"
21+
"net/http"
22+
"net/http/httptest"
23+
"testing"
24+
25+
"github.com/fluxcd/pkg/apis/event/v1beta1"
26+
"github.com/stretchr/testify/require"
27+
)
28+
29+
func TestOTEL_Post(t *testing.T) {
30+
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
31+
w.WriteHeader(http.StatusOK)
32+
33+
}))
34+
defer ts.Close()
35+
36+
tests := []struct {
37+
name string
38+
event func() v1beta1.Event
39+
}{
40+
{
41+
name: "test event",
42+
event: testEvent,
43+
},
44+
{
45+
name: "test event with empty metadata",
46+
event: func() v1beta1.Event {
47+
events := testEvent()
48+
events.Metadata = nil
49+
return events
50+
},
51+
},
52+
}
53+
54+
for _, tt := range tests {
55+
t.Run(tt.name, func(t *testing.T) {
56+
ctx := context.WithValue(context.TODO(), "alertUID", "test-alert-uid")
57+
ctx = context.WithValue(ctx, "alertName", "test-alert")
58+
ctx = context.WithValue(ctx, "alertNamespace", "test-namespace")
59+
60+
otelTrace, err := NewOTELTraceNotifier(ts.URL, "", nil, nil)
61+
require.NoError(t, err)
62+
63+
err = otelTrace.Post(ctx, tt.event())
64+
require.NoError(t, err)
65+
})
66+
}
67+
}

0 commit comments

Comments
 (0)