|
| 1 | +// +build integration |
| 2 | + |
| 3 | +package server |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "fmt" |
| 8 | + "os" |
| 9 | + "testing" |
| 10 | + "time" |
| 11 | + |
| 12 | + "github.com/iron-io/functions/api/models" |
| 13 | + "github.com/iron-io/functions/fn/app" |
| 14 | + "github.com/spf13/viper" |
| 15 | + "github.com/urfave/cli" |
| 16 | +) |
| 17 | + |
| 18 | +var DB_FILE string |
| 19 | +var MQ_FILE string |
| 20 | +var API_URL string |
| 21 | +var PORT int |
| 22 | +var funcServer *Server |
| 23 | +var Cancel context.CancelFunc |
| 24 | +var Ctx context.Context |
| 25 | +var fn *cli.App |
| 26 | + |
| 27 | +func setupServer() { |
| 28 | + viper.Set(EnvDBURL, fmt.Sprintf("bolt://%s?bucket=funcs", DB_FILE)) |
| 29 | + viper.Set(EnvMQURL, fmt.Sprintf("bolt://%s", MQ_FILE)) |
| 30 | + viper.Set(EnvPort, PORT) |
| 31 | + Ctx, Cancel = context.WithCancel(context.Background()) |
| 32 | + funcServer = NewFromEnv(Ctx) |
| 33 | + go funcServer.Start(Ctx) |
| 34 | + time.Sleep(2 * time.Second) |
| 35 | +} |
| 36 | + |
| 37 | +func setupCli() { |
| 38 | + viper.Set("API_URL", API_URL) |
| 39 | + fn = app.NewFn() |
| 40 | +} |
| 41 | + |
| 42 | +func teardown() { |
| 43 | + os.Remove(DB_FILE) |
| 44 | + os.Remove(MQ_FILE) |
| 45 | + Cancel() |
| 46 | + time.Sleep(2 * time.Second) |
| 47 | +} |
| 48 | + |
| 49 | +func TestIntegration(t *testing.T) { |
| 50 | + DB_FILE = "/tmp/bolt.db" |
| 51 | + MQ_FILE = "/tmp/bolt_mq.db" |
| 52 | + PORT = 8080 |
| 53 | + API_URL = "http://localhost:8080" |
| 54 | + setupServer() |
| 55 | + setupCli() |
| 56 | + testIntegration(t) |
| 57 | + teardown() |
| 58 | +} |
| 59 | + |
| 60 | +func TestIntegrationWithAuth(t *testing.T) { |
| 61 | + viper.Set("jwt_auth_key", "test") |
| 62 | + DB_FILE = "/tmp/bolt_auth.db" |
| 63 | + MQ_FILE = "/tmp/bolt_auth_mq.db" |
| 64 | + PORT = 8081 |
| 65 | + API_URL = "http://localhost:8081" |
| 66 | + setupServer() |
| 67 | + setupCli() |
| 68 | + testIntegration(t) |
| 69 | + teardown() |
| 70 | +} |
| 71 | + |
| 72 | +func testIntegration(t *testing.T) { |
| 73 | + // Test list |
| 74 | + |
| 75 | + err := fn.Run([]string{"fn", "apps", "l"}) |
| 76 | + if err != nil { |
| 77 | + t.Error(err) |
| 78 | + } |
| 79 | + |
| 80 | + // Test create app |
| 81 | + |
| 82 | + err = fn.Run([]string{"fn", "apps", "c", "test"}) |
| 83 | + if err != nil { |
| 84 | + t.Error(err) |
| 85 | + } |
| 86 | + |
| 87 | + filter := &models.AppFilter{} |
| 88 | + apps, err := funcServer.Datastore.GetApps(Ctx, filter) |
| 89 | + |
| 90 | + if len(apps) != 1 { |
| 91 | + t.Error("fn apps create failed.") |
| 92 | + } |
| 93 | + |
| 94 | + if apps[0].Name != "test" { |
| 95 | + t.Error("fn apps create failed. - name doesnt match") |
| 96 | + } |
| 97 | + |
| 98 | + // Test create route |
| 99 | + |
| 100 | + err = fn.Run([]string{"fn", "routes", "c", "test", "/new-route", "--jwt-key", "route_key"}) |
| 101 | + if err != nil { |
| 102 | + t.Error(err) |
| 103 | + } |
| 104 | + |
| 105 | + routeFilter := &models.RouteFilter{} |
| 106 | + routes, err := funcServer.Datastore.GetRoutes(Ctx, routeFilter) |
| 107 | + |
| 108 | + if len(routes) != 1 { |
| 109 | + t.Error("fn routes create failed.") |
| 110 | + } |
| 111 | + |
| 112 | + if routes[0].Path != "/new-route" { |
| 113 | + t.Error("fn routes create failed. - path doesnt match") |
| 114 | + } |
| 115 | + |
| 116 | + // Test call route |
| 117 | + |
| 118 | + err = fn.Run([]string{"fn", "routes", "call", "test", "/new-route"}) |
| 119 | + if err != nil { |
| 120 | + t.Error(err) |
| 121 | + } |
| 122 | + |
| 123 | + // Test delete route |
| 124 | + |
| 125 | + err = fn.Run([]string{"fn", "routes", "delete", "test", "/new-route"}) |
| 126 | + if err != nil { |
| 127 | + t.Error(err) |
| 128 | + } |
| 129 | + |
| 130 | + routes, err = funcServer.Datastore.GetRoutes(Ctx, routeFilter) |
| 131 | + |
| 132 | + if len(routes) != 0 { |
| 133 | + t.Error("fn routes delete failed.") |
| 134 | + } |
| 135 | + |
| 136 | + // Test delete app |
| 137 | + |
| 138 | + err = fn.Run([]string{"fn", "apps", "delete", "test"}) |
| 139 | + if err != nil { |
| 140 | + t.Error(err) |
| 141 | + } |
| 142 | + |
| 143 | + apps, err = funcServer.Datastore.GetApps(Ctx, filter) |
| 144 | + |
| 145 | + if len(apps) != 0 { |
| 146 | + t.Error("fn apps delete failed.") |
| 147 | + } |
| 148 | +} |
0 commit comments