ktgo/Magefile.go
2024-02-19 16:53:07 +03:00

271 строка
5,2 КиБ
Go

//go:build mage
package main
import (
"context"
"log"
"os"
"strings"
// mage:import
. "magefile/docker"
"github.com/magefile/mage/mg"
)
var (
GolangVolume = "golang.my"
tests_to_run = []string{}
)
func init() {
AppName = "ktgo"
ImageName = "my/go"
BuildFn = build
}
func Test() {
mg.Deps(TestLibs)
mg.Deps(TestIntegration)
mg.Deps(TestSharedLibsBuild)
}
func TestLibs(ctx context.Context) error {
paths := []string{
"pkg",
"pkg/printer",
"pkg/scenarios",
"pkg/link",
"pkg/link/wifi",
"pkg/parser",
"internal",
}
listTests(paths)
// err := addTest("test")
// if err != nil {
// println("ERROR: " + "test")
// return err
// }
return runListedTests()
}
func TestIntegration(ctx context.Context) error {
return runTests("./test/int")
}
func TestSystem(ctx context.Context) error {
return runTests("./test")
}
func listTests(paths []string) error {
for _, p := range paths {
listAndTest(p)
}
return nil
}
func listAndTest(p string) error {
entries, err := os.ReadDir(p)
if err != nil {
log.Fatal(err)
}
return testList(p, entries)
}
func testList(p string, entries []os.DirEntry) error {
for _, e := range entries {
testOne(p, e)
}
return nil
}
func testOne(p string, e os.DirEntry) error {
if !e.IsDir() {
return nil
}
if e.Name() == "driver" {
return nil
}
if e.Name() == "lib" {
return nil
}
if e.Name() == "testlib" {
return nil
}
entries, err := os.ReadDir(p + "/" + e.Name())
if err != nil {
log.Fatal(err)
}
found := false
for _, e2 := range entries {
if e2.IsDir() {
continue
}
if strings.Contains(e2.Name(), "_test.go") {
found = true
break
}
}
if !found {
return nil
}
testPath := p + "/" + e.Name()
err = addTest(testPath)
if err != nil {
println("ERROR: " + testPath)
return err
}
return nil
}
func addTest(p string) error {
tests_to_run = append(tests_to_run, "./"+p)
return nil
}
func runListedTests() error {
test_paths := strings.Join(tests_to_run, " ")
return runTests(test_paths)
}
func runTests(paths string) error {
Bash(`cp -f $HOME/.Xauthority /tmp/._test_Xauthority`)
return Bash(`sudo docker run -ti --rm \
-h host \
--net=none \
-v /etc/localtime:/etc/localtime:ro \
-v ` + GolangVolume + `:/usr/local/go:ro \
\
-v /gopath:/gopath:rw \
-v ${PWD}:/app \
-v /usr/bin/docker:/usr/bin/docker:ro \
-v /var/run/docker.sock:/var/run/docker.sock:ro \
-v /usr/bin/socat:/usr/bin/socat \
\
-e GOPATH=/gopath \
-e GOCACHE=/gopath/gocache \
-e APPDIR=${PWD} \
\
--device=/dev/dri/card0 \
-e DISPLAY=:0 \
-v /tmp/.X11-unix:/tmp/.X11-unix:ro \
-v /tmp/._test_Xauthority:/home/user/.Xauthority:ro \
\
-w /app \
-u 0 \
\
--entrypoint=/bin/bash \
\
` + ImageName + " -c '" + ` \
for t in ` + paths + `; do \
go test -v -p 1 -gcflags=-l -count=1 $t || exit 1; \
done'`)
}
func TestSharedLibsBuild(ctx context.Context) error {
mg.Deps(Build_shared_lib)
return Bash(`rm lib/lib3dscreen.*`)
}
func Build_shared_lib(ctx context.Context) error {
return Bash(`sudo docker run --rm \
-h host \
--net=none \
-v /etc/localtime:/etc/localtime:ro \
-v ` + GolangVolume + `:/usr/local/go:ro \
\
-v /gopath:/gopath:rw \
-v ${PWD}:/app \
\
-e GOPATH=/gopath \
-e GOCACHE=/gopath/gocache \
\
-w /app/pkg/sharedlib \
-u 1000 \
\
--entrypoint=/bin/bash \
\
` + ImageName + " -c '" + ` \
go build -o /app/lib/libktgo.so -buildmode=c-shared \
&& strip /app/lib/libktgo.so \
'`)
}
func Build_installer(ctx context.Context) error {
return Bash(`sudo docker run --rm \
-h host \
--net=none \
-v /etc/localtime:/etc/localtime:ro \
-v ` + GolangVolume + `:/usr/local/go:ro \
\
-v /gopath:/gopath:rw \
-v ${PWD}:/app \
\
-e GOPATH=/gopath \
-e GOCACHE=/gopath/gocache \
\
-w /app/cmd/installer \
-u 1000 \
\
--entrypoint=/bin/bash \
\
` + ImageName + " -c '" + ` \
mkdir -p /app/bin/ \
&& GOARCH=arm go build -o /app/install_helper \
' \
&& arm-linux-gnueabi-strip install_helper \
&& mv -fv install_helper ../../../src/installer/ \
`)
}
func build() {
Bash(`GOPHERJS_GOROOT=/gopath/go/1.18/ gopherjs build 3diy/3dscreen/web/main -o js/app.js \
&& ls -l js/app.js \
&& node -e 'go = require("./js/app.js"); console.log(go);'`)
}
func ControlLinkWifi0() {
Bash(`curl '0:5005?conn=Wifi&enabled=0'`)
}
func ControlLinkWifi1() {
Bash(`curl '0:5005?conn=Wifi&enabled=1'`)
}
func ControlLinkWifiConn0() {
Bash(`curl '0:5005?conn=Wifi&connected=0'`)
}
func ControlLinkWifiConn1() {
Bash(`curl '0:5005?conn=Wifi&connected=1&ip=192.168.1.150&gw=192.168.1.1'`)
}
func ControlLinkEther0() {
Bash(`curl '0:5005?conn=Ether&enabled=0'`)
}
func ControlLinkEther1() {
Bash(`curl '0:5005?conn=Ether&enabled=1'`)
}
func ControlLinkEtherConn0() {
Bash(`curl '0:5005?conn=Ether&connected=0'`)
}
func ControlLinkEtherConn1() {
Bash(`curl '0:5005?conn=Ether&connected=1&ip=192.168.1.150&gw=192.168.1.1'`)
}
func TestControlLinkStatus() {
Bash(`curl '0:5005/link_status?interface=eth0&reason=RECONFIGURE&ip=192.168.1.150&routers=192.168.1.1'`)
}
func TestControlZamenaInsertFilament() {
Bash(`curl '0:5005/send_from_terminal?m=Insert%20filament%20and%20press%20button'`)
}
func TestControlZamenaPurgeMore() {
Bash(`curl '0:5005/send_from_terminal?m=PurgeMore'`)
}