fix: cookie test (#9608)
This commit is contained in:
parent
41c384a978
commit
02ccbec348
|
|
@ -9,6 +9,7 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
clockutil "github.com/benbjohnson/clock"
|
||||||
"github.com/influxdata/telegraf"
|
"github.com/influxdata/telegraf"
|
||||||
"github.com/influxdata/telegraf/config"
|
"github.com/influxdata/telegraf/config"
|
||||||
)
|
)
|
||||||
|
|
@ -27,7 +28,7 @@ type CookieAuthConfig struct {
|
||||||
client *http.Client
|
client *http.Client
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *CookieAuthConfig) Start(client *http.Client, log telegraf.Logger) (err error) {
|
func (c *CookieAuthConfig) Start(client *http.Client, log telegraf.Logger, clock clockutil.Clock) (err error) {
|
||||||
c.client = client
|
c.client = client
|
||||||
|
|
||||||
if c.Method == "" {
|
if c.Method == "" {
|
||||||
|
|
@ -45,7 +46,7 @@ func (c *CookieAuthConfig) Start(client *http.Client, log telegraf.Logger) (err
|
||||||
|
|
||||||
// continual auth renewal if set
|
// continual auth renewal if set
|
||||||
if c.Renewal > 0 {
|
if c.Renewal > 0 {
|
||||||
ticker := time.NewTicker(time.Duration(c.Renewal))
|
ticker := clock.Ticker(time.Duration(c.Renewal))
|
||||||
go func() {
|
go func() {
|
||||||
for range ticker.C {
|
for range ticker.C {
|
||||||
if err := c.auth(); err != nil && log != nil {
|
if err := c.auth(); err != nil && log != nil {
|
||||||
|
|
|
||||||
|
|
@ -9,6 +9,7 @@ import (
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
clockutil "github.com/benbjohnson/clock"
|
||||||
"github.com/google/go-cmp/cmp"
|
"github.com/google/go-cmp/cmp"
|
||||||
"github.com/influxdata/telegraf/config"
|
"github.com/influxdata/telegraf/config"
|
||||||
"github.com/influxdata/telegraf/plugins/common/cookie"
|
"github.com/influxdata/telegraf/plugins/common/cookie"
|
||||||
|
|
@ -121,7 +122,7 @@ func TestAuthConfig_Start(t *testing.T) {
|
||||||
fields fields
|
fields fields
|
||||||
args args
|
args args
|
||||||
wantErr error
|
wantErr error
|
||||||
assert func(t *testing.T, c *cookie.CookieAuthConfig, srv fakeServer)
|
assert func(t *testing.T, c *cookie.CookieAuthConfig, srv fakeServer, mock *clockutil.Mock)
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "zero renewal does not renew",
|
name: "zero renewal does not renew",
|
||||||
|
|
@ -129,12 +130,11 @@ func TestAuthConfig_Start(t *testing.T) {
|
||||||
renewal: 0,
|
renewal: 0,
|
||||||
endpoint: authEndpointNoCreds,
|
endpoint: authEndpointNoCreds,
|
||||||
},
|
},
|
||||||
assert: func(t *testing.T, c *cookie.CookieAuthConfig, srv fakeServer) {
|
assert: func(t *testing.T, c *cookie.CookieAuthConfig, srv fakeServer, mock *clockutil.Mock) {
|
||||||
// should have Cookie Authed once
|
// should have Cookie Authed once
|
||||||
srv.checkAuthCount(t, 1)
|
srv.checkAuthCount(t, 1)
|
||||||
srv.checkResp(t, http.StatusOK)
|
srv.checkResp(t, http.StatusOK)
|
||||||
time.Sleep(renewalCheck)
|
mock.Add(renewalCheck)
|
||||||
// should have never Cookie Authed again
|
|
||||||
srv.checkAuthCount(t, 1)
|
srv.checkAuthCount(t, 1)
|
||||||
srv.checkResp(t, http.StatusOK)
|
srv.checkResp(t, http.StatusOK)
|
||||||
},
|
},
|
||||||
|
|
@ -145,13 +145,13 @@ func TestAuthConfig_Start(t *testing.T) {
|
||||||
renewal: renewal,
|
renewal: renewal,
|
||||||
endpoint: authEndpointNoCreds,
|
endpoint: authEndpointNoCreds,
|
||||||
},
|
},
|
||||||
assert: func(t *testing.T, c *cookie.CookieAuthConfig, srv fakeServer) {
|
assert: func(t *testing.T, c *cookie.CookieAuthConfig, srv fakeServer, mock *clockutil.Mock) {
|
||||||
// should have Cookie Authed once
|
// should have Cookie Authed once
|
||||||
srv.checkAuthCount(t, 1)
|
srv.checkAuthCount(t, 1)
|
||||||
// default method set
|
// default method set
|
||||||
require.Equal(t, http.MethodPost, c.Method)
|
require.Equal(t, http.MethodPost, c.Method)
|
||||||
srv.checkResp(t, http.StatusOK)
|
srv.checkResp(t, http.StatusOK)
|
||||||
time.Sleep(renewalCheck)
|
mock.Add(renewalCheck)
|
||||||
// should have Cookie Authed at least twice more
|
// should have Cookie Authed at least twice more
|
||||||
srv.checkAuthCount(t, 3)
|
srv.checkAuthCount(t, 3)
|
||||||
srv.checkResp(t, http.StatusOK)
|
srv.checkResp(t, http.StatusOK)
|
||||||
|
|
@ -168,11 +168,11 @@ func TestAuthConfig_Start(t *testing.T) {
|
||||||
renewal: renewal,
|
renewal: renewal,
|
||||||
endpoint: authEndpointWithBasicAuth,
|
endpoint: authEndpointWithBasicAuth,
|
||||||
},
|
},
|
||||||
assert: func(t *testing.T, c *cookie.CookieAuthConfig, srv fakeServer) {
|
assert: func(t *testing.T, c *cookie.CookieAuthConfig, srv fakeServer, mock *clockutil.Mock) {
|
||||||
// should have Cookie Authed once
|
// should have Cookie Authed once
|
||||||
srv.checkAuthCount(t, 1)
|
srv.checkAuthCount(t, 1)
|
||||||
srv.checkResp(t, http.StatusOK)
|
srv.checkResp(t, http.StatusOK)
|
||||||
time.Sleep(renewalCheck)
|
mock.Add(renewalCheck)
|
||||||
// should have Cookie Authed at least twice more
|
// should have Cookie Authed at least twice more
|
||||||
srv.checkAuthCount(t, 3)
|
srv.checkAuthCount(t, 3)
|
||||||
srv.checkResp(t, http.StatusOK)
|
srv.checkResp(t, http.StatusOK)
|
||||||
|
|
@ -190,11 +190,11 @@ func TestAuthConfig_Start(t *testing.T) {
|
||||||
endpoint: authEndpointWithBasicAuth,
|
endpoint: authEndpointWithBasicAuth,
|
||||||
},
|
},
|
||||||
wantErr: fmt.Errorf("cookie auth renewal received status code: 401 (Unauthorized)"),
|
wantErr: fmt.Errorf("cookie auth renewal received status code: 401 (Unauthorized)"),
|
||||||
assert: func(t *testing.T, c *cookie.CookieAuthConfig, srv fakeServer) {
|
assert: func(t *testing.T, c *cookie.CookieAuthConfig, srv fakeServer, mock *clockutil.Mock) {
|
||||||
// should have never Cookie Authed
|
// should have never Cookie Authed
|
||||||
srv.checkAuthCount(t, 0)
|
srv.checkAuthCount(t, 0)
|
||||||
srv.checkResp(t, http.StatusForbidden)
|
srv.checkResp(t, http.StatusForbidden)
|
||||||
time.Sleep(renewalCheck)
|
mock.Add(renewalCheck)
|
||||||
// should have still never Cookie Authed
|
// should have still never Cookie Authed
|
||||||
srv.checkAuthCount(t, 0)
|
srv.checkAuthCount(t, 0)
|
||||||
srv.checkResp(t, http.StatusForbidden)
|
srv.checkResp(t, http.StatusForbidden)
|
||||||
|
|
@ -210,11 +210,11 @@ func TestAuthConfig_Start(t *testing.T) {
|
||||||
renewal: renewal,
|
renewal: renewal,
|
||||||
endpoint: authEndpointWithBody,
|
endpoint: authEndpointWithBody,
|
||||||
},
|
},
|
||||||
assert: func(t *testing.T, c *cookie.CookieAuthConfig, srv fakeServer) {
|
assert: func(t *testing.T, c *cookie.CookieAuthConfig, srv fakeServer, mock *clockutil.Mock) {
|
||||||
// should have Cookie Authed once
|
// should have Cookie Authed once
|
||||||
srv.checkAuthCount(t, 1)
|
srv.checkAuthCount(t, 1)
|
||||||
srv.checkResp(t, http.StatusOK)
|
srv.checkResp(t, http.StatusOK)
|
||||||
time.Sleep(renewalCheck)
|
mock.Add(renewalCheck)
|
||||||
// should have Cookie Authed at least twice more
|
// should have Cookie Authed at least twice more
|
||||||
srv.checkAuthCount(t, 3)
|
srv.checkAuthCount(t, 3)
|
||||||
srv.checkResp(t, http.StatusOK)
|
srv.checkResp(t, http.StatusOK)
|
||||||
|
|
@ -231,11 +231,11 @@ func TestAuthConfig_Start(t *testing.T) {
|
||||||
endpoint: authEndpointWithBody,
|
endpoint: authEndpointWithBody,
|
||||||
},
|
},
|
||||||
wantErr: fmt.Errorf("cookie auth renewal received status code: 401 (Unauthorized)"),
|
wantErr: fmt.Errorf("cookie auth renewal received status code: 401 (Unauthorized)"),
|
||||||
assert: func(t *testing.T, c *cookie.CookieAuthConfig, srv fakeServer) {
|
assert: func(t *testing.T, c *cookie.CookieAuthConfig, srv fakeServer, mock *clockutil.Mock) {
|
||||||
// should have never Cookie Authed
|
// should have never Cookie Authed
|
||||||
srv.checkAuthCount(t, 0)
|
srv.checkAuthCount(t, 0)
|
||||||
srv.checkResp(t, http.StatusForbidden)
|
srv.checkResp(t, http.StatusForbidden)
|
||||||
time.Sleep(renewalCheck)
|
mock.Add(renewalCheck)
|
||||||
// should have still never Cookie Authed
|
// should have still never Cookie Authed
|
||||||
srv.checkAuthCount(t, 0)
|
srv.checkAuthCount(t, 0)
|
||||||
srv.checkResp(t, http.StatusForbidden)
|
srv.checkResp(t, http.StatusForbidden)
|
||||||
|
|
@ -255,15 +255,17 @@ func TestAuthConfig_Start(t *testing.T) {
|
||||||
Renewal: config.Duration(tt.args.renewal),
|
Renewal: config.Duration(tt.args.renewal),
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := c.Start(srv.Client(), testutil.Logger{Name: "cookie_auth"}); tt.wantErr != nil {
|
mock := clockutil.NewMock()
|
||||||
|
if err := c.Start(srv.Client(), testutil.Logger{Name: "cookie_auth"}, mock); tt.wantErr != nil {
|
||||||
require.EqualError(t, err, tt.wantErr.Error())
|
require.EqualError(t, err, tt.wantErr.Error())
|
||||||
} else {
|
} else {
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if tt.assert != nil {
|
if tt.assert != nil {
|
||||||
tt.assert(t, c, srv)
|
tt.assert(t, c, srv, mock)
|
||||||
}
|
}
|
||||||
|
srv.Close()
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/benbjohnson/clock"
|
||||||
"github.com/influxdata/telegraf"
|
"github.com/influxdata/telegraf"
|
||||||
"github.com/influxdata/telegraf/config"
|
"github.com/influxdata/telegraf/config"
|
||||||
"github.com/influxdata/telegraf/plugins/common/cookie"
|
"github.com/influxdata/telegraf/plugins/common/cookie"
|
||||||
|
|
@ -54,7 +55,7 @@ func (h *HTTPClientConfig) CreateClient(ctx context.Context, log telegraf.Logger
|
||||||
client = h.OAuth2Config.CreateOauth2Client(ctx, client)
|
client = h.OAuth2Config.CreateOauth2Client(ctx, client)
|
||||||
|
|
||||||
if h.CookieAuthConfig.URL != "" {
|
if h.CookieAuthConfig.URL != "" {
|
||||||
if err := h.CookieAuthConfig.Start(client, log); err != nil {
|
if err := h.CookieAuthConfig.Start(client, log, clock.New()); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue