-
Notifications
You must be signed in to change notification settings - Fork 1
feat: add new command to send test data to Observe #113
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
32212b1
9a095a0
1a3c9a2
7190956
7a5c1ff
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package sendtestdata | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"strings" | ||
|
||
"github.com/spf13/viper" | ||
) | ||
|
||
func PostTestData(data any, URL string, headers map[string]string) (string, error) { | ||
postBody, err := json.Marshal(data) | ||
if err != nil { | ||
return "", err | ||
} | ||
client := &http.Client{} | ||
req, err := http.NewRequest("POST", URL, bytes.NewBuffer(postBody)) | ||
if err != nil { | ||
return "", err | ||
} | ||
headers["Content-Type"] = "application/json" | ||
for key, value := range headers { | ||
req.Header.Add(key, value) | ||
} | ||
resp, err := client.Do(req) | ||
if err != nil { | ||
return "", err | ||
} | ||
bodyBytes, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
return "", err | ||
} | ||
bodyString := string(bodyBytes) | ||
if resp.StatusCode != 200 { | ||
return "", fmt.Errorf("sending test data to %s failed with response: %s", URL, bodyString) | ||
} | ||
return bodyString, nil | ||
} | ||
|
||
func PostDataToObserve(data any, extraPath string, v *viper.Viper) (string, error) { | ||
collector_url := v.GetString("observe_url") | ||
endpoint := fmt.Sprintf("%s/v1/http%s", strings.TrimRight(collector_url, "/"), extraPath) | ||
authToken := fmt.Sprintf("Bearer %s", v.GetString("token")) | ||
return PostTestData(data, endpoint, map[string]string{"Authorization": authToken}) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package sendtestdata | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/jarcoal/httpmock" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah httpmock is the best way to do this! |
||
"github.com/spf13/viper" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestPostTestData(t *testing.T) { | ||
httpmock.Activate() | ||
t.Cleanup(httpmock.DeactivateAndReset) | ||
|
||
testURL := "https://example.com/test" | ||
expectedResponse := `{"ok":true}` | ||
// Verify that the data is sent to the expected endpoint along with the bearer and json headers. | ||
httpmock.RegisterMatcherResponder("POST", testURL, | ||
httpmock.BodyContainsString(`"hello":"world"`).And( | ||
httpmock.HeaderIs("Content-Type", "application/json"), | ||
httpmock.HeaderIs("SomeHeader", "some value"), | ||
), | ||
httpmock.NewStringResponder(200, expectedResponse), | ||
) | ||
|
||
testData := map[string]string{"hello": "world"} | ||
testHeaders := map[string]string{"SomeHeader": "some value"} | ||
resp, err := PostTestData(testData, testURL, testHeaders) | ||
assert.NoError(t, err) | ||
assert.Equal(t, expectedResponse, resp) | ||
} | ||
|
||
func TestPostDataToObserve(t *testing.T) { | ||
httpmock.Activate() | ||
t.Cleanup(httpmock.DeactivateAndReset) | ||
|
||
expectedResponse := `{"ok":true}` | ||
// Verify that the data is sent to the expected endpoint along with the bearer and json headers. | ||
httpmock.RegisterMatcherResponder("POST", "https://123456.collect.observe-eng.com/v1/http/test", | ||
httpmock.BodyContainsString(`"hello":"world"`).And( | ||
httpmock.HeaderIs("Content-Type", "application/json"), | ||
httpmock.HeaderIs("Authorization", "Bearer test-token"), | ||
), | ||
httpmock.NewStringResponder(200, expectedResponse), | ||
) | ||
|
||
v := viper.New() | ||
v.Set("observe_url", "https://123456.collect.observe-eng.com/") | ||
v.Set("token", "test-token") | ||
testData := map[string]string{"hello": "world"} | ||
resp, err := PostDataToObserve(testData, "/test", v) | ||
assert.NoError(t, err) | ||
assert.Equal(t, expectedResponse, resp) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
Copyright © 2024 NAME HERE <EMAIL ADDRESS> | ||
*/ | ||
package sendtestdata | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
|
||
"github.com/observeinc/observe-agent/internal/root" | ||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
const TestDataExtraPath = "/observe-agent/test" | ||
|
||
var defaultTestData = map[string]any{ | ||
"hello": "world", | ||
} | ||
|
||
func NewSendTestDataCmd() *cobra.Command { | ||
return &cobra.Command{ | ||
Use: "send-test-data", | ||
Short: "Sends test data to Observe", | ||
Long: "Sends test data to Observe", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
var testData map[string]any | ||
dataFlag, _ := cmd.Flags().GetString("data") | ||
if dataFlag != "" { | ||
err := json.Unmarshal([]byte(dataFlag), &testData) | ||
if err != nil { | ||
return err | ||
} | ||
} else { | ||
testData = defaultTestData | ||
} | ||
respBody, err := PostDataToObserve(testData, TestDataExtraPath, viper.GetViper()) | ||
if err != nil { | ||
return err | ||
} | ||
fmt.Printf("Successfully sent test data. Saw response: %s\n", respBody) | ||
return nil | ||
}, | ||
} | ||
} | ||
|
||
func init() { | ||
sendTestDataCmd := NewSendTestDataCmd() | ||
RegisterTestDataFlags(sendTestDataCmd) | ||
root.RootCmd.AddCommand(sendTestDataCmd) | ||
} | ||
|
||
func RegisterTestDataFlags(cmd *cobra.Command) { | ||
cmd.Flags().String("data", "", "specify a given json object to send") | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.