longganhua
2019-07-18 c610f5adea868e6d46ac63a2b9ffeff2c24225fc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package command
 
import (
    "fmt"
    "strings"
    "testing"
 
    "github.com/mitchellh/cli"
)
 
func TestRTTCommand_Implements(t *testing.T) {
    var _ cli.Command = &RTTCommand{}
}
 
func TestRTTCommand_Run_BadArgs(t *testing.T) {
    a1 := testAgent(t)
    defer a1.Shutdown()
    _, ipc := testIPC(t, a1)
    defer ipc.Shutdown()
 
    ui := new(cli.MockUi)
    c := &RTTCommand{Ui: ui}
 
    code := c.Run([]string{})
    if code != 1 {
        t.Fatalf("bad: %d. %#v", code, ui.ErrorWriter.String())
    }
}
 
func TestRTTCommand_Run(t *testing.T) {
    a1 := testAgent(t)
    defer a1.Shutdown()
    rpcAddr, ipc := testIPC(t, a1)
    defer ipc.Shutdown()
 
    coord, ok := a1.Serf().GetCachedCoordinate(a1.SerfConfig().NodeName)
    if !ok {
        t.Fatalf("should have a coordinate for the agent")
    }
    dist_str := fmt.Sprintf("%.3f ms", coord.DistanceTo(coord).Seconds()*1000.0)
 
    // Try with the default of the agent's node.
    args := []string{"-rpc-addr=" + rpcAddr, a1.SerfConfig().NodeName}
    {
        ui := new(cli.MockUi)
        c := &RTTCommand{Ui: ui}
        code := c.Run(args)
        if code != 0 {
            t.Fatalf("bad: %d. %#v", code, ui.ErrorWriter.String())
        }
 
        // Make sure the proper RTT was reported in the output.
        expected := fmt.Sprintf("rtt: %s", dist_str)
        if !strings.Contains(ui.OutputWriter.String(), expected) {
            t.Fatalf("bad: %#v", ui.OutputWriter.String())
        }
    }
 
    // Explicitly set the agent's node twice.
    args = append(args, a1.SerfConfig().NodeName)
    {
        ui := new(cli.MockUi)
        c := &RTTCommand{Ui: ui}
        code := c.Run(args)
        if code != 0 {
            t.Fatalf("bad: %d. %#v", code, ui.ErrorWriter.String())
        }
 
        // Make sure the proper RTT was reported in the output.
        expected := fmt.Sprintf("rtt: %s", dist_str)
        if !strings.Contains(ui.OutputWriter.String(), expected) {
            t.Fatalf("bad: %#v", ui.OutputWriter.String())
        }
    }
 
    // Try an unknown node.
    args = []string{"nope"}
    {
        ui := new(cli.MockUi)
        c := &RTTCommand{Ui: ui}
        code := c.Run(args)
        if code != 1 {
            t.Fatalf("bad: %d. %#v", code, ui.ErrorWriter.String())
        }
    }
}