2
0
mirror of https://github.com/acepanel/panel.git synced 2026-02-04 07:57:21 +08:00

feat: 移动部分方法到utils

This commit is contained in:
耗子
2024-11-04 03:04:34 +08:00
parent bf8c832b32
commit e0daee4766
18 changed files with 73 additions and 144 deletions

View File

@@ -4,6 +4,7 @@ package tools
import (
"context"
"errors"
"fmt"
stdnet "net"
"net/http"
"slices"
@@ -175,3 +176,15 @@ func GetLocalIPv6() (string, error) {
local := conn.LocalAddr().(*stdnet.UDPAddr)
return local.IP.String(), nil
}
// FormatBytes 格式化bytes
func FormatBytes(size float64) string {
units := []string{"B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"}
i := 0
for ; size >= 1024 && i < len(units); i++ {
size /= 1024
}
return fmt.Sprintf("%.2f %s", size, units[i])
}

View File

@@ -6,38 +6,50 @@ import (
"github.com/stretchr/testify/suite"
)
type HelperTestSuite struct {
type ToolsTestSuite struct {
suite.Suite
}
func TestHelperTestSuite(t *testing.T) {
suite.Run(t, &HelperTestSuite{})
func TestToolsTestSuite(t *testing.T) {
suite.Run(t, &ToolsTestSuite{})
}
func (s *HelperTestSuite) TestGetMonitoringInfo() {
func (s *ToolsTestSuite) TestGetMonitoringInfo() {
s.NotNil(CurrentInfo(nil, nil))
}
func (s *HelperTestSuite) TestGetPublicIPv4() {
func (s *ToolsTestSuite) TestGetPublicIPv4() {
ip, err := GetPublicIPv4()
s.NoError(err)
s.NotEmpty(ip)
}
func (s *HelperTestSuite) TestGetPublicIPv6() {
func (s *ToolsTestSuite) TestGetPublicIPv6() {
ip, err := GetPublicIPv6()
s.Error(err)
s.Empty(ip)
}
func (s *HelperTestSuite) TestGetLocalIPv4() {
func (s *ToolsTestSuite) TestGetLocalIPv4() {
ip, err := GetLocalIPv4()
s.NoError(err)
s.NotEmpty(ip)
}
func (s *HelperTestSuite) TestGetLocalIPv6() {
func (s *ToolsTestSuite) TestGetLocalIPv6() {
ip, err := GetLocalIPv6()
s.Error(err)
s.Empty(ip)
}
func (s *ToolsTestSuite) TestFormatBytes() {
s.Equal("1.00 B", FormatBytes(1))
s.Equal("1.00 KB", FormatBytes(1024))
s.Equal("1.00 MB", FormatBytes(1024*1024))
s.Equal("1.00 GB", FormatBytes(1024*1024*1024))
s.Equal("1.00 TB", FormatBytes(1024*1024*1024*1024))
s.Equal("1.00 PB", FormatBytes(1024*1024*1024*1024*1024))
s.Equal("1.00 EB", FormatBytes(1024*1024*1024*1024*1024*1024))
s.Equal("1.00 ZB", FormatBytes(1024*1024*1024*1024*1024*1024*1024))
s.Equal("1.00 YB", FormatBytes(1024*1024*1024*1024*1024*1024*1024*1024))
}