Skip to content

Commit

Permalink
feat(dnsp-server): add support for dnsp-server tests (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
William Fleming authored Apr 5, 2020
1 parent 8ab63f3 commit 007eb6d
Show file tree
Hide file tree
Showing 2 changed files with 375 additions and 0 deletions.
95 changes: 95 additions & 0 deletions dnsp_server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package thousandeyes

import "fmt"

// DNSPServer - DNSP server test
type DNSPServer struct {
Agents []Agent `json:"agents,omitempty"`
AlertsEnabled int `json:"alertsEnabled,omitempty"`
AlertRules []AlertRule `json:"alertRules,omitempty"`
APILinks APILinks `json:"apiLinks,omitempty"`
CreatedBy string `json:"createdBy,omitempty"`
CreatedDate string `json:"createdDate,omitempty"`
Description string `json:"description,omitempty"`
Enabled int `json:"enabled,omitempty"`
Groups []GroupLabel `json:"groups,omitempty"`
LiveShare int `json:"liveShare,omitempty"`
ModifiedBy string `json:"modifiedBy,omitempty"`
ModifiedDate string `json:"modifiedDate,omitempty"`
SavedEvent int `json:"savedEvent,omitempty"`
SharedWithAccounts []AccountGroup `json:"sharedWithAccounts,omitempty"`
Type string `json:"type"`
TestID int `json:"testId,omitempty"`
TestName string `json:"testName,omitempty"`
Server string `json:"server,omitempty"`
Interval int `json:"interval,omitempty"`
}

// AddAgent - Add agent to DNSP server test
func (t *DNSPServer) AddAgent(id int) {
agent := Agent{AgentID: id}
t.Agents = append(t.Agents, agent)
}

// AddAlertRule - Adds an alert to agent test
func (t *DNSPServer) AddAlertRule(id int) {
alertRule := AlertRule{RuleID: id}
t.AlertRules = append(t.AlertRules, alertRule)
}

// GetDNSPServer - get dnsp server test
func (c *Client) GetDNSPServer(id int) (*DNSPServer, error) {
resp, err := c.get(fmt.Sprintf("/tests/%d", id))
if err != nil {
return &DNSPServer{}, err
}
var target map[string][]DNSPServer
if dErr := c.decodeJSON(resp, &target); dErr != nil {
return nil, fmt.Errorf("Could not decode JSON response: %v", dErr)
}
return &target["test"][0], nil
}

// CreateDNSPServer - Create dnsp server test
func (c Client) CreateDNSPServer(t DNSPServer) (*DNSPServer, error) {
resp, err := c.post("/tests/dnsp-server/new", t, nil)
if err != nil {
return &t, err
}
if resp.StatusCode != 201 {
return &t, fmt.Errorf("failed to dnsp server create test, response code %d", resp.StatusCode)
}
var target map[string][]DNSPServer
if dErr := c.decodeJSON(resp, &target); dErr != nil {
return nil, fmt.Errorf("Could not decode JSON response: %v", dErr)
}
return &target["test"][0], nil
}

//DeleteDNSPServer - delete dnsp server test
func (c *Client) DeleteDNSPServer(id int) error {
resp, err := c.post(fmt.Sprintf("/tests/dnsp-server/%d/delete", id), nil, nil)
if err != nil {
return err
}
if resp.StatusCode != 204 {
return fmt.Errorf("failed to delete dnsp server test, response code %d", resp.StatusCode)
}
return nil
}

//UpdateDNSPServer - update dnsp server test
func (c *Client) UpdateDNSPServer(id int, t DNSPServer) (*DNSPServer, error) {
resp, err := c.post(fmt.Sprintf("/tests/dnsp-server/%d/update", id), t, nil)
if err != nil {
return &t, err
}
if resp.StatusCode != 200 {
return &t, fmt.Errorf("failed to update test, response code %d", resp.StatusCode)
}
var target map[string][]DNSPServer
if dErr := c.decodeJSON(resp, &target); dErr != nil {
return nil, fmt.Errorf("Could not decode JSON response: %v", dErr)
}
return &target["test"][0], nil
}
280 changes: 280 additions & 0 deletions dnsp_server_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,280 @@
package thousandeyes

import (
"net/http"
"testing"

"github.com/stretchr/testify/assert"
)

func TestClient_GetDNSPServer(t *testing.T) {
out := `{"test":[{"createdDate":"2020-02-06 15:28:07","createdBy":"William Fleming ([email protected])","enabled":1,"savedEvent":0,"testId":122621,"testName":"test123","type":"dnsp-server","interval":300,"alertsEnabled":1,"liveShare":0,"agents":[{"agentId":48620,"agentName":"Seattle, WA (Trial) - IPv6","agentType":"Cloud","countryId":"US","ipAddresses":["135.84.184.153"],"location":"Seattle Area","network":"Astute Hosting Inc. (AS 54527)","prefix":"135.84.184.0/22"}],"sharedWithAccounts":[{"aid":176592,"name":"Cloudreach"}],"server": "webex.com","apiLinks":[{"rel":"self","href":"https://api.thousandeyes.com/v6/tests/1226221"},{"rel":"data","href":"https://api.thousandeyes.com/v6/web/dnsp-server/1226221"},{"rel":"data","href":"https://api.thousandeyes.com/v6/net/metrics/1226221"},{"rel":"data","href":"https://api.thousandeyes.com/v6/net/path-vis/1226221"},{"rel":"data","href":"https://api.thousandeyes.com/v6/net/bgp-metrics/1226221"}]}]}`
setup()
var client = &Client{APIEndpoint: server.URL, AuthToken: "foo"}
mux.HandleFunc("/tests/122621.json", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "GET", r.Method)
_, _ = w.Write([]byte(out))
})

expected := DNSPServer{
TestID: 122621,
Enabled: 1,
CreatedBy: "William Fleming ([email protected])",
CreatedDate: "2020-02-06 15:28:07",
SavedEvent: 0,
AlertsEnabled: 1,
TestName: "test123",
Type: "dnsp-server",
Interval: 300,
Server: "webex.com",
Agents: []Agent{
{
AgentID: 48620,
AgentType: "Cloud",
AgentName: "Seattle, WA (Trial) - IPv6",
CountryID: "US",
IPAddresses: []string{"135.84.184.153"},
Location: "Seattle Area",
Network: "Astute Hosting Inc. (AS 54527)",
Prefix: "135.84.184.0/22",
},
},
SharedWithAccounts: []AccountGroup{
{
Aid: 176592,
Name: "Cloudreach",
},
},
APILinks: APILinks{
{
Href: "https://api.thousandeyes.com/v6/tests/1226221",
Rel: "self",
},
{
Href: "https://api.thousandeyes.com/v6/web/dnsp-server/1226221",
Rel: "data",
},
{
Href: "https://api.thousandeyes.com/v6/net/metrics/1226221",
Rel: "data",
},
{
Href: "https://api.thousandeyes.com/v6/net/path-vis/1226221",
Rel: "data",
},
{
Href: "https://api.thousandeyes.com/v6/net/bgp-metrics/1226221",
Rel: "data",
},
},
}

res, err := client.GetDNSPServer(122621)
teardown()
assert.Nil(t, err)
assert.Equal(t, &expected, res)
}

func TestClient_GetDNSPServerJsonError(t *testing.T) {
out := `{"test":[{"createdDate":"2020-02-06 15:28:07",createdBy":"William Fleming ([email protected])","enabled":1,"savedEvent":0,"testId":122621,"testName":"test123","type":"dnsp-server","interval":300,"alertsEnabled":1,"liveShare":0,"probeMode":"AUTO","agents":[{"agentId":48620,"agentName":"Seattle, WA (Trial) - IPv6","agentType":"Cloud","countryId":"US","ipAddresses":["135.84.184.153"],"location":"Seattle Area","network":"Astute Hosting Inc. (AS 54527)","prefix":"135.84.184.0/22"}],"sharedWithAccounts":[{"aid":176592,"name":"Cloudreach"}],"server": "webex.com","dnsTransportProtocol": "UDP"}]"apiLinks":[{"rel":"self","href":"https://api.thousandeyes.com/v6/tests/1226221"},{"rel":"data","href":"https://api.thousandeyes.com/v6/web/dnsp-server/1226221"},{"rel":"data","href":"https://api.thousandeyes.com/v6/net/metrics/1226221"},{"rel":"data","href":"https://api.thousandeyes.com/v6/net/path-vis/1226221"},{"rel":"data","href":"https://api.thousandeyes.com/v6/net/bgp-metrics/1226221"}]}]}`
setup()
var client = &Client{APIEndpoint: server.URL, AuthToken: "foo"}
mux.HandleFunc("/tests/122621.json", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "GET", r.Method)
_, _ = w.Write([]byte(out))
})
_, err := client.GetDNSPServer(122621)
assert.Error(t, err)
assert.EqualError(t, err, "Could not decode JSON response: invalid character 'c' looking for beginning of object key string")
}

func TestClient_CreateDNSPServer(t *testing.T) {
out := `{"test":[{"createdDate":"2020-02-06 15:28:07","createdBy":"William Fleming ([email protected])","enabled":1,"savedEvent":0,"testId":122621,"testName":"test123","type":"dnsp-server","interval":300,"alertsEnabled":1,"liveShare":0,"agents":[{"agentId":48620,"agentName":"Seattle, WA (Trial) - IPv6","agentType":"Cloud","countryId":"US","ipAddresses":["135.84.184.153"],"location":"Seattle Area","network":"Astute Hosting Inc. (AS 54527)","prefix":"135.84.184.0/22"}],"sharedWithAccounts":[{"aid":176592,"name":"Cloudreach"}],"server": "webex.com","apiLinks":[{"rel":"self","href":"https://api.thousandeyes.com/v6/tests/1226221"},{"rel":"data","href":"https://api.thousandeyes.com/v6/web/dnsp-server/1226221"},{"rel":"data","href":"https://api.thousandeyes.com/v6/net/metrics/1226221"},{"rel":"data","href":"https://api.thousandeyes.com/v6/net/path-vis/1226221"},{"rel":"data","href":"https://api.thousandeyes.com/v6/net/bgp-metrics/1226221"}]}]}`
setup()
var client = &Client{APIEndpoint: server.URL, AuthToken: "foo"}
mux.HandleFunc("/tests/dnsp-server/new.json", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "POST", r.Method)
w.WriteHeader(http.StatusCreated)
_, _ = w.Write([]byte(out))
})

expected := DNSPServer{
TestID: 122621,
Enabled: 1,
CreatedBy: "William Fleming ([email protected])",
CreatedDate: "2020-02-06 15:28:07",
SavedEvent: 0,
TestName: "test123",
Type: "dnsp-server",
Interval: 300,
AlertsEnabled: 1,
Server: "webex.com",
Agents: []Agent{
{
AgentID: 48620,
AgentType: "Cloud",
AgentName: "Seattle, WA (Trial) - IPv6",
CountryID: "US",
IPAddresses: []string{"135.84.184.153"},
Location: "Seattle Area",
Network: "Astute Hosting Inc. (AS 54527)",
Prefix: "135.84.184.0/22",
},
},
SharedWithAccounts: []AccountGroup{
{
Aid: 176592,
Name: "Cloudreach",
},
},

APILinks: APILinks{
{
Href: "https://api.thousandeyes.com/v6/tests/1226221",
Rel: "self",
},
{
Href: "https://api.thousandeyes.com/v6/web/dnsp-server/1226221",
Rel: "data",
},
{
Href: "https://api.thousandeyes.com/v6/net/metrics/1226221",
Rel: "data",
},
{
Href: "https://api.thousandeyes.com/v6/net/path-vis/1226221",
Rel: "data",
},
{
Href: "https://api.thousandeyes.com/v6/net/bgp-metrics/1226221",
Rel: "data",
},
},
}
create := DNSPServer{
TestName: "test123",
Server: "webex.com",
Interval: 300,
}
res, err := client.CreateDNSPServer(create)
teardown()
assert.Nil(t, err)
assert.Equal(t, &expected, res)
}

func TestClient_DeleteDNSPServer(t *testing.T) {
setup()
mux.HandleFunc("/tests/dnsp-server/1/delete.json", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNoContent)
assert.Equal(t, "POST", r.Method)
})

var client = &Client{APIEndpoint: server.URL, AuthToken: "foo"}
id := 1
err := client.DeleteDNSPServer(id)

if err != nil {
t.Fatal(err)
}
}

func TestClient_AddDNSPServerAlertRule(t *testing.T) {
test := DNSPServer{TestName: "test", AlertRules: []AlertRule{}}
expected := DNSPServer{TestName: "test", AlertRules: []AlertRule{{RuleID: 1}}}
test.AddAlertRule(1)
assert.Equal(t, expected, test)
}

func TestClient_UpdateDNSPServer(t *testing.T) {
setup()
out := `{"test":[{"testId":1,"testName":"test123","type":"dnsp-server","server":"webex.com" }]}`
mux.HandleFunc("/tests/dnsp-server/1/update.json", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "POST", r.Method)
_, _ = w.Write([]byte(out))
})

var client = &Client{APIEndpoint: server.URL, AuthToken: "foo"}
id := 1
dnsp := DNSPServer{Server: "webex.com"}
res, err := client.UpdateDNSPServer(id, dnsp)
if err != nil {
t.Fatal(err)
}
expected := DNSPServer{TestID: 1, TestName: "test123", Type: "dnsp-server", Server: "webex.com"}
assert.Equal(t, &expected, res)

}

func TestDNSPServer_AddAgent(t *testing.T) {
test := DNSPServer{TestName: "test", Agents: Agents{}}
expected := DNSPServer{TestName: "test", Agents: []Agent{{AgentID: 1}}}
test.AddAgent(1)
assert.Equal(t, expected, test)
}

func TestClient_GetDNSPServerError(t *testing.T) {
setup()
var client = &Client{APIEndpoint: server.URL, AuthToken: "foo"}
mux.HandleFunc("/tests/dnsp-server/1.json", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "GET", r.Method)
w.WriteHeader(http.StatusBadRequest)
})

_, err := client.GetDNSPServer(1)
teardown()
assert.Error(t, err)
}

func TestClient_GetDNSPServernStatusCode(t *testing.T) {
setup()
out := `{"test":[{"testId":1,"testName":"test123","type":"dnsp-trace"}]}`
var client = &Client{APIEndpoint: server.URL, AuthToken: "foo"}
mux.HandleFunc("/tests/1.json", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "GET", r.Method)
w.WriteHeader(http.StatusBadRequest)
_, _ = w.Write([]byte(out))
})

_, err := client.GetDNSPServer(1)
teardown()
assert.EqualError(t, err, "Failed call API endpoint. HTTP response code: 400. Error: &{}")
}

func TestClient_CreateDNSPServerStatusCode(t *testing.T) {
setup()
var client = &Client{APIEndpoint: server.URL, AuthToken: "foo"}
mux.HandleFunc("/tests/dnsp-server/new.json", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "POST", r.Method)
w.WriteHeader(http.StatusBadRequest)
_, _ = w.Write([]byte(`{}`))
})
_, err := client.CreateDNSPServer(DNSPServer{})
teardown()
assert.EqualError(t, err, "Failed call API endpoint. HTTP response code: 400. Error: &{}")
}

func TestClient_UpdateDNSPServerStatusCode(t *testing.T) {
setup()
var client = &Client{APIEndpoint: server.URL, AuthToken: "foo"}
mux.HandleFunc("/tests/dnsp-server/1/update.json", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "POST", r.Method)
w.WriteHeader(http.StatusBadRequest)
_, _ = w.Write([]byte(`{}`))
})
_, err := client.UpdateDNSPServer(1, DNSPServer{})
teardown()
assert.EqualError(t, err, "Failed call API endpoint. HTTP response code: 400. Error: &{}")
}

func TestClient_DeleteDNSPServerStatusCode(t *testing.T) {
setup()
var client = &Client{APIEndpoint: server.URL, AuthToken: "foo"}
mux.HandleFunc("/tests/dnsp-server/1/delete.json", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "POST", r.Method)
w.WriteHeader(http.StatusBadRequest)
_, _ = w.Write([]byte(`{}`))
})
err := client.DeleteDNSPServer(1)
teardown()
assert.EqualError(t, err, "Failed call API endpoint. HTTP response code: 400. Error: &{}")
}

0 comments on commit 007eb6d

Please sign in to comment.