From 629dc17c62d6ba62556dedc23f63e4c256f2870b Mon Sep 17 00:00:00 2001 From: CorrectRoadH Date: Fri, 12 Jul 2024 16:13:42 +0800 Subject: [PATCH] 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) -}