func TestCanWrapFilesOnlyReadableForRoot(t *testing.T) { if testing.Short() { t.SkipNow() } if err := os.MkdirAll("test", 0755); err != nil { t.Fatal(err) } c, err := docker.NewClientFromEnv() if err != nil { t.Fatal(err) } defer func() { app.Main([]string{ "involucro", "-e", "inv.task('x').using('busybox').run('/bin/sh', '-c', 'rm -f /source/test/only_root')", "x", }) c.RemoveImage("inttest/wrap_root") }() if err := app.Main([]string{ "involucro", "-e", "inv.task('x').using('busybox').run('/bin/sh', '-c', 'echo FLAG > /source/test/only_root && chmod 0400 /source/test/only_root')", "x", }); err != nil { t.Fatal(err) } file, err := os.Open("test/only_root") if err == nil { file.Close() t.Fatal("File opening succeeded") } if !os.IsPermission(err) { t.Fatal("Error was not a permission error, but", err) } if err := app.Main([]string{ "involucro", "-e", "inv.task('w').wrap('test').inImage('busybox').at('/data').as('inttest/wrap_root')", "w", }); err != nil { t.Fatal(err) } assertStdoutContainsFlag([]string{ "-e", "inv.task('x').using('inttest/wrap_root').run('cat', '/data/only_root')", "x", }, "FLAG", t) }
func TestWrapCurrentDir(t *testing.T) { if testing.Short() { t.SkipNow() } dir, err := ioutil.TempDir("", "inttest-58") if err != nil { t.Fatal(err) } defer os.RemoveAll(dir) if err := ioutil.WriteFile(filepath.Join(dir, "a"), []byte("123"), 0755); err != nil { t.Fatal(err) } c, err := docker.NewClientFromEnv() if err != nil { t.Fatal(err) } cwd, err := os.Getwd() if err != nil { t.Fatal(err) } defer func() { c.RemoveImage("inttest/58") os.Chdir(cwd) }() if err := os.Chdir(dir); err != nil { t.Fatal(err) } if err := app.Main([]string{ "involucro", "-e", "inv.task('wrap').wrap('.').inImage('busybox').at('/data').as('inttest/15')", "wrap", }); err != nil { t.Fatal(err) } if err := app.Main([]string{ "involucro", "-e", "inv.task('x').using('inttest/15').run('grep', '123', '/data/a')", "x", }); err != nil { t.Error(err) } }
func TestWrapNestedDirsCorrectly(t *testing.T) { if testing.Short() { t.SkipNow() } dir, err := ioutil.TempDir("", "inttest-15") if err != nil { t.Fatal(err) } defer os.RemoveAll(dir) nestedPath := filepath.Join(dir, "asd", "p", "aaa") if err := os.MkdirAll(nestedPath, 0755); err != nil { t.Fatal(err) } if err := ioutil.WriteFile(filepath.Join(nestedPath, "a"), []byte("123"), 0755); err != nil { t.Fatal(err) } if err := ioutil.WriteFile(filepath.Join(nestedPath, "b"), []byte("456"), 0755); err != nil { t.Fatal(err) } c, err := docker.NewClientFromEnv() if err != nil { t.Fatal(err) } defer func() { c.RemoveImage("inttest/15") }() if err := app.Main([]string{ "involucro", "-e", "inv.task('wrap').wrap('.').inImage('busybox').at('/data').as('inttest/15')", "-w", dir, "wrap", }); err != nil { t.Fatal(err) } if err := app.Main([]string{ "involucro", "-e", "inv.task('x').using('inttest/15').run('grep', '123', '/data/asd/p/aaa/a').run('grep', '456', '/data/asd/p/aaa/b')", "x", }); err != nil { t.Error(err) } }
func TestFileHandlingSymlinks(t *testing.T) { if testing.Short() { t.SkipNow() } dir, err := ioutil.TempDir("", "inttest-26") if err != nil { t.Fatal(err) } defer os.RemoveAll(dir) if err := os.Mkdir(filepath.Join(dir, "p"), 0755); err != nil { t.Fatal(err) } if err, ok := os.Symlink("../p", filepath.Join(dir, "p", "cur")).(*os.LinkError); ok && err != nil { t.Fatal(err) } c, err := docker.NewClientFromEnv() if err != nil { t.Fatal(err) } args := []string{ "involucro", "-e", "inv.task('wrap').wrap('" + dir + "').at('/data').inImage('busybox').as('inttest/26')", "wrap", } defer func() { c.RemoveImage("inttest/26") }() if err := app.Main(args); err != nil { t.Fatal(err) } args = []string{ "involucro", "-e", "inv.task('test').using('inttest/26').run('ls', '/data/p/cur/cur/cur/cur')", "test", } if err := app.Main(args); err != nil { t.Error(err) } }
func assertStdoutContainsFlag(args []string, lineFlag string, t *testing.T) { oldPrint := ilog.StdLog.PrintFunc() defer ilog.StdLog.SetPrintFunc(oldPrint) args = append([]string{"involucro", "-v=2"}, args...) var found bool ilog.StdLog.SetPrintFunc(func(b ilog.Bough) { if testing.Verbose() && oldPrint != nil { oldPrint(b) } if b.Prefix == "SOUT" && b.Message == lineFlag { found = true } }) if err := app.Main(args); err != nil { debug.PrintStack() t.Fatal(err) } if !found { t.Error("Did not find expected flag", lineFlag) } }
func TestTagging(t *testing.T) { if testing.Short() { t.SkipNow() } c, err := docker.NewClientFromEnv() if err != nil { t.Fatal(err) } defer func() { c.RemoveImage("inttest/20:v1") c.RemoveImage("inttest/20_b") }() if err := app.Main([]string{ "involucro", "-e", "inv.task('package').wrap('.').inImage('busybox').at('/data').as('inttest/20:v1').tag('inttest/20:v1').as('inttest/20_b')", "package", }); err != nil { t.Fatal(err) } image, err := c.InspectImage("inttest/20:v1") if err != nil { t.Fatal(err) } image2, err := c.InspectImage("inttest/20_b") if err != nil { t.Fatal(err) } if err := app.Main([]string{ "involucro", "-e", "inv.task('run').using('inttest/20:v1').run('true')", "run", }); err != nil { t.Fatal(err) } if image.ID != image2.ID { t.Error("Images do not share an ID", image.ID, image2.ID) } }
func TestExpectationMatchExitCode(t *testing.T) { if testing.Short() { t.SkipNow() } if err := app.Main([]string{ "involucro", "-e", "inv.task('test').using('busybox').withExpectation({code = 1}).run('false')", "test", }); err != nil { t.Error(err) } }
func TestExpectationMatchStdout(t *testing.T) { if testing.Short() { t.SkipNow() } if err := app.Main([]string{ "involucro", "-e", "inv.task('test').using('busybox').withExpectation({stdout = 'Hello, World!'}).run('echo', 'Hello, World!')", "test", }); err != nil { t.Error(err) } }
func TestTaskListWithTasksAndDirectScript(t *testing.T) { if testing.Short() { t.SkipNow() } testStdoutOf(func() error { return app.Main([]string{ "involucro", "-e", "inv.task('a').using('busybox').run('x'); inv.task('b').using('busybox').run('z')", "--tasks", }) }, "a\nb\n", t) }
func main() { err := app.Main(os.Args) switch err { case flag.ErrHelp: os.Exit(0) case nil: os.Exit(0) default: ilog.Error.Logf("Task processing failed: %s", err) os.Exit(1) } }
func TestExpectationsCrashesWhenExitCodeNotMet(t *testing.T) { if testing.Short() { t.SkipNow() } if err := app.Main([]string{ "involucro", "-e", "inv.task('test').using('busybox').withExpectation({code = 1}).run('true')", "test", }); err == nil { t.Error("Expected error") } }
func TestExpectationsCrashesWhenStderrNotMet(t *testing.T) { if testing.Short() { t.SkipNow() } if err := app.Main([]string{ "involucro", "-e", "inv.task('test').using('busybox').withExpectation({stderr = 'Hello, World'}).run('/bin/sh', '-c', 'echo Hello, Moon 1>&2')", "test", }); err == nil { t.Error("Expected error") } }
func TestRuntaskOtherTaskNotPresent(t *testing.T) { if testing.Short() { t.SkipNow() } if err := app.Main([]string{ "involucro", "-e", "inv.task('test').runTask('udef')", "test", }); err != nil { t.Error(err) } }
func TestWrapOptionSetEntrypoint(t *testing.T) { if testing.Short() { t.SkipNow() } c, err := docker.NewClientFromEnv() if err != nil { t.Fatal(err) } defer func() { c.RemoveImage("inttest/14") }() cases := []string{ "inv.task('wrap').wrap('.').inImage('busybox').at('/data').withConfig({Entrypoint = {'/bin/echo', 'Hello_Options'}}).as('inttest/14')", "inv.task('wrap').wrap('.').at('/data').withConfig({Entrypoint = {'/bin/echo', 'Hello_Options'}}).as('inttest/14')", "inv.task('wrap').wrap('.').inImage('alpine').at('/data').withConfig({Entrypoint = {'/bin/echo', 'Hello_Options'}}).as('inttest/14')", } for index, el := range cases { if err := app.Main([]string{"involucro", "-e", el, "wrap"}); err != nil { t.Error(err) } image, err := c.InspectImage("inttest/14") if err != nil { t.Fatalf("Test case %v failed with %v", index, err) } if image == nil { t.Error("Image is nil") } if image.Config == nil { t.Error("Image Config is nil") } if len(image.Config.Entrypoint) != 2 { t.Error("Unexpected entrypoint", image.Config.Entrypoint) } if image.Config.Entrypoint[0] != "/bin/echo" { t.Error("Unexpected first entrypoint element", image.Config.Entrypoint[0]) } if image.Config.Entrypoint[1] != "Hello_Options" { t.Error("Unexpected first entrypoint element", image.Config.Entrypoint[0]) } if err := c.RemoveImage("inttest/14"); err != nil { t.Error("Unable to remove image", err) } } }
func TestParametersRejectBothInlineScriptAndFilename(t *testing.T) { if testing.Short() { t.SkipNow() } if err := app.Main([]string{ "involucro", "-e", "inv.task('test').runTask('udef')", "-f", "custom-invfile.lua", "test", }); err == nil { t.Error("Did not fail") } }
func TestAutopullImage(t *testing.T) { if testing.Short() { t.SkipNow() } c, err := docker.NewClientFromEnv() if err != nil { t.Fatal(err) } c.RemoveImage("tianon/true") if err := app.Main([]string{ "involucro", "-e", "inv.task('test').using('tianon/true').run()", "test", }); err != nil { t.Error(err) } }
func TestFlexibleMountDirs(t *testing.T) { if testing.Short() { t.SkipNow() } pwd, err := filepath.Abs(".") if err != nil { t.Fatal(err) } cases := []string{ "./int21:/ttt", pwd + "/int21:/ttt", } if err := os.MkdirAll("int21", 0755); err != nil { t.Fatal(err) } for _, el := range cases { if err := ioutil.WriteFile(filepath.Join(pwd, "int21", "testfile"), []byte{0}, 0755); err != nil { t.Error("case failed", err) continue } if err := app.Main([]string{ "involucro", "-e", "inv.task('p').using('busybox').withHostConfig({Binds = {'" + el + "'}}).run('rm', '/ttt/testfile')", "p", }); err != nil { t.Fatal(err) } if _, err := os.Stat(filepath.Join(pwd, "int21", "testfile")); (err == nil) || (!os.IsNotExist(err)) { t.Error("Unexpected error", err) } } }
func TestFromScratchWithoutParentImage(t *testing.T) { if testing.Short() { t.SkipNow() } c, err := docker.NewClientFromEnv() if err != nil { t.Fatal("Unexpected error", err) } args := []string{ "involucro", "-e", "inv.task('package').wrap('.').at('/').as('inttest/16')", "package", } defer func() { c.RemoveImage("inttest/16") }() if err := app.Main(args); err != nil { t.Fatal(err) } history, err := c.ImageHistory("inttest/16") if err != nil { t.Fatal(err) } if len(history) != 2 { t.Fatal("Unexpected history length, was expecting 2", len(history)) } if history[0].Size != 0 { t.Error("Top-most layer is not config-only, but has size", history[0].Size) } }
func TestRemoteWrappableViaTcp(t *testing.T) { if testing.Short() { t.SkipNow() } if err := os.MkdirAll("fixture_09", 0755); err != nil { t.Fatal(err) } if err := ioutil.WriteFile("fixture_09/asd", []byte("blahblubb\n"), 0200); err != nil { t.Fatal(err) } defer os.RemoveAll("fixture_09") ln, err := net.Listen("tcp4", "127.0.0.1:4243") if err != nil { t.Fatal(err) } defer ln.Close() go func() { for { c, err := ln.Accept() if err != nil { break } defer c.Close() upstream, err := net.Dial("unix", "/var/run/docker.sock") if err != nil { break } go func(c, upstream net.Conn) { defer upstream.Close() io.Copy(c, upstream) }(c, upstream) go func(c, upstream net.Conn) { defer c.Close() io.Copy(upstream, c) }(c, upstream) } }() c, err := docker.NewClientFromEnv() if err != nil { t.Fatal(err) } defer func() { c.RemoveImage("inttest/9") }() pwd, err := os.Getwd() if err != nil { t.Fatal(err) } if err := app.Main([]string{ "involucro", "-H", "tcp://127.0.0.1:4243", "-v=2", "-w", pwd, "-e", "inv.task('wrap').wrap('fixture_09').at('/blah').inImage('busybox').as('inttest/9')", "wrap", }); err != nil { t.Fatal(err) } _, err = c.InspectImage("inttest/9") if err != nil { t.Fatal(err) } assertStdoutContainsFlag([]string{ "-e", "inv.task('a').using('inttest/9').run('/bin/cat', '/blah/asd')", "a", }, "blahblubb", t) }