mirror of
				https://github.com/gravitl/netmaker.git
				synced 2025-11-01 04:32:40 +08:00 
			
		
		
		
	fix(go): workflow errors fixes;
This commit is contained in:
		| @@ -2,6 +2,8 @@ package controller | |||||||
|  |  | ||||||
| import ( | import ( | ||||||
| 	"context" | 	"context" | ||||||
|  | 	"github.com/gravitl/netmaker/db" | ||||||
|  | 	"github.com/gravitl/netmaker/schema" | ||||||
| 	"os" | 	"os" | ||||||
| 	"testing" | 	"testing" | ||||||
|  |  | ||||||
| @@ -23,6 +25,9 @@ type NetworkValidationTestCase struct { | |||||||
| var netHost models.Host | var netHost models.Host | ||||||
|  |  | ||||||
| func TestMain(m *testing.M) { | func TestMain(m *testing.M) { | ||||||
|  | 	db.InitializeDB(schema.ListModels()...) | ||||||
|  | 	defer db.CloseDB() | ||||||
|  |  | ||||||
| 	database.InitializeDatabase() | 	database.InitializeDatabase() | ||||||
| 	defer database.CloseDB() | 	defer database.CloseDB() | ||||||
| 	logic.CreateSuperAdmin(&models.User{ | 	logic.CreateSuperAdmin(&models.User{ | ||||||
|   | |||||||
| @@ -233,6 +233,9 @@ func getConfig(w http.ResponseWriter, r *http.Request) { | |||||||
| 	if servercfg.IsPro { | 	if servercfg.IsPro { | ||||||
| 		scfg.IsPro = "yes" | 		scfg.IsPro = "yes" | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|  | 	scfg.ClientID = logic.Mask() | ||||||
|  | 	scfg.ClientSecret = logic.Mask() | ||||||
| 	json.NewEncoder(w).Encode(scfg) | 	json.NewEncoder(w).Encode(scfg) | ||||||
| 	// w.WriteHeader(http.StatusOK) | 	// w.WriteHeader(http.StatusOK) | ||||||
| } | } | ||||||
|   | |||||||
| @@ -4,11 +4,9 @@ import ( | |||||||
| 	"context" | 	"context" | ||||||
| 	"database/sql" | 	"database/sql" | ||||||
| 	"errors" | 	"errors" | ||||||
| 	"fmt" |  | ||||||
| 	"github.com/gravitl/netmaker/db" | 	"github.com/gravitl/netmaker/db" | ||||||
| 	"time" | 	"time" | ||||||
|  |  | ||||||
| 	"github.com/gravitl/netmaker/servercfg" |  | ||||||
| 	_ "github.com/lib/pq" | 	_ "github.com/lib/pq" | ||||||
| ) | ) | ||||||
|  |  | ||||||
| @@ -28,14 +26,6 @@ var PG_FUNCTIONS = map[string]interface{}{ | |||||||
| 	isConnected:  pgIsConnected, | 	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 { | func initPGDB() error { | ||||||
| 	gormDB := db.FromContext(db.WithContext(context.TODO())) | 	gormDB := db.FromContext(db.WithContext(context.TODO())) | ||||||
|  |  | ||||||
|   | |||||||
| @@ -10,9 +10,6 @@ import ( | |||||||
| 	_ "github.com/mattn/go-sqlite3" // need to blank import this package | 	_ "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 | // SqliteDB is the db object for sqlite database connections | ||||||
| var SqliteDB *sql.DB | var SqliteDB *sql.DB | ||||||
|  |  | ||||||
|   | |||||||
							
								
								
									
										21
									
								
								db/db.go
									
									
									
									
									
								
							
							
						
						
									
										21
									
								
								db/db.go
									
									
									
									
									
								
							| @@ -110,3 +110,24 @@ func BeginTx(ctx context.Context) context.Context { | |||||||
|  |  | ||||||
| 	return context.WithValue(ctx, dbCtxKey, dbInCtx.Begin()) | 	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 | ||||||
|  | } | ||||||
|   | |||||||
| @@ -3,6 +3,8 @@ package functions | |||||||
| import ( | import ( | ||||||
| 	"context" | 	"context" | ||||||
| 	"encoding/json" | 	"encoding/json" | ||||||
|  | 	"github.com/gravitl/netmaker/db" | ||||||
|  | 	"github.com/gravitl/netmaker/schema" | ||||||
| 	"os" | 	"os" | ||||||
| 	"testing" | 	"testing" | ||||||
|  |  | ||||||
| @@ -23,6 +25,9 @@ var ( | |||||||
| ) | ) | ||||||
|  |  | ||||||
| func TestMain(m *testing.M) { | func TestMain(m *testing.M) { | ||||||
|  | 	db.InitializeDB(schema.ListModels()...) | ||||||
|  | 	defer db.CloseDB() | ||||||
|  |  | ||||||
| 	database.InitializeDatabase() | 	database.InitializeDatabase() | ||||||
| 	defer database.CloseDB() | 	defer database.CloseDB() | ||||||
| 	logic.CreateSuperAdmin(&models.User{ | 	logic.CreateSuperAdmin(&models.User{ | ||||||
|   | |||||||
| @@ -1,6 +1,8 @@ | |||||||
| package logic | package logic | ||||||
|  |  | ||||||
| import ( | import ( | ||||||
|  | 	"github.com/gravitl/netmaker/db" | ||||||
|  | 	"github.com/gravitl/netmaker/schema" | ||||||
| 	"testing" | 	"testing" | ||||||
| 	"time" | 	"time" | ||||||
|  |  | ||||||
| @@ -11,6 +13,9 @@ import ( | |||||||
| ) | ) | ||||||
|  |  | ||||||
| func TestCreateEnrollmentKey(t *testing.T) { | func TestCreateEnrollmentKey(t *testing.T) { | ||||||
|  | 	db.InitializeDB(schema.ListModels()...) | ||||||
|  | 	defer db.CloseDB() | ||||||
|  |  | ||||||
| 	database.InitializeDatabase() | 	database.InitializeDatabase() | ||||||
| 	defer database.CloseDB() | 	defer database.CloseDB() | ||||||
| 	t.Run("Can_Not_Create_Key", func(t *testing.T) { | 	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) { | func TestDelete_EnrollmentKey(t *testing.T) { | ||||||
|  | 	db.InitializeDB(schema.ListModels()...) | ||||||
|  | 	defer db.CloseDB() | ||||||
|  |  | ||||||
| 	database.InitializeDatabase() | 	database.InitializeDatabase() | ||||||
| 	defer database.CloseDB() | 	defer database.CloseDB() | ||||||
| 	newKey, _ := CreateEnrollmentKey(0, time.Time{}, []string{"mynet", "skynet"}, nil, nil, true, uuid.Nil, false, false) | 	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) { | func TestDecrement_EnrollmentKey(t *testing.T) { | ||||||
|  | 	db.InitializeDB(schema.ListModels()...) | ||||||
|  | 	defer db.CloseDB() | ||||||
|  |  | ||||||
| 	database.InitializeDatabase() | 	database.InitializeDatabase() | ||||||
| 	defer database.CloseDB() | 	defer database.CloseDB() | ||||||
| 	newKey, _ := CreateEnrollmentKey(1, time.Time{}, nil, nil, nil, false, uuid.Nil, false, false) | 	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) { | func TestUsability_EnrollmentKey(t *testing.T) { | ||||||
|  | 	db.InitializeDB(schema.ListModels()...) | ||||||
|  | 	defer db.CloseDB() | ||||||
|  |  | ||||||
| 	database.InitializeDatabase() | 	database.InitializeDatabase() | ||||||
| 	defer database.CloseDB() | 	defer database.CloseDB() | ||||||
| 	key1, _ := CreateEnrollmentKey(1, time.Time{}, nil, nil, nil, false, uuid.Nil, false, false) | 	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 | //Test that cheks if it can't tokenize | ||||||
|  |  | ||||||
| func TestTokenize_EnrollmentKeys(t *testing.T) { | func TestTokenize_EnrollmentKeys(t *testing.T) { | ||||||
|  | 	db.InitializeDB(schema.ListModels()...) | ||||||
|  | 	defer db.CloseDB() | ||||||
|  |  | ||||||
| 	database.InitializeDatabase() | 	database.InitializeDatabase() | ||||||
| 	defer database.CloseDB() | 	defer database.CloseDB() | ||||||
| 	newKey, _ := CreateEnrollmentKey(0, time.Time{}, []string{"mynet", "skynet"}, nil, nil, true, uuid.Nil, false, false) | 	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) { | func TestDeTokenize_EnrollmentKeys(t *testing.T) { | ||||||
|  | 	db.InitializeDB(schema.ListModels()...) | ||||||
|  | 	defer db.CloseDB() | ||||||
|  |  | ||||||
| 	database.InitializeDatabase() | 	database.InitializeDatabase() | ||||||
| 	defer database.CloseDB() | 	defer database.CloseDB() | ||||||
| 	newKey, _ := CreateEnrollmentKey(0, time.Time{}, []string{"mynet", "skynet"}, nil, nil, true, uuid.Nil, false, false) | 	newKey, _ := CreateEnrollmentKey(0, time.Time{}, []string{"mynet", "skynet"}, nil, nil, true, uuid.Nil, false, false) | ||||||
|   | |||||||
| @@ -3,6 +3,8 @@ package logic | |||||||
| import ( | import ( | ||||||
| 	"context" | 	"context" | ||||||
| 	"fmt" | 	"fmt" | ||||||
|  | 	"github.com/gravitl/netmaker/db" | ||||||
|  | 	"github.com/gravitl/netmaker/schema" | ||||||
| 	"net" | 	"net" | ||||||
| 	"os" | 	"os" | ||||||
| 	"testing" | 	"testing" | ||||||
| @@ -14,6 +16,9 @@ import ( | |||||||
| ) | ) | ||||||
|  |  | ||||||
| func TestMain(m *testing.M) { | func TestMain(m *testing.M) { | ||||||
|  | 	db.InitializeDB(schema.ListModels()...) | ||||||
|  | 	defer db.CloseDB() | ||||||
|  |  | ||||||
| 	database.InitializeDatabase() | 	database.InitializeDatabase() | ||||||
| 	defer database.CloseDB() | 	defer database.CloseDB() | ||||||
| 	peerUpdate := make(chan *models.Node) | 	peerUpdate := make(chan *models.Node) | ||||||
| @@ -41,6 +46,9 @@ func TestCheckPorts(t *testing.T) { | |||||||
| 	} | 	} | ||||||
| 	//not sure why this initialization is required but without it | 	//not sure why this initialization is required but without it | ||||||
| 	// RemoveHost returns database is closed | 	// RemoveHost returns database is closed | ||||||
|  | 	db.InitializeDB(schema.ListModels()...) | ||||||
|  | 	defer db.CloseDB() | ||||||
|  |  | ||||||
| 	database.InitializeDatabase() | 	database.InitializeDatabase() | ||||||
| 	RemoveHost(&h, true) | 	RemoveHost(&h, true) | ||||||
| 	CreateHost(&h) | 	CreateHost(&h) | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Vishal Dalwadi
					Vishal Dalwadi