2021-05-06 22:35:04 +08:00
|
|
|
package controller
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/gravitl/netmaker/models"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
type NodeValidationTC struct {
|
|
|
|
testname string
|
|
|
|
node models.Node
|
|
|
|
errorMessage string
|
|
|
|
}
|
|
|
|
|
2021-05-06 23:57:32 +08:00
|
|
|
type NodeValidationUpdateTC struct {
|
|
|
|
testname string
|
|
|
|
node models.NodeUpdate
|
|
|
|
errorMessage string
|
|
|
|
}
|
|
|
|
|
2021-05-06 22:35:04 +08:00
|
|
|
func TestCreateNode(t *testing.T) {
|
|
|
|
}
|
|
|
|
func TestDeleteNode(t *testing.T) {
|
|
|
|
}
|
|
|
|
func TestGetNode(t *testing.T) {
|
|
|
|
}
|
|
|
|
func TestGetPeerList(t *testing.T) {
|
|
|
|
}
|
|
|
|
func TestNodeCheckIn(t *testing.T) {
|
|
|
|
}
|
|
|
|
func TestSetNetworkNodesLastModified(t *testing.T) {
|
|
|
|
}
|
|
|
|
func TestTimestampNode(t *testing.T) {
|
|
|
|
}
|
|
|
|
func TestUpdateNode(t *testing.T) {
|
|
|
|
}
|
|
|
|
func TestValidateNodeCreate(t *testing.T) {
|
|
|
|
cases := []NodeValidationTC{
|
|
|
|
// NodeValidationTC{
|
|
|
|
// testname: "EmptyAddress",
|
|
|
|
// node: models.Node{
|
|
|
|
// Address: "",
|
|
|
|
// },
|
|
|
|
// errorMessage: "Field validation for 'Endpoint' failed on the 'address_check' tag",
|
|
|
|
// },
|
|
|
|
NodeValidationTC{
|
|
|
|
testname: "BadAddress",
|
|
|
|
node: models.Node{
|
|
|
|
Address: "256.0.0.1",
|
|
|
|
},
|
2021-05-06 23:57:32 +08:00
|
|
|
errorMessage: "Field validation for 'Address' failed on the 'ipv4' tag",
|
2021-05-06 22:35:04 +08:00
|
|
|
},
|
|
|
|
NodeValidationTC{
|
|
|
|
testname: "BadAddress6",
|
|
|
|
node: models.Node{
|
|
|
|
Address6: "2607::abcd:efgh::1",
|
|
|
|
},
|
2021-05-06 23:57:32 +08:00
|
|
|
errorMessage: "Field validation for 'Address6' failed on the 'ipv6' tag",
|
2021-05-06 22:35:04 +08:00
|
|
|
},
|
|
|
|
NodeValidationTC{
|
|
|
|
testname: "BadLocalAddress",
|
|
|
|
node: models.Node{
|
|
|
|
LocalAddress: "10.0.200.300",
|
|
|
|
},
|
2021-05-06 23:57:32 +08:00
|
|
|
errorMessage: "Field validation for 'LocalAddress' failed on the 'ip' tag",
|
2021-05-06 22:35:04 +08:00
|
|
|
},
|
|
|
|
NodeValidationTC{
|
|
|
|
testname: "InvalidName",
|
|
|
|
node: models.Node{
|
|
|
|
Name: "mynode*",
|
|
|
|
},
|
2021-05-06 23:57:32 +08:00
|
|
|
errorMessage: "Field validation for 'Name' failed on the 'alphanum' tag",
|
2021-05-06 22:35:04 +08:00
|
|
|
},
|
|
|
|
NodeValidationTC{
|
|
|
|
testname: "NameTooLong",
|
|
|
|
node: models.Node{
|
|
|
|
Name: "mynodexmynode",
|
|
|
|
},
|
|
|
|
errorMessage: "Field validation for 'Name' failed on the 'max' tag",
|
|
|
|
},
|
|
|
|
NodeValidationTC{
|
|
|
|
testname: "ListenPortMin",
|
|
|
|
node: models.Node{
|
|
|
|
ListenPort: 1023,
|
|
|
|
},
|
|
|
|
errorMessage: "Field validation for 'ListenPort' failed on the 'min' tag",
|
|
|
|
},
|
|
|
|
NodeValidationTC{
|
|
|
|
testname: "ListenPortMax",
|
|
|
|
node: models.Node{
|
|
|
|
ListenPort: 65536,
|
|
|
|
},
|
|
|
|
errorMessage: "Field validation for 'ListenPort' failed on the 'max' tag",
|
|
|
|
},
|
|
|
|
NodeValidationTC{
|
2021-05-06 23:57:32 +08:00
|
|
|
testname: "PublicKeyEmpty",
|
2021-05-06 22:35:04 +08:00
|
|
|
node: models.Node{
|
|
|
|
PublicKey: "",
|
|
|
|
},
|
2021-05-06 23:57:32 +08:00
|
|
|
errorMessage: "Field validation for 'PublicKey' failed on the 'required' tag",
|
|
|
|
},
|
|
|
|
NodeValidationTC{
|
|
|
|
testname: "PublicKeyInvalid",
|
|
|
|
node: models.Node{
|
|
|
|
PublicKey: "junk%key",
|
|
|
|
},
|
|
|
|
errorMessage: "Field validation for 'PublicKey' failed on the 'base64' tag",
|
2021-05-06 22:35:04 +08:00
|
|
|
},
|
|
|
|
NodeValidationTC{
|
|
|
|
testname: "EndpointInvalid",
|
|
|
|
node: models.Node{
|
|
|
|
Endpoint: "10.2.0.300",
|
|
|
|
},
|
2021-05-06 23:57:32 +08:00
|
|
|
errorMessage: "Field validation for 'Endpoint' failed on the 'ip' tag",
|
|
|
|
},
|
|
|
|
NodeValidationTC{
|
|
|
|
testname: "EndpointEmpty",
|
|
|
|
node: models.Node{
|
|
|
|
Endpoint: "",
|
|
|
|
},
|
|
|
|
errorMessage: "Field validation for 'Endpoint' failed on the 'required' tag",
|
2021-05-06 22:35:04 +08:00
|
|
|
},
|
|
|
|
NodeValidationTC{
|
|
|
|
testname: "PersistentKeepaliveMax",
|
|
|
|
node: models.Node{
|
|
|
|
PersistentKeepalive: 1001,
|
|
|
|
},
|
|
|
|
errorMessage: "Field validation for 'PersistentKeepalive' failed on the 'max' tag",
|
|
|
|
},
|
|
|
|
NodeValidationTC{
|
|
|
|
testname: "MacAddressInvalid",
|
|
|
|
node: models.Node{
|
|
|
|
MacAddress: "01:02:03:04:05",
|
|
|
|
},
|
2021-05-06 23:57:32 +08:00
|
|
|
errorMessage: "Field validation for 'MacAddress' failed on the 'mac' tag",
|
2021-05-06 22:35:04 +08:00
|
|
|
},
|
|
|
|
NodeValidationTC{
|
|
|
|
testname: "MacAddressMissing",
|
|
|
|
node: models.Node{
|
|
|
|
MacAddress: "",
|
|
|
|
},
|
|
|
|
errorMessage: "Field validation for 'MacAddress' failed on the 'required' tag",
|
|
|
|
},
|
|
|
|
NodeValidationTC{
|
|
|
|
testname: "EmptyPassword",
|
|
|
|
node: models.Node{
|
|
|
|
Password: "",
|
|
|
|
},
|
2021-05-06 23:57:32 +08:00
|
|
|
errorMessage: "Field validation for 'Password' failed on the 'required' tag",
|
2021-05-06 22:35:04 +08:00
|
|
|
},
|
|
|
|
NodeValidationTC{
|
|
|
|
testname: "ShortPassword",
|
|
|
|
node: models.Node{
|
|
|
|
Password: "1234",
|
|
|
|
},
|
2021-05-06 23:57:32 +08:00
|
|
|
errorMessage: "Field validation for 'Password' failed on the 'min' tag",
|
2021-05-06 22:35:04 +08:00
|
|
|
},
|
|
|
|
NodeValidationTC{
|
|
|
|
testname: "NoNetwork",
|
|
|
|
node: models.Node{
|
|
|
|
Network: "badnet",
|
|
|
|
},
|
|
|
|
errorMessage: "Field validation for 'Network' failed on the 'network_exists' tag",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
|
|
|
t.Run(tc.testname, func(t *testing.T) {
|
|
|
|
err := ValidateNodeCreate("skynet", tc.node)
|
|
|
|
assert.NotNil(t, err)
|
|
|
|
assert.Contains(t, err.Error(), tc.errorMessage)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
t.Run("MacAddresUnique", func(t *testing.T) {
|
|
|
|
createNet()
|
|
|
|
node := models.Node{MacAddress: "01:02:03:04:05:06", Network: "skynet"}
|
|
|
|
_, err := CreateNode(node, "skynet")
|
|
|
|
assert.Nil(t, err)
|
|
|
|
err = ValidateNodeCreate("skynet", node)
|
|
|
|
assert.NotNil(t, err)
|
|
|
|
assert.Contains(t, err.Error(), "Field validation for 'MacAddress' failed on the 'macaddress_unique' tag")
|
|
|
|
})
|
|
|
|
t.Run("EmptyAddress", func(t *testing.T) {
|
|
|
|
node := models.Node{Address: ""}
|
|
|
|
err := ValidateNodeCreate("skynet", node)
|
|
|
|
assert.NotNil(t, err)
|
|
|
|
assert.NotContains(t, err.Error(), "Field validation for 'Address' failed on the 'address_check' tag")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
func TestValidateNodeUpdate(t *testing.T) {
|
|
|
|
//cases
|
2021-05-06 23:57:32 +08:00
|
|
|
cases := []NodeValidationUpdateTC{
|
|
|
|
NodeValidationUpdateTC{
|
|
|
|
testname: "BadAddress",
|
|
|
|
node: models.NodeUpdate{
|
|
|
|
Address: "256.0.0.1",
|
|
|
|
},
|
2021-05-07 04:14:31 +08:00
|
|
|
errorMessage: "Field validation for 'Address' failed on the 'ip' tag",
|
2021-05-06 23:57:32 +08:00
|
|
|
},
|
|
|
|
NodeValidationUpdateTC{
|
|
|
|
testname: "BadAddress6",
|
|
|
|
node: models.NodeUpdate{
|
|
|
|
Address6: "2607::abcd:efgh::1",
|
|
|
|
},
|
2021-05-07 04:14:31 +08:00
|
|
|
errorMessage: "Field validation for 'Address6' failed on the 'ipv6' tag",
|
2021-05-06 23:57:32 +08:00
|
|
|
},
|
|
|
|
NodeValidationUpdateTC{
|
|
|
|
testname: "BadLocalAddress",
|
|
|
|
node: models.NodeUpdate{
|
|
|
|
LocalAddress: "10.0.200.300",
|
|
|
|
},
|
2021-05-07 04:14:31 +08:00
|
|
|
errorMessage: "Field validation for 'LocalAddress' failed on the 'ip' tag",
|
2021-05-06 23:57:32 +08:00
|
|
|
},
|
|
|
|
NodeValidationUpdateTC{
|
|
|
|
testname: "InvalidName",
|
|
|
|
node: models.NodeUpdate{
|
|
|
|
Name: "mynode*",
|
|
|
|
},
|
2021-05-07 04:14:31 +08:00
|
|
|
errorMessage: "Field validation for 'Name' failed on the 'alphanum' tag",
|
2021-05-06 23:57:32 +08:00
|
|
|
},
|
|
|
|
NodeValidationUpdateTC{
|
|
|
|
testname: "NameTooLong",
|
|
|
|
node: models.NodeUpdate{
|
|
|
|
Name: "mynodexmynode",
|
|
|
|
},
|
|
|
|
errorMessage: "Field validation for 'Name' failed on the 'max' tag",
|
|
|
|
},
|
|
|
|
NodeValidationUpdateTC{
|
|
|
|
testname: "ListenPortMin",
|
|
|
|
node: models.NodeUpdate{
|
|
|
|
ListenPort: 1023,
|
|
|
|
},
|
|
|
|
errorMessage: "Field validation for 'ListenPort' failed on the 'min' tag",
|
|
|
|
},
|
|
|
|
NodeValidationUpdateTC{
|
|
|
|
testname: "ListenPortMax",
|
|
|
|
node: models.NodeUpdate{
|
|
|
|
ListenPort: 65536,
|
|
|
|
},
|
|
|
|
errorMessage: "Field validation for 'ListenPort' failed on the 'max' tag",
|
|
|
|
},
|
|
|
|
NodeValidationUpdateTC{
|
|
|
|
testname: "PublicKeyInvalid",
|
|
|
|
node: models.NodeUpdate{
|
2021-05-07 04:14:31 +08:00
|
|
|
PublicKey: "bad&key",
|
2021-05-06 23:57:32 +08:00
|
|
|
},
|
2021-05-07 04:14:31 +08:00
|
|
|
errorMessage: "Field validation for 'PublicKey' failed on the 'base64' tag",
|
2021-05-06 23:57:32 +08:00
|
|
|
},
|
|
|
|
NodeValidationUpdateTC{
|
|
|
|
testname: "EndpointInvalid",
|
|
|
|
node: models.NodeUpdate{
|
|
|
|
Endpoint: "10.2.0.300",
|
|
|
|
},
|
2021-05-07 04:14:31 +08:00
|
|
|
errorMessage: "Field validation for 'Endpoint' failed on the 'ip' tag",
|
2021-05-06 23:57:32 +08:00
|
|
|
},
|
|
|
|
NodeValidationUpdateTC{
|
|
|
|
testname: "PersistentKeepaliveMax",
|
|
|
|
node: models.NodeUpdate{
|
|
|
|
PersistentKeepalive: 1001,
|
|
|
|
},
|
|
|
|
errorMessage: "Field validation for 'PersistentKeepalive' failed on the 'max' tag",
|
|
|
|
},
|
|
|
|
NodeValidationUpdateTC{
|
|
|
|
testname: "MacAddressInvalid",
|
|
|
|
node: models.NodeUpdate{
|
|
|
|
MacAddress: "01:02:03:04:05",
|
|
|
|
},
|
2021-05-07 04:14:31 +08:00
|
|
|
errorMessage: "Field validation for 'MacAddress' failed on the 'mac' tag",
|
2021-05-06 23:57:32 +08:00
|
|
|
},
|
|
|
|
NodeValidationUpdateTC{
|
|
|
|
testname: "MacAddressMissing",
|
|
|
|
node: models.NodeUpdate{
|
|
|
|
MacAddress: "",
|
|
|
|
},
|
|
|
|
errorMessage: "Field validation for 'MacAddress' failed on the 'required' tag",
|
|
|
|
},
|
|
|
|
NodeValidationUpdateTC{
|
|
|
|
testname: "ShortPassword",
|
|
|
|
node: models.NodeUpdate{
|
|
|
|
Password: "1234",
|
|
|
|
},
|
2021-05-07 04:14:31 +08:00
|
|
|
errorMessage: "Field validation for 'Password' failed on the 'min' tag",
|
2021-05-06 23:57:32 +08:00
|
|
|
},
|
|
|
|
NodeValidationUpdateTC{
|
|
|
|
testname: "NoNetwork",
|
|
|
|
node: models.NodeUpdate{
|
|
|
|
Network: "badnet",
|
|
|
|
},
|
|
|
|
errorMessage: "Field validation for 'Network' failed on the 'network_exists' tag",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range cases {
|
|
|
|
t.Run(tc.testname, func(t *testing.T) {
|
|
|
|
err := ValidateNodeUpdate("skynet", tc.node)
|
|
|
|
assert.NotNil(t, err)
|
|
|
|
assert.Contains(t, err.Error(), tc.errorMessage)
|
|
|
|
})
|
|
|
|
}
|
2021-05-06 22:35:04 +08:00
|
|
|
|
|
|
|
}
|