From da5115221f6f590e3302645bbc45148cd95f5b0a Mon Sep 17 00:00:00 2001 From: Vishal Dalwadi Date: Fri, 6 Jun 2025 15:42:30 +0530 Subject: [PATCH] fix(go): workflow errors fixes; --- controllers/network_test.go | 5 +++++ controllers/server.go | 3 +++ database/postgres.go | 10 ---------- database/sqlite.go | 3 --- db/db.go | 21 +++++++++++++++++++++ functions/helpers_test.go | 5 +++++ logic/enrollmentkey_test.go | 20 ++++++++++++++++++++ logic/host_test.go | 8 ++++++++ 8 files changed, 62 insertions(+), 13 deletions(-) diff --git a/controllers/network_test.go b/controllers/network_test.go index 87cb6ee3..258f4883 100644 --- a/controllers/network_test.go +++ b/controllers/network_test.go @@ -2,6 +2,8 @@ package controller import ( "context" + "github.com/gravitl/netmaker/db" + "github.com/gravitl/netmaker/schema" "os" "testing" @@ -23,6 +25,9 @@ type NetworkValidationTestCase struct { var netHost models.Host func TestMain(m *testing.M) { + db.InitializeDB(schema.ListModels()...) + defer db.CloseDB() + database.InitializeDatabase() defer database.CloseDB() logic.CreateSuperAdmin(&models.User{ diff --git a/controllers/server.go b/controllers/server.go index 6f5ee765..675a9910 100644 --- a/controllers/server.go +++ b/controllers/server.go @@ -233,6 +233,9 @@ func getConfig(w http.ResponseWriter, r *http.Request) { if servercfg.IsPro { scfg.IsPro = "yes" } + + scfg.ClientID = logic.Mask() + scfg.ClientSecret = logic.Mask() json.NewEncoder(w).Encode(scfg) // w.WriteHeader(http.StatusOK) } diff --git a/database/postgres.go b/database/postgres.go index a28f49a6..c28e9425 100644 --- a/database/postgres.go +++ b/database/postgres.go @@ -4,11 +4,9 @@ import ( "context" "database/sql" "errors" - "fmt" "github.com/gravitl/netmaker/db" "time" - "github.com/gravitl/netmaker/servercfg" _ "github.com/lib/pq" ) @@ -28,14 +26,6 @@ var PG_FUNCTIONS = map[string]interface{}{ isConnected: pgIsConnected, } -func getPGConnString() string { - pgconf := servercfg.GetSQLConf() - pgConn := fmt.Sprintf("host=%s port=%d user=%s "+ - "password=%s dbname=%s sslmode=%s connect_timeout=5", - pgconf.Host, pgconf.Port, pgconf.Username, pgconf.Password, pgconf.DB, pgconf.SSLMode) - return pgConn -} - func initPGDB() error { gormDB := db.FromContext(db.WithContext(context.TODO())) diff --git a/database/sqlite.go b/database/sqlite.go index 7880861b..f922e7b6 100644 --- a/database/sqlite.go +++ b/database/sqlite.go @@ -10,9 +10,6 @@ import ( _ "github.com/mattn/go-sqlite3" // need to blank import this package ) -// == sqlite == -const dbFilename = "netmaker.db" - // SqliteDB is the db object for sqlite database connections var SqliteDB *sql.DB diff --git a/db/db.go b/db/db.go index f0919fae..491ee8d8 100644 --- a/db/db.go +++ b/db/db.go @@ -110,3 +110,24 @@ func BeginTx(ctx context.Context) context.Context { return context.WithValue(ctx, dbCtxKey, dbInCtx.Begin()) } + +// CloseDB close a connection to the database +// (if one exists). It panics if any error +// occurs. +func CloseDB() { + if db == nil { + return + } + + sqlDB, err := db.DB() + if err != nil { + panic(err) + } + + err = sqlDB.Close() + if err != nil { + panic(err) + } + + db = nil +} diff --git a/functions/helpers_test.go b/functions/helpers_test.go index ecc34330..7c5dd1aa 100644 --- a/functions/helpers_test.go +++ b/functions/helpers_test.go @@ -3,6 +3,8 @@ package functions import ( "context" "encoding/json" + "github.com/gravitl/netmaker/db" + "github.com/gravitl/netmaker/schema" "os" "testing" @@ -23,6 +25,9 @@ var ( ) func TestMain(m *testing.M) { + db.InitializeDB(schema.ListModels()...) + defer db.CloseDB() + database.InitializeDatabase() defer database.CloseDB() logic.CreateSuperAdmin(&models.User{ diff --git a/logic/enrollmentkey_test.go b/logic/enrollmentkey_test.go index 1a32d63f..5da5b30e 100644 --- a/logic/enrollmentkey_test.go +++ b/logic/enrollmentkey_test.go @@ -1,6 +1,8 @@ package logic import ( + "github.com/gravitl/netmaker/db" + "github.com/gravitl/netmaker/schema" "testing" "time" @@ -11,6 +13,9 @@ import ( ) func TestCreateEnrollmentKey(t *testing.T) { + db.InitializeDB(schema.ListModels()...) + defer db.CloseDB() + database.InitializeDatabase() defer database.CloseDB() t.Run("Can_Not_Create_Key", func(t *testing.T) { @@ -60,6 +65,9 @@ func TestCreateEnrollmentKey(t *testing.T) { } func TestDelete_EnrollmentKey(t *testing.T) { + db.InitializeDB(schema.ListModels()...) + defer db.CloseDB() + database.InitializeDatabase() defer database.CloseDB() newKey, _ := CreateEnrollmentKey(0, time.Time{}, []string{"mynet", "skynet"}, nil, nil, true, uuid.Nil, false, false) @@ -81,6 +89,9 @@ func TestDelete_EnrollmentKey(t *testing.T) { } func TestDecrement_EnrollmentKey(t *testing.T) { + db.InitializeDB(schema.ListModels()...) + defer db.CloseDB() + database.InitializeDatabase() defer database.CloseDB() newKey, _ := CreateEnrollmentKey(1, time.Time{}, nil, nil, nil, false, uuid.Nil, false, false) @@ -105,6 +116,9 @@ func TestDecrement_EnrollmentKey(t *testing.T) { } func TestUsability_EnrollmentKey(t *testing.T) { + db.InitializeDB(schema.ListModels()...) + defer db.CloseDB() + database.InitializeDatabase() defer database.CloseDB() key1, _ := CreateEnrollmentKey(1, time.Time{}, nil, nil, nil, false, uuid.Nil, false, false) @@ -143,6 +157,9 @@ func removeAllEnrollments() { //Test that cheks if it can't tokenize func TestTokenize_EnrollmentKeys(t *testing.T) { + db.InitializeDB(schema.ListModels()...) + defer db.CloseDB() + database.InitializeDatabase() defer database.CloseDB() newKey, _ := CreateEnrollmentKey(0, time.Time{}, []string{"mynet", "skynet"}, nil, nil, true, uuid.Nil, false, false) @@ -176,6 +193,9 @@ func TestTokenize_EnrollmentKeys(t *testing.T) { } func TestDeTokenize_EnrollmentKeys(t *testing.T) { + db.InitializeDB(schema.ListModels()...) + defer db.CloseDB() + database.InitializeDatabase() defer database.CloseDB() newKey, _ := CreateEnrollmentKey(0, time.Time{}, []string{"mynet", "skynet"}, nil, nil, true, uuid.Nil, false, false) diff --git a/logic/host_test.go b/logic/host_test.go index 405ef7fb..46401a45 100644 --- a/logic/host_test.go +++ b/logic/host_test.go @@ -3,6 +3,8 @@ package logic import ( "context" "fmt" + "github.com/gravitl/netmaker/db" + "github.com/gravitl/netmaker/schema" "net" "os" "testing" @@ -14,6 +16,9 @@ import ( ) func TestMain(m *testing.M) { + db.InitializeDB(schema.ListModels()...) + defer db.CloseDB() + database.InitializeDatabase() defer database.CloseDB() peerUpdate := make(chan *models.Node) @@ -41,6 +46,9 @@ func TestCheckPorts(t *testing.T) { } //not sure why this initialization is required but without it // RemoveHost returns database is closed + db.InitializeDB(schema.ListModels()...) + defer db.CloseDB() + database.InitializeDatabase() RemoveHost(&h, true) CreateHost(&h)