44 "context"
55 "flag"
66 "fmt"
7+ "github.com/serverledge-faas/serverledge/internal/config"
78 "log"
89 "os"
910 "os/exec"
@@ -22,7 +23,6 @@ import (
2223
2324const HOST = "127.0.0.1"
2425const PORT = 1323
25- const AREA = "ROME"
2626
2727func getShell () string {
2828 if IsWindows () {
@@ -40,48 +40,37 @@ func getShellExt() string {
4040 }
4141}
4242
43- func testStartServerledge (isInCloud bool , outboundIp string ) ( * registration. Registry , * echo.Echo ) {
43+ func testStartServerledge (isInCloud bool , outboundIp string ) * echo.Echo {
4444 //setting up cache parameters
4545 api .CacheSetup ()
4646 schedulingPolicy := & scheduling.DefaultLocalPolicy {}
4747 // register to etcd, this way server is visible to the others under a given local area
48- registry := new (registration.Registry )
49- if isInCloud {
50- registry .Area = "cloud/" + AREA
51- } else {
52- registry .Area = AREA
53- }
54- // before register checkout other servers into the local area
55- _ , err := registry .GetAll (true )
56- if err != nil {
57- log .Fatal (err )
58- }
48+ myArea := config .GetString (config .REGISTRY_AREA , "ROME" )
49+ node .LocalNode = node .NewIdentifier (myArea )
5950
60- myKey , err := registry . RegisterToEtcd ()
51+ err := registration . RegisterNode ()
6152 if err != nil {
6253 log .Fatal (err )
6354 }
6455
65- node .LocalNode = myKey
66-
6756 metrics .Init ()
6857
6958 e := echo .New ()
7059
7160 // Register a signal handler to cleanup things on termination
72- api .RegisterTerminationHandler (registry , e )
61+ api .RegisterTerminationHandler (e )
7362
7463 go scheduling .Run (schedulingPolicy )
7564
7665 if ! isInCloud {
77- err = registration .StartMonitoring (registry )
66+ err = registration .StartMonitoring ()
7867 if err != nil {
7968 log .Fatal (err )
8069 }
8170 }
8271 // needed: if you call a function composition, internally will invoke each function
8372 go api .StartAPIServer (e )
84- return registry , e
73+ return e
8574
8675}
8776
@@ -96,7 +85,7 @@ func TestMain(m *testing.M) {
9685
9786 // TODO: avoid full setup if testing.Short()
9887
99- registry , echoServer , ok := setupServerledge (outboundIp .String ())
88+ echoServer , ok := setupServerledge (outboundIp .String ())
10089 if ok != nil {
10190 fmt .Printf ("failed to initialize serverledgde: %v\n " , ok )
10291 os .Exit (int (codes .Internal ))
@@ -106,38 +95,32 @@ func TestMain(m *testing.M) {
10695 code := m .Run ()
10796
10897 // tear down containers in order
109- err = teardownServerledge (registry , echoServer )
98+ err = teardownServerledge (echoServer )
11099 if err != nil {
111100 fmt .Printf ("failed to remove serverledgde: %v\n " , err )
112101 }
113102 os .Exit (code )
114103}
115104
116105// startReliably can start the containers, or restart them if needed
117- func startReliably (startScript string , stopScript string , msg string ) error {
106+ func startReliably (startScript string ) error {
118107 cmd := exec .CommandContext (context .Background (), getShell (), startScript )
119108 err := cmd .Run ()
120109 if err != nil {
121- antiCmd := exec .CommandContext (context .Background (), getShell (), stopScript )
122- err = antiCmd .Run ()
123- if err != nil {
124- return fmt .Errorf ("stopping of %s failed" , msg )
125- }
126- cmd = exec .CommandContext (context .Background (), getShell (), startScript )
127- err = cmd .Run ()
110+ log .Fatalf ("failed to start (%s): %v" , startScript , err )
128111 }
129112 return err
130113}
131114
132115// run the bash script to initialize serverledge
133- func setupServerledge (outboundIp string ) (* registration. Registry , * echo.Echo , error ) {
134- _ = startReliably ("../../scripts/start-etcd" + getShellExt (), "../../scripts/stop-etcd" + getShellExt (), "ETCD" )
135- registry , echoServer := testStartServerledge (false , outboundIp )
136- return registry , echoServer , nil
116+ func setupServerledge (outboundIp string ) (* echo.Echo , error ) {
117+ _ = startReliably ("../../scripts/start-etcd" + getShellExt ())
118+ echoServer := testStartServerledge (false , outboundIp )
119+ return echoServer , nil
137120}
138121
139122// run the bash script to stop serverledge
140- func teardownServerledge (registry * registration. Registry , e * echo.Echo ) error {
123+ func teardownServerledge (e * echo.Echo ) error {
141124 cmd1 := exec .CommandContext (context .Background (), getShell (), "../../scripts/remove-etcd" + getShellExt ())
142125
143126 node .ShutdownAllContainers ()
@@ -148,7 +131,7 @@ func teardownServerledge(registry *registration.Registry, e *echo.Echo) error {
148131 defer cancel ()
149132 errEcho := e .Shutdown (ctx )
150133
151- errRegistry := registry .Deregister ()
134+ errRegistry := registration .Deregister ()
152135 err1 := cmd1 .Run ()
153136 return u .ReturnNonNilErr (errEcho , errRegistry , err1 )
154137}
0 commit comments