github.com/devcamcar/cli@v0.0.0-20181107134215-706a05759d18/test/cli_init_test.go (about) 1 package test 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/fnproject/cli/testharness" 8 ) 9 10 func TestSettingFuncName(t *testing.T) { 11 t.Run("`fn init --name` should set the name in func.yaml", func(t *testing.T) { 12 t.Parallel() 13 h := testharness.Create(t) 14 defer h.Cleanup() 15 16 appName := h.NewAppName() 17 funcName := h.NewFuncName(appName) 18 dirName := funcName + "_dir" 19 h.Fn("init", "--runtime", "go", "--name", funcName, dirName).AssertSuccess() 20 21 h.Cd(dirName) 22 23 yamlFile := h.GetYamlFile("func.yaml") 24 if yamlFile.Name != funcName { 25 t.Fatalf("Name was not set to %s in func.yaml", funcName) 26 } 27 }) 28 } 29 30 func TestInitImage(t *testing.T) { 31 32 // NB this test creates a function with `rn init --runtime` then creates an init-image from that 33 // This will not be necessary when there are init-images publicly available to pull during this test 34 35 t.Run("`fn init --init-image=<...>` should produce a working function template", func(t *testing.T) { 36 h := testharness.Create(t) 37 var err error 38 39 // Create the init-image 40 appName := h.NewAppName() 41 origFuncName := h.NewFuncName(appName) 42 h.Fn("init", "--runtime", "go", origFuncName) 43 h.Cd(origFuncName) 44 45 origYaml := h.GetYamlFile("func.yaml") 46 origYaml.Name = "" 47 origYaml.Version = "" 48 h.WriteYamlFile("func.init.yaml", origYaml) 49 50 err = h.Exec("tar", "-cf", "go.tar", "func.go", "func.init.yaml", "Gopkg.toml") 51 if err != nil { 52 fmt.Println(err) 53 t.Fatal("Failed to create tarball for init-image") 54 } 55 56 const initDockerFile = `FROM alpine:latest 57 COPY go.tar / 58 CMD ["cat", "/go.tar"] 59 ` 60 h.WithFile("Dockerfile", initDockerFile, 0600) 61 62 err = h.Exec("docker", "build", "-t", origFuncName+"-init", ".") 63 if err != nil { 64 fmt.Println(err) 65 t.Fatal("Failed to create init-image") 66 } 67 68 // Hooray we have an init-image!! 69 // Lets use it 70 h.Cd("") 71 newFuncName := h.NewFuncName(appName) 72 73 h.Fn("init", "--init-image", origFuncName+"-init", newFuncName) 74 h.Cd(newFuncName) 75 h.Fn("--registry", "test", "deploy", "--local", "--no-bump", "--app", appName).AssertSuccess() 76 h.Fn("invoke", appName, newFuncName).AssertSuccess() 77 78 newYaml := h.GetYamlFile("func.yaml") 79 if newYaml.Name != newFuncName { 80 t.Fatalf("generated function name is %s not %s", newYaml.Name, newFuncName) 81 } 82 83 if newYaml.Version != "0.0.1" { 84 t.Fatalf("generated function version is %s not 0.0.1", newYaml.Version) 85 } 86 87 }) 88 }