Skip to content

Making the HTTP Collector tests more robust. #1787

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/collect/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ func responseToOutput(response *http.Response, err error) ([]byte, error) {
var rawJSON json.RawMessage
if len(body) > 0 {
if err := json.Unmarshal(body, &rawJSON); err != nil {
klog.Infof("failed to unmarshal response body as JSON: %+v", err)
klog.Infof("response body is not in JSON format: %+v", err)
rawJSON = json.RawMessage{}
}
} else {
Expand Down
33 changes: 21 additions & 12 deletions pkg/collect/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io"
"net/http"
"net/http/httptest"
"regexp"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -42,6 +43,7 @@ type CollectorTest struct {
Collector *troubleshootv1beta2.HTTP
args args
checkTimeout bool
checkCert bool
want CollectorResult
wantErr bool
}
Expand Down Expand Up @@ -77,18 +79,16 @@ func TestCollectHTTP_Collect(t *testing.T) {
res.Write([]byte("Hello, PUT!"))
})
mux.HandleFunc("/error", func(res http.ResponseWriter, req *http.Request) {
time.Sleep(1 * time.Millisecond)
time.Sleep(2000 * time.Millisecond)
fmt.Println("Sleeping for 2 seconds on /error call")
res.Header().Set("Content-Type", "application/json; charset=utf-8")
res.WriteHeader(http.StatusInternalServerError)
res.Write([]byte("{\"error\": { \"message\": \"context deadline exceeded\"}}"))
})
mux.HandleFunc("/certificate-mismatch", func(res http.ResponseWriter, req *http.Request) {
time.Sleep(1 * time.Millisecond)
fmt.Println("Sleeping for 2 seconds on /error call")
res.Header().Set("Content-Type", "application/json; charset=utf-8")
res.WriteHeader(http.StatusInternalServerError)
res.Write([]byte("{\"error\": { \"message\": \"Request failed: proxyconnect tcp: tls: failed to verify certificate: x509: \"10.0.0.254\" certificate is not trusted\"}}"))
res.Write([]byte("{\"error\": { \"message\": \"Request failed: proxyconnect tcp: tls: failed to verify certificate: x509: 10.0.0.254 certificate is not trusted\"}}"))
})

sample_get_response := &ResponseData{
Expand Down Expand Up @@ -129,14 +129,14 @@ func TestCollectHTTP_Collect(t *testing.T) {

sample_error_response := &ErrorResponse{
Error: HTTPError{
Message: "context deadline exceeded",
Message: "Get \"http://127.0.0.1:57932/error\": context deadline exceeded",
},
}
sample_error_bytes, _ := sample_error_response.ToJSONbytes()

sample_certificate_untrusted := &ErrorResponse{
Error: HTTPError{
Message: "Request failed: proxyconnect tcp: tls: failed to verify certificate: x509: \"10.0.0.254\" certificate is not trusted",
Message: "Request failed: proxyconnect tcp: tls: failed to verify certificate: x509: 10.0.0.254 certificate is not trusted",
},
}
sample_certificate_untrusted_bytes, _ := sample_certificate_untrusted.ToJSONbytes()
Expand Down Expand Up @@ -279,8 +279,8 @@ func TestCollectHTTP_Collect(t *testing.T) {
want: CollectorResult{
"result.json": sample_certificate_untrusted_bytes,
},
checkTimeout: true,
wantErr: true,
checkCert: true,
wantErr: true,
},
}
for _, tt := range tests {
Expand All @@ -304,8 +304,9 @@ func TestCollectHTTP_Collect(t *testing.T) {
c.Collector.Get.URL = fmt.Sprintf("%s%s", url, "/error")
response_data := sample_error_response
response_data.testCollectHTTP(t, &tt, c)
} else if tt.checkCert {
c.Collector.Get.URL = fmt.Sprintf("%s%s", url, "/certificate-mismatch")
response_data = sample_certificate_untrusted
response_data := sample_certificate_untrusted
response_data.testCollectHTTP(t, &tt, c)
} else {
c.Collector.Get.URL = fmt.Sprintf("%s%s", url, "/get")
Expand Down Expand Up @@ -374,9 +375,8 @@ func (er *ErrorResponse) testCollectHTTP(t *testing.T, tt *CollectorTest, c *Col
t.Errorf("CollectHTTP.Collect() error = %v, wantErr %v", err, tt.wantErr)
return
}

if strings.Contains(strings.TrimSpace(er.Error.Message), "context deadline exceeded") != tt.wantErr {
t.Errorf("CollectHTTP.Collect() response = %v, wantErr %v", er.Error.Message, tt.wantErr)
if !compareStringsAlphaOnly(string(tt.want[expected_filename]), string(er.Error.Message)) {
t.Errorf("CollectHTTP.Collect() response = %v, wantErr %s", er.Error.Message, tt.want[expected_filename])
}
}

Expand Down Expand Up @@ -473,3 +473,12 @@ func Test_responseToOutput(t *testing.T) {
})
}
}

func compareStringsAlphaOnly(str1, str2 string) bool {
reg := regexp.MustCompile("[^a-zA-Z]+")

processedStr1 := reg.ReplaceAllString(str1, "")
processedStr2 := reg.ReplaceAllString(str2, "")

return strings.Contains(processedStr1, processedStr2)
}
Loading