-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add common generic Get for system-probe checks (#31254)
- Loading branch information
1 parent
78d29ba
commit 0fdbad2
Showing
13 changed files
with
151 additions
and
328 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// Unless explicitly stated otherwise all files in this repository are licensed | ||
// under the Apache License Version 2.0. | ||
// This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
// Copyright 2024-present Datadog, Inc. | ||
|
||
package client | ||
|
||
import ( | ||
"context" | ||
"net" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestConstructURL(t *testing.T) { | ||
u := constructURL("", "/asdf?a=b") | ||
assert.Equal(t, "http://sysprobe/asdf?a=b", u) | ||
|
||
u = constructURL("zzzz", "/asdf?a=b") | ||
assert.Equal(t, "http://sysprobe/zzzz/asdf?a=b", u) | ||
|
||
u = constructURL("zzzz", "asdf") | ||
assert.Equal(t, "http://sysprobe/zzzz/asdf", u) | ||
} | ||
|
||
func TestGetCheck(t *testing.T) { | ||
type testData struct { | ||
Str string | ||
Num int | ||
} | ||
|
||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
if r.URL.Path == "/test/check" { | ||
_, _ = w.Write([]byte(`{"Str": "asdf", "Num": 42}`)) | ||
} else { | ||
w.WriteHeader(http.StatusNotFound) | ||
} | ||
})) | ||
t.Cleanup(server.Close) | ||
|
||
client := &http.Client{Transport: &http.Transport{DialContext: func(_ context.Context, _, _ string) (net.Conn, error) { | ||
return net.Dial("tcp", server.Listener.Addr().String()) | ||
}}} | ||
|
||
resp, err := GetCheck[testData](client, "test") | ||
require.NoError(t, err) | ||
assert.Equal(t, "asdf", resp.Str) | ||
assert.Equal(t, 42, resp.Num) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.