2017-11-29 19:50:32 +00:00
|
|
|
package dcos
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"net/url"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
jwt "github.com/dgrijalva/jwt-go"
|
2018-05-04 23:33:23 +00:00
|
|
|
"github.com/influxdata/telegraf/testutil"
|
2017-11-29 19:50:32 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2018-05-04 23:33:23 +00:00
|
|
|
var privateKey = testutil.NewPKI("../../../testutil/pki").ReadServerKey()
|
2017-11-29 19:50:32 +00:00
|
|
|
|
|
|
|
func TestLogin(t *testing.T) {
|
2018-03-24 02:14:07 +00:00
|
|
|
ts := httptest.NewServer(http.NotFoundHandler())
|
|
|
|
defer ts.Close()
|
|
|
|
|
2017-11-29 19:50:32 +00:00
|
|
|
var tests = []struct {
|
|
|
|
name string
|
|
|
|
responseCode int
|
|
|
|
responseBody string
|
|
|
|
expectedError error
|
|
|
|
expectedToken string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Login successful",
|
2018-03-24 02:14:07 +00:00
|
|
|
responseCode: http.StatusOK,
|
2017-11-29 19:50:32 +00:00
|
|
|
responseBody: `{"token": "XXX.YYY.ZZZ"}`,
|
|
|
|
expectedError: nil,
|
|
|
|
expectedToken: "XXX.YYY.ZZZ",
|
|
|
|
},
|
|
|
|
{
|
2018-03-24 02:14:07 +00:00
|
|
|
name: "Unauthorized Error",
|
|
|
|
responseCode: http.StatusUnauthorized,
|
|
|
|
responseBody: `{"title": "x", "description": "y"}`,
|
|
|
|
expectedError: &APIError{
|
|
|
|
URL: ts.URL + "/acs/api/v1/auth/login",
|
|
|
|
StatusCode: http.StatusUnauthorized,
|
|
|
|
Title: "x",
|
|
|
|
Description: "y",
|
|
|
|
},
|
2017-11-29 19:50:32 +00:00
|
|
|
expectedToken: "",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
key, err := jwt.ParseRSAPrivateKeyFromPEM([]byte(privateKey))
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2018-03-24 02:14:07 +00:00
|
|
|
ts.Config.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2017-11-29 19:50:32 +00:00
|
|
|
w.WriteHeader(tt.responseCode)
|
|
|
|
fmt.Fprintln(w, tt.responseBody)
|
|
|
|
})
|
2018-03-24 02:14:07 +00:00
|
|
|
|
2017-11-29 19:50:32 +00:00
|
|
|
u, err := url.Parse(ts.URL)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
sa := &ServiceAccount{
|
|
|
|
AccountID: "telegraf",
|
|
|
|
PrivateKey: key,
|
|
|
|
}
|
|
|
|
client := NewClusterClient(u, defaultResponseTimeout, 1, nil)
|
|
|
|
auth, err := client.Login(ctx, sa)
|
|
|
|
|
|
|
|
require.Equal(t, tt.expectedError, err)
|
|
|
|
|
|
|
|
if tt.expectedToken != "" {
|
|
|
|
require.Equal(t, tt.expectedToken, auth.Text)
|
|
|
|
} else {
|
|
|
|
require.Nil(t, auth)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetSummary(t *testing.T) {
|
2018-03-24 02:14:07 +00:00
|
|
|
ts := httptest.NewServer(http.NotFoundHandler())
|
|
|
|
defer ts.Close()
|
|
|
|
|
2017-11-29 19:50:32 +00:00
|
|
|
var tests = []struct {
|
|
|
|
name string
|
|
|
|
responseCode int
|
|
|
|
responseBody string
|
|
|
|
expectedValue *Summary
|
|
|
|
expectedError error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "No nodes",
|
2018-03-24 02:14:07 +00:00
|
|
|
responseCode: http.StatusOK,
|
2017-11-29 19:50:32 +00:00
|
|
|
responseBody: `{"cluster": "a", "slaves": []}`,
|
|
|
|
expectedValue: &Summary{Cluster: "a", Slaves: []Slave{}},
|
|
|
|
expectedError: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Unauthorized Error",
|
|
|
|
responseCode: http.StatusUnauthorized,
|
|
|
|
responseBody: `<html></html>`,
|
|
|
|
expectedValue: nil,
|
2018-03-24 02:14:07 +00:00
|
|
|
expectedError: &APIError{
|
|
|
|
URL: ts.URL + "/mesos/master/state-summary",
|
|
|
|
StatusCode: http.StatusUnauthorized,
|
|
|
|
Title: "401 Unauthorized",
|
|
|
|
},
|
2017-11-29 19:50:32 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Has nodes",
|
2018-03-24 02:14:07 +00:00
|
|
|
responseCode: http.StatusOK,
|
2017-11-29 19:50:32 +00:00
|
|
|
responseBody: `{"cluster": "a", "slaves": [{"id": "a"}, {"id": "b"}]}`,
|
|
|
|
expectedValue: &Summary{
|
|
|
|
Cluster: "a",
|
|
|
|
Slaves: []Slave{
|
|
|
|
Slave{ID: "a"},
|
|
|
|
Slave{ID: "b"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedError: nil,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2018-03-24 02:14:07 +00:00
|
|
|
ts.Config.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2017-11-29 19:50:32 +00:00
|
|
|
// check the path
|
|
|
|
w.WriteHeader(tt.responseCode)
|
|
|
|
fmt.Fprintln(w, tt.responseBody)
|
|
|
|
})
|
2018-03-24 02:14:07 +00:00
|
|
|
|
2017-11-29 19:50:32 +00:00
|
|
|
u, err := url.Parse(ts.URL)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
client := NewClusterClient(u, defaultResponseTimeout, 1, nil)
|
|
|
|
summary, err := client.GetSummary(ctx)
|
|
|
|
|
|
|
|
require.Equal(t, tt.expectedError, err)
|
|
|
|
require.Equal(t, tt.expectedValue, summary)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetNodeMetrics(t *testing.T) {
|
2018-03-24 02:14:07 +00:00
|
|
|
ts := httptest.NewServer(http.NotFoundHandler())
|
|
|
|
defer ts.Close()
|
|
|
|
|
2017-11-29 19:50:32 +00:00
|
|
|
var tests = []struct {
|
|
|
|
name string
|
|
|
|
responseCode int
|
|
|
|
responseBody string
|
|
|
|
expectedValue *Metrics
|
|
|
|
expectedError error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Empty Body",
|
2018-03-24 02:14:07 +00:00
|
|
|
responseCode: http.StatusOK,
|
2017-11-29 19:50:32 +00:00
|
|
|
responseBody: `{}`,
|
|
|
|
expectedValue: &Metrics{},
|
|
|
|
expectedError: nil,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2018-03-24 02:14:07 +00:00
|
|
|
ts.Config.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2017-11-29 19:50:32 +00:00
|
|
|
// check the path
|
|
|
|
w.WriteHeader(tt.responseCode)
|
|
|
|
fmt.Fprintln(w, tt.responseBody)
|
|
|
|
})
|
2018-03-24 02:14:07 +00:00
|
|
|
|
2017-11-29 19:50:32 +00:00
|
|
|
u, err := url.Parse(ts.URL)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
client := NewClusterClient(u, defaultResponseTimeout, 1, nil)
|
|
|
|
m, err := client.GetNodeMetrics(ctx, "foo")
|
|
|
|
|
|
|
|
require.Equal(t, tt.expectedError, err)
|
|
|
|
require.Equal(t, tt.expectedValue, m)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetContainerMetrics(t *testing.T) {
|
2018-03-24 02:14:07 +00:00
|
|
|
ts := httptest.NewServer(http.NotFoundHandler())
|
|
|
|
defer ts.Close()
|
|
|
|
|
2017-11-29 19:50:32 +00:00
|
|
|
var tests = []struct {
|
|
|
|
name string
|
|
|
|
responseCode int
|
|
|
|
responseBody string
|
|
|
|
expectedValue *Metrics
|
|
|
|
expectedError error
|
|
|
|
}{
|
|
|
|
{
|
2018-03-24 02:14:07 +00:00
|
|
|
name: "204 No Content",
|
|
|
|
responseCode: http.StatusNoContent,
|
2017-11-29 19:50:32 +00:00
|
|
|
responseBody: ``,
|
|
|
|
expectedValue: &Metrics{},
|
|
|
|
expectedError: nil,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2018-03-24 02:14:07 +00:00
|
|
|
ts.Config.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2017-11-29 19:50:32 +00:00
|
|
|
// check the path
|
|
|
|
w.WriteHeader(tt.responseCode)
|
|
|
|
fmt.Fprintln(w, tt.responseBody)
|
|
|
|
})
|
2018-03-24 02:14:07 +00:00
|
|
|
|
2017-11-29 19:50:32 +00:00
|
|
|
u, err := url.Parse(ts.URL)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
client := NewClusterClient(u, defaultResponseTimeout, 1, nil)
|
|
|
|
m, err := client.GetContainerMetrics(ctx, "foo", "bar")
|
|
|
|
|
|
|
|
require.Equal(t, tt.expectedError, err)
|
|
|
|
require.Equal(t, tt.expectedValue, m)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|