-
Notifications
You must be signed in to change notification settings - Fork 712
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve probe docker code quality & test coverage.
- Move docker probe code into it's own module - Put PIDTree behind and interface for mocking - Disaggregate dockerTagger into a registry, tagger and reporter - Similarly disaggregate tests - Add mocks for docker container and registry - Add test for docker events & stats
- Loading branch information
Tom Wilkie
committed
Jun 18, 2015
1 parent
a6ef295
commit 0e6f22d
Showing
20 changed files
with
1,041 additions
and
590 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package docker_test | ||
|
||
import ( | ||
"bufio" | ||
"encoding/json" | ||
"io" | ||
"net" | ||
"net/http" | ||
"runtime" | ||
"testing" | ||
|
||
client "github.com/fsouza/go-dockerclient" | ||
"github.com/weaveworks/scope/probe/docker" | ||
) | ||
|
||
type mockConnection struct { | ||
reader *io.PipeReader | ||
} | ||
|
||
func (c *mockConnection) Do(req *http.Request) (resp *http.Response, err error) { | ||
return &http.Response{ | ||
Body: c.reader, | ||
}, nil | ||
} | ||
|
||
func (c *mockConnection) Close() error { | ||
return c.reader.Close() | ||
} | ||
|
||
func TestContainer(t *testing.T) { | ||
oldDialStub, oldNewClientConnStub := docker.DialStub, docker.NewClientConnStub | ||
defer func() { docker.DialStub, docker.NewClientConnStub = oldDialStub, oldNewClientConnStub }() | ||
|
||
docker.DialStub = func(network, address string) (net.Conn, error) { | ||
return nil, nil | ||
} | ||
|
||
reader, writer := io.Pipe() | ||
connection := &mockConnection{reader} | ||
|
||
docker.NewClientConnStub = func(c net.Conn, r *bufio.Reader) docker.ClientConn { | ||
return connection | ||
} | ||
|
||
c := docker.NewContainer(container1) | ||
err := c.StartGatheringStats() | ||
if err != nil { | ||
t.Errorf("%v", err) | ||
} | ||
defer c.StopGatheringStats() | ||
runtime.Gosched() // wait for StartGatheringStats goroutine to call connection.Do | ||
|
||
// Send some stats to the docker container | ||
stats := &client.Stats{} | ||
stats.MemoryStats.Usage = 12345 | ||
err = json.NewEncoder(writer).Encode(&stats) | ||
if err != nil { | ||
t.Errorf("%v", err) | ||
} | ||
runtime.Gosched() // wait for StartGatheringStats goroutine to receive the stats | ||
|
||
// Now see if we go them | ||
nmd := c.GetNodeMetadata() | ||
if nmd[docker.MemoryUsage] != "12345" { | ||
t.Errorf("want 12345, got %s", nmd[docker.MemoryUsage]) | ||
} | ||
} |
Oops, something went wrong.