netmaker/controllers/node_test.go

209 lines
7.7 KiB
Go
Raw Normal View History

2021-05-10 23:44:19 +08:00
package controller
import (
2021-09-10 01:59:43 +08:00
"testing"
"github.com/gravitl/netmaker/database"
"github.com/gravitl/netmaker/logic"
2022-02-26 00:06:03 +08:00
"github.com/gravitl/netmaker/logic/acls"
2022-02-25 02:48:35 +08:00
nodeacls "github.com/gravitl/netmaker/logic/acls/node-acls"
2021-05-10 23:44:19 +08:00
"github.com/gravitl/netmaker/models"
2021-09-10 01:59:43 +08:00
"github.com/stretchr/testify/assert"
2021-05-10 23:44:19 +08:00
)
2021-09-10 01:59:43 +08:00
func TestCreateEgressGateway(t *testing.T) {
var gateway models.EgressGatewayRequest
gateway.Interface = "eth0"
gateway.Ranges = []string{"10.100.100.0/24"}
gateway.NetID = "skynet"
2021-09-10 01:59:43 +08:00
database.InitializeDatabase()
deleteAllNetworks()
createNet()
t.Run("NoNodes", func(t *testing.T) {
2021-12-08 01:46:55 +08:00
node, err := logic.CreateEgressGateway(gateway)
2021-09-10 01:59:43 +08:00
assert.Equal(t, models.Node{}, node)
2022-01-11 08:43:20 +08:00
assert.EqualError(t, err, "could not find any records")
2021-09-10 01:59:43 +08:00
})
t.Run("Non-linux node", func(t *testing.T) {
createnode := models.Node{PublicKey: "DM5qhLAE20PG9BbfBCger+Ac9D2NDOwCtY1rbYDLf34=", Name: "testnode", Endpoint: "10.0.0.1", MacAddress: "01:02:03:04:05:06", Password: "password", Network: "skynet", OS: "freebsd"}
err := logic.CreateNode(&createnode)
assert.Nil(t, err)
gateway.NodeID = createnode.ID
node, err := logic.CreateEgressGateway(gateway)
assert.Equal(t, models.Node{}, node)
assert.EqualError(t, err, "freebsd is unsupported for egress gateways")
})
2021-09-10 01:59:43 +08:00
t.Run("Success", func(t *testing.T) {
2022-02-06 04:33:57 +08:00
deleteAllNodes()
2021-09-10 01:59:43 +08:00
testnode := createTestNode()
2022-01-11 08:43:20 +08:00
gateway.NodeID = testnode.ID
2021-09-10 01:59:43 +08:00
2021-12-08 01:46:55 +08:00
node, err := logic.CreateEgressGateway(gateway)
2022-02-06 04:33:57 +08:00
t.Log(node)
2021-09-10 01:59:43 +08:00
assert.Nil(t, err)
assert.Equal(t, "yes", node.IsEgressGateway)
assert.Equal(t, gateway.Ranges, node.EgressGatewayRanges)
})
}
func TestDeleteEgressGateway(t *testing.T) {
var gateway models.EgressGatewayRequest
database.InitializeDatabase()
deleteAllNetworks()
createNet()
testnode := createTestNode()
gateway.Interface = "eth0"
gateway.Ranges = []string{"10.100.100.0/24"}
gateway.NetID = "skynet"
2022-01-11 08:43:20 +08:00
gateway.NodeID = testnode.ID
2021-09-10 01:59:43 +08:00
t.Run("Success", func(t *testing.T) {
2021-12-08 01:46:55 +08:00
node, err := logic.CreateEgressGateway(gateway)
2021-09-10 01:59:43 +08:00
assert.Nil(t, err)
assert.Equal(t, "yes", node.IsEgressGateway)
assert.Equal(t, []string{"10.100.100.0/24"}, node.EgressGatewayRanges)
2021-12-08 01:46:55 +08:00
node, err = logic.DeleteEgressGateway(gateway.NetID, gateway.NodeID)
2021-09-10 01:59:43 +08:00
assert.Nil(t, err)
assert.Equal(t, "no", node.IsEgressGateway)
assert.Equal(t, []string([]string{}), node.EgressGatewayRanges)
assert.Equal(t, "", node.PostUp)
assert.Equal(t, "", node.PostDown)
})
t.Run("NotGateway", func(t *testing.T) {
2021-12-08 01:46:55 +08:00
node, err := logic.DeleteEgressGateway(gateway.NetID, gateway.NodeID)
2021-09-10 01:59:43 +08:00
assert.Nil(t, err)
assert.Equal(t, "no", node.IsEgressGateway)
assert.Equal(t, []string([]string{}), node.EgressGatewayRanges)
assert.Equal(t, "", node.PostUp)
assert.Equal(t, "", node.PostDown)
})
t.Run("BadNode", func(t *testing.T) {
2021-12-08 01:46:55 +08:00
node, err := logic.DeleteEgressGateway(gateway.NetID, "01:02:03")
2021-09-10 01:59:43 +08:00
assert.EqualError(t, err, "no result found")
assert.Equal(t, models.Node{}, node)
2022-01-11 09:50:18 +08:00
deleteAllNodes()
2021-09-10 01:59:43 +08:00
})
}
func TestGetNetworkNodes(t *testing.T) {
database.InitializeDatabase()
deleteAllNetworks()
createNet()
t.Run("BadNet", func(t *testing.T) {
node, err := logic.GetNetworkNodes("badnet")
2021-09-10 01:59:43 +08:00
assert.Nil(t, err)
2022-01-23 00:38:56 +08:00
assert.Nil(t, node)
2021-09-10 01:59:43 +08:00
})
t.Run("NoNodes", func(t *testing.T) {
node, err := logic.GetNetworkNodes("skynet")
2021-09-10 01:59:43 +08:00
assert.Nil(t, err)
2022-01-23 00:38:56 +08:00
assert.Nil(t, node)
2021-09-10 01:59:43 +08:00
})
t.Run("Success", func(t *testing.T) {
createTestNode()
node, err := logic.GetNetworkNodes("skynet")
2021-09-10 01:59:43 +08:00
assert.Nil(t, err)
assert.NotEqual(t, []models.Node(nil), node)
})
}
func TestUncordonNode(t *testing.T) {
database.InitializeDatabase()
deleteAllNetworks()
createNet()
node := createTestNode()
2022-01-11 06:52:21 +08:00
t.Run("BadID", func(t *testing.T) {
resp, err := logic.UncordonNode("blahblah")
2021-09-10 01:59:43 +08:00
assert.Equal(t, models.Node{}, resp)
assert.EqualError(t, err, "no result found")
})
t.Run("Success", func(t *testing.T) {
2022-01-11 06:52:21 +08:00
resp, err := logic.UncordonNode(node.ID)
2021-09-10 01:59:43 +08:00
assert.Nil(t, err)
assert.Equal(t, "no", resp.IsPending)
})
}
func TestValidateEgressGateway(t *testing.T) {
var gateway models.EgressGatewayRequest
t.Run("EmptyRange", func(t *testing.T) {
gateway.Interface = "eth0"
gateway.Ranges = []string{}
2021-12-08 01:46:55 +08:00
err := logic.ValidateEgressGateway(gateway)
2021-09-10 01:59:43 +08:00
assert.EqualError(t, err, "IP Ranges Cannot Be Empty")
})
t.Run("EmptyInterface", func(t *testing.T) {
gateway.Interface = ""
2021-12-08 01:46:55 +08:00
err := logic.ValidateEgressGateway(gateway)
2021-09-10 01:59:43 +08:00
assert.NotNil(t, err)
2022-02-09 00:05:22 +08:00
assert.Equal(t, "interface cannot be empty", err.Error())
2021-09-10 01:59:43 +08:00
})
t.Run("Success", func(t *testing.T) {
gateway.Interface = "eth0"
gateway.Ranges = []string{"10.100.100.0/24"}
2021-12-08 01:46:55 +08:00
err := logic.ValidateEgressGateway(gateway)
2021-09-10 01:59:43 +08:00
assert.Nil(t, err)
})
}
2022-02-25 02:48:35 +08:00
func TestNodeACLs(t *testing.T) {
deleteAllNodes()
node1 := models.Node{PublicKey: "DM5qhLAE20PG9BbfBCger+Ac9D2NDOwCtY1rbYDLf34=", Name: "testnode", Endpoint: "10.0.0.50", MacAddress: "01:02:03:04:05:06", Password: "password", Network: "skynet", OS: "linux"}
node2 := models.Node{PublicKey: "DM5qhLAE20FG7BbfBCger+Ac9D2NDOwCtY1rbYDXf14=", Name: "testnode", Endpoint: "10.0.0.100", MacAddress: "01:02:03:04:05:07", Password: "password", Network: "skynet", OS: "linux"}
logic.CreateNode(&node1)
logic.CreateNode(&node2)
t.Run("acls not present", func(t *testing.T) {
2022-02-26 03:08:44 +08:00
currentACL, err := nodeacls.FetchAllACLs(nodeacls.NetworkID(node1.Network))
2022-02-25 02:48:35 +08:00
assert.Nil(t, err)
2022-02-26 00:06:03 +08:00
assert.Nil(t, currentACL[acls.AclID(node1.ID)])
assert.Nil(t, currentACL[acls.AclID(node2.ID)])
2022-02-26 03:08:44 +08:00
node1ACL, err := nodeacls.FetchNodeACL(nodeacls.NetworkID(node1.Network), nodeacls.NodeID(node1.ID))
2022-02-25 02:48:35 +08:00
assert.NotNil(t, err)
assert.Nil(t, node1ACL)
assert.EqualError(t, err, "no node ACL present for node "+node1.ID)
})
t.Run("node acls exists after creates", func(t *testing.T) {
2022-02-26 03:08:44 +08:00
node1ACL, err := nodeacls.CreateNodeACL(nodeacls.NetworkID(node1.Network), nodeacls.NodeID(node1.ID), acls.Allowed)
2022-02-25 02:48:35 +08:00
assert.Nil(t, err)
assert.NotNil(t, node1ACL)
2022-02-26 00:06:03 +08:00
assert.Equal(t, node1ACL[acls.AclID(node2.ID)], acls.NotPresent)
2022-02-26 03:08:44 +08:00
node2ACL, err := nodeacls.CreateNodeACL(nodeacls.NetworkID(node1.Network), nodeacls.NodeID(node2.ID), acls.Allowed)
2022-02-25 02:48:35 +08:00
assert.Nil(t, err)
assert.NotNil(t, node2ACL)
2022-02-26 00:06:03 +08:00
assert.Equal(t, acls.Allowed, node2ACL[acls.AclID(node1.ID)])
2022-02-25 02:48:35 +08:00
})
t.Run("node acls correct after fetch", func(t *testing.T) {
2022-02-26 03:08:44 +08:00
node1ACL, err := nodeacls.FetchNodeACL(nodeacls.NetworkID(node1.Network), nodeacls.NodeID(node1.ID))
2022-02-25 02:48:35 +08:00
assert.Nil(t, err)
2022-02-26 00:06:03 +08:00
assert.Equal(t, acls.Allowed, node1ACL[acls.AclID(node2.ID)])
2022-02-25 02:48:35 +08:00
})
t.Run("node acls correct after modify", func(t *testing.T) {
2022-02-26 03:08:44 +08:00
node1ACL, err := nodeacls.CreateNodeACL(nodeacls.NetworkID(node1.Network), nodeacls.NodeID(node1.ID), acls.Allowed)
2022-02-25 02:48:35 +08:00
assert.Nil(t, err)
2022-02-26 03:08:44 +08:00
node2ACL, err := nodeacls.CreateNodeACL(nodeacls.NetworkID(node1.Network), nodeacls.NodeID(node2.ID), acls.Allowed)
assert.Nil(t, err)
assert.NotNil(t, node1ACL)
assert.NotNil(t, node2ACL)
2022-02-26 03:08:44 +08:00
currentACL, err := nodeacls.ChangeNodesAccess(nodeacls.NetworkID(node1.Network), nodeacls.NodeID(node1.ID), nodeacls.NodeID(node2.ID), acls.NotAllowed)
assert.Nil(t, err)
2022-02-26 00:06:03 +08:00
assert.Equal(t, acls.NotAllowed, currentACL[acls.AclID(node1.ID)][acls.AclID(node2.ID)])
assert.Equal(t, acls.NotAllowed, currentACL[acls.AclID(node2.ID)][acls.AclID(node1.ID)])
2022-02-25 02:48:35 +08:00
})
t.Run("node acls removed", func(t *testing.T) {
2022-02-26 03:08:44 +08:00
retNetworkACL, err := nodeacls.RemoveNodeACL(nodeacls.NetworkID(node1.Network), nodeacls.NodeID(node1.ID))
2022-02-25 02:48:35 +08:00
assert.Nil(t, err)
assert.NotNil(t, retNetworkACL)
2022-02-26 00:06:03 +08:00
assert.Equal(t, acls.NotPresent, retNetworkACL[acls.AclID(node2.ID)][acls.AclID(node1.ID)])
2022-02-25 02:48:35 +08:00
})
deleteAllNodes()
}
2021-09-07 03:30:39 +08:00
func deleteAllNodes() {
2022-01-11 21:40:51 +08:00
database.DeleteAllRecords(database.NODES_TABLE_NAME)
2021-09-07 03:30:39 +08:00
}
2021-12-08 04:51:57 +08:00
func createTestNode() *models.Node {
createnode := models.Node{PublicKey: "DM5qhLAE20PG9BbfBCger+Ac9D2NDOwCtY1rbYDLf34=", Name: "testnode", Endpoint: "10.0.0.1", MacAddress: "01:02:03:04:05:06", Password: "password", Network: "skynet", OS: "linux"}
2021-12-11 10:09:42 +08:00
logic.CreateNode(&createnode)
return &createnode
2021-12-08 04:51:57 +08:00
}