-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7d6ffdc
commit 542e68f
Showing
3 changed files
with
240 additions
and
20 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,189 @@ | ||
package nomad | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"net" | ||
"strings" | ||
"testing" | ||
"time" | ||
|
||
"github.com/hashicorp/nomad/client" | ||
"github.com/hashicorp/nomad/client/config" | ||
cstructs "github.com/hashicorp/nomad/client/structs" | ||
"github.com/hashicorp/nomad/nomad/structs" | ||
"github.com/hashicorp/nomad/testutil" | ||
"github.com/stretchr/testify/require" | ||
"github.com/ugorji/go/codec" | ||
) | ||
|
||
func TestMonitor_Monitor_Remote_Server(t *testing.T) { | ||
t.Parallel() | ||
require := require.New(t) | ||
|
||
// start server and client | ||
s1 := TestServer(t, nil) | ||
defer s1.Shutdown() | ||
s2 := TestServer(t, func(c *Config) { | ||
c.DevDisableBootstrap = true | ||
}) | ||
defer s2.Shutdown() | ||
TestJoin(t, s1, s2) | ||
testutil.WaitForLeader(t, s1.RPC) | ||
testutil.WaitForLeader(t, s2.RPC) | ||
|
||
c, cleanup := client.TestClient(t, func(c *config.Config) { | ||
c.Servers = []string{s2.GetConfig().RPCAddr.String()} | ||
}) | ||
defer cleanup() | ||
|
||
testutil.WaitForResult(func() (bool, error) { | ||
nodes := s2.connectedNodes() | ||
return len(nodes) == 1, nil | ||
}, func(err error) { | ||
t.Fatalf("should have a clients") | ||
}) | ||
|
||
// No node ID to monitor the remote server | ||
req := cstructs.MonitorRequest{ | ||
LogLevel: "debug", | ||
NodeID: c.NodeID(), | ||
} | ||
|
||
handler, err := s1.StreamingRpcHandler("Agent.Monitor") | ||
require.Nil(err) | ||
|
||
// create pipe | ||
p1, p2 := net.Pipe() | ||
defer p1.Close() | ||
defer p2.Close() | ||
|
||
errCh := make(chan error) | ||
streamMsg := make(chan *cstructs.StreamErrWrapper) | ||
|
||
go handler(p2) | ||
|
||
// Start decoder | ||
go func() { | ||
decoder := codec.NewDecoder(p1, structs.MsgpackHandle) | ||
for { | ||
var msg cstructs.StreamErrWrapper | ||
if err := decoder.Decode(&msg); err != nil { | ||
if err == io.EOF || strings.Contains(err.Error(), "closed") { | ||
return | ||
} | ||
errCh <- fmt.Errorf("error decoding: %v", err) | ||
} | ||
|
||
streamMsg <- &msg | ||
} | ||
}() | ||
|
||
// send request | ||
encoder := codec.NewEncoder(p1, structs.MsgpackHandle) | ||
require.Nil(encoder.Encode(req)) | ||
|
||
timeout := time.After(1 * time.Second) | ||
expected := "[DEBUG]" | ||
received := "" | ||
|
||
OUTER: | ||
for { | ||
select { | ||
case <-timeout: | ||
t.Fatal("timeout waiting for logs") | ||
case err := <-errCh: | ||
t.Fatal(err) | ||
case msg := <-streamMsg: | ||
if msg.Error != nil { | ||
t.Fatalf("Got error: %v", msg.Error.Error()) | ||
} | ||
|
||
received += string(msg.Payload) | ||
if strings.Contains(received, expected) { | ||
require.Nil(p2.Close()) | ||
break OUTER | ||
} | ||
} | ||
} | ||
} | ||
|
||
func TestMonitor_MonitorServer(t *testing.T) { | ||
t.Parallel() | ||
require := require.New(t) | ||
|
||
// start server and client | ||
s := TestServer(t, nil) | ||
defer s.Shutdown() | ||
testutil.WaitForLeader(t, s.RPC) | ||
|
||
// No node ID to monitor the remote server | ||
req := cstructs.MonitorRequest{ | ||
LogLevel: "debug", | ||
} | ||
|
||
handler, err := s.StreamingRpcHandler("Agent.Monitor") | ||
require.Nil(err) | ||
|
||
// create pipe | ||
p1, p2 := net.Pipe() | ||
defer p1.Close() | ||
defer p2.Close() | ||
|
||
errCh := make(chan error) | ||
streamMsg := make(chan *cstructs.StreamErrWrapper) | ||
|
||
go handler(p2) | ||
|
||
// Start decoder | ||
go func() { | ||
decoder := codec.NewDecoder(p1, structs.MsgpackHandle) | ||
for { | ||
var msg cstructs.StreamErrWrapper | ||
if err := decoder.Decode(&msg); err != nil { | ||
if err == io.EOF || strings.Contains(err.Error(), "closed") { | ||
return | ||
} | ||
errCh <- fmt.Errorf("error decoding: %v", err) | ||
} | ||
|
||
streamMsg <- &msg | ||
} | ||
}() | ||
|
||
// send request | ||
encoder := codec.NewEncoder(p1, structs.MsgpackHandle) | ||
require.Nil(encoder.Encode(req)) | ||
|
||
timeout := time.After(1 * time.Second) | ||
expected := "[DEBUG]" | ||
received := "" | ||
|
||
// send logs | ||
go func() { | ||
for { | ||
s.logger.Debug("test log") | ||
time.Sleep(100 * time.Millisecond) | ||
} | ||
}() | ||
|
||
OUTER: | ||
for { | ||
select { | ||
case <-timeout: | ||
t.Fatal("timeout waiting for logs") | ||
case err := <-errCh: | ||
t.Fatal(err) | ||
case msg := <-streamMsg: | ||
if msg.Error != nil { | ||
t.Fatalf("Got error: %v", msg.Error.Error()) | ||
} | ||
|
||
received += string(msg.Payload) | ||
if strings.Contains(received, expected) { | ||
require.Nil(p2.Close()) | ||
break OUTER | ||
} | ||
} | ||
} | ||
} |