Files
netmaker/controllers/node_test.go
T
Abhishek Kondur c3c3ed1fb8 NM-254: add bulk delete apis, rm old acl code (#3937)
* NM-254: add bulk delete apis for users, hosts, nodes and optimise postgres connection settings

* NM-254: rm debug logs

* NM-254: add bulk delete apis, remove old acl code

* NM-254: rm unused flag

* NM-254: fix bulk delete bugs, add security and performance improvements

- Fix host delete notifying peers before confirming deletion from DB
- Fix self-delete vulnerability in bulk user delete
- Fix DissasociateNodeFromHost failing when host.Nodes is empty
- Fix AssociateNodeToHost/DissasociateNodeFromHost stale read race
- Hoist GetAllExtClients outside loop in bulk user delete/status
- Move initializeUUID outside master-pod guard for HA correctness

* NM-254: return 202 Accepted for async bulk APIs, fix relay allowedIPs and host association error handling

- Change all bulk endpoints (hosts, nodes, users, ext clients) from
  200 OK to 202 Accepted to correctly signal async processing
- Add ReturnAcceptedResponse helper in logic/errors.go
- Fix GetAllowedIpsForRelayed returning empty allowedIPs slice,
  restoring relay connectivity
- Make AssociateNodeToHost and DissasociateNodeFromHost return an
  error when the host DB re-fetch fails instead of silently using
  stale data
- Add bulk-apis.md documenting all five bulk endpoints

* NM-254: rm coredns container

* NM-254: add bulk apis for node,extclient status, add activity logs to bulk apis

* NM-254: add bulk api for connection toggle

* NM-254: add network check

* Update controllers/hosts.go

Co-authored-by: tenki-reviewer[bot] <262613592+tenki-reviewer[bot]@users.noreply.github.com>

* NM-254: optimise bulk extclient deletion

---------

Co-authored-by: tenki-reviewer[bot] <262613592+tenki-reviewer[bot]@users.noreply.github.com>
2026-03-26 10:15:07 +05:30

106 lines
2.4 KiB
Go

package controller
import (
"net"
"testing"
"github.com/google/uuid"
"github.com/gravitl/netmaker/database"
"github.com/gravitl/netmaker/logic"
"github.com/gravitl/netmaker/models"
"github.com/gravitl/netmaker/schema"
"github.com/gravitl/netmaker/servercfg"
"github.com/stretchr/testify/assert"
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
)
var nonLinuxHost schema.Host
var linuxHost schema.Host
func TestGetNetworkNodes(t *testing.T) {
deleteAllNetworks()
createNet()
t.Run("BadNet", func(t *testing.T) {
node, err := logic.GetNetworkNodes("badnet")
assert.Nil(t, err)
assert.Equal(t, []models.Node{}, node)
})
t.Run("NoNodes", func(t *testing.T) {
node, err := logic.GetNetworkNodes("skynet")
assert.Nil(t, err)
assert.Equal(t, []models.Node{}, node)
})
t.Run("Success", func(t *testing.T) {
createTestNode()
node, err := logic.GetNetworkNodes("skynet")
assert.Nil(t, err)
assert.NotEqual(t, []models.LegacyNode(nil), node)
})
}
func TestValidateEgressGateway(t *testing.T) {
var gateway models.EgressGatewayRequest
t.Run("Success", func(t *testing.T) {
gateway.Ranges = []string{"10.100.100.0/24"}
err := logic.ValidateEgressGateway(gateway)
assert.Nil(t, err)
})
}
func deleteAllNodes() {
if servercfg.CacheEnabled() {
logic.ClearNodeCache()
}
database.DeleteAllRecords(database.NODES_TABLE_NAME)
}
func createTestNode() *models.Node {
createNodeHosts()
n := createNodeWithParams("skynet", "")
_ = logic.AssociateNodeToHost(n, &linuxHost)
return n
}
func createNodeWithParams(network, address string) *models.Node {
_, ipnet, _ := net.ParseCIDR("10.0.0.1/32")
tmpCNode := models.CommonNode{
ID: uuid.New(),
Network: "skynet",
Address: *ipnet,
}
if len(network) > 0 {
tmpCNode.Network = network
}
if len(address) > 0 {
_, ipnet2, _ := net.ParseCIDR(address)
tmpCNode.Address = *ipnet2
}
createnode := models.Node{
CommonNode: tmpCNode,
}
return &createnode
}
func createNodeHosts() {
k, _ := wgtypes.ParseKey("DM5qhLAE20PG9BbfBCger+Ac9D2NDOwCtY1rbYDLf34=")
linuxHost = schema.Host{
ID: uuid.New(),
PublicKey: schema.WgKey{Key: k.PublicKey()},
HostPass: "password",
OS: "linux",
Name: "linuxhost",
}
_ = logic.CreateHost(&linuxHost)
nonLinuxHost = schema.Host{
ID: uuid.New(),
OS: "windows",
PublicKey: schema.WgKey{Key: k.PublicKey()},
Name: "windowshost",
HostPass: "password",
}
_ = logic.CreateHost(&nonLinuxHost)
}