From 146d8f5dcbe1f950069fc7c781ff1774df0cb20b Mon Sep 17 00:00:00 2001 From: Derek Collison Date: Fri, 12 Jun 2020 17:26:43 -0700 Subject: [PATCH] Updates based on feedback, sped up some slow tests Signed-off-by: Derek Collison --- server/events_test.go | 3 +++ server/gateway_test.go | 1 + server/leafnode_test.go | 4 ++++ server/monitor_test.go | 9 +++++++++ server/opts.go | 10 ---------- server/opts_test.go | 24 ++++++++++++++++++++++++ server/route.go | 12 ++++++++---- server/routes_test.go | 3 ++- server/server.go | 7 +++++++ server/server_test.go | 5 ++++- test/cluster_test.go | 19 ------------------- test/leafnode_test.go | 7 +++++++ test/monitor_test.go | 4 ++-- test/operator_test.go | 4 ++++ test/opts_test.go | 4 +++- test/routes_test.go | 1 + test/service_latency_test.go | 1 + 17 files changed, 80 insertions(+), 38 deletions(-) diff --git a/server/events_test.go b/server/events_test.go index 4f6e0a6f..774b2f1b 100644 --- a/server/events_test.go +++ b/server/events_test.go @@ -95,6 +95,7 @@ func runTrustedCluster(t *testing.T) (*Server, *Options, *Server, *Options, nkey mr.Store(apub, jwt) optsA := DefaultOptions() + optsA.Cluster.Name = "TEST CLUSTER 22" optsA.Cluster.Host = "127.0.0.1" optsA.TrustedKeys = []string{pub} optsA.AccountResolver = mr @@ -138,6 +139,7 @@ func runTrustedGateways(t *testing.T) (*Server, *Options, *Server, *Options, nke mr.Store(apub, jwt) optsA := testDefaultOptionsForGateway("A") + optsA.Cluster.Name = "A" optsA.Cluster.Host = "127.0.0.1" optsA.TrustedKeys = []string{pub} optsA.AccountResolver = mr @@ -146,6 +148,7 @@ func runTrustedGateways(t *testing.T) (*Server, *Options, *Server, *Options, nke sa := RunServer(optsA) optsB := testGatewayOptionsFromToWithServers(t, "B", "A", sa) + optsB.Cluster.Name = "B" optsB.TrustedKeys = []string{pub} optsB.AccountResolver = mr optsB.SystemAccount = apub diff --git a/server/gateway_test.go b/server/gateway_test.go index 1d67e75e..6f002945 100644 --- a/server/gateway_test.go +++ b/server/gateway_test.go @@ -240,6 +240,7 @@ func testDefaultOptionsForGateway(name string) *Options { o := DefaultOptions() o.NoSystemAccount = true o.ServerName = name + o.Cluster.Name = name o.Gateway.Name = name o.Gateway.Host = "127.0.0.1" o.Gateway.Port = -1 diff --git a/server/leafnode_test.go b/server/leafnode_test.go index b5265e36..d411d319 100644 --- a/server/leafnode_test.go +++ b/server/leafnode_test.go @@ -346,6 +346,7 @@ func TestLeafNodeBasicAuthFailover(t *testing.T) { content := ` listen: "127.0.0.1:-1" cluster { + name: "abc" listen: "127.0.0.1:-1" %s } @@ -948,6 +949,7 @@ func TestLeafNodeRemoteWrongPort(t *testing.T) { // Make sure we have all ports (client, route, gateway) and we will try // to create a leafnode to connection to each and make sure we get the error. oa.Cluster.NoAdvertise = test1.clusterAdvertise + oa.Cluster.Name = "A" oa.Cluster.Host = "127.0.0.1" oa.Cluster.Port = -1 oa.Gateway.Host = "127.0.0.1" @@ -963,6 +965,7 @@ func TestLeafNodeRemoteWrongPort(t *testing.T) { ob := DefaultOptions() ob.Cluster.NoAdvertise = test1.clusterAdvertise + ob.Cluster.Name = "A" ob.Cluster.Host = "127.0.0.1" ob.Cluster.Port = -1 ob.Routes = RoutesFromStr(fmt.Sprintf("nats://%s:%d", oa.Cluster.Host, oa.Cluster.Port)) @@ -1271,6 +1274,7 @@ func TestLeafNodeExportPermissionsNotForSpecialSubs(t *testing.T) { lo1 := DefaultOptions() lo1.Accounts = []*Account{NewAccount("SYS")} lo1.SystemAccount = "SYS" + lo1.Cluster.Name = "A" lo1.Gateway.Name = "A" lo1.Gateway.Port = -1 lo1.LeafNode.Host = "127.0.0.1" diff --git a/server/monitor_test.go b/server/monitor_test.go index 0393c084..6d0cc10b 100644 --- a/server/monitor_test.go +++ b/server/monitor_test.go @@ -1267,6 +1267,7 @@ func TestConnzWithRoutes(t *testing.T) { resetPreviousHTTPConnections() opts := DefaultMonitorOptions() opts.NoSystemAccount = true + opts.Cluster.Name = "A" opts.Cluster.Host = "127.0.0.1" opts.Cluster.Port = CLUSTER_PORT @@ -1277,6 +1278,7 @@ func TestConnzWithRoutes(t *testing.T) { Host: "127.0.0.1", Port: -1, Cluster: ClusterOpts{ + Name: "A", Host: "127.0.0.1", Port: -1, }, @@ -2003,6 +2005,7 @@ func TestMonitorRoutezRace(t *testing.T) { resetPreviousHTTPConnections() srvAOpts := DefaultMonitorOptions() srvAOpts.NoSystemAccount = true + srvAOpts.Cluster.Name = "B" srvAOpts.Cluster.Port = -1 srvA := RunServer(srvAOpts) defer srvA.Shutdown() @@ -2155,6 +2158,7 @@ func TestRoutezPermissions(t *testing.T) { resetPreviousHTTPConnections() opts := DefaultMonitorOptions() opts.NoSystemAccount = true + opts.Cluster.Name = "A" opts.Cluster.Host = "127.0.0.1" opts.Cluster.Port = -1 opts.Cluster.Permissions = &RoutePermissions{ @@ -2172,6 +2176,7 @@ func TestRoutezPermissions(t *testing.T) { opts = DefaultMonitorOptions() opts.Cluster.Host = "127.0.0.1" + opts.Cluster.Name = "A" opts.Cluster.Port = -1 routeURL, _ := url.Parse(fmt.Sprintf("nats-route://127.0.0.1:%d", s1.ClusterAddr().Port)) opts.Routes = []*url.URL{routeURL} @@ -2402,6 +2407,7 @@ func TestMonitorCluster(t *testing.T) { resetPreviousHTTPConnections() opts := DefaultMonitorOptions() opts.NoSystemAccount = true + opts.Cluster.Name = "A" opts.Cluster.Port = -1 opts.Cluster.AuthTimeout = 1 opts.Routes = RoutesFromStr("nats://127.0.0.1:1234") @@ -2447,6 +2453,8 @@ func TestMonitorClusterURLs(t *testing.T) { o2 := DefaultOptions() o2.Cluster.Host = "127.0.0.1" + o2.Cluster.Name = "A" + s2 := RunServer(o2) defer s2.Shutdown() @@ -2456,6 +2464,7 @@ func TestMonitorClusterURLs(t *testing.T) { port: -1 http: -1 cluster: { + name: "A" port: -1 routes [ %s diff --git a/server/opts.go b/server/opts.go index 9fc5347c..88a63a76 100644 --- a/server/opts.go +++ b/server/opts.go @@ -1012,11 +1012,6 @@ func parseCluster(v interface{}, opts *Options, errors *[]error, warnings *[]err switch strings.ToLower(mk) { case "name": opts.Cluster.Name = mv.(string) - // Check to see if a cluster name has been defined in gateway section and if so and they do not match err. - if opts.Gateway.Name != "" && opts.Cluster.Name != opts.Gateway.Name { - *errors = append(*errors, ErrClusterNameConfigConflict) - continue - } case "listen": hp, err := parseListen(mv) if err != nil { @@ -1157,11 +1152,6 @@ func parseGateway(v interface{}, o *Options, errors *[]error, warnings *[]error) switch strings.ToLower(mk) { case "name": o.Gateway.Name = mv.(string) - // Check to see if a cluster name has been defined. And if so and they do not match err. - if o.Cluster.Name != "" && o.Cluster.Name != o.Gateway.Name { - *errors = append(*errors, ErrClusterNameConfigConflict) - continue - } case "listen": hp, err := parseListen(mv) if err != nil { diff --git a/server/opts_test.go b/server/opts_test.go index 0e083f83..bb348f9b 100644 --- a/server/opts_test.go +++ b/server/opts_test.go @@ -2760,3 +2760,27 @@ func TestReadOperatorAssertVersionFail(t *testing.T) { t.Fatal("expected different error got: ", err) } } + +func TestClusterNameAndGatewayNameConflict(t *testing.T) { + conf := createConfFile(t, []byte(` + listen: 127.0.0.1:-1 + cluster { + name: A + listen: 127.0.0.1:-1 + } + gateway { + name: B + listen: 127.0.0.1:-1 + } + `)) + defer os.Remove(conf) + + opts, err := ProcessConfigFile(conf) + if err != nil { + t.Fatalf("Unexpected error: %v", err) + } + + if err := validateOptions(opts); err != ErrClusterNameConfigConflict { + t.Fatalf("Expected ErrClusterNameConfigConflict got %v", err) + } +} diff --git a/server/route.go b/server/route.go index a9dcc964..c25ed4e7 100644 --- a/server/route.go +++ b/server/route.go @@ -359,6 +359,7 @@ func (c *client) sendRouteConnect(tlsRequired bool) { user = userInfo.Username() pass, _ = userInfo.Password() } + s := c.srv cinfo := connectInfo{ Echo: true, Verbose: false, @@ -366,10 +367,10 @@ func (c *client) sendRouteConnect(tlsRequired bool) { User: user, Pass: pass, TLS: tlsRequired, - Name: c.srv.info.ID, - Headers: c.srv.supportsHeaders(), - Cluster: c.srv.ClusterName(), - Dynamic: c.srv.getOpts().Cluster.Name == "", + Name: s.info.ID, + Headers: s.supportsHeaders(), + Cluster: s.info.Cluster, + Dynamic: s.getOpts().Cluster.Name == "", } b, err := json.Marshal(cinfo) @@ -1784,6 +1785,9 @@ func (c *client) processRouteConnect(srv *Server, arg []byte, lang string) error if !proto.Dynamic || strings.Compare(clusterName, proto.Cluster) < 0 { // We will take on their name since theirs is configured or higher then ours. srv.setClusterName(proto.Cluster) + if !proto.Dynamic { + srv.getOpts().Cluster.Name = proto.Cluster + } srv.removeAllRoutesExcept(c) shouldReject = false } diff --git a/server/routes_test.go b/server/routes_test.go index 226db20e..d7b7cc06 100644 --- a/server/routes_test.go +++ b/server/routes_test.go @@ -1205,6 +1205,7 @@ func TestRouteRTT(t *testing.T) { func TestRouteCloseTLSConnection(t *testing.T) { opts := DefaultOptions() opts.DisableShortFirstPing = true + opts.Cluster.Name = "A" opts.Cluster.Host = "127.0.0.1" opts.Cluster.Port = -1 opts.Cluster.TLSTimeout = 100 @@ -1235,7 +1236,7 @@ func TestRouteCloseTLSConnection(t *testing.T) { if err := tlsConn.Handshake(); err != nil { t.Fatalf("Unexpected error during handshake: %v", err) } - connectOp := []byte("CONNECT {\"name\":\"route\",\"verbose\":false,\"pedantic\":false,\"tls_required\":true}\r\n") + connectOp := []byte("CONNECT {\"name\":\"route\",\"verbose\":false,\"pedantic\":false,\"tls_required\":true,\"cluster\":\"A\"}\r\n") if _, err := tlsConn.Write(connectOp); err != nil { t.Fatalf("Unexpected error writing CONNECT: %v", err) } diff --git a/server/server.go b/server/server.go index 49f231ef..a62de5f0 100644 --- a/server/server.go +++ b/server/server.go @@ -480,6 +480,13 @@ func validateOptions(o *Options) error { if err := validateGatewayOptions(o); err != nil { return err } + // Check that cluster name if defined matches any gateway name. + if o.Gateway.Name != "" && o.Gateway.Name != o.Cluster.Name { + if o.Cluster.Name != "" { + return ErrClusterNameConfigConflict + } + o.Cluster.Name = o.Gateway.Name + } return validateWebsocketOptions(o) } diff --git a/server/server_test.go b/server/server_test.go index a3e81912..1be7607f 100644 --- a/server/server_test.go +++ b/server/server_test.go @@ -58,7 +58,7 @@ func DefaultOptions() *Options { Host: "127.0.0.1", Port: -1, HTTPPort: -1, - Cluster: ClusterOpts{Port: -1}, + Cluster: ClusterOpts{Port: -1, Name: "abc"}, NoLog: true, NoSigs: true, Debug: true, @@ -1521,6 +1521,7 @@ func TestConnectErrorReports(t *testing.T) { // Now try with gateways opts.LeafNode.Remotes = nil + opts.Cluster.Name = "A" opts.Gateway.Name = "A" opts.Gateway.Port = -1 opts.Gateway.Gateways = []*RemoteGatewayOpts{ @@ -1700,11 +1701,13 @@ func TestReconnectErrorReports(t *testing.T) { // Now try with gateways csOpts.LeafNode.Port = 0 + csOpts.Cluster.Name = "B" csOpts.Gateway.Name = "B" csOpts.Gateway.Port = -1 cs = RunServer(csOpts) opts.LeafNode.Remotes = nil + opts.Cluster.Name = "A" opts.Gateway.Name = "A" opts.Gateway.Port = -1 remoteGWPort := cs.GatewayAddr().Port diff --git a/test/cluster_test.go b/test/cluster_test.go index 0f98bf64..1a0dd7b8 100644 --- a/test/cluster_test.go +++ b/test/cluster_test.go @@ -541,25 +541,6 @@ func TestClusterNameOption(t *testing.T) { } } -func TestClusterNameAndGatewayName(t *testing.T) { - conf := createConfFile(t, []byte(` - listen: 127.0.0.1:-1 - cluster { - name: A - listen: 127.0.0.1:-1 - } - gateway { - name: B - listen: 127.0.0.1:-1 - } - `)) - defer os.Remove(conf) - - if _, err := server.ProcessConfigFile(conf); err == nil || !strings.Contains(err.Error(), "cluster name conflicts") { - t.Fatalf("Expected an error with conflicting cluster names") - } -} - func TestEphemeralClusterName(t *testing.T) { conf := createConfFile(t, []byte(` listen: 127.0.0.1:-1 diff --git a/test/leafnode_test.go b/test/leafnode_test.go index c55f73bc..58305752 100644 --- a/test/leafnode_test.go +++ b/test/leafnode_test.go @@ -1813,6 +1813,7 @@ func TestLeafNodeInfoURLs(t *testing.T) { } { t.Run(test.name, func(t *testing.T) { opts := testDefaultOptionsForLeafNodes() + opts.Cluster.Name = "A" opts.Cluster.Port = -1 opts.LeafNode.Host = "127.0.0.1" if test.useAdvertise { @@ -1839,6 +1840,7 @@ func TestLeafNodeInfoURLs(t *testing.T) { lc.Close() opts2 := testDefaultOptionsForLeafNodes() + opts2.Cluster.Name = "A" opts2.Cluster.Port = -1 opts2.Routes = server.RoutesFromStr(fmt.Sprintf("nats://%s:%d", opts.Cluster.Host, opts.Cluster.Port)) opts2.LeafNode.Host = "127.0.0.1" @@ -2024,11 +2026,13 @@ func TestLeafNodeAdvertise(t *testing.T) { o2 := testDefaultOptionsForLeafNodes() o2.LeafNode.Advertise = fmt.Sprintf("127.0.0.1:%d", port) + o2.Cluster.Name = "A" o2.Cluster.Port = -1 s2 := RunServer(o2) defer s2.Shutdown() o1 := testDefaultOptionsForLeafNodes() + o1.Cluster.Name = "A" o1.Cluster.Port = -1 o1.Routes = server.RoutesFromStr(fmt.Sprintf("nats://127.0.0.1:%d", o2.Cluster.Port)) s1 := RunServer(o1) @@ -2964,6 +2968,7 @@ func runSolicitLeafCluster(t *testing.T, clusterName string, d1, d2 *cluster) *c rurl, _ := url.Parse(surl) o.LeafNode.Remotes = []*server.RemoteLeafOpts{{URLs: []*url.URL{rurl}}} o.LeafNode.ReconnectInterval = 100 * time.Millisecond + o.Cluster.Name = clusterName o.Cluster.Host = o.Host o.Cluster.Port = -1 s := RunServer(&o) @@ -2989,6 +2994,7 @@ func runSolicitLeafCluster(t *testing.T, clusterName string, d1, d2 *cluster) *c rurl, _ = url.Parse(surl) o2.LeafNode.Remotes = []*server.RemoteLeafOpts{{URLs: []*url.URL{rurl}}} o2.LeafNode.ReconnectInterval = 100 * time.Millisecond + o2.Cluster.Name = clusterName o2.Cluster.Host = o.Host o2.Cluster.Port = -1 o2.Routes = []*url.URL{curl} @@ -3107,6 +3113,7 @@ func TestLeafNodeCycleWithSolicited(t *testing.T) { func TestLeafNodeNoRaceGeneratingNonce(t *testing.T) { opts := testDefaultOptionsForLeafNodes() opts.Cluster.Port = -1 + opts.Cluster.Name = "xyz" s := RunServer(opts) defer s.Shutdown() diff --git a/test/monitor_test.go b/test/monitor_test.go index 9007aa99..d103b68c 100644 --- a/test/monitor_test.go +++ b/test/monitor_test.go @@ -51,7 +51,7 @@ func runMonitorServerClusteredPair(t *testing.T) (*server.Server, *server.Server opts.Port = CLIENT_PORT opts.HTTPPort = MONITOR_PORT opts.HTTPHost = "127.0.0.1" - opts.Cluster = server.ClusterOpts{Host: "127.0.0.1", Port: 10223} + opts.Cluster = server.ClusterOpts{Name: "M", Host: "127.0.0.1", Port: 10223} opts.Routes = server.RoutesFromStr("nats-route://127.0.0.1:10222") opts.NoSystemAccount = true @@ -61,7 +61,7 @@ func runMonitorServerClusteredPair(t *testing.T) (*server.Server, *server.Server opts2.Port = CLIENT_PORT + 1 opts2.HTTPPort = MONITOR_PORT + 1 opts2.HTTPHost = "127.0.0.1" - opts2.Cluster = server.ClusterOpts{Host: "127.0.0.1", Port: 10222} + opts2.Cluster = server.ClusterOpts{Name: "M", Host: "127.0.0.1", Port: 10222} opts2.Routes = server.RoutesFromStr("nats-route://127.0.0.1:10223") opts2.NoSystemAccount = true diff --git a/test/operator_test.go b/test/operator_test.go index 47d549d3..2ee585c3 100644 --- a/test/operator_test.go +++ b/test/operator_test.go @@ -333,6 +333,7 @@ func TestReloadDoesNotWipeAccountsWithOperatorMode(t *testing.T) { cf := ` listen: 127.0.0.1:-1 cluster { + name: "A" listen: 127.0.0.1:-1 authorization { timeout: 2.2 @@ -456,6 +457,7 @@ func TestReloadDoesUpdateAccountsWithMemoryResolver(t *testing.T) { cf := ` listen: 127.0.0.1:-1 cluster { + name: "A" listen: 127.0.0.1:-1 authorization { timeout: 2.2 @@ -547,6 +549,7 @@ func TestReloadFailsWithBadAccountsWithMemoryResolver(t *testing.T) { cf := ` listen: 127.0.0.1:-1 cluster { + name: "A" listen: 127.0.0.1:-1 authorization { timeout: 2.2 @@ -610,6 +613,7 @@ func TestConnsRequestDoesNotLoadAccountCheckingConnLimits(t *testing.T) { cf := ` listen: 127.0.0.1:-1 cluster { + name: "A" listen: 127.0.0.1:-1 authorization { timeout: 2.2 diff --git a/test/opts_test.go b/test/opts_test.go index 0275989e..ae7f69e0 100644 --- a/test/opts_test.go +++ b/test/opts_test.go @@ -13,7 +13,9 @@ package test -import "testing" +import ( + "testing" +) func TestServerConfig(t *testing.T) { srv, opts := RunServerWithConfig("./configs/override.conf") diff --git a/test/routes_test.go b/test/routes_test.go index a7c96478..b4d8816c 100644 --- a/test/routes_test.go +++ b/test/routes_test.go @@ -1123,6 +1123,7 @@ func TestRoutesOnlyImportOrExport(t *testing.T) { cf := createConfFile(t, []byte(fmt.Sprintf(` port: -1 cluster { + name: "Z" port: -1 authorization { user: ivan diff --git a/test/service_latency_test.go b/test/service_latency_test.go index e19917e3..831056be 100644 --- a/test/service_latency_test.go +++ b/test/service_latency_test.go @@ -719,6 +719,7 @@ func TestServiceLatencyWithJWT(t *testing.T) { cf := ` listen: 127.0.0.1:-1 cluster { + name: "A" listen: 127.0.0.1:-1 authorization { timeout: 2.2