-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: zzzk1 <[email protected]>
- Loading branch information
Showing
5 changed files
with
152 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// Copyright (c) 2025 The Jaeger Authors. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package client | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"net/http" | ||
) | ||
|
||
var _ DataStreamAPI = (*DataStreamClient)(nil) | ||
|
||
type DataStreamClient struct { | ||
Client | ||
} | ||
|
||
func (ds DataStreamClient) Create(dataStream string) error { | ||
_, err := ds.request(elasticRequest{ | ||
endpoint: "_data_stream/" + dataStream, | ||
method: http.MethodPut, | ||
}) | ||
if err != nil { | ||
var responseError ResponseError | ||
if errors.As(err, &responseError) { | ||
if responseError.StatusCode != http.StatusOK { | ||
return responseError.prefixMessage("failed to create data stream: " + dataStream) | ||
} | ||
} | ||
return fmt.Errorf("failed to create data stream: %w", err) | ||
} | ||
return nil | ||
} |
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,60 @@ | ||
// Copyright (c) 2025 The Jaeger Authors. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package client | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestCreate(t *testing.T) { | ||
dataStreamName := "jaeger-span" | ||
tests := []struct { | ||
name string | ||
responseCode int | ||
response string | ||
errContains string | ||
}{ | ||
{ | ||
name: "success", | ||
responseCode: http.StatusOK, | ||
}, | ||
{ | ||
name: "client error", | ||
responseCode: http.StatusBadRequest, | ||
response: esErrResponse, | ||
errContains: "failed to create data stream: jaeger-span", | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
testServer := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) { | ||
assert.True(t, strings.HasSuffix(req.URL.String(), dataStreamName)) | ||
assert.Equal(t, http.MethodPut, req.Method) | ||
assert.Equal(t, "Basic foobar", req.Header.Get("Authorization")) | ||
res.WriteHeader(test.responseCode) | ||
res.Write([]byte(test.response)) | ||
})) | ||
defer testServer.Close() | ||
|
||
c := &DataStreamClient{ | ||
Client: Client{ | ||
Client: testServer.Client(), | ||
Endpoint: testServer.URL, | ||
BasicAuth: "foobar", | ||
}, | ||
} | ||
err := c.Create(dataStreamName) | ||
if test.errContains != "" { | ||
require.ErrorContains(t, err, test.errContains) | ||
} | ||
}) | ||
} | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.