mirror of
https://github.com/gravitl/netmaker.git
synced 2025-10-16 13:51:42 +08:00
fix tests
This commit is contained in:
@@ -15,7 +15,6 @@ import (
|
||||
var dnsHost models.Host
|
||||
|
||||
func TestGetAllDNS(t *testing.T) {
|
||||
initialize()
|
||||
deleteAllDNS(t)
|
||||
deleteAllNetworks()
|
||||
createNet()
|
||||
@@ -46,7 +45,6 @@ func TestGetAllDNS(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestGetNodeDNS(t *testing.T) {
|
||||
initialize()
|
||||
deleteAllDNS(t)
|
||||
deleteAllNetworks()
|
||||
createNet()
|
||||
@@ -93,7 +91,6 @@ func TestGetNodeDNS(t *testing.T) {
|
||||
})
|
||||
}
|
||||
func TestGetCustomDNS(t *testing.T) {
|
||||
initialize()
|
||||
deleteAllDNS(t)
|
||||
deleteAllNetworks()
|
||||
t.Run("NoNetworks", func(t *testing.T) {
|
||||
@@ -132,7 +129,6 @@ func TestGetCustomDNS(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestGetDNSEntryNum(t *testing.T) {
|
||||
initialize()
|
||||
deleteAllDNS(t)
|
||||
deleteAllNetworks()
|
||||
createNet()
|
||||
@@ -151,7 +147,6 @@ func TestGetDNSEntryNum(t *testing.T) {
|
||||
})
|
||||
}
|
||||
func TestGetDNS(t *testing.T) {
|
||||
initialize()
|
||||
deleteAllDNS(t)
|
||||
deleteAllNetworks()
|
||||
createNet()
|
||||
@@ -195,7 +190,6 @@ func TestGetDNS(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestCreateDNS(t *testing.T) {
|
||||
initialize()
|
||||
deleteAllDNS(t)
|
||||
deleteAllNetworks()
|
||||
createNet()
|
||||
@@ -206,7 +200,6 @@ func TestCreateDNS(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestSetDNS(t *testing.T) {
|
||||
initialize()
|
||||
deleteAllDNS(t)
|
||||
deleteAllNetworks()
|
||||
t.Run("NoNetworks", func(t *testing.T) {
|
||||
@@ -254,7 +247,6 @@ func TestSetDNS(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestGetDNSEntry(t *testing.T) {
|
||||
initialize()
|
||||
deleteAllDNS(t)
|
||||
deleteAllNetworks()
|
||||
createNet()
|
||||
@@ -284,7 +276,6 @@ func TestGetDNSEntry(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestDeleteDNS(t *testing.T) {
|
||||
initialize()
|
||||
deleteAllDNS(t)
|
||||
deleteAllNetworks()
|
||||
createNet()
|
||||
@@ -306,7 +297,6 @@ func TestDeleteDNS(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestValidateDNSUpdate(t *testing.T) {
|
||||
initialize()
|
||||
deleteAllDNS(t)
|
||||
deleteAllNetworks()
|
||||
createNet()
|
||||
@@ -368,7 +358,6 @@ func TestValidateDNSUpdate(t *testing.T) {
|
||||
|
||||
}
|
||||
func TestValidateDNSCreate(t *testing.T) {
|
||||
initialize()
|
||||
_ = logic.DeleteDNS("mynode", "skynet")
|
||||
t.Run("NoNetwork", func(t *testing.T) {
|
||||
entry := models.DNSEntry{"10.0.0.2", "", "myhost", "badnet"}
|
||||
|
@@ -7,6 +7,7 @@ import (
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/gravitl/netmaker/database"
|
||||
"github.com/gravitl/netmaker/logger"
|
||||
"github.com/gravitl/netmaker/logic"
|
||||
"github.com/gravitl/netmaker/models"
|
||||
"github.com/stretchr/testify/assert"
|
||||
@@ -21,8 +22,27 @@ type NetworkValidationTestCase struct {
|
||||
|
||||
var netHost models.Host
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
database.InitializeDatabase()
|
||||
defer database.CloseDB()
|
||||
logic.CreateAdmin(&models.User{
|
||||
UserName: "admin",
|
||||
Password: "password",
|
||||
IsAdmin: true,
|
||||
Networks: []string{},
|
||||
Groups: []string{},
|
||||
})
|
||||
peerUpdate := make(chan *models.Node)
|
||||
go logic.ManageZombies(context.Background(), peerUpdate)
|
||||
go func() {
|
||||
for update := range peerUpdate {
|
||||
//do nothing
|
||||
logger.Log(3, "received node update", update.Action)
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
func TestCreateNetwork(t *testing.T) {
|
||||
initialize()
|
||||
deleteAllNetworks()
|
||||
|
||||
var network models.Network
|
||||
@@ -35,7 +55,6 @@ func TestCreateNetwork(t *testing.T) {
|
||||
assert.Nil(t, err)
|
||||
}
|
||||
func TestGetNetwork(t *testing.T) {
|
||||
initialize()
|
||||
createNet()
|
||||
|
||||
t.Run("GetExistingNetwork", func(t *testing.T) {
|
||||
@@ -51,7 +70,6 @@ func TestGetNetwork(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestDeleteNetwork(t *testing.T) {
|
||||
initialize()
|
||||
createNet()
|
||||
//create nodes
|
||||
t.Run("NetworkwithNodes", func(t *testing.T) {
|
||||
@@ -67,7 +85,6 @@ func TestDeleteNetwork(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestCreateKey(t *testing.T) {
|
||||
initialize()
|
||||
createNet()
|
||||
keys, _ := logic.GetKeys("skynet")
|
||||
for _, key := range keys {
|
||||
@@ -139,7 +156,6 @@ func TestCreateKey(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestGetKeys(t *testing.T) {
|
||||
initialize()
|
||||
deleteAllNetworks()
|
||||
createNet()
|
||||
network, err := logic.GetNetwork("skynet")
|
||||
@@ -162,7 +178,6 @@ func TestGetKeys(t *testing.T) {
|
||||
})
|
||||
}
|
||||
func TestDeleteKey(t *testing.T) {
|
||||
initialize()
|
||||
createNet()
|
||||
network, err := logic.GetNetwork("skynet")
|
||||
assert.Nil(t, err)
|
||||
@@ -184,7 +199,6 @@ func TestDeleteKey(t *testing.T) {
|
||||
func TestSecurityCheck(t *testing.T) {
|
||||
//these seem to work but not sure it the tests are really testing the functionality
|
||||
|
||||
initialize()
|
||||
os.Setenv("MASTER_KEY", "secretkey")
|
||||
t.Run("NoNetwork", func(t *testing.T) {
|
||||
networks, username, err := logic.UserPermissions(false, "", "Bearer secretkey")
|
||||
@@ -215,7 +229,6 @@ func TestValidateNetwork(t *testing.T) {
|
||||
//t.Skip()
|
||||
//This functions is not called by anyone
|
||||
//it panics as validation function 'display_name_valid' is not defined
|
||||
initialize()
|
||||
//yes := true
|
||||
//no := false
|
||||
//deleteNet(t)
|
||||
@@ -292,7 +305,6 @@ func TestValidateNetwork(t *testing.T) {
|
||||
func TestIpv6Network(t *testing.T) {
|
||||
//these seem to work but not sure it the tests are really testing the functionality
|
||||
|
||||
initialize()
|
||||
os.Setenv("MASTER_KEY", "secretkey")
|
||||
deleteAllNetworks()
|
||||
createNet()
|
||||
@@ -319,22 +331,6 @@ func deleteAllNetworks() {
|
||||
}
|
||||
}
|
||||
|
||||
func initialize() {
|
||||
database.InitializeDatabase()
|
||||
createAdminUser()
|
||||
go logic.ManageZombies(context.Background())
|
||||
}
|
||||
|
||||
func createAdminUser() {
|
||||
logic.CreateAdmin(&models.User{
|
||||
UserName: "admin",
|
||||
Password: "password",
|
||||
IsAdmin: true,
|
||||
Networks: []string{},
|
||||
Groups: []string{},
|
||||
})
|
||||
}
|
||||
|
||||
func createNet() {
|
||||
var network models.Network
|
||||
network.NetID = "skynet"
|
||||
|
@@ -21,7 +21,6 @@ func TestCreateEgressGateway(t *testing.T) {
|
||||
var gateway models.EgressGatewayRequest
|
||||
gateway.Ranges = []string{"10.100.100.0/24"}
|
||||
gateway.NetID = "skynet"
|
||||
initialize()
|
||||
deleteAllNetworks()
|
||||
createNet()
|
||||
t.Run("NoNodes", func(t *testing.T) {
|
||||
@@ -78,7 +77,6 @@ func TestCreateEgressGateway(t *testing.T) {
|
||||
}
|
||||
func TestDeleteEgressGateway(t *testing.T) {
|
||||
var gateway models.EgressGatewayRequest
|
||||
initialize()
|
||||
deleteAllNetworks()
|
||||
createNet()
|
||||
testnode := createTestNode()
|
||||
@@ -110,7 +108,6 @@ func TestDeleteEgressGateway(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestGetNetworkNodes(t *testing.T) {
|
||||
initialize()
|
||||
deleteAllNetworks()
|
||||
createNet()
|
||||
t.Run("BadNet", func(t *testing.T) {
|
||||
|
@@ -17,7 +17,6 @@ func deleteAllUsers() {
|
||||
|
||||
func TestHasAdmin(t *testing.T) {
|
||||
//delete all current users
|
||||
initialize()
|
||||
users, _ := logic.GetUsers()
|
||||
for _, user := range users {
|
||||
success, err := logic.DeleteUser(user.UserName)
|
||||
@@ -56,7 +55,6 @@ func TestHasAdmin(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestCreateUser(t *testing.T) {
|
||||
initialize()
|
||||
deleteAllUsers()
|
||||
user := models.User{"admin", "password", nil, true, nil}
|
||||
t.Run("NoUser", func(t *testing.T) {
|
||||
@@ -71,7 +69,6 @@ func TestCreateUser(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestCreateAdmin(t *testing.T) {
|
||||
initialize()
|
||||
deleteAllUsers()
|
||||
var user models.User
|
||||
t.Run("NoAdmin", func(t *testing.T) {
|
||||
@@ -89,7 +86,6 @@ func TestCreateAdmin(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestDeleteUser(t *testing.T) {
|
||||
initialize()
|
||||
deleteAllUsers()
|
||||
t.Run("NonExistent User", func(t *testing.T) {
|
||||
deleted, err := logic.DeleteUser("admin")
|
||||
@@ -106,7 +102,6 @@ func TestDeleteUser(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestValidateUser(t *testing.T) {
|
||||
initialize()
|
||||
var user models.User
|
||||
t.Run("Valid Create", func(t *testing.T) {
|
||||
user.UserName = "admin"
|
||||
@@ -154,7 +149,6 @@ func TestValidateUser(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestGetUser(t *testing.T) {
|
||||
initialize()
|
||||
deleteAllUsers()
|
||||
t.Run("NonExistantUser", func(t *testing.T) {
|
||||
admin, err := logic.GetUser("admin")
|
||||
@@ -171,7 +165,6 @@ func TestGetUser(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestGetUsers(t *testing.T) {
|
||||
initialize()
|
||||
deleteAllUsers()
|
||||
t.Run("NonExistantUser", func(t *testing.T) {
|
||||
admin, err := logic.GetUsers()
|
||||
@@ -202,7 +195,6 @@ func TestGetUsers(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestUpdateUser(t *testing.T) {
|
||||
initialize()
|
||||
deleteAllUsers()
|
||||
user := models.User{"admin", "password", nil, true, nil}
|
||||
newuser := models.User{"hello", "world", []string{"wirecat, netmaker"}, true, []string{}}
|
||||
@@ -245,7 +237,6 @@ func TestUpdateUser(t *testing.T) {
|
||||
// }
|
||||
|
||||
func TestVerifyAuthRequest(t *testing.T) {
|
||||
initialize()
|
||||
deleteAllUsers()
|
||||
var authRequest models.UserAuthParams
|
||||
t.Run("EmptyUserName", func(t *testing.T) {
|
||||
|
@@ -1,10 +1,12 @@
|
||||
package functions
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
"github.com/gravitl/netmaker/database"
|
||||
"github.com/gravitl/netmaker/logger"
|
||||
"github.com/gravitl/netmaker/logic"
|
||||
"github.com/gravitl/netmaker/models"
|
||||
)
|
||||
@@ -19,11 +21,27 @@ var (
|
||||
}
|
||||
)
|
||||
|
||||
func TestNetworkExists(t *testing.T) {
|
||||
err := initialize()
|
||||
if err != nil {
|
||||
t.Fatalf("error initilizing database: %s", err)
|
||||
func TestMain(m *testing.M) {
|
||||
database.InitializeDatabase()
|
||||
defer database.CloseDB()
|
||||
logic.CreateAdmin(&models.User{
|
||||
UserName: "admin",
|
||||
Password: "password",
|
||||
IsAdmin: true,
|
||||
Networks: []string{},
|
||||
Groups: []string{},
|
||||
})
|
||||
peerUpdate := make(chan *models.Node)
|
||||
go logic.ManageZombies(context.Background(), peerUpdate)
|
||||
go func() {
|
||||
for update := range peerUpdate {
|
||||
//do nothing
|
||||
logger.Log(3, "received node update", update.Action)
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
func TestNetworkExists(t *testing.T) {
|
||||
database.DeleteRecord(database.NETWORKS_TABLE_NAME, testNetwork.NetID)
|
||||
defer database.CloseDB()
|
||||
exists, err := logic.NetworkExists(testNetwork.NetID)
|
||||
@@ -53,10 +71,6 @@ func TestNetworkExists(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestGetAllExtClients(t *testing.T) {
|
||||
err := initialize()
|
||||
if err != nil {
|
||||
t.Fatalf("error initilizing database: %s", err)
|
||||
}
|
||||
defer database.CloseDB()
|
||||
database.DeleteRecord(database.EXT_CLIENT_TABLE_NAME, testExternalClient.ClientID)
|
||||
|
||||
|
@@ -1,16 +1,38 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
"testing"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/gravitl/netmaker/database"
|
||||
"github.com/gravitl/netmaker/logger"
|
||||
"github.com/gravitl/netmaker/models"
|
||||
"github.com/matryer/is"
|
||||
)
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
database.InitializeDatabase()
|
||||
defer database.CloseDB()
|
||||
CreateAdmin(&models.User{
|
||||
UserName: "admin",
|
||||
Password: "password",
|
||||
IsAdmin: true,
|
||||
Networks: []string{},
|
||||
Groups: []string{},
|
||||
})
|
||||
peerUpdate := make(chan *models.Node)
|
||||
go ManageZombies(context.Background(), peerUpdate)
|
||||
go func() {
|
||||
for update := range peerUpdate {
|
||||
//do nothing
|
||||
logger.Log(3, "received node update", update.Action)
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
func TestCheckPorts(t *testing.T) {
|
||||
initialize()
|
||||
h := models.Host{
|
||||
ID: uuid.New(),
|
||||
EndpointIP: net.ParseIP("192.168.1.1"),
|
||||
|
@@ -4,13 +4,18 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/gravitl/netmaker/database"
|
||||
"github.com/gravitl/netmaker/models"
|
||||
"github.com/gravitl/netmaker/models/promodels"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
database.InitializeDatabase()
|
||||
defer database.CloseDB()
|
||||
}
|
||||
|
||||
func TestNetworkUserLogic(t *testing.T) {
|
||||
initialize()
|
||||
networkUser := promodels.NetworkUser{
|
||||
ID: "helloworld",
|
||||
}
|
||||
|
@@ -8,7 +8,6 @@ import (
|
||||
)
|
||||
|
||||
func TestUserGroupLogic(t *testing.T) {
|
||||
initialize()
|
||||
|
||||
t.Run("User Groups initialized successfully", func(t *testing.T) {
|
||||
err := InitializeGroups()
|
||||
|
@@ -80,6 +80,7 @@ func ManageZombies(ctx context.Context, peerUpdate chan *models.Node) {
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
close(peerUpdate)
|
||||
return
|
||||
case id := <-newZombie:
|
||||
zombies = append(zombies, id)
|
||||
|
Reference in New Issue
Block a user