Skip to content

Commit

Permalink
Add service test for v2 endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
JiriPapousek committed Jul 24, 2024
1 parent 73ea18e commit 7a09b1f
Showing 1 changed file with 58 additions and 1 deletion.
59 changes: 58 additions & 1 deletion internal/service/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
"github.com/stretchr/testify/assert"
)

func TestService(t *testing.T) {
func TestServiceV1(t *testing.T) {
type testCase struct {
name string
mockData []byte
Expand Down Expand Up @@ -91,3 +91,60 @@ func TestService(t *testing.T) {
})
}
}

func TestServiceV2(t *testing.T) {
type testCase struct {
name string
mockData []byte
expectedAnError bool
}

testCases := []testCase{
{
name: "valid remote configuration stored",
mockData: []byte(validRemoteConfigurationJSON),
expectedAnError: false,
},
{
name: "invalid remote configuration stored",
mockData: []byte("not a remote configuration"),
expectedAnError: true,
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
store := mockStorage{
remoteConfig: tc.mockData,
}
repo := service.NewRepository(&store)
svc := service.New(repo)

// Create the request:
req, err := http.NewRequest("GET", fmt.Sprintf("%s/v2/some_version/gathering_rules", service.APIPrefix), http.NoBody)
if err != nil {
t.Fatal(err)
}

rr := httptest.NewRecorder() // Used to record the response.
handler := service.NewHandler(svc)

router := mux.NewRouter()

handler.Register(router)

router.ServeHTTP(rr, req)

if tc.expectedAnError {
assert.Equal(t, http.StatusInternalServerError, rr.Code)
assert.Contains(t, rr.Body.String(), "error")
} else {
assert.Equal(t, http.StatusOK, rr.Code)
assert.Contains(
t,
rr.Body.String(),
`{"conditional_gathering_rules":[{"conditions":["condition 1","condition 2"],"gathering_functions":"the gathering functions"}],"container_logs":[{"namespace":"namespace-1","pod_name_regex":"test regex","previous":true,"messages":["first message","second message"]}],"version":"0.0.1"}`)
}
})
}
}

0 comments on commit 7a09b1f

Please sign in to comment.