2021-06-15 18:11:47 +02:00
|
|
|
package mlablocatev2
|
2021-02-02 12:05:47 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/apex/log"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestSuccess(t *testing.T) {
|
2021-06-15 19:25:09 +02:00
|
|
|
// this test is ~0.5 s, so we can always run it
|
2021-06-15 18:11:47 +02:00
|
|
|
client := NewClient(http.DefaultClient, log.Log, "miniooni/0.1.0-dev")
|
2021-02-02 12:05:47 +01:00
|
|
|
result, err := client.QueryNDT7(context.Background())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if len(result) <= 0 {
|
|
|
|
t.Fatal("unexpected empty result")
|
|
|
|
}
|
|
|
|
for _, entry := range result {
|
|
|
|
if entry.Hostname == "" {
|
2021-06-15 19:25:09 +02:00
|
|
|
t.Fatal("expected non empty Hostname here")
|
2021-02-02 12:05:47 +01:00
|
|
|
}
|
|
|
|
if entry.Site == "" {
|
|
|
|
t.Fatal("expected non=-empty Site here")
|
|
|
|
}
|
|
|
|
if entry.WSSDownloadURL == "" {
|
|
|
|
t.Fatal("expected non-empty WSSDownloadURL here")
|
|
|
|
}
|
|
|
|
if _, err := url.Parse(entry.WSSDownloadURL); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if entry.WSSUploadURL == "" {
|
|
|
|
t.Fatal("expected non-empty WSSUploadURL here")
|
|
|
|
}
|
|
|
|
if _, err := url.Parse(entry.WSSUploadURL); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test404Response(t *testing.T) {
|
2021-06-15 19:25:09 +02:00
|
|
|
// this test is ~0.5 s, so we can always run it
|
2021-06-15 18:11:47 +02:00
|
|
|
client := NewClient(http.DefaultClient, log.Log, "miniooni/0.1.0-dev")
|
|
|
|
result, err := client.query(context.Background(), "nonexistent")
|
|
|
|
if !errors.Is(err, ErrRequestFailed) {
|
2021-02-02 12:05:47 +01:00
|
|
|
t.Fatal("not the error we expected")
|
|
|
|
}
|
|
|
|
if result.Results != nil {
|
|
|
|
t.Fatal("expected empty results")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewRequestFailure(t *testing.T) {
|
2021-06-15 18:11:47 +02:00
|
|
|
client := NewClient(http.DefaultClient, log.Log, "miniooni/0.1.0-dev")
|
2021-02-02 12:05:47 +01:00
|
|
|
client.Hostname = "\t"
|
2021-06-15 18:11:47 +02:00
|
|
|
result, err := client.query(context.Background(), "nonexistent")
|
2021-02-02 12:05:47 +01:00
|
|
|
if err == nil || !strings.Contains(err.Error(), "invalid URL escape") {
|
|
|
|
t.Fatal("not the error we expected")
|
|
|
|
}
|
|
|
|
if result.Results != nil {
|
2021-06-15 19:25:09 +02:00
|
|
|
t.Fatal("expected nil results")
|
2021-02-02 12:05:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHTTPClientDoFailure(t *testing.T) {
|
2021-06-15 18:11:47 +02:00
|
|
|
client := NewClient(http.DefaultClient, log.Log, "miniooni/0.1.0-dev")
|
2021-02-02 12:05:47 +01:00
|
|
|
expected := errors.New("mocked error")
|
|
|
|
client.HTTPClient = &http.Client{
|
2021-06-15 18:11:47 +02:00
|
|
|
Transport: FakeTransport{Err: expected},
|
2021-02-02 12:05:47 +01:00
|
|
|
}
|
2021-06-15 18:11:47 +02:00
|
|
|
result, err := client.query(context.Background(), "nonexistent")
|
2021-02-02 12:05:47 +01:00
|
|
|
if !errors.Is(err, expected) {
|
|
|
|
t.Fatal("not the error we expected")
|
|
|
|
}
|
|
|
|
if result.Results != nil {
|
2021-06-15 19:25:09 +02:00
|
|
|
t.Fatal("expected nil results")
|
2021-02-02 12:05:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCannotReadBody(t *testing.T) {
|
2021-06-15 18:11:47 +02:00
|
|
|
client := NewClient(http.DefaultClient, log.Log, "miniooni/0.1.0-dev")
|
2021-02-02 12:05:47 +01:00
|
|
|
expected := errors.New("mocked error")
|
|
|
|
client.HTTPClient = &http.Client{
|
2021-06-15 18:11:47 +02:00
|
|
|
Transport: FakeTransport{
|
2021-02-02 12:05:47 +01:00
|
|
|
Resp: &http.Response{
|
|
|
|
StatusCode: 200,
|
2021-06-15 18:11:47 +02:00
|
|
|
Body: FakeBody{
|
2021-02-02 12:05:47 +01:00
|
|
|
Err: expected,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2021-06-15 18:11:47 +02:00
|
|
|
result, err := client.query(context.Background(), "nonexistent")
|
2021-02-02 12:05:47 +01:00
|
|
|
if !errors.Is(err, expected) {
|
|
|
|
t.Fatal("not the error we expected")
|
|
|
|
}
|
|
|
|
if result.Results != nil {
|
2021-06-15 19:25:09 +02:00
|
|
|
t.Fatal("expected nil results")
|
2021-02-02 12:05:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInvalidJSON(t *testing.T) {
|
2021-06-15 18:11:47 +02:00
|
|
|
client := NewClient(http.DefaultClient, log.Log, "miniooni/0.1.0-dev")
|
2021-02-02 12:05:47 +01:00
|
|
|
client.HTTPClient = &http.Client{
|
2021-06-15 18:11:47 +02:00
|
|
|
Transport: FakeTransport{
|
2021-02-02 12:05:47 +01:00
|
|
|
Resp: &http.Response{
|
|
|
|
StatusCode: 200,
|
2021-06-15 18:11:47 +02:00
|
|
|
Body: FakeBody{
|
2021-02-02 12:05:47 +01:00
|
|
|
Err: io.EOF,
|
|
|
|
Data: []byte(`{`),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2021-06-15 18:11:47 +02:00
|
|
|
result, err := client.query(context.Background(), "nonexistent")
|
2021-02-02 12:05:47 +01:00
|
|
|
if err == nil || !strings.Contains(err.Error(), "unexpected end of JSON input") {
|
|
|
|
t.Fatal("not the error we expected")
|
|
|
|
}
|
|
|
|
if result.Results != nil {
|
2021-06-15 19:25:09 +02:00
|
|
|
t.Fatal("expected nil results")
|
2021-02-02 12:05:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEmptyResponse(t *testing.T) {
|
2021-06-15 18:11:47 +02:00
|
|
|
client := NewClient(http.DefaultClient, log.Log, "miniooni/0.1.0-dev")
|
2021-02-02 12:05:47 +01:00
|
|
|
client.HTTPClient = &http.Client{
|
2021-06-15 18:11:47 +02:00
|
|
|
Transport: FakeTransport{
|
2021-02-02 12:05:47 +01:00
|
|
|
Resp: &http.Response{
|
|
|
|
StatusCode: 200,
|
2021-06-15 18:11:47 +02:00
|
|
|
Body: FakeBody{
|
2021-02-02 12:05:47 +01:00
|
|
|
Err: io.EOF,
|
|
|
|
Data: []byte(`{}`),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
result, err := client.QueryNDT7(context.Background())
|
2021-06-15 18:11:47 +02:00
|
|
|
if !errors.Is(err, ErrEmptyResponse) {
|
2021-02-02 12:05:47 +01:00
|
|
|
t.Fatal("not the error we expected")
|
|
|
|
}
|
|
|
|
if result != nil {
|
2021-06-15 19:25:09 +02:00
|
|
|
t.Fatal("expected nil results")
|
2021-02-02 12:05:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNDT7QueryFails(t *testing.T) {
|
2021-06-15 18:11:47 +02:00
|
|
|
client := NewClient(http.DefaultClient, log.Log, "miniooni/0.1.0-dev")
|
2021-02-02 12:05:47 +01:00
|
|
|
client.HTTPClient = &http.Client{
|
2021-06-15 18:11:47 +02:00
|
|
|
Transport: FakeTransport{
|
2021-02-02 12:05:47 +01:00
|
|
|
Resp: &http.Response{
|
|
|
|
StatusCode: 404,
|
2021-06-15 18:11:47 +02:00
|
|
|
Body: FakeBody{Err: io.EOF},
|
2021-02-02 12:05:47 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
result, err := client.QueryNDT7(context.Background())
|
2021-06-15 18:11:47 +02:00
|
|
|
if !errors.Is(err, ErrRequestFailed) {
|
2021-02-02 12:05:47 +01:00
|
|
|
t.Fatal("not the error we expected")
|
|
|
|
}
|
|
|
|
if result != nil {
|
2021-06-15 19:25:09 +02:00
|
|
|
t.Fatal("expected nil results")
|
2021-02-02 12:05:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNDT7InvalidURLs(t *testing.T) {
|
2021-06-15 18:11:47 +02:00
|
|
|
client := NewClient(http.DefaultClient, log.Log, "miniooni/0.1.0-dev")
|
2021-02-02 12:05:47 +01:00
|
|
|
client.HTTPClient = &http.Client{
|
2021-06-15 18:11:47 +02:00
|
|
|
Transport: FakeTransport{
|
2021-02-02 12:05:47 +01:00
|
|
|
Resp: &http.Response{
|
|
|
|
StatusCode: 200,
|
2021-06-15 18:11:47 +02:00
|
|
|
Body: FakeBody{
|
2021-02-02 12:05:47 +01:00
|
|
|
Data: []byte(
|
|
|
|
`{"results":[{"machine":"mlab3-mil04.mlab-oti.measurement-lab.org","urls":{"wss:///ndt/v7/download":":","wss:///ndt/v7/upload":":"}}]}`),
|
|
|
|
Err: io.EOF,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
result, err := client.QueryNDT7(context.Background())
|
2021-06-15 18:11:47 +02:00
|
|
|
if !errors.Is(err, ErrEmptyResponse) {
|
2021-02-02 12:05:47 +01:00
|
|
|
t.Fatal("not the error we expected")
|
|
|
|
}
|
|
|
|
if result != nil {
|
2021-06-15 19:25:09 +02:00
|
|
|
t.Fatal("expected nil results")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNDT7EmptyURLs(t *testing.T) {
|
|
|
|
client := NewClient(http.DefaultClient, log.Log, "miniooni/0.1.0-dev")
|
|
|
|
client.HTTPClient = &http.Client{
|
|
|
|
Transport: FakeTransport{
|
|
|
|
Resp: &http.Response{
|
|
|
|
StatusCode: 200,
|
|
|
|
Body: FakeBody{
|
|
|
|
Data: []byte(
|
|
|
|
`{"results":[{"machine":"mlab3-mil04.mlab-oti.measurement-lab.org","urls":{"wss:///ndt/v7/download":"","wss:///ndt/v7/upload":""}}]}`),
|
|
|
|
Err: io.EOF,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
result, err := client.QueryNDT7(context.Background())
|
|
|
|
if !errors.Is(err, ErrEmptyResponse) {
|
|
|
|
t.Fatal("not the error we expected")
|
|
|
|
}
|
|
|
|
if result != nil {
|
|
|
|
t.Fatal("expected nil results")
|
2021-02-02 12:05:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEntryRecordSite(t *testing.T) {
|
|
|
|
type fields struct {
|
|
|
|
Machine string
|
|
|
|
URLs map[string]string
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields fields
|
|
|
|
want string
|
|
|
|
}{{
|
|
|
|
name: "with invalid machine name",
|
|
|
|
fields: fields{
|
|
|
|
Machine: "ndt-iupui-mlab3-mil02.mlab-oti.measurement-lab.org",
|
|
|
|
},
|
|
|
|
want: "",
|
|
|
|
}, {
|
|
|
|
name: "with valid machine name",
|
|
|
|
fields: fields{
|
|
|
|
Machine: "mlab3-mil04.mlab-oti.measurement-lab.org",
|
|
|
|
},
|
|
|
|
want: "mil04",
|
|
|
|
}}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2021-06-15 18:11:47 +02:00
|
|
|
er := entryRecord{
|
2021-02-02 12:05:47 +01:00
|
|
|
Machine: tt.fields.Machine,
|
|
|
|
URLs: tt.fields.URLs,
|
|
|
|
}
|
|
|
|
if got := er.Site(); got != tt.want {
|
|
|
|
t.Errorf("entryRecord.Site() = %v, want %v", got, tt.want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|