github.com/devcamcar/cli@v0.0.0-20181107134215-706a05759d18/test/cli_misc_test.go (about) 1 package test 2 3 import ( 4 "fmt" 5 "log" 6 "net/url" 7 "os" 8 "strings" 9 "testing" 10 11 "github.com/fnproject/cli/testharness" 12 ) 13 14 func TestFnVersion(t *testing.T) { 15 t.Parallel() 16 17 tctx := testharness.Create(t) 18 res := tctx.Fn("version") 19 res.AssertSuccess() 20 } 21 22 func withMinimalFunction(h *testharness.CLIHarness) { 23 24 h.CopyFiles(map[string]string{ 25 "simplefunc/vendor": "vendor", 26 "simplefunc/func.go": "func.go", 27 "simplefunc/Gopkg.lock": "Gopkg.lock", 28 "simplefunc/Gopkg.toml": "Gopkg.toml", 29 "simplefunc/func.yaml": "func.yaml", 30 }) 31 } 32 33 34 // this is messy and nasty as we generate different potential values for FN_API_URL based on its type 35 func fnApiUrlVariations(t *testing.T) []string { 36 37 srcUrl := os.Getenv("FN_API_URL") 38 39 if srcUrl == "" { 40 srcUrl = "http://localhost:8080/" 41 } 42 43 if !strings.HasPrefix(srcUrl, "http:") && !strings.HasPrefix(srcUrl, "https:") { 44 srcUrl = "http://" + srcUrl 45 } 46 parsed, err := url.Parse(srcUrl) 47 48 if err != nil { 49 t.Fatalf("Invalid/unparsable TEST_API_URL %s: %s", srcUrl, err) 50 } 51 52 var cases []string 53 54 if parsed.Scheme == "http" { 55 cases = append(cases, "http://"+parsed.Host+parsed.Path) 56 cases = append(cases, parsed.Host+parsed.Path) 57 cases = append(cases, parsed.Host) 58 } else if parsed.Scheme == "https" { 59 cases = append(cases, "https://"+parsed.Host+parsed.Path) 60 cases = append(cases, "https://"+parsed.Host) 61 } else { 62 log.Fatalf("Unsupported url scheme for testing %s: %s", srcUrl, parsed.Scheme) 63 } 64 65 return cases 66 } 67 68 func TestFnApiUrlSupportsDifferentFormats(t *testing.T) { 69 t.Parallel() 70 71 h := testharness.Create(t) 72 defer h.Cleanup() 73 74 for _, candidateUrl := range fnApiUrlVariations(t) { 75 h.WithEnv("FN_API_URL", candidateUrl) 76 h.Fn("list", "apps").AssertSuccess() 77 } 78 } 79 80 // Not sure what this test was intending (copied from old test.sh) 81 func TestSettingTimeoutWorks(t *testing.T) { 82 t.Parallel() 83 84 h := testharness.Create(t) 85 defer h.Cleanup() 86 h.WithEnv("FN_REGISTRY", "some_random_registry") 87 88 appName := h.NewAppName() 89 h.Fn("create", "app", appName).AssertSuccess() 90 res := h.Fn("list", "apps") 91 92 if !strings.Contains(res.Stdout, fmt.Sprintf("%s", appName)) { 93 t.Fatalf("Expecting list apps to contain app name , got %v", res) 94 } 95 96 funcName := h.NewFuncName(appName) 97 98 h.MkDir(funcName) 99 h.Cd(funcName) 100 withMinimalFunction(h) 101 h.FileAppend("func.yaml", "\ntimeout: 50\n\nschema_version: 20180708\n") 102 h.Fn("--verbose", "deploy", "--app", appName, "--local").AssertSuccess() 103 h.Fn("invoke", appName, funcName).AssertSuccess() 104 105 inspectRes := h.Fn("inspect", "fn", appName, funcName) 106 inspectRes.AssertSuccess() 107 if !strings.Contains(inspectRes.Stdout, `"timeout": 50`) { 108 t.Errorf("Expecting fn inspect to contain CPU %v", inspectRes) 109 } 110 111 h.Fn("create", "fn", appName, "another", "some_random_registry/"+funcName+":0.0.2").AssertSuccess() 112 113 h.Fn("invoke", appName, "another").AssertSuccess() 114 } 115 116 //Memory doesn't seem to get persisted/returned 117 func TestSettingMemoryWorks(t *testing.T) { 118 t.Parallel() 119 120 h := testharness.Create(t) 121 defer h.Cleanup() 122 h.WithEnv("FN_REGISTRY", "some_random_registry") 123 124 appName := h.NewAppName() 125 h.Fn("create", "app", appName).AssertSuccess() 126 res := h.Fn("list", "apps") 127 128 if !strings.Contains(res.Stdout, fmt.Sprintf("%s", appName)) { 129 t.Fatalf("Expecting list apps to contain app name , got %v", res) 130 } 131 132 funcName := h.NewFuncName(appName) 133 134 h.MkDir(funcName) 135 h.Cd(funcName) 136 withMinimalFunction(h) 137 h.FileAppend("func.yaml", "memory: 100\nschema_version: 20180708\n") 138 h.Fn("--verbose", "deploy", "--app", appName, "--local").AssertSuccess() 139 h.Fn("invoke", appName, funcName).AssertSuccess() 140 141 inspectRes := h.Fn("inspect", "fn", appName, funcName) 142 inspectRes.AssertSuccess() 143 if !strings.Contains(inspectRes.Stdout, `"memory": 100`) { 144 t.Errorf("Expecting fn inspect to contain CPU %v", inspectRes) 145 } 146 147 h.Fn("create", "fn", appName, "another", "some_random_registry/"+funcName+":0.0.2").AssertSuccess() 148 149 h.Fn("invoke", appName, "another").AssertSuccess() 150 } 151 152 func TestAllMainCommandsExist(t *testing.T) { 153 t.Parallel() 154 155 h := testharness.Create(t) 156 defer h.Cleanup() 157 158 testCommands := []string{ 159 "build", 160 "bump", 161 "call", 162 "create", 163 "delete", 164 "deploy", 165 "get", 166 "init", 167 "inspect", 168 "list", 169 "push", 170 "run", 171 "set", 172 "start", 173 "test", 174 "unset", 175 "update", 176 "use", 177 "version", 178 } 179 180 for _, cmd := range testCommands { 181 res := h.Fn(cmd) 182 if strings.Contains(res.Stderr, "command not found") { 183 t.Errorf("Expected command %s to exist", cmd) 184 } 185 } 186 } 187 188 func TestAppYamlDeploy(t *testing.T) { 189 t.Parallel() 190 191 h := testharness.Create(t) 192 defer h.Cleanup() 193 194 appName := h.NewAppName() 195 fnName := h.NewFuncName(appName) 196 h.WithFile("app.yaml", fmt.Sprintf(`name: %s`, appName), 0644) 197 h.MkDir(fnName) 198 h.Cd(fnName) 199 withMinimalFunction(h) 200 h.Cd("") 201 h.Fn("deploy", "--all", "--local").AssertSuccess() 202 h.Fn("invoke", appName, fnName).AssertSuccess() 203 h.Fn("deploy", "--all", "--local").AssertSuccess() 204 h.Fn("invoke", appName, fnName).AssertSuccess() 205 } 206 207 func TestBump(t *testing.T) { 208 t.Parallel() 209 210 h := testharness.Create(t) 211 defer h.Cleanup() 212 213 expectFuncYamlVersion := func(v string) { 214 funcYaml := h.GetFile("func.yaml") 215 if !strings.Contains(funcYaml, fmt.Sprintf("version: %s", v)) { 216 t.Fatalf("Exepected version to be %s but got %s", v, funcYaml) 217 } 218 219 } 220 221 appName := h.NewAppName() 222 fnName := h.NewFuncName(appName) 223 h.MkDir(fnName) 224 h.Cd(fnName) 225 withMinimalFunction(h) 226 227 expectFuncYamlVersion("0.0.1") 228 229 h.Fn("bump").AssertSuccess() 230 expectFuncYamlVersion("0.0.2") 231 232 h.Fn("bump", "--major").AssertSuccess() 233 expectFuncYamlVersion("1.0.0") 234 235 h.Fn("bump").AssertSuccess() 236 expectFuncYamlVersion("1.0.1") 237 238 h.Fn("bump", "--minor").AssertSuccess() 239 expectFuncYamlVersion("1.1.0") 240 241 h.Fn("deploy", "--local", "--app", appName).AssertSuccess() 242 expectFuncYamlVersion("1.1.1") 243 244 h.Fn("i", "function", appName, fnName).AssertSuccess().AssertStdoutContains(fmt.Sprintf(`%s:1.1.1`, fnName)) 245 246 h.Fn("deploy", "--local", "--no-bump", "--app", appName).AssertSuccess() 247 expectFuncYamlVersion("1.1.1") 248 249 h.Fn("i", "function", appName, fnName).AssertSuccess().AssertStdoutContains(fmt.Sprintf(`%s:1.1.1`, fnName)) 250 251 } 252 253 // test fn list id value matches fn inspect id value for app, function & trigger 254 func TestListID(t *testing.T) { 255 t.Parallel() 256 257 h := testharness.Create(t) 258 defer h.Cleanup() 259 260 // execute create app, func & trigger commands 261 appName := h.NewAppName() 262 funcName := h.NewFuncName(appName) 263 triggerName := h.NewTriggerName(appName, funcName) 264 h.Fn("create", "app", appName).AssertSuccess() 265 h.Fn("create", "function", appName, funcName, "foo/duffimage:0.0.1").AssertSuccess() 266 h.Fn("create", "trigger", appName, funcName, triggerName, "--type", "http", "--source", "/mytrigger").AssertSuccess() 267 268 h.Fn("list", "apps", appName).AssertSuccess().AssertStdoutContains("ID") 269 h.Fn("list", "fn", appName).AssertSuccess().AssertStdoutContains("ID") 270 h.Fn("list", "triggers", appName).AssertSuccess().AssertStdoutContains("ID") 271 272 }