github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/client/allocrunner/taskrunner/getter/testing.go (about) 1 package getter 2 3 import ( 4 "os" 5 "path/filepath" 6 "testing" 7 8 cconfig "github.com/hashicorp/nomad/client/config" 9 "github.com/hashicorp/nomad/helper/testlog" 10 sconfig "github.com/hashicorp/nomad/nomad/structs/config" 11 "github.com/shoenig/test/must" 12 ) 13 14 // TestSandbox creates a real artifact downloader configured via the default 15 // artifact config. It is good enough for tests so no mock implementation exists. 16 func TestSandbox(t *testing.T) *Sandbox { 17 ac, err := cconfig.ArtifactConfigFromAgent(sconfig.DefaultArtifactConfig()) 18 must.NoError(t, err) 19 return New(ac, testlog.HCLogger(t)) 20 } 21 22 // SetupDir creates a directory suitable for testing artifact - i.e. it is 23 // owned by the nobody user as would be the case in a normal client operation. 24 // 25 // returns alloc_dir, task_dir 26 func SetupDir(t *testing.T) (string, string) { 27 uid, gid := credentials() 28 29 allocDir := t.TempDir() 30 taskDir := filepath.Join(allocDir, "local") 31 topDir := filepath.Dir(allocDir) 32 33 must.NoError(t, os.Chown(topDir, int(uid), int(gid))) 34 must.NoError(t, os.Chmod(topDir, 0o755)) 35 36 must.NoError(t, os.Chown(allocDir, int(uid), int(gid))) 37 must.NoError(t, os.Chmod(allocDir, 0o755)) 38 39 must.NoError(t, os.Mkdir(taskDir, 0o755)) 40 must.NoError(t, os.Chown(taskDir, int(uid), int(gid))) 41 must.NoError(t, os.Chmod(taskDir, 0o755)) 42 return allocDir, taskDir 43 }