Skip to content

Commit

Permalink
Add test to verify functionality and fix things
Browse files Browse the repository at this point in the history
  • Loading branch information
jmeagher committed Oct 10, 2024
1 parent 464ca45 commit 03db92f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
6 changes: 4 additions & 2 deletions receiver/datadogreceiver/receiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"fmt"
"io"
"net/http"
"strings"

"github.com/DataDog/agent-payload/v5/gogen"
pb "github.com/DataDog/datadog-agent/pkg/proto/pbgo/trace"
Expand Down Expand Up @@ -243,8 +244,9 @@ func (ddr *datadogReceiver) handleTraces(w http.ResponseWriter, req *http.Reques
}
}

switch version := req.Header.Get(header.TracerVersion); version {
case "0.1", "0.2", "0.3":
urlSplit := strings.Split(req.RequestURI, "/")
switch version := urlSplit[1]; version {
case "v0.1", "v0.2", "v0.3":
_, _ = w.Write([]byte("OK"))
default:
_, _ = w.Write([]byte("{}"))
Expand Down
23 changes: 20 additions & 3 deletions receiver/datadogreceiver/receiver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,32 +74,49 @@ func TestDatadogServer(t *testing.T) {
})

for _, tc := range []struct {
name string
op io.Reader
name string
op io.Reader
endpoint string

expectCode int
expectContent string
}{
{
name: "invalid data",
op: strings.NewReader("{"),
endpoint: "http://%s/v0.7/traces",
expectCode: http.StatusBadRequest,
expectContent: "Unable to unmarshal reqs\n",
},
{
name: "Fake featuresdiscovery",
op: nil, // Content-length: 0.
endpoint: "http://%s/v0.7/traces",
expectCode: http.StatusBadRequest,
expectContent: "Fake featuresdiscovery\n",
},
{
name: "Older version returns OK",
op: strings.NewReader("[]"),
endpoint: "http://%s/v0.3/traces",
expectCode: http.StatusOK,
expectContent: "OK",
},
{
name: "Older version returns JSON",
op: strings.NewReader("[]"),
endpoint: "http://%s/v0.4/traces",
expectCode: http.StatusOK,
expectContent: "{}",
},
} {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()

req, err := http.NewRequest(
http.MethodPost,
fmt.Sprintf("http://%s/v0.7/traces", dd.(*datadogReceiver).address),
fmt.Sprintf(tc.endpoint, dd.(*datadogReceiver).address),
tc.op,
)
require.NoError(t, err, "Must not error when creating request")
Expand Down

0 comments on commit 03db92f

Please sign in to comment.