github.com/kaleido-io/firefly@v0.0.0-20210622132723-8b4b6aacb971/test/e2e/e2e_test.go (about)

     1  // Copyright © 2021 Kaleido, Inc.
     2  //
     3  // SPDX-License-Identifier: Apache-2.0
     4  //
     5  // Licensed under the Apache License, Version 2.0 (the "License");
     6  // you may not use this file except in compliance with the License.
     7  // You may obtain a copy of the License at
     8  //
     9  //     http://www.apache.org/licenses/LICENSE-2.0
    10  //
    11  // Unless required by applicable law or agreed to in writing, software
    12  // distributed under the License is distributed on an "AS IS" BASIS,
    13  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  // See the License for the specific language governing permissions and
    15  // limitations under the License.
    16  
    17  package e2e
    18  
    19  import (
    20  	"fmt"
    21  	"os"
    22  	"testing"
    23  	"time"
    24  
    25  	"github.com/go-resty/resty/v2"
    26  	"github.com/kaleido-io/firefly/pkg/fftypes"
    27  	"github.com/stretchr/testify/assert"
    28  	"github.com/stretchr/testify/require"
    29  )
    30  
    31  func pollForUp(t *testing.T, client *resty.Client) {
    32  	var resp *resty.Response
    33  	var err error
    34  	for i := 0; i < 3; i++ {
    35  		resp, err = GetNamespaces(client)
    36  		if err == nil && resp.StatusCode() == 200 {
    37  			break
    38  		}
    39  		time.Sleep(5 * time.Second)
    40  	}
    41  	require.NoError(t, err)
    42  	assert.Equal(t, 200, resp.StatusCode())
    43  }
    44  
    45  func TestEndToEnd(t *testing.T) {
    46  	stackFile := os.Getenv("STACK_FILE")
    47  	if stackFile == "" {
    48  		t.Fatal("STACK_FILE must be set")
    49  	}
    50  
    51  	port1, err := GetMemberPort(stackFile, 0)
    52  	require.NoError(t, err)
    53  	port2, err := GetMemberPort(stackFile, 1)
    54  	require.NoError(t, err)
    55  
    56  	client1 := resty.New()
    57  	client1.SetHostURL(fmt.Sprintf("http://localhost:%d/api/v1", port1))
    58  	client2 := resty.New()
    59  	client2.SetHostURL(fmt.Sprintf("http://localhost:%d/api/v1", port2))
    60  
    61  	t.Logf("Client 1: " + client1.HostURL)
    62  	t.Logf("Client 2: " + client2.HostURL)
    63  	pollForUp(t, client1)
    64  	pollForUp(t, client2)
    65  
    66  	var resp *resty.Response
    67  	definitionName := "definition1"
    68  
    69  	resp, err = BroadcastDatatype(client1, definitionName)
    70  	require.NoError(t, err)
    71  	assert.Equal(t, 202, resp.StatusCode())
    72  
    73  	resp, err = GetData(client1)
    74  	require.NoError(t, err)
    75  	assert.Equal(t, 200, resp.StatusCode())
    76  	data := resp.Result().(*[]fftypes.Data)
    77  	assert.Equal(t, 1, len(*data))
    78  	assert.Equal(t, "default", (*data)[0].Namespace)
    79  	assert.Equal(t, fftypes.ValidatorType("datadef"), (*data)[0].Validator)
    80  	assert.Equal(t, definitionName, (*data)[0].Value.JSONObject().GetString("name"))
    81  
    82  	resp, err = GetData(client2)
    83  	require.NoError(t, err)
    84  	assert.Equal(t, 200, resp.StatusCode())
    85  	data = resp.Result().(*[]fftypes.Data)
    86  	t.Logf("Returned results from member 2: %d", len(*data))
    87  }