From add230f21c9e12a6d93006d16c55fddf4e00bde0 Mon Sep 17 00:00:00 2001 From: CorrectRoadH Date: Fri, 12 Jul 2024 14:56:18 +0800 Subject: [PATCH 1/7] feat: add pkg to get gateway port --- go.mod | 3 +++ go.sum | 2 ++ pkg/port.go | 27 +++++++++++++++++++++++++++ pkg/port_test.go | 43 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 75 insertions(+) create mode 100644 pkg/port.go create mode 100644 pkg/port_test.go diff --git a/go.mod b/go.mod index b70cabb..de309b5 100644 --- a/go.mod +++ b/go.mod @@ -7,6 +7,7 @@ require ( github.com/gin-contrib/gzip v0.0.6 github.com/gin-gonic/gin v1.9.1 github.com/spf13/viper v1.18.2 + github.com/stretchr/testify v1.8.4 go.uber.org/fx v1.20.1 gotest.tools v2.2.0+incompatible ) @@ -16,9 +17,11 @@ require ( github.com/bytedance/sonic v1.9.1 // indirect github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect github.com/coreos/go-systemd/v22 v22.5.0 // indirect + github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect github.com/gabriel-vasile/mimetype v1.4.2 // indirect github.com/godbus/dbus/v5 v5.1.0 // indirect github.com/klauspost/cpuid/v2 v2.2.4 // indirect + github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect github.com/rogpeppe/go-internal v1.10.0 // indirect github.com/sagikazarmark/locafero v0.4.0 // indirect github.com/sagikazarmark/slog-shim v0.1.0 // indirect diff --git a/go.sum b/go.sum index 9af7935..fadd183 100644 --- a/go.sum +++ b/go.sum @@ -16,6 +16,7 @@ github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ3 github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM= +github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8= github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA= github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM= @@ -90,6 +91,7 @@ github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U= +github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc= github.com/rogpeppe/go-internal v1.8.0/go.mod h1:WmiCO8CzOY8rg0OYDC4/i/2WRWAB6poM+XZ2dLUbcbE= github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ= diff --git a/pkg/port.go b/pkg/port.go new file mode 100644 index 0000000..7c53aee --- /dev/null +++ b/pkg/port.go @@ -0,0 +1,27 @@ +package pkg + +import ( + "errors" + "fmt" + "os" + "path/filepath" + + "github.com/IceWhaleTech/CasaOS-Common/utils/constants" + "github.com/IceWhaleTech/CasaOS-Gateway/common" +) + +func GetGatewayPort() (string, error) { + ConfigFilePath := filepath.Join(constants.DefaultConfigPath, common.GatewayName+"."+common.GatewayConfigType) + if _, err := os.Stat(ConfigFilePath); os.IsNotExist(err) { + return "", errors.New(fmt.Sprintf("config file %s not exist", ConfigFilePath)) + } + + config, err := common.LoadConfig() + if err != nil { + return "", err + } + if config != nil { + return config.GetString(common.ConfigKeyGatewayPort), nil + } + return "", errors.New("config is nil") +} diff --git a/pkg/port_test.go b/pkg/port_test.go new file mode 100644 index 0000000..0ada09c --- /dev/null +++ b/pkg/port_test.go @@ -0,0 +1,43 @@ +package pkg_test + +import ( + "os" + "path/filepath" + "testing" + + "github.com/IceWhaleTech/CasaOS-Common/utils/constants" + "github.com/IceWhaleTech/CasaOS-Gateway/common" + "github.com/IceWhaleTech/CasaOS-Gateway/pkg" + "github.com/stretchr/testify/assert" +) + +const _confSample = `[common] +runtimepath=/var/run/casaos + +[gateway] +port=80` + +func setupGatewayConfig(t *testing.T) { + // the setup should only run in CICD environment + + ConfigFilePath := filepath.Join(constants.DefaultConfigPath, common.GatewayName+"."+common.GatewayConfigType) + if _, err := os.Stat(ConfigFilePath); os.IsNotExist(err) { + // create config file + file, err := os.Create(ConfigFilePath) + if err != nil { + panic(err) + } + defer file.Close() + + // write default config + _, err = file.WriteString(_confSample) + assert.NoError(t, err) + } +} + +func TestGetPort(t *testing.T) { + setupGatewayConfig(t) + port, err := pkg.GetGatewayPort() + assert.NoError(t, err) + assert.Equal(t, "80", port) +} From 0ee38cc1b8d0c7f7cf518df24baeb91b7b4fed8e Mon Sep 17 00:00:00 2001 From: CorrectRoadH Date: Fri, 12 Jul 2024 15:30:48 +0800 Subject: [PATCH 2/7] wip --- pkg/port.go | 13 ++++++++----- pkg/port_test.go | 48 +++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 53 insertions(+), 8 deletions(-) diff --git a/pkg/port.go b/pkg/port.go index 7c53aee..3154179 100644 --- a/pkg/port.go +++ b/pkg/port.go @@ -10,18 +10,21 @@ import ( "github.com/IceWhaleTech/CasaOS-Gateway/common" ) -func GetGatewayPort() (string, error) { +func GetGatewayPort() (int, error) { ConfigFilePath := filepath.Join(constants.DefaultConfigPath, common.GatewayName+"."+common.GatewayConfigType) if _, err := os.Stat(ConfigFilePath); os.IsNotExist(err) { - return "", errors.New(fmt.Sprintf("config file %s not exist", ConfigFilePath)) + return 0, errors.New(fmt.Sprintf("config file %s not exist", ConfigFilePath)) } config, err := common.LoadConfig() if err != nil { - return "", err + return 0, err } if config != nil { - return config.GetString(common.ConfigKeyGatewayPort), nil + // convert port to int + port := config.GetInt(common.ConfigKeyGatewayPort) + + return port, nil } - return "", errors.New("config is nil") + return 0, errors.New("config is nil") } diff --git a/pkg/port_test.go b/pkg/port_test.go index 0ada09c..fc12466 100644 --- a/pkg/port_test.go +++ b/pkg/port_test.go @@ -17,7 +17,13 @@ runtimepath=/var/run/casaos [gateway] port=80` -func setupGatewayConfig(t *testing.T) { +const _incorrectConfSample = `[common] +runtimepath=/var/run/casaos + +[gateway] +port=80` + +func setupGatewayConfig(t *testing.T) func() { // the setup should only run in CICD environment ConfigFilePath := filepath.Join(constants.DefaultConfigPath, common.GatewayName+"."+common.GatewayConfigType) @@ -33,11 +39,47 @@ func setupGatewayConfig(t *testing.T) { _, err = file.WriteString(_confSample) assert.NoError(t, err) } + return func() { + // remove config file + err := os.Remove(ConfigFilePath) + assert.NoError(t, err) + } +} + +func setupIncorrectGatewayConfig(t *testing.T) { + // the setup should only run in CICD environment + + ConfigFilePath := filepath.Join(constants.DefaultConfigPath, common.GatewayName+"."+common.GatewayConfigType) + if _, err := os.Stat(ConfigFilePath); os.IsNotExist(err) { + // create config file + file, err := os.Create(ConfigFilePath) + if err != nil { + panic(err) + } + defer file.Close() + + // write default config + _, err = file.WriteString(_incorrectConfSample) + assert.NoError(t, err) + } } func TestGetPort(t *testing.T) { - setupGatewayConfig(t) + defer setupGatewayConfig(t)() port, err := pkg.GetGatewayPort() assert.NoError(t, err) - assert.Equal(t, "80", port) + assert.Equal(t, 80, port) +} + +func TestGetBlankPort(t *testing.T) { + ConfigFilePath := filepath.Join(constants.DefaultConfigPath, common.GatewayName+"."+common.GatewayConfigType) + // only run in CICD environment + if _, err := os.Stat(ConfigFilePath); !os.IsNotExist(err) { + t.Skip("the test only run in CICD environment to avoid overwrite the config file") + } + + setupIncorrectGatewayConfig(t) + port, err := pkg.GetGatewayPort() + assert.NotNil(t, err) + assert.Equal(t, 0, port) } From 68acdfc5316a7e8a4b7138389da4a5c3068fb5c2 Mon Sep 17 00:00:00 2001 From: CorrectRoadH Date: Fri, 12 Jul 2024 15:32:42 +0800 Subject: [PATCH 3/7] wip --- pkg/port_test.go | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/pkg/port_test.go b/pkg/port_test.go index fc12466..1da02a6 100644 --- a/pkg/port_test.go +++ b/pkg/port_test.go @@ -26,6 +26,8 @@ port=80` func setupGatewayConfig(t *testing.T) func() { // the setup should only run in CICD environment + testInCICD := false + ConfigFilePath := filepath.Join(constants.DefaultConfigPath, common.GatewayName+"."+common.GatewayConfigType) if _, err := os.Stat(ConfigFilePath); os.IsNotExist(err) { // create config file @@ -38,11 +40,15 @@ func setupGatewayConfig(t *testing.T) func() { // write default config _, err = file.WriteString(_confSample) assert.NoError(t, err) + testInCICD = true } + return func() { - // remove config file - err := os.Remove(ConfigFilePath) - assert.NoError(t, err) + if testInCICD { + // remove config file + err := os.Remove(ConfigFilePath) + assert.NoError(t, err) + } } } From 6290cf8afcdf08e90681dbca639af07f1f4f7b02 Mon Sep 17 00:00:00 2001 From: CorrectRoadH Date: Fri, 12 Jul 2024 15:37:03 +0800 Subject: [PATCH 4/7] fix test --- pkg/port_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/port_test.go b/pkg/port_test.go index 1da02a6..c46278c 100644 --- a/pkg/port_test.go +++ b/pkg/port_test.go @@ -21,7 +21,7 @@ const _incorrectConfSample = `[common] runtimepath=/var/run/casaos [gateway] -port=80` +port=` func setupGatewayConfig(t *testing.T) func() { // the setup should only run in CICD environment From 0674805a0eda33f48fd73ffdeb0aaca5025b83ac Mon Sep 17 00:00:00 2001 From: CorrectRoadH Date: Fri, 12 Jul 2024 15:37:54 +0800 Subject: [PATCH 5/7] go mod tidy --- go.mod | 2 ++ go.sum | 2 -- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/go.mod b/go.mod index 4a3b9f1..f84667e 100644 --- a/go.mod +++ b/go.mod @@ -22,6 +22,8 @@ require ( github.com/godbus/dbus/v5 v5.1.0 // indirect github.com/golang-jwt/jwt v3.2.2+incompatible // indirect github.com/klauspost/cpuid/v2 v2.2.4 // indirect + github.com/labstack/gommon v0.4.2 // indirect + github.com/mattn/go-colorable v0.1.13 // indirect github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect github.com/rogpeppe/go-internal v1.10.0 // indirect github.com/sagikazarmark/locafero v0.4.0 // indirect diff --git a/go.sum b/go.sum index f6e2245..c7f14a0 100644 --- a/go.sum +++ b/go.sum @@ -85,8 +85,6 @@ github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U= github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc= -github.com/rogpeppe/go-internal v1.8.0/go.mod h1:WmiCO8CzOY8rg0OYDC4/i/2WRWAB6poM+XZ2dLUbcbE= github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ= github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog= github.com/sagikazarmark/locafero v0.4.0 h1:HApY1R9zGo4DBgr7dqsTH/JJxLTTsOt7u6keLGt6kNQ= From 6055ab1d157971a24dedb0b0fdcee8ead16fbb77 Mon Sep 17 00:00:00 2001 From: CorrectRoadH Date: Fri, 12 Jul 2024 15:40:56 +0800 Subject: [PATCH 6/7] fix cicd test --- pkg/port_test.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pkg/port_test.go b/pkg/port_test.go index c46278c..6ca88e8 100644 --- a/pkg/port_test.go +++ b/pkg/port_test.go @@ -31,6 +31,7 @@ func setupGatewayConfig(t *testing.T) func() { ConfigFilePath := filepath.Join(constants.DefaultConfigPath, common.GatewayName+"."+common.GatewayConfigType) if _, err := os.Stat(ConfigFilePath); os.IsNotExist(err) { // create config file + os.MkdirAll(constants.DefaultConfigPath, os.ModePerm) file, err := os.Create(ConfigFilePath) if err != nil { panic(err) @@ -58,6 +59,7 @@ func setupIncorrectGatewayConfig(t *testing.T) { ConfigFilePath := filepath.Join(constants.DefaultConfigPath, common.GatewayName+"."+common.GatewayConfigType) if _, err := os.Stat(ConfigFilePath); os.IsNotExist(err) { // create config file + os.MkdirAll(constants.DefaultConfigPath, os.ModePerm) file, err := os.Create(ConfigFilePath) if err != nil { panic(err) From 629dc17c62d6ba62556dedc23f63e4c256f2870b Mon Sep 17 00:00:00 2001 From: CorrectRoadH Date: Fri, 12 Jul 2024 16:13:42 +0800 Subject: [PATCH 7/7] wip --- pkg/port.go | 9 ----- pkg/port_test.go | 93 ------------------------------------------------ 2 files changed, 102 deletions(-) delete mode 100644 pkg/port_test.go diff --git a/pkg/port.go b/pkg/port.go index 3154179..7f636e6 100644 --- a/pkg/port.go +++ b/pkg/port.go @@ -2,20 +2,11 @@ package pkg import ( "errors" - "fmt" - "os" - "path/filepath" - "github.com/IceWhaleTech/CasaOS-Common/utils/constants" "github.com/IceWhaleTech/CasaOS-Gateway/common" ) func GetGatewayPort() (int, error) { - ConfigFilePath := filepath.Join(constants.DefaultConfigPath, common.GatewayName+"."+common.GatewayConfigType) - if _, err := os.Stat(ConfigFilePath); os.IsNotExist(err) { - return 0, errors.New(fmt.Sprintf("config file %s not exist", ConfigFilePath)) - } - config, err := common.LoadConfig() if err != nil { return 0, err diff --git a/pkg/port_test.go b/pkg/port_test.go deleted file mode 100644 index 6ca88e8..0000000 --- a/pkg/port_test.go +++ /dev/null @@ -1,93 +0,0 @@ -package pkg_test - -import ( - "os" - "path/filepath" - "testing" - - "github.com/IceWhaleTech/CasaOS-Common/utils/constants" - "github.com/IceWhaleTech/CasaOS-Gateway/common" - "github.com/IceWhaleTech/CasaOS-Gateway/pkg" - "github.com/stretchr/testify/assert" -) - -const _confSample = `[common] -runtimepath=/var/run/casaos - -[gateway] -port=80` - -const _incorrectConfSample = `[common] -runtimepath=/var/run/casaos - -[gateway] -port=` - -func setupGatewayConfig(t *testing.T) func() { - // the setup should only run in CICD environment - - testInCICD := false - - ConfigFilePath := filepath.Join(constants.DefaultConfigPath, common.GatewayName+"."+common.GatewayConfigType) - if _, err := os.Stat(ConfigFilePath); os.IsNotExist(err) { - // create config file - os.MkdirAll(constants.DefaultConfigPath, os.ModePerm) - file, err := os.Create(ConfigFilePath) - if err != nil { - panic(err) - } - defer file.Close() - - // write default config - _, err = file.WriteString(_confSample) - assert.NoError(t, err) - testInCICD = true - } - - return func() { - if testInCICD { - // remove config file - err := os.Remove(ConfigFilePath) - assert.NoError(t, err) - } - } -} - -func setupIncorrectGatewayConfig(t *testing.T) { - // the setup should only run in CICD environment - - ConfigFilePath := filepath.Join(constants.DefaultConfigPath, common.GatewayName+"."+common.GatewayConfigType) - if _, err := os.Stat(ConfigFilePath); os.IsNotExist(err) { - // create config file - os.MkdirAll(constants.DefaultConfigPath, os.ModePerm) - file, err := os.Create(ConfigFilePath) - if err != nil { - panic(err) - } - defer file.Close() - - // write default config - _, err = file.WriteString(_incorrectConfSample) - assert.NoError(t, err) - } -} - -func TestGetPort(t *testing.T) { - defer setupGatewayConfig(t)() - port, err := pkg.GetGatewayPort() - assert.NoError(t, err) - assert.Equal(t, 80, port) -} - -func TestGetBlankPort(t *testing.T) { - ConfigFilePath := filepath.Join(constants.DefaultConfigPath, common.GatewayName+"."+common.GatewayConfigType) - // only run in CICD environment - if _, err := os.Stat(ConfigFilePath); !os.IsNotExist(err) { - t.Skip("the test only run in CICD environment to avoid overwrite the config file") - } - - setupIncorrectGatewayConfig(t) - port, err := pkg.GetGatewayPort() - assert.NotNil(t, err) - assert.Equal(t, 0, port) -}