1
0
mirror of https://github.com/1Panel-dev/1Panel.git synced 2025-01-20 08:49:16 +08:00
1Panel/backend/app/service/database_test.go

72 lines
1.7 KiB
Go
Raw Normal View History

2022-10-21 18:50:38 +08:00
package service
import (
2022-10-25 18:34:33 +08:00
"encoding/json"
2022-10-21 18:50:38 +08:00
"fmt"
2022-10-25 18:34:33 +08:00
"os/exec"
"strings"
2022-10-21 18:50:38 +08:00
"testing"
"github.com/1Panel-dev/1Panel/backend/app/dto"
2022-10-21 18:50:38 +08:00
_ "github.com/go-sql-driver/mysql"
)
func TestMysql(t *testing.T) {
2022-10-25 18:34:33 +08:00
cmd := exec.Command("docker", "exec", "-i", "1Panel-mysql5.7-RnzE", "mysql", "-uroot", "-pCalong@2016", "-e", "show global variables;")
stdout, err := cmd.CombinedOutput()
2022-10-21 18:50:38 +08:00
if err != nil {
fmt.Println(err)
}
2022-10-25 18:34:33 +08:00
kk := strings.Split(string(stdout), "\n")
testMap := make(map[string]interface{})
for _, v := range kk {
itemRow := strings.Split(v, "\t")
if len(itemRow) == 2 {
testMap[itemRow[0]] = itemRow[1]
2022-10-21 18:50:38 +08:00
}
}
2022-10-25 18:34:33 +08:00
var info dto.MysqlVariables
arr, err := json.Marshal(testMap)
if err != nil {
fmt.Println(err)
}
2022-10-25 18:34:33 +08:00
_ = json.Unmarshal(arr, &info)
fmt.Print(info)
// fmt.Println(string(stdout))
// for {
// str, err := hr.Reader.ReadString('\n')
// if err == nil {
// testMap := make(map[string]interface{})
// err = json.Unmarshal([]byte(str), &testMap)
// fmt.Println(err)
// for k, v := range testMap {
// fmt.Println(k, v)
// }
// // fmt.Print(str)
// } else if err == io.EOF {
// // ReadString最后会同EOF和最后的数据一起返回
// fmt.Println(str)
// break
// } else {
// fmt.Println("出错!!")
// return
// }
// }
// input, err := hr.Reader.ReadString('\n')
// if err == nil {
// fmt.Printf("The input was: %s\n", input)
// }
2022-10-25 18:34:33 +08:00
// _, err = hr.Conn.Write([]byte("show global variables; \n"))
// if err != nil {
// fmt.Println(err)
// }
// time.Sleep(3 * time.Second)
// buf1 := make([]byte, 1024)
// _, err = hr.Reader.Read(buf1)
// if err != nil {
// fmt.Println(err)
// }
// fmt.Println(string(buf1))
}