github.com/hellofresh/janus@v0.0.0-20230925145208-ce8de8183c67/main_test.go (about) 1 package main 2 3 import ( 4 "context" 5 "net/url" 6 "os" 7 "strconv" 8 "testing" 9 10 "github.com/cucumber/godog" 11 "github.com/stretchr/testify/assert" 12 13 "github.com/hellofresh/janus/features/bootstrap" 14 "github.com/hellofresh/janus/pkg/api" 15 "github.com/hellofresh/janus/pkg/config" 16 ) 17 18 const defaultUpstreamsPort = 9089 19 20 var ( 21 apiRepo api.Repository 22 cfg *config.Specification 23 cfgChan chan api.ConfigurationMessage 24 portSecondary int 25 apiPortSecondary int 26 upstreamsPort int 27 ) 28 29 func InitializeTestSuite(ctx *godog.TestSuiteContext) { 30 var err error 31 32 ctx.BeforeSuite(func() { 33 cfg, err = config.LoadEnv() 34 if err != nil { 35 panic(err) 36 } 37 38 dsnURL, err := url.Parse(cfg.Database.DSN) 39 if nil != err { 40 panic(err) 41 } 42 43 switch dsnURL.Scheme { 44 case "mongodb": 45 apiRepo, err = api.NewMongoAppRepository(cfg.Database.DSN, cfg.BackendFlushInterval) 46 if err != nil { 47 panic(err) 48 } 49 case "file": 50 var apiPath = dsnURL.Path + "/apis" 51 52 apiRepo, err = api.NewFileSystemRepository(apiPath) 53 if err != nil { 54 panic(err) 55 } 56 default: 57 panic("invalid database") 58 } 59 60 portSecondary, err = strconv.Atoi(os.Getenv("PORT_SECONDARY")) 61 if err != nil { 62 panic(err) 63 } 64 65 apiPortSecondary, err = strconv.Atoi(os.Getenv("API_PORT_SECONDARY")) 66 if err != nil { 67 panic(err) 68 } 69 70 upstreamsPort = defaultUpstreamsPort 71 if dynamicUpstreamsPort, exists := os.LookupEnv("DYNAMIC_UPSTREAMS_PORT"); exists { 72 upstreamsPort, err = strconv.Atoi(dynamicUpstreamsPort) 73 if err != nil { 74 panic(err) 75 } 76 } 77 78 cfgChan = make(chan api.ConfigurationMessage, 100) 79 if listener, ok := apiRepo.(api.Listener); ok { 80 listener.Listen(context.Background(), cfgChan) 81 } 82 }) 83 } 84 85 func InitializeScenario(ctx *godog.ScenarioContext) { 86 bootstrap.RegisterRequestContext(ctx, cfg.Port, cfg.Web.Port, portSecondary, apiPortSecondary, defaultUpstreamsPort, upstreamsPort, cfg.Web.Credentials) 87 bootstrap.RegisterAPIContext(ctx, apiRepo, cfgChan) 88 bootstrap.RegisterMiscContext(ctx) 89 } 90 91 func Test_Fake(t *testing.T) { 92 assert.True(t, true) 93 }