rootcmd_test.go 2.55 KB
package test

import (
	"fmt"
	"net"
	"os/exec"
	"strings"
	"sync"
	"testing"
	"time"
)

func Test_initConfig(t *testing.T) {
	//prvKey, _ := crypto.HexToECDSA("e3b097b0c171e2489973a277b1546392db97e359505cd64b9b52966cb87a0f08")
	//fmt.Println("prvKey:", prvKey)
	//pubKey := common.Bytes2Hex(crypto.FromECDSAPub(&prvKey.PublicKey))
	//fmt.Println("pubKey:", pubKey)
	//address := crypto.PubkeyToAddress(prvKey.PublicKey)
	//fmt.Println("address:", address)

	//// JSON 2 数据
	//jsonData := `{
	//	"completed_at": "2023-07-02T02:13:48.764861Z",
	//	"output": ["ss","sss"]
	//}`
	//
	//// 解析 JSON 数据到 map[string]json.RawMessage
	//var m map[string]json.RawMessage
	//if err := json.Unmarshal([]byte(jsonData), &m); err != nil {
	//	fmt.Println("解析 JSON 数据时出错:", err)
	//	return
	//}
	//
	//// 解析 "output" 字段
	////var output [][]string
	////if err := json.Unmarshal(m["output"], &output); err != nil {
	////	fmt.Println("解析 output 字段时出错:", err)
	////	return
	////}
	//
	//var output []string
	//if err := json.Unmarshal(m["output"], &output); err != nil {
	//	fmt.Println("解析 output 字段时出错:", err)
	//	return
	//}
	//
	//// 输出结果
	//fmt.Println("Output Type:", output)

	wg := &sync.WaitGroup{}
	ticker := time.NewTicker(time.Second * 1)
	for {
		select {
		case <-ticker.C:
			go func(wg *sync.WaitGroup) {
				wg.Wait()
				fmt.Println("111")

				wg.Add(1)
				go Sell(wg)
				wg.Wait()

				fmt.Println("333")
			}(wg)
		}
	}
}

func Test_getDiskInfo(t *testing.T) {
	// 运行 df -h /var/lib/docker 命令并获取输出
	cmd := exec.Command("df", "/")
	out, err := cmd.Output()
	if err != nil {
		fmt.Println("Error:", err)
		return
	}

	// 将输出按换行符分割成多行
	lines := strings.Split(string(out), "\n")

	// 提取第二行,即包含文件系统信息的行
	if len(lines) >= 2 {
		fields := strings.Fields(lines[1]) // 将行按空格分割成多个字段
		if len(fields) >= 6 {
			// 打印各个字段的值
			fmt.Println("Filesystem:", fields[0])
			fmt.Println("Size:", fields[1])
			fmt.Println("Used:", fields[2])
			fmt.Println("Avail:", fields[3])
			fmt.Println("Use%:", fields[4])
			fmt.Println("Mounted on:", fields[5])
		}
	}
}

func TestNetworkDelay(t *testing.T) {
	start := time.Now()
	ip := "43.198.29.144"
	conn, err := net.Dial("ip4:icmp", ip)
	if err != nil {
		fmt.Println("Error:", err)
		return
	}
	defer conn.Close()

	duration := time.Since(start)
	fmt.Printf("Ping %s: %v\n", ip, duration)

}

func Sell(wg *sync.WaitGroup) {
	defer wg.Done()
	time.Sleep(6 * time.Second)
	fmt.Println("222")
}