Skip to content

Commit

Permalink
add additional gateway testState functionality
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Shorsher <alex.shorsher@kaleido.io>
  • Loading branch information
shorsher committed Aug 3, 2022
1 parent c5e24a1 commit 2a71a09
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 25 deletions.
52 changes: 50 additions & 2 deletions test/e2e/gateway/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ import (
"fmt"
"net/http"
"os"
"path/filepath"
"testing"
"time"

"github.com/gorilla/websocket"
"github.com/hyperledger/firefly/pkg/core"
"github.com/hyperledger/firefly/test/e2e"
"github.com/hyperledger/firefly/test/e2e/client"
)
Expand All @@ -39,9 +41,20 @@ type testState struct {
startTime time.Time
done func()
ws1 *websocket.Conn
ws2 *websocket.Conn
org1 *core.Identity
org1key *core.Verifier
org2 *core.Identity
org2key *core.Verifier
client1 *client.FireFlyClient
client2 *client.FireFlyClient
unregisteredAccounts []interface{}
namespace string
stackName string
adminHost1 string
adminHost2 string
configFile1 string
configFile2 string
}

func (m *testState) T() *testing.T {
Expand All @@ -57,33 +70,56 @@ func (m *testState) Done() func() {
}

func beforeE2ETest(t *testing.T) *testState {
stackDir := os.Getenv("STACK_DIR")
if stackDir == "" {
t.Fatal("STACK_DIR must be set")
}
stack := e2e.ReadStack(t)
stackState := e2e.ReadStackState(t)

var authHeader1 http.Header
var authHeader2 http.Header

httpProtocolClient1 := schemeHTTP
httpProtocolClient2 := schemeHTTP
if stack.Members[0].UseHTTPS {
httpProtocolClient1 = schemeHTTPS
}
if stack.Members[1].UseHTTPS {
httpProtocolClient2 = schemeHTTPS
}

member0WithPort := ""
if stack.Members[0].ExposedFireflyPort != 0 {
member0WithPort = fmt.Sprintf(":%d", stack.Members[0].ExposedFireflyPort)
}
member1WithPort := ""
if stack.Members[1].ExposedFireflyPort != 0 {
member1WithPort = fmt.Sprintf(":%d", stack.Members[1].ExposedFireflyPort)
}

base1 := fmt.Sprintf("%s://%s%s", httpProtocolClient1, stack.Members[0].FireflyHostname, member0WithPort)
base2 := fmt.Sprintf("%s://%s%s", httpProtocolClient2, stack.Members[1].FireflyHostname, member1WithPort)
admin1 := fmt.Sprintf("%s://%s:%d", httpProtocolClient1, stack.Members[0].FireflyHostname, stack.Members[0].ExposedAdminPort)
admin2 := fmt.Sprintf("%s://%s:%d", httpProtocolClient2, stack.Members[1].FireflyHostname, stack.Members[1].ExposedAdminPort)

baseURL := fmt.Sprintf("%s://%s%s", httpProtocolClient1, stack.Members[0].FireflyHostname, member0WithPort)
namespace := os.Getenv("NAMESPACE")
if namespace == "" {
namespace = "default"
}

ts := &testState{
t: t,
stackName: stack.Name,
startTime: time.Now(),
client1: client.NewFireFly(t, baseURL, namespace),
client1: client.NewFireFly(t, base1, namespace),
client2: client.NewFireFly(t, base2, namespace),
unregisteredAccounts: stackState.Accounts[2:],
namespace: namespace,
adminHost1: admin1,
adminHost2: admin2,
configFile1: filepath.Join(stackDir, "runtime", "config", "firefly_core_0.yml"),
configFile2: filepath.Join(stackDir, "runtime", "config", "firefly_core_1.yml"),
}

t.Logf("Blockchain provider: %s", stack.BlockchainProvider)
Expand All @@ -96,21 +132,33 @@ func beforeE2ETest(t *testing.T) *testState {
}
}

if stack.Members[1].Username != "" && stack.Members[1].Password != "" {
t.Log("Setting auth for user 2")
ts.client2.Client.SetBasicAuth(stack.Members[1].Username, stack.Members[1].Password)
authHeader2 = http.Header{
"Authorization": []string{fmt.Sprintf("Basic %s", base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%s:%s", stack.Members[1].Username, stack.Members[1].Password))))},
}
}

// If no namespace is set to run tests against, use the default namespace
if os.Getenv("NAMESPACE") != "" {
namespace := os.Getenv("NAMESPACE")
ts.namespace = namespace
}

t.Logf("Client 1: " + ts.client1.Client.HostURL)
t.Logf("Client 2: " + ts.client2.Client.HostURL)
e2e.PollForUp(t, ts.client1)
e2e.PollForUp(t, ts.client2)

eventNames := "message_confirmed|token_pool_confirmed|token_transfer_confirmed|blockchain_event_received|token_approval_confirmed|identity_confirmed"
queryString := fmt.Sprintf("namespace=%s&ephemeral&autoack&filter.events=%s&changeevents=.*", ts.namespace, eventNames)
ts.ws1 = ts.client1.WebSocket(t, queryString, authHeader1)
ts.ws2 = ts.client2.WebSocket(t, queryString, authHeader2)

ts.done = func() {
ts.ws1.Close()
ts.ws2.Close()
t.Log("WebSockets closed")
}
return ts
Expand Down
30 changes: 7 additions & 23 deletions test/e2e/gateway/tokens_only.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ package gateway

import (
"fmt"
"os"
"path/filepath"

"github.com/hyperledger/firefly-common/pkg/fftypes"
"github.com/hyperledger/firefly/pkg/core"
Expand All @@ -31,31 +29,17 @@ import (

type TokensOnlyTestSuite struct {
suite.Suite
testState *testState
connector string
db string
key string
adminHost1 string
configFile1 string
testState *testState
connector string
db string
key string
}

func (suite *TokensOnlyTestSuite) SetupSuite() {
suite.testState = beforeE2ETest(suite.T())
stack := e2e.ReadStack(suite.T())
stackState := e2e.ReadStackState(suite.T())

adminProtocol1 := schemeHTTP
if stack.Members[0].UseHTTPS {
adminProtocol1 = schemeHTTPS
}
suite.adminHost1 = fmt.Sprintf("%s://%s:%d", adminProtocol1, stack.Members[0].FireflyHostname, stack.Members[0].ExposedAdminPort)

stackDir := os.Getenv("STACK_DIR")
if stackDir == "" {
suite.T().Fatal("STACK_DIR must be set")
}
suite.configFile1 = filepath.Join(stackDir, "runtime", "config", "firefly_core_0.yml")

suite.connector = stack.TokenProviders[0]
suite.db = stack.Database
account := stackState.Accounts[0].(map[string]interface{})
Expand Down Expand Up @@ -86,12 +70,12 @@ func (suite *TokensOnlyTestSuite) TestTokensOnlyNamespaces() {
}

// Add the new namespace
data1 := e2e.ReadConfig(suite.T(), suite.configFile1)
data1 := e2e.ReadConfig(suite.T(), suite.testState.configFile1)
e2e.AddNamespace(data1, namespaceInfo)
e2e.WriteConfig(suite.T(), suite.configFile1, data1)
e2e.WriteConfig(suite.T(), suite.testState.configFile1, data1)

admin1 := client.NewResty(suite.T())
admin1.SetBaseURL(suite.adminHost1 + "/spi/v1")
admin1.SetBaseURL(suite.testState.adminHost1 + "/spi/v1")

e2e.ResetFireFly(suite.T(), admin1)
e2e.PollForUp(suite.T(), suite.testState.client1)
Expand Down

0 comments on commit 2a71a09

Please sign in to comment.