Skip to content

Commit

Permalink
Merge pull request #1886 from dedis/work-karate-4xiom5-handle-multipl…
Browse files Browse the repository at this point in the history
…e-concurrent-servers

Prepare karate for tests with multiple backends running at the same time
  • Loading branch information
4xiom5 authored May 26, 2024
2 parents b5596a6 + 72a9d0e commit 956db47
Show file tree
Hide file tree
Showing 16 changed files with 252 additions and 61 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,6 @@ fe2-android/app/src/main/resources/protocol

.DS_Store
**/.DS_Store

# Scala Metals
*.metals
15 changes: 6 additions & 9 deletions be1-go/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (
"encoding/base64"
"encoding/json"
"fmt"
"github.com/rs/zerolog"
"golang.org/x/exp/slices"
"net/url"
"os"
popstellar "popstellar"
Expand All @@ -24,6 +22,9 @@ import (
"sync"
"time"

"github.com/rs/zerolog"
"golang.org/x/exp/slices"

"github.com/rs/zerolog/log"
"go.dedis.ch/kyber/v3"

Expand Down Expand Up @@ -57,6 +58,7 @@ type ServerConfig struct {
ServerPort int `json:"server-port"`
AuthPort int `json:"auth-port"`
OtherServers []string `json:"other-servers"`
DatabasePath string `json:"database-path"`
}

func (s *ServerConfig) newHub(l *zerolog.Logger) (hub.Hub, error) {
Expand All @@ -69,12 +71,6 @@ func (s *ServerConfig) newHub(l *zerolog.Logger) (hub.Hub, error) {
s.ServerAddress = fmt.Sprintf("ws://%s:%d/server", s.PublicAddress, s.ServerPort)
}

path := "./database-a/" + sqlite.DefaultPath

if s.ClientPort == 9002 {
path = "./database-b/" + sqlite.DefaultPath
}

var point kyber.Point = nil
err := ownerKey(s.PublicKey, &point)
if err != nil {
Expand All @@ -86,7 +82,7 @@ func (s *ServerConfig) newHub(l *zerolog.Logger) (hub.Hub, error) {
return nil, err
}

db, err := sqlite.NewSQLite(path, true)
db, err := sqlite.NewSQLite(s.DatabasePath, true)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -407,6 +403,7 @@ func startWithFlags(cliCtx *cli.Context) (ServerConfig, error) {
ServerPort: serverPort,
AuthPort: authPort,
OtherServers: cliCtx.StringSlice("other-servers"),
DatabasePath: cliCtx.String("database-path"),
}, nil
}

Expand Down
6 changes: 6 additions & 0 deletions be1-go/cli/pop.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ func run(ctx context.Context, args []string) {
Aliases: []string{"cf"},
Usage: "path to the config file which will override other flags if present",
}
databasePathFlag := &cli.StringFlag{
Name: "database-path",
Aliases: []string{"dbp"},
Usage: "path to the database file",
}

app := &cli.App{
Name: "pop",
Expand All @@ -119,6 +124,7 @@ func run(ctx context.Context, args []string) {
authServerPortFlag,
otherServersFlag,
configFileFlag,
databasePathFlag,
},
Action: func(c *cli.Context) error {
err := Serve(c)
Expand Down
3 changes: 2 additions & 1 deletion be1-go/configServer1.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@
"client-port" : 9000,
"server-port" : 9001,
"auth-port" : 9100,
"other-servers": []
"other-servers": [],
"database-path" : "./database-a/sqlite.db"
}
3 changes: 2 additions & 1 deletion be1-go/configServer2.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@
"auth-port" : 9101,
"other-servers": [
"localhost:9001"
]
],
"database-path" : "./database-b/sqlite.db"
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ object RuntimeEnvironment {
private val configParam = "scala.config"
private val securityParam = "scala.security"
private val dbPathParam = "scala.db"
private val peerListParam = "scala.peerlist"
private val dbFolder = "database"
private val testParam = "test"

Expand All @@ -45,12 +46,7 @@ object RuntimeEnvironment {

// Needed for unit tests
lazy val isTestMode: Boolean = testMode(testParam)
lazy val serverPeersListPath: String =
if (isTestMode) {
confDir + File.separator + "server-peers-list-mock.conf"
} else {
confDir + File.separator + "server-peers-list.conf"
}
lazy val serverPeersListPath: String = getPeerListPath()

def readServerPeers(): List[String] = {
val source =
Expand Down Expand Up @@ -102,6 +98,17 @@ object RuntimeEnvironment {
}
}

private def getPeerListPath(): String = {
val path = sp(peerListParam)
if (path != null && path.trim.nonEmpty) {
path.trim
} else if (isTestMode) {
confDir + File.separator + "server-peers-list-mock.conf"
} else {
confDir + File.separator + "server-peers-list.conf"
}
}

private def testMode(testParam: String): Boolean = {
sp(testParam) != null
}
Expand Down
2 changes: 1 addition & 1 deletion tests/karate/src/test/java/be/features/LAO/create.feature
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ Feature: Create a pop LAO

@create6
Scenario: Create Lao request with public key different from the sender public key should fail
Given def notOrganizer = call createMockClient
Given def notOrganizer = call createMockFrontend
And def laoCreateRequest =
"""
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ Feature: Cast a vote
# upon a non-attendee casting a valid vote.
@castVote6
Scenario: Non attendee casting a vote should return an error
Given def nonAttendee = call createMockClient
Given def nonAttendee = call createMockFrontend
And def validCastVote =
"""
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ Feature: Open an Election
# if an open election message is sent by a non-organizer
@electionOpen4
Scenario: Non organizer opening the election should result in an error
Given def notOrganizer = call createMockClient
Given def notOrganizer = call createMockFrontend
And def validElectionOpen =
"""
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Feature: Close a Roll Call

@closeRollCall2
Scenario: Non-organizer closing a roll call should fail
Given def notOrganizer = call createMockClient
Given def notOrganizer = call createMockFrontend
And def validCloseRollCall =
"""
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ Feature: Create a Roll Call
# a non-organizer should result in an error message being sent by the backend.
@createRollCall3
Scenario: Roll Call Creation with non-organizer as sender should return an error
Given def notOrganizer = call createMockClient
Given def notOrganizer = call createMockFrontend
And def validCreateRollCall =
"""
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Feature: Roll Call Open

@openRollCall2
Scenario: Opening a Roll Call with non-organizer as sender should fail
Given def notOrganizer = call createMockClient
Given def notOrganizer = call createMockFrontend
And def validOpenRollCall =
"""
{
Expand Down
58 changes: 41 additions & 17 deletions tests/karate/src/test/java/be/utils/GoServer.java
Original file line number Diff line number Diff line change
@@ -1,32 +1,57 @@
package be.utils;

import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
import java.nio.file.Files;
import java.io.IOException;

public class GoServer extends Server implements Configurable {
private String dbPath;

@Override
public boolean start() {
return super.start(getCmd(), getDir(), getLogPath());
public GoServer(String host, int clientPort, int serverPort, int authPort, String dbPath, String logPath) {
super(host, clientPort, serverPort, authPort, logPath);
this.dbPath = dbPath;
}

@Override
public void stop() {
super.stop();
public GoServer() {
this("localhost", 9000, 9001, 9100, null, null);
}

@Override
public String[] getCmd() {
public String[] getCmd() throws IOException {
Map<String, String> args = new HashMap<>();
args.put("client-port", String.valueOf(clientPort));
args.put("server-port", String.valueOf(serverPort));
args.put("auth-port", String.valueOf(authPort));
args.put("server-public-address", host);
args.put("server-listen-address", host);

if (dbPath == null) {
dbPath = Files.createTempFile("go_database", ".sqlite").toString();
}
args.put("database-path", dbPath);

if (peers.size() > 0) {
args.put("other-servers", String.join(",", peers));
}

String cmd = "server serve";
for (Map.Entry<String, String> entry : args.entrySet()) {
cmd += " --" + entry.getKey() + " " + entry.getValue();
}

if (isWindowsOS()) {
return new String[]{
"cmd",
"/c",
"\"pop.exe server serve\""
"\"pop.exe " + cmd + "\""
};
} else {
return new String[]{
"bash",
"-c",
"./pop server serve"
"./pop " + cmd
};
}
}
Expand All @@ -36,15 +61,14 @@ public String getDir() {
return Paths.get("..", "..", "be1-go").toString();
}

@Override
public String getLogPath() {
return Paths.get("go.log").toString();
}

@Override
public void deleteDatabaseDir() {
//TODO: delete GO backend database if necessary
System.out.println("No database to delete for GO backend");

if (dbPath != null) {
try {
Files.deleteIfExists(Paths.get(dbPath));
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
51 changes: 36 additions & 15 deletions tests/karate/src/test/java/be/utils/ScalaServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,49 @@
import java.util.stream.Stream;

public class ScalaServer extends Server implements Configurable {
private String dbPath;

@Override
public boolean start() throws IOException {
return super.start(getCmd(), getDir(), getLogPath());
public ScalaServer(String host, int port, String dbPath, String logPath) {
super(host, port, port, port, logPath);
this.dbPath = dbPath;
}

@Override
public void stop() {
super.stop();
public ScalaServer() {
this("127.0.0.1", 8000, null, null);
}

@Override
public String[] getCmd() throws IOException {
String configPath = Paths.get("src", "main", "scala", "ch", "epfl", "pop", "config").toString();
Path workingDirectory = Paths.get("").toAbsolutePath();
Path tempDir = Files.createTempDirectory("scala-server");

// Create a temporary config file
Path configTemplate = workingDirectory.resolve("src/test/java/data/scala.template.conf");
String config = new String(Files.readAllBytes(configTemplate));
config = config.replace("{{host}}", host);
config = config.replace("{{port}}", String.valueOf(serverPort));
File configFile = new File(tempDir.toFile(), "application.conf");
Files.write(configFile.toPath(), config.getBytes());

// Create a temporary peer file
File peerFile = File.createTempFile("scala-peers", ".conf");
for (String peer : peers) {
Files.write(peerFile.toPath(), ("ws://" + peer + "/server\n").getBytes());
}

if (this.dbPath == null) {
dbPath = Files.createTempDirectory("scala-db").toString();
}

String securityDirPath = Paths.get("src", "security").toString();
File targetJar = getTargetJar();

return new String[] {
"java",
"-Dscala.config=" + configPath,
"-Dscala.config=" + tempDir.toString(),
"-Dscala.peerlist=" + peerFile.getCanonicalPath(),
"-Dscala.security=" + securityDirPath,
"-Dscala.db=" + dbPath,
"-jar", targetJar.getCanonicalPath()
};
}
Expand Down Expand Up @@ -67,15 +89,14 @@ public String getDir() {
return Paths.get("..", "..", "be2-scala").toString();
}

@Override
public String getLogPath() {
return Paths.get("scala.log").toString();
}

@Override
public void deleteDatabaseDir() {
System.out.println("Deleting database...");
Path path = Paths.get("..", "..", "be2-scala", "database");
if (dbPath == null) {
System.out.println("No database to delete");
return;
}

Path path = Paths.get(dbPath);
try (Stream<Path> walk = Files.walk(path)) {
walk.sorted(Comparator.reverseOrder())
.map(Path::toFile)
Expand Down
Loading

0 comments on commit 956db47

Please sign in to comment.