Skip to content

Commit

Permalink
test(clusterconfig): add unit test for silenced_alerts.go and rename …
Browse files Browse the repository at this point in the history
…it to gather_silenced_alerts.go (#728)

* test(silenced_alerts): adding unit tests

* chore(clusterconfig): rename silenced_alerts.go to gather_silenced_alerts.go

* test(silenced_alerts): convert unit test to table-driven

* test(clusterconfig): review slienced_alerts tests

* test(clusterconfig): handle errs on gather_silenced_alerts_test.go
  • Loading branch information
Ricardo Lüders authored Mar 2, 2023
1 parent 6c444ef commit 6b00277
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 0 deletions.
98 changes: 98 additions & 0 deletions pkg/gatherers/clusterconfig/gather_silenced_alerts_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package clusterconfig

import (
"context"
"net/http"
"net/http/httptest"
"net/url"
"testing"

"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/stretchr/testify/assert"

"github.com/openshift/insights-operator/pkg/record"
"k8s.io/client-go/rest"

"github.com/openshift/insights-operator/pkg/utils/marshal"
)

type mockAlertsClient struct {
data []byte
}

func (c *mockAlertsClient) RestClient(t *testing.T) *rest.RESTClient {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if c.data == nil {
w.WriteHeader(http.StatusNotFound)
} else {
w.WriteHeader(http.StatusOK)
_, err := w.Write(c.data)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
}
}
}))

baseURL, err := url.Parse(ts.URL)
assert.NoError(t, err, "failed to parse server URL")
client, err := rest.NewRESTClient(baseURL, "", rest.ClientContentConfig{}, nil, nil)
assert.NoError(t, err, "failed to create the client")

return client
}

func TestGatherSilencedAlerts(t *testing.T) {
tests := []struct {
name string
mockAlertsClient *mockAlertsClient
wantRecords []record.Record
wantErrs []error
}{
{
name: "Get silenced alerts successfully",
mockAlertsClient: &mockAlertsClient{
data: []byte(`[{"status": {"state": "suppressed"}}]`),
},
wantRecords: []record.Record{
{
Name: "config/silenced_alerts.json",
Item: marshal.RawByte(`[{"status": {"state": "suppressed"}}]`),
},
},
wantErrs: nil,
},
{
name: "Get silenced alerts with error",
mockAlertsClient: &mockAlertsClient{data: nil},
wantRecords: nil,
wantErrs: []error{
&errors.StatusError{ErrStatus: metav1.Status{
Status: metav1.StatusFailure,
Message: "the server could not find the requested resource",
Reason: metav1.StatusReasonNotFound,
Details: &metav1.StatusDetails{
Causes: []metav1.StatusCause{
{
Type: metav1.CauseTypeUnexpectedServerResponse,
Message: "",
Field: "",
},
},
},
Code: http.StatusNotFound,
}},
},
},
}

ctx := context.Background()
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
records, errs := gatherSilencedAlerts(ctx, tt.mockAlertsClient.RestClient(t))
assert.Equal(t, tt.wantErrs, errs)
assert.Equal(t, tt.wantRecords, records)
})
}
}

0 comments on commit 6b00277

Please sign in to comment.