Merge pull request #2072 from gravitl/GRA-1217-tests

Gra 1217 tests
This commit is contained in:
dcarns 2023-03-15 08:53:09 -04:00 committed by GitHub
commit 419cd35662
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 44 additions and 29 deletions

View file

@ -40,6 +40,8 @@ func TestMain(m *testing.M) {
logger.Log(3, "received node update", update.Action)
}
}()
os.Exit(m.Run())
}
func TestCreateNetwork(t *testing.T) {
@ -325,10 +327,7 @@ func TestIpv6Network(t *testing.T) {
func deleteAllNetworks() {
deleteAllNodes()
nets, _ := logic.GetNetworks()
for _, net := range nets {
logic.DeleteNetwork(net.NetID)
}
database.DeleteAllRecords(database.NETWORKS_TABLE_NAME)
}
func createNet() {

View file

@ -113,12 +113,12 @@ func TestGetNetworkNodes(t *testing.T) {
t.Run("BadNet", func(t *testing.T) {
node, err := logic.GetNetworkNodes("badnet")
assert.Nil(t, err)
assert.Nil(t, node)
assert.Equal(t, []models.Node{}, node)
})
t.Run("NoNodes", func(t *testing.T) {
node, err := logic.GetNetworkNodes("skynet")
assert.Nil(t, err)
assert.Nil(t, node)
assert.Equal(t, []models.Node{}, node)
})
t.Run("Success", func(t *testing.T) {
createTestNode()

View file

@ -186,6 +186,7 @@ func TestGetUsers(t *testing.T) {
assert.Equal(t, []models.ReturnUser(nil), admin)
})
t.Run("UserExisits", func(t *testing.T) {
user.UserName = "anotheruser"
if err := logic.CreateUser(&adminUser); err != nil {
t.Error(err)
}
@ -281,6 +282,9 @@ func TestVerifyAuthRequest(t *testing.T) {
assert.EqualError(t, err, "error retrieving user from db: could not find any records")
})
t.Run("Non-Admin", func(t *testing.T) {
user.IsAdmin = false
user.Password = "somepass"
user.UserName = "nonadmin"
if err := logic.CreateUser(&user); err != nil {
t.Error(err)
}

View file

@ -3,12 +3,14 @@ package functions
import (
"context"
"encoding/json"
"os"
"testing"
"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"
)
var (
@ -39,40 +41,29 @@ func TestMain(m *testing.M) {
logger.Log(3, "received node update", update.Action)
}
}()
os.Exit(m.Run())
}
func TestNetworkExists(t *testing.T) {
database.DeleteRecord(database.NETWORKS_TABLE_NAME, testNetwork.NetID)
defer database.CloseDB()
exists, err := logic.NetworkExists(testNetwork.NetID)
if err == nil {
t.Fatalf("expected error, received nil")
}
if exists {
t.Fatalf("expected false")
}
assert.NotNil(t, err)
assert.False(t, exists)
err = logic.SaveNetwork(testNetwork)
if err != nil {
t.Fatalf("failed to save test network in databse: %s", err)
}
assert.Nil(t, err)
exists, err = logic.NetworkExists(testNetwork.NetID)
if err != nil {
t.Fatalf("expected nil, received err: %s", err)
}
if !exists {
t.Fatalf("expected network to exist in database")
}
assert.Nil(t, err)
assert.True(t, exists)
err = database.DeleteRecord(database.NETWORKS_TABLE_NAME, testNetwork.NetID)
if err != nil {
t.Fatalf("expected nil, failed to delete test network: %s", err)
}
assert.Nil(t, err)
}
func TestGetAllExtClients(t *testing.T) {
defer database.CloseDB()
database.DeleteRecord(database.EXT_CLIENT_TABLE_NAME, testExternalClient.ClientID)
err := database.DeleteRecord(database.EXT_CLIENT_TABLE_NAME, testExternalClient.ClientID)
assert.Nil(t, err)
extClients, err := GetAllExtClients()
if err == nil {

View file

@ -4,6 +4,7 @@ import (
"context"
"fmt"
"net"
"os"
"testing"
"github.com/google/uuid"
@ -12,7 +13,7 @@ import (
"github.com/matryer/is"
)
func TestCheckPorts(t *testing.T) {
func TestMain(m *testing.M) {
database.InitializeDatabase()
defer database.CloseDB()
peerUpdate := make(chan *models.Node)
@ -24,6 +25,10 @@ func TestCheckPorts(t *testing.T) {
}
}()
os.Exit(m.Run())
}
func TestCheckPorts(t *testing.T) {
h := models.Host{
ID: uuid.New(),
EndpointIP: net.ParseIP("192.168.1.1"),
@ -36,10 +41,16 @@ func TestCheckPorts(t *testing.T) {
ListenPort: 51830,
ProxyListenPort: 51730,
}
//not sure why this initialization is required but without it
// RemoveHost returns database is closed
database.InitializeDatabase()
RemoveHost(&h)
CreateHost(&h)
t.Run("no change", func(t *testing.T) {
is := is.New(t)
CheckHostPorts(&testHost)
t.Log(testHost.ListenPort, testHost.ProxyListenPort)
t.Log(h.ListenPort, h.ProxyListenPort)
is.Equal(testHost.ListenPort, 51830)
is.Equal(testHost.ProxyListenPort, 51730)
})
@ -47,6 +58,8 @@ func TestCheckPorts(t *testing.T) {
is := is.New(t)
testHost.ListenPort = 51821
CheckHostPorts(&testHost)
t.Log(testHost.ListenPort, testHost.ProxyListenPort)
t.Log(h.ListenPort, h.ProxyListenPort)
is.Equal(testHost.ListenPort, 51822)
is.Equal(testHost.ProxyListenPort, 51730)
})
@ -54,6 +67,8 @@ func TestCheckPorts(t *testing.T) {
is := is.New(t)
testHost.ProxyListenPort = 65535
CheckHostPorts(&testHost)
t.Log(testHost.ListenPort, testHost.ProxyListenPort)
t.Log(h.ListenPort, h.ProxyListenPort)
is.Equal(testHost.ListenPort, 51822)
is.Equal(testHost.ProxyListenPort, minPort)
})
@ -61,6 +76,8 @@ func TestCheckPorts(t *testing.T) {
is := is.New(t)
testHost.ListenPort = maxPort
CheckHostPorts(&testHost)
t.Log(testHost.ListenPort, testHost.ProxyListenPort)
t.Log(h.ListenPort, h.ProxyListenPort)
is.Equal(testHost.ListenPort, minPort)
is.Equal(testHost.ProxyListenPort, minPort+1)
})
@ -68,6 +85,8 @@ func TestCheckPorts(t *testing.T) {
is := is.New(t)
testHost.ProxyListenPort = 51821
CheckHostPorts(&testHost)
t.Log(testHost.ListenPort, testHost.ProxyListenPort)
t.Log(h.ListenPort, h.ProxyListenPort)
is.Equal(testHost.ListenPort, minPort)
is.Equal(testHost.ProxyListenPort, 51822)
})

View file

@ -1,6 +1,7 @@
package pro
import (
"os"
"testing"
"github.com/google/uuid"
@ -13,6 +14,7 @@ import (
func TestMain(m *testing.M) {
database.InitializeDatabase()
defer database.CloseDB()
os.Exit(m.Run())
}
func TestNetworkUserLogic(t *testing.T) {
@ -33,7 +35,7 @@ func TestNetworkUserLogic(t *testing.T) {
}
clients := []models.ExtClient{
models.ExtClient{
{
ClientID: "coolclient",
},
}