Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prepare karate for tests with multiple backends running at the same time #1886

Merged
merged 7 commits into from
May 26, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
19 changes: 12 additions & 7 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,10 +71,12 @@ 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
if s.DatabasePath == "" {
if s.ClientPort == 9002 {
s.DatabasePath = "./database-b/" + sqlite.DefaultPath
} else {
s.DatabasePath = "./database-a/" + sqlite.DefaultPath
}
}
4xiom5 marked this conversation as resolved.
Show resolved Hide resolved

var point kyber.Point = nil
Expand All @@ -86,7 +90,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 +411,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
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
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
Loading