GitRoot
craft your forge, build your project, grow your community freely
1// SPDX-FileCopyrightText: 2025 Romain Maneschi <romain@gitroot.dev>
2//
3// SPDX-License-Identifier: EUPL-1.2
4
5package exec
6
7import (
8 "context"
9 "encoding/json"
10 "strings"
11 "testing"
12
13 "github.com/go-git/go-git/v5/plumbing"
14 "gitroot.dev/libs/golang/plugin/model"
15 "gitroot.dev/server/configuration"
16 "gitroot.dev/server/repository"
17 "gitroot.dev/server/user"
18)
19
20type fakeConf struct {
21 kind string
22}
23
24func (f *fakeConf) GetExecConf() configuration.ExecConf {
25 dc := configuration.ExecDefaultConf
26 switch f.kind {
27 case "bareMetal":
28 dc.BareMetal.Enabled = true
29 case "bwrap":
30 dc.Bwrap.Enabled = true
31 dc.Bwrap.RoBind = []string{"/tmp/:/cache/mise"}
32 case "container":
33 dc.Bwrap.Enabled = true
34 }
35 return dc
36}
37
38type testDate struct {
39 executor string
40 build string
41 status *model.ExecStatus
42 env []string
43}
44
45var testData = []*testDate{
46 {executor: "bareMetal"},
47 {executor: "bwrap", env: []string{"MISE_CACHE_DIR=/cache/mise"}},
48 {executor: "container", build: "docker.io/bash:5.3.3"},
49 {executor: "container", build: "./Containerfile"},
50}
51
52func TestExec(t *testing.T) {
53 for _, tt := range testData {
54 dataDir := t.TempDir()
55 conf := configuration.NewConfiguration(dataDir)
56 userManager, _ := user.NewManager(conf)
57 m := NewManager(t.Context(), &fakeConf{kind: tt.executor}, userManager)
58 repoManager := repository.NewManager(conf, userManager)
59 err := repoManager.CreateUserRepo(context.TODO(), repository.RepoConf{Name: "exec", DefaultBranch: "main"}, []user.SimpleUser{}, []byte(""))
60 if err != nil {
61 t.Fatal(err)
62 }
63 repo, err := repoManager.Open(t.Context(), "exec")
64 if err != nil {
65 t.Fatal(err)
66 }
67 repoWrite, _ := repo.WillWrite(plumbing.NewBranchReferenceName("main"))
68 repoWrite.Write("Containerfile", []byte("from docker.io/bash:5.3.3"))
69 repoWrite.CommitAll("add containerfile", userManager.RootCommiter())
70 repoWrite.Accept()
71 repoWrite.Storer().Commit()
72
73 status, err := m.Exec(repo, "main", model.Exec{
74 Build: tt.build,
75 Cmds: []model.Cmd{
76 //{Cmd: "/usr/bin/xrandr", Args: []string{}},
77 {Cmd: "ls", Args: []string{"-a", "."}},
78 //{Cmd: "cat", Args: []string{"/var/log/auth.log"}},
79 },
80 Env: tt.env,
81 })
82 if err != nil {
83 t.Fatal(err)
84 }
85
86 tt.status = status
87
88 goodCmd := 0
89 if len(tt.status.CmdsExec) > 1 {
90 goodCmd = 1
91 }
92 if !strings.Contains(tt.status.CmdsLogs[goodCmd], ".gitroot") {
93 //t.Fatal(status.CmdsLogs[0])
94 j, _ := json.Marshal(status)
95 t.Fatal(string(j))
96 }
97 }
98
99 t.Log("|executor|nb cmd|status|memory|cpu|nb thread|read|write|")
100 t.Log("|--------|------|------|------|---|---------|----|-----|")
101 for _, tt := range testData {
102 goodCmd := 0
103 if len(tt.status.CmdsExec) > 1 {
104 goodCmd = 1
105 }
106 stat := tt.status.CmdsStats[goodCmd]
107 t.Logf("|%s|%d|%d|%d|%d|%d|%d|%d|", tt.executor, len(tt.status.CmdsExec), tt.status.CmdsStatus[goodCmd], stat.MaxMemoryBytes, stat.TotalCPUTimeMs, stat.MaxThreads, stat.ReadBytesTotal, stat.WriteBytesTotal)
108 }
109 //uncomment to see stats
110 //t.FailNow()
111}