~juju-qa/ubuntu/yakkety/juju/2.0-beta12

« back to all changes in this revision

Viewing changes to src/github.com/juju/juju/standards/rfc5424/rfc5424test/server_test.go

  • Committer: Nicholas Skaggs
  • Date: 2016-07-18 18:41:24 UTC
  • mfrom: (1.4.5)
  • Revision ID: nicholas.skaggs@canonical.com-20160718184124-76sg7nr3zf2o6o63
* New upstream release 2.0-beta12
* Update debian/copyright

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// Copyright 2016 Canonical Ltd.
2
 
// Licensed under the AGPLv3, see LICENCE file for details.
3
 
 
4
 
package rfc5424test_test
5
 
 
6
 
import (
7
 
        "net"
8
 
        "time"
9
 
 
10
 
        gitjujutesting "github.com/juju/testing"
11
 
        jc "github.com/juju/testing/checkers"
12
 
        gc "gopkg.in/check.v1"
13
 
 
14
 
        "github.com/juju/juju/standards/rfc5424"
15
 
        "github.com/juju/juju/standards/rfc5424/rfc5424test"
16
 
        "github.com/juju/juju/testing"
17
 
)
18
 
 
19
 
type ServerSuite struct {
20
 
        gitjujutesting.IsolationSuite
21
 
}
22
 
 
23
 
var _ = gc.Suite(&ServerSuite{})
24
 
 
25
 
func (s *ServerSuite) SetUpTest(c *gc.C) {
26
 
        s.IsolationSuite.SetUpTest(c)
27
 
}
28
 
 
29
 
func (s *ServerSuite) TestSend(c *gc.C) {
30
 
        received := make(chan rfc5424test.Message, 1)
31
 
        server := rfc5424test.NewServer(rfc5424test.HandlerFunc(func(msg rfc5424test.Message) {
32
 
                received <- msg
33
 
        }))
34
 
        server.Start()
35
 
        defer server.Close()
36
 
 
37
 
        cfg := rfc5424.ClientConfig{
38
 
                MaxSize:     8192,
39
 
                SendTimeout: time.Minute,
40
 
        }
41
 
        var clientAddr net.Addr
42
 
        netDial := func(network, address string) (rfc5424.Conn, error) {
43
 
                conn, err := net.Dial(network, address)
44
 
                clientAddr = conn.LocalAddr()
45
 
                return conn, err
46
 
        }
47
 
        client, err := rfc5424.Open(server.Listener.Addr().String(), cfg, netDial)
48
 
        c.Assert(err, jc.ErrorIsNil)
49
 
 
50
 
        msg := rfc5424.Message{
51
 
                Header: rfc5424.Header{
52
 
                        Priority: rfc5424.Priority{
53
 
                                Severity: rfc5424.SeverityWarning,
54
 
                                Facility: rfc5424.FacilityDaemon,
55
 
                        },
56
 
                        Timestamp: rfc5424.Timestamp{time.Unix(54321, 123).UTC()},
57
 
                        Hostname:  rfc5424.Hostname{FQDN: "a.b.org"},
58
 
                        AppName:   "an-app",
59
 
                        ProcID:    "119",
60
 
                        MsgID:     "xyz...",
61
 
                },
62
 
                StructuredData: rfc5424.StructuredData{
63
 
                        fakeStructuredDataElement{"sde0"},
64
 
                        fakeStructuredDataElement{"sde1"},
65
 
                },
66
 
                Msg: "a message",
67
 
        }
68
 
 
69
 
        err = client.Send(msg)
70
 
        c.Assert(err, jc.ErrorIsNil)
71
 
        err = client.Close()
72
 
        c.Assert(err, jc.ErrorIsNil)
73
 
 
74
 
        select {
75
 
        case msg, ok := <-received:
76
 
                c.Assert(ok, jc.IsTrue)
77
 
                c.Assert(msg.RemoteAddr, gc.Equals, clientAddr.String())
78
 
                c.Assert(msg.Message, gc.Equals, `<28>1 1970-01-01T15:05:21.000000123Z a.b.org an-app 119 xyz... [sde0 abc="123" def="456"][sde1 abc="123" def="456"] a message`)
79
 
        case <-time.After(testing.LongWait):
80
 
                c.Fatal("timed out waiting for message")
81
 
        }
82
 
}
83
 
 
84
 
type fakeStructuredDataElement struct {
85
 
        id rfc5424.StructuredDataName
86
 
}
87
 
 
88
 
func (f fakeStructuredDataElement) ID() rfc5424.StructuredDataName {
89
 
        return f.id
90
 
}
91
 
 
92
 
func (fakeStructuredDataElement) Params() []rfc5424.StructuredDataParam {
93
 
        return []rfc5424.StructuredDataParam{
94
 
                {"abc", "123"},
95
 
                {"def", "456"},
96
 
        }
97
 
}
98
 
 
99
 
func (fakeStructuredDataElement) Validate() error {
100
 
        return nil
101
 
}