feat: Traefik Pilot integration.
Co-authored-by: Ludovic Fernandez <ldez@users.noreply.github.com>
This commit is contained in:
parent
cb6ec507e2
commit
4a31544024
7 changed files with 421 additions and 14 deletions
123
pkg/pilot/pilot_test.go
Normal file
123
pkg/pilot/pilot_test.go
Normal file
|
@ -0,0 +1,123 @@
|
|||
package pilot
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/containous/traefik/v2/pkg/config/runtime"
|
||||
"github.com/containous/traefik/v2/pkg/safe"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestTick(t *testing.T) {
|
||||
receivedConfig := make(chan bool)
|
||||
|
||||
mux := http.NewServeMux()
|
||||
server := httptest.NewServer(mux)
|
||||
t.Cleanup(server.Close)
|
||||
|
||||
mux.HandleFunc("/", func(rw http.ResponseWriter, req *http.Request) {
|
||||
if req.Method != http.MethodPost {
|
||||
http.Error(rw, "invalid method", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
|
||||
err := json.NewEncoder(rw).Encode(instanceInfo{ID: "123"})
|
||||
if err != nil {
|
||||
http.Error(rw, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
})
|
||||
|
||||
mux.HandleFunc("/command", func(rw http.ResponseWriter, req *http.Request) {
|
||||
if req.Method != http.MethodPost {
|
||||
http.Error(rw, "invalid method", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
|
||||
receivedConfig <- true
|
||||
})
|
||||
|
||||
pilot := New("token", safe.NewPool(context.Background()))
|
||||
pilot.client.baseURL = server.URL
|
||||
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
t.Cleanup(cancel)
|
||||
|
||||
go pilot.Tick(ctx)
|
||||
|
||||
pilot.SetRuntimeConfiguration(&runtime.Configuration{})
|
||||
pilot.SetRuntimeConfiguration(&runtime.Configuration{})
|
||||
|
||||
select {
|
||||
case <-time.Tick(10 * time.Second):
|
||||
t.Fatal("Timeout")
|
||||
case <-receivedConfig:
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func TestClient_SendConfiguration(t *testing.T) {
|
||||
myToken := "myToken"
|
||||
|
||||
mux := http.NewServeMux()
|
||||
server := httptest.NewServer(mux)
|
||||
t.Cleanup(server.Close)
|
||||
|
||||
mux.HandleFunc("/", func(rw http.ResponseWriter, req *http.Request) {
|
||||
if req.Method != http.MethodPost {
|
||||
http.Error(rw, "invalid method", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
|
||||
tk := req.Header.Get(tokenHeader)
|
||||
if tk != myToken {
|
||||
http.Error(rw, fmt.Sprintf("invalid token: %s", tk), http.StatusUnauthorized)
|
||||
}
|
||||
|
||||
err := json.NewEncoder(rw).Encode(instanceInfo{ID: "123"})
|
||||
if err != nil {
|
||||
http.Error(rw, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
})
|
||||
|
||||
mux.HandleFunc("/command", func(rw http.ResponseWriter, req *http.Request) {
|
||||
if req.Method != http.MethodPost {
|
||||
http.Error(rw, "invalid method", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
|
||||
tk := req.Header.Get(tokenHeader)
|
||||
if tk != myToken {
|
||||
http.Error(rw, fmt.Sprintf("invalid token: %s", tk), http.StatusUnauthorized)
|
||||
}
|
||||
|
||||
defer req.Body.Close()
|
||||
|
||||
info := &instanceInfo{}
|
||||
err := json.NewDecoder(req.Body).Decode(info)
|
||||
if err != nil {
|
||||
http.Error(rw, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
if info.ID != "123" {
|
||||
http.Error(rw, fmt.Sprintf("invalid ID: %s", info.ID), http.StatusBadRequest)
|
||||
}
|
||||
})
|
||||
|
||||
client := client{
|
||||
baseURL: server.URL,
|
||||
httpClient: http.DefaultClient,
|
||||
token: myToken,
|
||||
}
|
||||
|
||||
err := client.SendData(context.Background(), RunTimeRepresentation{})
|
||||
require.NoError(t, err)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue