gobot.io/x/gobot/v2@v2.1.0/platforms/nats/nats_adaptor_test.go (about) 1 package nats 2 3 import ( 4 "errors" 5 "fmt" 6 "log" 7 "strings" 8 "testing" 9 10 "github.com/nats-io/nats.go" 11 "gobot.io/x/gobot/v2" 12 "gobot.io/x/gobot/v2/gobottest" 13 ) 14 15 var _ gobot.Adaptor = (*Adaptor)(nil) 16 17 func connStub(options ...nats.Option) func() (*nats.Conn, error) { 18 return func() (*nats.Conn, error) { 19 opts := nats.DefaultOptions 20 for _, opt := range options { 21 if err := opt(&opts); err != nil { 22 return nil, err 23 } 24 } 25 c := &nats.Conn{Opts: opts} 26 return c, nil 27 } 28 } 29 30 func initTestNatsAdaptor() *Adaptor { 31 a := NewAdaptor("localhost:4222", 19999) 32 a.connect = func() (*nats.Conn, error) { 33 c := &nats.Conn{} 34 return c, nil 35 } 36 return a 37 } 38 39 func initTestNatsAdaptorWithAuth() *Adaptor { 40 a := NewAdaptorWithAuth("localhost:4222", 29999, "user", "pass") 41 a.connect = func() (*nats.Conn, error) { 42 c := &nats.Conn{} 43 return c, nil 44 } 45 return a 46 } 47 48 func initTestNatsAdaptorTLS(options ...nats.Option) *Adaptor { 49 a := NewAdaptor("tls://localhost:4242", 39999, options...) 50 a.connect = connStub(options...) 51 return a 52 } 53 54 func TestNatsAdaptorName(t *testing.T) { 55 a := initTestNatsAdaptor() 56 gobottest.Assert(t, strings.HasPrefix(a.Name(), "NATS"), true) 57 a.SetName("NewName") 58 gobottest.Assert(t, a.Name(), "NewName") 59 } 60 61 func TestNatsAdaptorReturnsHost(t *testing.T) { 62 a := initTestNatsAdaptor() 63 gobottest.Assert(t, a.Host, "nats://localhost:4222") 64 } 65 66 func TestNatsAdaptorWithAuth(t *testing.T) { 67 a := initTestNatsAdaptorWithAuth() 68 gobottest.Assert(t, a.username, "user") 69 gobottest.Assert(t, a.password, "pass") 70 } 71 72 func TestNatsAdapterSetsRootCAs(t *testing.T) { 73 a := initTestNatsAdaptorTLS(nats.RootCAs("test_certs/catest.pem")) 74 gobottest.Assert(t, a.Host, "tls://localhost:4242") 75 a.Connect() 76 o := a.client.Opts 77 gobottest.Assert(t, len(o.TLSConfig.RootCAs.Subjects()), 1) 78 gobottest.Assert(t, o.Secure, true) 79 } 80 81 func TestNatsAdapterSetsClientCerts(t *testing.T) { 82 a := initTestNatsAdaptorTLS(nats.ClientCert("test_certs/client-cert.pem", "test_certs/client-key.pem")) 83 gobottest.Assert(t, a.Host, "tls://localhost:4242") 84 a.Connect() 85 certs := a.client.Opts.TLSConfig.Certificates 86 gobottest.Assert(t, len(certs), 1) 87 gobottest.Assert(t, a.client.Opts.Secure, true) 88 } 89 90 func TestNatsAdapterSetsClientCertsWithUserInfo(t *testing.T) { 91 a := initTestNatsAdaptorTLS(nats.ClientCert("test_certs/client-cert.pem", "test_certs/client-key.pem"), nats.UserInfo("test", "testwd")) 92 gobottest.Assert(t, a.Host, "tls://localhost:4242") 93 a.Connect() 94 certs := a.client.Opts.TLSConfig.Certificates 95 gobottest.Assert(t, len(certs), 1) 96 gobottest.Assert(t, a.client.Opts.Secure, true) 97 gobottest.Assert(t, a.client.Opts.User, "test") 98 gobottest.Assert(t, a.client.Opts.Password, "testwd") 99 } 100 101 // TODO: implement this test without requiring actual server connection 102 func TestNatsAdaptorPublishWhenConnected(t *testing.T) { 103 t.Skip("TODO: implement this test without requiring actual server connection") 104 a := initTestNatsAdaptor() 105 a.Connect() 106 data := []byte("o") 107 gobottest.Assert(t, a.Publish("test", data), true) 108 } 109 110 // TODO: implement this test without requiring actual server connection 111 func TestNatsAdaptorOnWhenConnected(t *testing.T) { 112 t.Skip("TODO: implement this test without requiring actual server connection") 113 a := initTestNatsAdaptor() 114 a.Connect() 115 gobottest.Assert(t, a.On("hola", func(msg Message) { 116 fmt.Println("hola") 117 }), true) 118 } 119 120 // TODO: implement this test without requiring actual server connection 121 func TestNatsAdaptorPublishWhenConnectedWithAuth(t *testing.T) { 122 t.Skip("TODO: implement this test without requiring actual server connection") 123 a := NewAdaptorWithAuth("localhost:4222", 49999, "test", "testwd") 124 a.Connect() 125 data := []byte("o") 126 gobottest.Assert(t, a.Publish("test", data), true) 127 } 128 129 // TODO: implement this test without requiring actual server connection 130 func TestNatsAdaptorOnWhenConnectedWithAuth(t *testing.T) { 131 t.Skip("TODO: implement this test without requiring actual server connection") 132 log.Println("###not skipped###") 133 a := NewAdaptorWithAuth("localhost:4222", 59999, "test", "testwd") 134 a.Connect() 135 gobottest.Assert(t, a.On("hola", func(msg Message) { 136 fmt.Println("hola") 137 }), true) 138 } 139 140 func TestNatsAdaptorFailedConnect(t *testing.T) { 141 a := NewAdaptor("localhost:9999", 69999) 142 err := a.Connect() 143 if err != nil && strings.Contains(err.Error(), "cannot assign requested address") { 144 t.Skip("FLAKY: Can not test, because IP or port is in use.") 145 } 146 gobottest.Assert(t, err, errors.New("nats: no servers available for connection")) 147 } 148 149 func TestNatsAdaptorFinalize(t *testing.T) { 150 a := NewAdaptor("localhost:9999", 79999) 151 gobottest.Assert(t, a.Finalize(), nil) 152 } 153 154 func TestNatsAdaptorCannotPublishUnlessConnected(t *testing.T) { 155 a := NewAdaptor("localhost:9999", 89999) 156 data := []byte("o") 157 gobottest.Assert(t, a.Publish("test", data), false) 158 } 159 160 func TestNatsAdaptorCannotOnUnlessConnected(t *testing.T) { 161 a := NewAdaptor("localhost:9999", 99999) 162 gobottest.Assert(t, a.On("hola", func(msg Message) { 163 fmt.Println("hola") 164 }), false) 165 }