This repository has been archived by the owner on Apr 29, 2020. It is now read-only.
/
test_helper.go
65 lines (54 loc) · 1.56 KB
/
test_helper.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package hoist
import (
"io/ioutil"
"os"
"os/user"
"runtime"
"github.com/square/p2/pkg/runit"
"github.com/square/p2/pkg/util"
)
func FakeHoistLaunchableForDirUUIDPod(dirName string) (*Launchable, *runit.ServiceBuilder) {
return fakeHoistLaunchableForDir(dirName, true)
}
func FakeHoistLaunchableForDirLegacyPod(dirName string) (*Launchable, *runit.ServiceBuilder) {
return fakeHoistLaunchableForDir(dirName, false)
}
func fakeHoistLaunchableForDir(dirName string, isUUIDPod bool) (*Launchable, *runit.ServiceBuilder) {
tempDir, _ := ioutil.TempDir("", "fakeenv")
launchableInstallDir := util.From(runtime.Caller(0)).ExpandPath(dirName)
launchable := &Launchable{
Id: "testLaunchable",
ServiceId: "testPod__testLaunchable",
RunAs: "testPod",
PodEnvDir: tempDir,
RootDir: launchableInstallDir,
P2Exec: util.From(runtime.Caller(0)).ExpandPath("fake_p2-exec"),
Version: "abc123",
EntryPoints: EntryPoints{
Paths: []string{"bin/launch"},
Implicit: true,
},
IsUUIDPod: isUUIDPod,
}
curUser, err := user.Current()
if err == nil {
launchable.RunAs = curUser.Username
}
sbTemp, _ := ioutil.TempDir("", "fakesvdir")
sb := &runit.ServiceBuilder{
RunitRoot: sbTemp,
}
executables, _ := launchable.Executables(sb)
for _, exe := range executables {
_ = os.MkdirAll(exe.Service.Path, 0644)
}
return launchable, sb
}
func CleanupFakeLaunchable(h *Launchable, s *runit.ServiceBuilder) {
if os.TempDir() != h.PodEnvDir {
_ = os.RemoveAll(h.PodEnvDir)
}
if os.TempDir() != s.RunitRoot {
_ = os.RemoveAll(s.RunitRoot)
}
}