Use testing.TempDir() where possible

Refactor tests to use go built-in temporary directory utility for tests.

Also avoid binding to default port (which may be in use)
This commit is contained in:
Marco Primi
2022-12-08 11:16:45 -08:00
parent dcd7ffdc4b
commit f8a030bc4a
49 changed files with 599 additions and 2237 deletions

View File

@@ -1138,7 +1138,6 @@ func TestLeafNodeBasicAuth(t *testing.T) {
}
`
conf := createConfFile(t, []byte(content))
defer removeFile(t, conf)
s, opts := RunServerWithConfig(conf)
defer s.Shutdown()
@@ -1205,7 +1204,6 @@ func TestLeafNodeTLS(t *testing.T) {
}
`
conf := createConfFile(t, []byte(content))
defer removeFile(t, conf)
s, opts := RunServerWithConfig(conf)
defer s.Shutdown()
@@ -1244,7 +1242,6 @@ func TestLeafNodeTLSConnCloseEarly(t *testing.T) {
}
`
conf := createConfFile(t, []byte(content))
defer removeFile(t, conf)
s, opts := RunServerWithConfig(conf)
defer s.Shutdown()
@@ -1293,7 +1290,6 @@ func TestLeafNodeTLSMixIP(t *testing.T) {
}
`
conf := createConfFile(t, []byte(content))
defer removeFile(t, conf)
s, opts := RunServerWithConfig(conf)
defer s.Shutdown()
@@ -1311,7 +1307,6 @@ func TestLeafNodeTLSMixIP(t *testing.T) {
}
`
slconf := createConfFile(t, []byte(fmt.Sprintf(slContent, opts.LeafNode.Port, opts.LeafNode.Port)))
defer removeFile(t, slconf)
// This will fail but we want to make sure in the correct way, not with
// TLS issue because we used an IP for serverName.
@@ -1387,8 +1382,7 @@ func runSolicitWithCredentials(t *testing.T, opts *server.Options, creds string)
}
func TestLeafNodeOperatorModel(t *testing.T) {
s, opts, conf := runLeafNodeOperatorServer(t)
defer removeFile(t, conf)
s, opts, _ := runLeafNodeOperatorServer(t)
defer s.Shutdown()
// Make sure we get disconnected without proper credentials etc.
@@ -1415,18 +1409,15 @@ func TestLeafNodeOperatorModel(t *testing.T) {
}
seed, _ := kp.Seed()
mycreds := genCredsFile(t, ujwt, seed)
defer removeFile(t, mycreds)
sl, _, lnconf := runSolicitWithCredentials(t, opts, mycreds)
defer removeFile(t, lnconf)
sl, _, _ := runSolicitWithCredentials(t, opts, mycreds)
defer sl.Shutdown()
checkLeafNodeConnected(t, s)
}
func TestLeafNodeUserPermsForConnection(t *testing.T) {
s, opts, conf := runLeafNodeOperatorServer(t)
defer removeFile(t, conf)
s, opts, _ := runLeafNodeOperatorServer(t)
defer s.Shutdown()
// Setup account and a user that will be used by the remote leaf node server.
@@ -1446,7 +1437,6 @@ func TestLeafNodeUserPermsForConnection(t *testing.T) {
}
seed, _ := kp.Seed()
mycreds := genCredsFile(t, ujwt, seed)
defer removeFile(t, mycreds)
content := `
port: -1
@@ -1463,7 +1453,6 @@ func TestLeafNodeUserPermsForConnection(t *testing.T) {
`
config := fmt.Sprintf(content, opts.LeafNode.Port, mycreds)
lnconf := createConfFile(t, []byte(config))
defer removeFile(t, lnconf)
sl, _ := RunServerWithConfig(lnconf)
defer sl.Shutdown()
@@ -1524,8 +1513,7 @@ func TestLeafNodeUserPermsForConnection(t *testing.T) {
func TestLeafNodeMultipleAccounts(t *testing.T) {
// So we will create a main server with two accounts. The remote server, acting as a leaf node, will simply have
// the $G global account and no auth. Make sure things work properly here.
s, opts, conf := runLeafNodeOperatorServer(t)
defer removeFile(t, conf)
s, opts, _ := runLeafNodeOperatorServer(t)
defer s.Shutdown()
// Setup the two accounts for this server.
@@ -1544,10 +1532,8 @@ func TestLeafNodeMultipleAccounts(t *testing.T) {
// Create the leaf node server using the first account.
seed, _ := kp1.Seed()
mycreds := genCredsFile(t, ujwt1, seed)
defer removeFile(t, mycreds)
sl, lopts, lnconf := runSolicitWithCredentials(t, opts, mycreds)
defer removeFile(t, lnconf)
sl, lopts, _ := runSolicitWithCredentials(t, opts, mycreds)
defer sl.Shutdown()
checkLeafNodeConnected(t, s)
@@ -1736,8 +1722,7 @@ func TestLeafNodeOperatorAndPermissions(t *testing.T) {
}
func TestLeafNodeSignerUser(t *testing.T) {
s, opts, conf := runLeafNodeOperatorServer(t)
defer removeFile(t, conf)
s, opts, _ := runLeafNodeOperatorServer(t)
defer s.Shutdown()
// Setup the two accounts for this server.
@@ -1802,10 +1787,8 @@ func TestLeafNodeSignerUser(t *testing.T) {
// Create the leaf node server using the first account.
seed, _ := kp1.Seed()
mycreds := genCredsFile(t, ujwt1, seed)
defer removeFile(t, mycreds)
sl, _, lnconf := runSolicitWithCredentials(t, opts, mycreds)
defer removeFile(t, lnconf)
sl, _, _ := runSolicitWithCredentials(t, opts, mycreds)
defer sl.Shutdown()
checkLeafNodeConnected(t, s)
@@ -1814,8 +1797,7 @@ func TestLeafNodeSignerUser(t *testing.T) {
func TestLeafNodeExportsImports(t *testing.T) {
// So we will create a main server with two accounts. The remote server, acting as a leaf node, will simply have
// the $G global account and no auth. Make sure things work properly here.
s, opts, conf := runLeafNodeOperatorServer(t)
defer removeFile(t, conf)
s, opts, _ := runLeafNodeOperatorServer(t)
defer s.Shutdown()
// Setup the two accounts for this server.
@@ -1866,10 +1848,8 @@ func TestLeafNodeExportsImports(t *testing.T) {
// Create the leaf node server using the first account.
seed, _ := kp1.Seed()
mycreds := genCredsFile(t, ujwt1, seed)
defer removeFile(t, mycreds)
sl, lopts, lnconf := runSolicitWithCredentials(t, opts, mycreds)
defer removeFile(t, lnconf)
sl, lopts, _ := runSolicitWithCredentials(t, opts, mycreds)
defer sl.Shutdown()
checkLeafNodeConnected(t, s)
@@ -1940,7 +1920,6 @@ func TestLeafNodeExportImportComplexSetup(t *testing.T) {
}
`
conf := createConfFile(t, []byte(content))
defer removeFile(t, conf)
s1, s1Opts := RunServerWithConfig(conf)
defer s1.Shutdown()
@@ -2025,10 +2004,8 @@ func TestLeafNodeExportImportComplexSetup(t *testing.T) {
// Create the leaf node server using the first account.
seed, _ := kp1.Seed()
mycreds := genCredsFile(t, ujwt1, seed)
defer removeFile(t, mycreds)
sl, lopts, lnconf := runSolicitWithCredentials(t, s1Opts, mycreds)
defer removeFile(t, lnconf)
sl, lopts, _ := runSolicitWithCredentials(t, s1Opts, mycreds)
defer sl.Shutdown()
checkLeafNodeConnected(t, s1)
@@ -2361,8 +2338,7 @@ func TestLeafNodeAdvertise(t *testing.T) {
}
func TestLeafNodeConnectionLimitsSingleServer(t *testing.T) {
s, opts, conf := runLeafNodeOperatorServer(t)
defer removeFile(t, conf)
s, opts, _ := runLeafNodeOperatorServer(t)
defer s.Shutdown()
// Setup account and a user that will be used by the remote leaf node server.
@@ -2392,7 +2368,6 @@ func TestLeafNodeConnectionLimitsSingleServer(t *testing.T) {
}
seed, _ := kp.Seed()
mycreds := genCredsFile(t, ujwt, seed)
defer removeFile(t, mycreds)
checkAccConnectionCounts := func(t *testing.T, expected int) {
t.Helper()
@@ -2419,8 +2394,7 @@ func TestLeafNodeConnectionLimitsSingleServer(t *testing.T) {
})
}
sl, _, lnconf := runSolicitWithCredentials(t, opts, mycreds)
defer removeFile(t, lnconf)
sl, _, _ := runSolicitWithCredentials(t, opts, mycreds)
defer sl.Shutdown()
checkLeafNodeConnections(t, s, 1)
@@ -2432,8 +2406,7 @@ func TestLeafNodeConnectionLimitsSingleServer(t *testing.T) {
t.Fatalf("Expected 1 for total connections, got %d", nc)
}
s2, _, lnconf2 := runSolicitWithCredentials(t, opts, mycreds)
defer removeFile(t, lnconf2)
s2, _, _ := runSolicitWithCredentials(t, opts, mycreds)
defer s2.Shutdown()
checkLeafNodeConnections(t, s, 2)
checkAccConnectionCounts(t, 2)
@@ -2450,16 +2423,14 @@ func TestLeafNodeConnectionLimitsSingleServer(t *testing.T) {
}
// Now add back the second one as #3.
s3, _, lnconf3 := runSolicitWithCredentials(t, opts, mycreds)
defer removeFile(t, lnconf3)
s3, _, _ := runSolicitWithCredentials(t, opts, mycreds)
defer s3.Shutdown()
checkLeafNodeConnections(t, s, 2)
checkAccConnectionCounts(t, 2)
// Once we are here we should not be able to create anymore. Limit == 2.
s4, _, lnconf4 := runSolicitWithCredentials(t, opts, mycreds)
defer removeFile(t, lnconf4)
s4, _, _ := runSolicitWithCredentials(t, opts, mycreds)
defer s4.Shutdown()
checkAccConnectionCounts(t, 2)
@@ -2496,7 +2467,6 @@ func TestLeafNodeConnectionLimitsCluster(t *testing.T) {
}
`
conf := createConfFile(t, []byte(content))
defer removeFile(t, conf)
s1, s1Opts := RunServerWithConfig(conf)
defer s1.Shutdown()
@@ -2563,18 +2533,15 @@ func TestLeafNodeConnectionLimitsCluster(t *testing.T) {
}
seed, _ := kp.Seed()
mycreds := genCredsFile(t, ujwt, seed)
defer removeFile(t, mycreds)
loop := maxleafs / 2
// Now create maxleafs/2 leaf node servers on each operator server.
for i := 0; i < loop; i++ {
sl1, _, lnconf1 := runSolicitWithCredentials(t, s1Opts, mycreds)
defer removeFile(t, lnconf1)
sl1, _, _ := runSolicitWithCredentials(t, s1Opts, mycreds)
defer sl1.Shutdown()
sl2, _, lnconf2 := runSolicitWithCredentials(t, s2Opts, mycreds)
defer removeFile(t, lnconf2)
sl2, _, _ := runSolicitWithCredentials(t, s2Opts, mycreds)
defer sl2.Shutdown()
}
@@ -2604,16 +2571,14 @@ func TestLeafNodeConnectionLimitsCluster(t *testing.T) {
checkAccLFCount(acc2, true, loop)
// Now that we are here we should not be allowed anymore leaf nodes.
l, _, lnconf := runSolicitWithCredentials(t, s1Opts, mycreds)
defer removeFile(t, lnconf)
l, _, _ := runSolicitWithCredentials(t, s1Opts, mycreds)
defer l.Shutdown()
checkAccLFCount(acc, false, maxleafs)
// Should still be at loop size.
checkLeafNodeConnections(t, s1, loop)
l, _, lnconf = runSolicitWithCredentials(t, s2Opts, mycreds)
defer removeFile(t, lnconf)
l, _, _ = runSolicitWithCredentials(t, s2Opts, mycreds)
defer l.Shutdown()
checkAccLFCount(acc2, false, maxleafs)
// Should still be at loop size.
@@ -2856,8 +2821,7 @@ func TestLeafNodeServiceImportResponderOnLeaf(t *testing.T) {
}
func TestLeafNodeSendsAccountingEvents(t *testing.T) {
s, opts, conf := runLeafNodeOperatorServer(t)
defer removeFile(t, conf)
s, opts, _ := runLeafNodeOperatorServer(t)
defer s.Shutdown()
// System account
@@ -2892,10 +2856,8 @@ func TestLeafNodeSendsAccountingEvents(t *testing.T) {
}
seed, _ := kp.Seed()
mycreds := genCredsFile(t, ujwt, seed)
defer removeFile(t, mycreds)
sl, _, lnconf := runSolicitWithCredentials(t, opts, mycreds)
defer removeFile(t, lnconf)
sl, _, _ := runSolicitWithCredentials(t, opts, mycreds)
defer sl.Shutdown()
// Wait for connect event
@@ -3113,7 +3075,6 @@ func TestLeafNodeDefaultPort(t *testing.T) {
]
}
`))
defer removeFile(t, conf)
sl, _ := RunServerWithConfig(conf)
defer sl.Shutdown()
@@ -3257,7 +3218,6 @@ func TestLeafNodeMultipleRemoteURLs(t *testing.T) {
config := fmt.Sprintf(content, opts.LeafNode.Port, opts.LeafNode.Port)
conf := createConfFile(t, []byte(config))
sl, _ := RunServerWithConfig(conf)
defer removeFile(t, conf)
defer sl.Shutdown()
checkLeafNodeConnected(t, s)
@@ -3705,7 +3665,6 @@ func TestServiceExportWithMultipleAccounts(t *testing.T) {
listen: "127.0.0.1:-1"
}
`))
defer removeFile(t, confA)
srvA, optsA := RunServerWithConfig(confA)
defer srvA.Shutdown()
@@ -3746,7 +3705,6 @@ func TestServiceExportWithMultipleAccounts(t *testing.T) {
`
confB := createConfFile(t, []byte(fmt.Sprintf(bConfigTemplate, optsA.LeafNode.Port)))
defer removeFile(t, confB)
srvB, optsB := RunServerWithConfig(confB)
defer srvB.Shutdown()
@@ -3826,7 +3784,6 @@ func TestServiceExportWithLeafnodeRestart(t *testing.T) {
}
}
`))
defer removeFile(t, confG)
srvG, optsG := RunServerWithConfig(confG)
defer srvG.Shutdown()
@@ -3877,7 +3834,6 @@ func TestServiceExportWithLeafnodeRestart(t *testing.T) {
`
confE := createConfFile(t, []byte(fmt.Sprintf(eConfigTemplate, optsG.LeafNode.Port)))
defer removeFile(t, confE)
srvE, optsE := RunServerWithConfig(confE)
defer srvE.Shutdown()
@@ -4006,7 +3962,6 @@ func TestLeafNodeOriginClusterSingleHub(t *testing.T) {
leafnodes { remotes = [{ url: nats-leaf://127.0.0.1:%d }] }
`
lconf1 := createConfFile(t, []byte(fmt.Sprintf(c1, opts.LeafNode.Port)))
defer removeFile(t, lconf1)
ln1, lopts1 := RunServerWithConfig(lconf1)
defer ln1.Shutdown()
@@ -4017,7 +3972,6 @@ func TestLeafNodeOriginClusterSingleHub(t *testing.T) {
leafnodes { remotes = [{ url: nats-leaf://127.0.0.1:%d }] }
`
lconf2 := createConfFile(t, []byte(fmt.Sprintf(c2, lopts1.Cluster.Port, opts.LeafNode.Port)))
defer removeFile(t, lconf2)
ln2, _ := RunServerWithConfig(lconf2)
defer ln2.Shutdown()
@@ -4096,7 +4050,6 @@ func TestLeafNodeOriginCluster(t *testing.T) {
leafnodes { remotes = [{ url: nats-leaf://127.0.0.1:%d }] }
`
lconf1 := createConfFile(t, []byte(fmt.Sprintf(c1, ca.opts[0].LeafNode.Port)))
defer removeFile(t, lconf1)
ln1, lopts1 := RunServerWithConfig(lconf1)
defer ln1.Shutdown()
@@ -4108,7 +4061,6 @@ func TestLeafNodeOriginCluster(t *testing.T) {
leafnodes { remotes = [{ url: nats-leaf://127.0.0.1:%d }] }
`
lconf2 := createConfFile(t, []byte(fmt.Sprintf(c2, lopts1.Cluster.Port, ca.opts[1].LeafNode.Port)))
defer removeFile(t, lconf2)
ln2, _ := RunServerWithConfig(lconf2)
defer ln2.Shutdown()
@@ -4120,7 +4072,6 @@ func TestLeafNodeOriginCluster(t *testing.T) {
leafnodes { remotes = [{ url: nats-leaf://127.0.0.1:%d }] }
`
lconf3 := createConfFile(t, []byte(fmt.Sprintf(c3, lopts1.Cluster.Port, ca.opts[2].LeafNode.Port)))
defer removeFile(t, lconf3)
ln3, _ := RunServerWithConfig(lconf3)
defer ln3.Shutdown()
@@ -4325,7 +4276,6 @@ func TestLeafNodeAndGatewaysStreamAndShadowSubs(t *testing.T) {
}
}
`))
defer removeFile(t, conf1)
s1, o1 := RunServerWithConfig(conf1)
defer s1.Shutdown()
@@ -4354,7 +4304,6 @@ func TestLeafNodeAndGatewaysStreamAndShadowSubs(t *testing.T) {
]
}
`, o1.Gateway.Port)))
defer removeFile(t, conf2)
s2, o2 := RunServerWithConfig(conf2)
defer s2.Shutdown()
@@ -4395,7 +4344,6 @@ func TestLeafNodeAndGatewaysStreamAndShadowSubs(t *testing.T) {
]
}
`, o1.LeafNode.Port)))
defer removeFile(t, conf3)
s3, o3 := RunServerWithConfig(conf3)
defer s3.Shutdown()