mirror of
https://github.com/weaveworks/scope.git
synced 2026-07-26 16:52:25 +00:00
- Add store of pipes in the app - Add pipe type, handling impedance mismatch, used in app and probe. - App <-> Probe pipes have their own websockets. - Add pipe websocket endpoint in app. - Pipe IDs are strings, lose the request/response IDs, and give the json encoder lowercase field names. - Add simple golang ws client, for testing. - Pipe lifecycle plumbing. - Ref count and timeout both ends of pipes in the app - Deal with POST /api/pipe/:pid?_method=delete - Add end-to-end unit test for pipes. - Add test for timing out pipes. - Update go-docker client to tomwilkie/go-dockerclient - Backend work for non-raw ttys - Close pipes when they close themselves in the probe - Ensure all http connections are done before returning from client.Stop()
108 lines
2.2 KiB
Go
108 lines
2.2 KiB
Go
package xfer_test
|
|
|
|
import (
|
|
"bytes"
|
|
"io"
|
|
"runtime"
|
|
"testing"
|
|
|
|
"github.com/weaveworks/scope/xfer"
|
|
)
|
|
|
|
type mockClient struct {
|
|
id string
|
|
count int
|
|
stopped int
|
|
publish int
|
|
}
|
|
|
|
func (c *mockClient) Details() (xfer.Details, error) {
|
|
return xfer.Details{ID: c.id}, nil
|
|
}
|
|
|
|
func (c *mockClient) ControlConnection() {
|
|
c.count++
|
|
}
|
|
|
|
func (c *mockClient) Stop() {
|
|
c.stopped++
|
|
}
|
|
|
|
func (c *mockClient) Publish(io.Reader) error {
|
|
c.publish++
|
|
return nil
|
|
}
|
|
|
|
func (c *mockClient) PipeConnection(_ string, _ xfer.Pipe) {}
|
|
func (c *mockClient) PipeClose(_ string) error { return nil }
|
|
|
|
var (
|
|
a1 = &mockClient{id: "1"} // hostname a, app id 1
|
|
a2 = &mockClient{id: "2"} // hostname a, app id 2
|
|
b2 = &mockClient{id: "2"} // hostname b, app id 2 (duplicate)
|
|
b3 = &mockClient{id: "3"} // hostname b, app id 3
|
|
factory = func(hostname, target string) (xfer.AppClient, error) {
|
|
switch target {
|
|
case "a1":
|
|
return a1, nil
|
|
case "a2":
|
|
return a2, nil
|
|
case "b2":
|
|
return b2, nil
|
|
case "b3":
|
|
return b3, nil
|
|
}
|
|
panic(target)
|
|
}
|
|
)
|
|
|
|
func TestMultiClient(t *testing.T) {
|
|
var (
|
|
expect = func(i, j int) {
|
|
if i != j {
|
|
_, file, line, _ := runtime.Caller(1)
|
|
t.Fatalf("%s:%d: %d != %d", file, line, i, j)
|
|
}
|
|
}
|
|
)
|
|
|
|
mp := xfer.NewMultiAppClient(factory)
|
|
defer mp.Stop()
|
|
|
|
// Add two hostnames with overlapping apps, check we don't add the same app twice
|
|
mp.Set("a", []string{"a1", "a2"})
|
|
mp.Set("b", []string{"b2", "b3"})
|
|
expect(a1.count, 1)
|
|
expect(a2.count+b2.count, 1)
|
|
expect(b3.count, 1)
|
|
|
|
// Now drop the overlap, check we don't remove the app
|
|
mp.Set("b", []string{"b3"})
|
|
expect(a1.count, 1)
|
|
expect(a2.count+b2.count, 1)
|
|
expect(b3.count, 1)
|
|
|
|
// Now check we remove apps
|
|
mp.Set("b", []string{})
|
|
expect(b3.stopped, 1)
|
|
}
|
|
|
|
func TestMultiClientPublish(t *testing.T) {
|
|
mp := xfer.NewMultiAppClient(factory)
|
|
defer mp.Stop()
|
|
|
|
sum := func() int { return a1.publish + a2.publish + b2.publish + b3.publish }
|
|
|
|
mp.Set("a", []string{"a1", "a2"})
|
|
mp.Set("b", []string{"b2", "b3"})
|
|
|
|
for i := 1; i < 10; i++ {
|
|
if err := mp.Publish(&bytes.Buffer{}); err != nil {
|
|
t.Error(err)
|
|
}
|
|
if want, have := 3*i, sum(); want != have {
|
|
t.Errorf("want %d, have %d", want, have)
|
|
}
|
|
}
|
|
}
|