github.com/iron-io/functions@v0.0.0-20180820112432-d59d7d1c40b2/api/server/integration_test.go (about) 1 // +build integration 2 3 package server 4 5 import ( 6 "context" 7 "fmt" 8 "os" 9 "strconv" 10 "testing" 11 "time" 12 13 "github.com/iron-io/functions/api/models" 14 "github.com/iron-io/functions/fn/app" 15 "github.com/spf13/viper" 16 "github.com/urfave/cli" 17 ) 18 19 var DB_FILE string 20 var MQ_FILE string 21 var API_URL string 22 var PORT int 23 var funcServer *Server 24 var Cancel context.CancelFunc 25 var Ctx context.Context 26 var fn *cli.App 27 28 func setupServer() { 29 viper.Set(EnvDBURL, fmt.Sprintf("bolt://%s?bucket=funcs", DB_FILE)) 30 viper.Set(EnvMQURL, fmt.Sprintf("bolt://%s", MQ_FILE)) 31 viper.Set(EnvPort, PORT) 32 Ctx, Cancel = context.WithCancel(context.Background()) 33 funcServer = NewFromEnv(Ctx) 34 go funcServer.Start(Ctx) 35 time.Sleep(2 * time.Second) 36 } 37 38 func setupCli() { 39 viper.Set("API_URL", API_URL) 40 fn = app.NewFn() 41 } 42 43 func teardown() { 44 os.Remove(DB_FILE) 45 os.Remove(MQ_FILE) 46 Cancel() 47 time.Sleep(2 * time.Second) 48 } 49 50 func TestIntegration(t *testing.T) { 51 DB_FILE = "/tmp/bolt_fn_db.db" 52 MQ_FILE = "/tmp/bolt_fn_mq.db" 53 PORT = 8080 54 API_URL = "http://localhost:8080" 55 setupServer() 56 setupCli() 57 testIntegration(t) 58 teardown() 59 } 60 61 func TestIntegrationWithAuth(t *testing.T) { 62 viper.Set("jwt_auth_key", "test") 63 DB_FILE = "/tmp/bolt_fn_auth_db.db" 64 MQ_FILE = "/tmp/bolt_fn_auth_mq.db" 65 PORT = 8081 66 API_URL = "http://localhost:8081" 67 setupServer() 68 setupCli() 69 testIntegration(t) 70 teardown() 71 } 72 73 func testIntegration(t *testing.T) { 74 // Test list 75 76 err := fn.Run([]string{"fn", "apps", "l"}) 77 if err != nil { 78 t.Error(err) 79 } 80 81 // Test create app 82 83 err = fn.Run([]string{"fn", "apps", "c", "test"}) 84 if err != nil { 85 t.Error(err) 86 } 87 88 filter := &models.AppFilter{} 89 apps, err := funcServer.Datastore.GetApps(Ctx, filter) 90 91 if len(apps) != 1 { 92 t.Error("fn apps create failed.") 93 } 94 95 if apps[0].Name != "test" { 96 t.Error("fn apps create failed. - name doesnt match") 97 } 98 99 routeConfig := models.Route{ 100 Path: "/new-route", 101 JwtKey: "route_key", 102 Image: "iron/hello", 103 Memory: 72, 104 Type: "sync", 105 Format: "http", 106 MaxConcurrency: 15, 107 Timeout: 65, 108 IdleTimeout: 55} 109 110 // Test create route 111 112 err = fn.Run([]string{"fn", "routes", "c", "test", routeConfig.Path, 113 "--jwt-key", routeConfig.JwtKey, 114 "--image", routeConfig.Image, 115 "--memory", strconv.Itoa(int(routeConfig.Memory)), 116 "--type", routeConfig.Type, 117 "--format", routeConfig.Format, 118 "--max-concurrency", strconv.Itoa(int(routeConfig.MaxConcurrency)), 119 "--timeout", strconv.Itoa(int(routeConfig.Timeout)) + "s", 120 "--idle-timeout", strconv.Itoa(int(routeConfig.IdleTimeout)) + "s"}) 121 122 if err != nil { 123 t.Error(err) 124 } 125 126 routeFilter := &models.RouteFilter{} 127 routes, err := funcServer.Datastore.GetRoutes(Ctx, routeFilter) 128 129 if len(routes) != 1 { 130 t.Error("fn routes create failed.") 131 } 132 133 if routes[0].Path != routeConfig.Path { 134 t.Error("fn routes create failed. - path doesnt match") 135 } 136 137 if routes[0].Image != routeConfig.Image { 138 t.Error("fn routes create failed. - image doesnt match") 139 } 140 141 if routes[0].Memory != routeConfig.Memory { 142 t.Error("fn routes create failed. - memory doesnt match") 143 } 144 145 if routes[0].Type != routeConfig.Type { 146 t.Error("fn routes create failed. - type doesnt match") 147 } 148 149 if routes[0].Format != routeConfig.Format { 150 t.Error("fn routes create failed. - format doesnt match") 151 } 152 153 if routes[0].MaxConcurrency != routeConfig.MaxConcurrency { 154 t.Error("fn routes create failed. - max-concurrency doesnt match") 155 } 156 157 if routes[0].Timeout != routeConfig.Timeout { 158 t.Error("fn routes create failed. - timeout doesnt match") 159 } 160 161 if routes[0].IdleTimeout != routeConfig.IdleTimeout { 162 t.Error("fn routes create failed. - idle timeout doesnt match") 163 } 164 165 if routes[0].JwtKey != routeConfig.JwtKey { 166 t.Error("fn routes create failed. - jwt-key doesnt match") 167 } 168 169 // Test call route 170 171 err = fn.Run([]string{"fn", "routes", "call", "test", routeConfig.Path}) 172 if err != nil { 173 t.Error(err) 174 } 175 176 // Test delete route 177 178 err = fn.Run([]string{"fn", "routes", "delete", "test", routeConfig.Path}) 179 if err != nil { 180 t.Error(err) 181 } 182 183 routes, err = funcServer.Datastore.GetRoutes(Ctx, routeFilter) 184 185 if len(routes) != 0 { 186 t.Error("fn routes delete failed.") 187 } 188 189 // Test delete app 190 191 err = fn.Run([]string{"fn", "apps", "delete", "test"}) 192 if err != nil { 193 t.Error(err) 194 } 195 196 apps, err = funcServer.Datastore.GetApps(Ctx, filter) 197 198 if len(apps) != 0 { 199 t.Error("fn apps delete failed.") 200 } 201 }