Skip to content

Commit e481bb3

Browse files
committed
Fixes tests
1 parent b2b0f29 commit e481bb3

3 files changed

Lines changed: 21 additions & 36 deletions

File tree

internal/test/main_test.go

Lines changed: 18 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
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

2324
const HOST = "127.0.0.1"
2425
const PORT = 1323
25-
const AREA = "ROME"
2626

2727
func 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
}

scripts/start-etcd.sh

100644100755
File mode changed.

utils/etcd.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package utils
22

33
import (
44
"fmt"
5+
"log"
56
"sync"
67
"time"
78

@@ -24,9 +25,10 @@ func GetEtcdClient() (*clientv3.Client, error) {
2425
etcdHost := config.GetString(config.ETCD_ADDRESS, "localhost:2379")
2526
cli, err := clientv3.New(clientv3.Config{
2627
Endpoints: []string{etcdHost},
27-
DialTimeout: 1 * time.Second,
28+
DialTimeout: 3 * time.Second,
2829
})
2930
if err != nil {
31+
log.Printf("Could not connect to etcd: %v", err)
3032
return nil, fmt.Errorf("Could not connect to etcd: %v", err)
3133
}
3234

0 commit comments

Comments
 (0)