container, tests: add Host and NewClient API

This commit is contained in:
Alan Chen 2017-08-15 15:19:43 +08:00
parent d090a771a3
commit cdf4c5605a
4 changed files with 45 additions and 8 deletions

View File

@ -16,9 +16,36 @@
package container package container
import (
"log"
"net"
"net/url"
)
func (eth *ethereum) Image() string { func (eth *ethereum) Image() string {
if eth.imageTag == "" { if eth.imageTag == "" {
return eth.imageRepository + ":latest" return eth.imageRepository + ":latest"
} }
return eth.imageRepository + ":" + eth.imageTag return eth.imageRepository + ":" + eth.imageTag
} }
func (eth *ethereum) Host() string {
var host string
daemonHost := eth.client.DaemonHost()
url, err := url.Parse(daemonHost)
if err != nil {
log.Printf("Failed to parse daemon host, err: %v", err)
return host
}
if url.Scheme == "unix" {
host = "localhost"
} else {
host, _, err = net.SplitHostPort(url.Host)
if err != nil {
log.Printf("Failed to split host and port, err: %v", err)
}
}
return host
}

View File

@ -45,6 +45,9 @@ type Ethereum interface {
Init(string) error Init(string) error
Start() error Start() error
Stop() error Stop() error
Host() string
NewClient() *ethclient.Client
} }
func NewEthereum(c *client.Client, options ...Option) *ethereum { func NewEthereum(c *client.Client, options ...Option) *ethereum {
@ -181,8 +184,8 @@ func (eth *ethereum) Start() error {
} }
for i := 0; i < healthCheckRetryCount; i++ { for i := 0; i < healthCheckRetryCount; i++ {
cli, err := ethclient.Dial("http://localhost:" + eth.rpcPort) cli := eth.NewClient()
if err != nil { if cli == nil {
time.Sleep(healthCheckRetryDelay) time.Sleep(healthCheckRetryDelay)
continue continue
} }
@ -238,6 +241,15 @@ func (eth *ethereum) Running() bool {
return false return false
} }
func (eth *ethereum) NewClient() *ethclient.Client {
client, err := ethclient.Dial("http://" + eth.Host() + ":" + eth.rpcPort)
if err != nil {
log.Printf("Failed to dial to geth, err: %v", err)
return nil
}
return client
}
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
func (eth *ethereum) showLog(context context.Context) { func (eth *ethereum) showLog(context context.Context) {

View File

@ -45,7 +45,6 @@ func TestEthereumContainer(t *testing.T) {
RPCAddress("0.0.0.0"), RPCAddress("0.0.0.0"),
RPCAPI("eth,net,web3,personal"), RPCAPI("eth,net,web3,personal"),
RPCPort(fmt.Sprintf("%d", env.RpcPort)), RPCPort(fmt.Sprintf("%d", env.RpcPort)),
Logging(true),
) )
err := geth.Init(filepath.Join(env.DataDir, genesis.FileName)) err := geth.Init(filepath.Join(env.DataDir, genesis.FileName))

View File

@ -26,7 +26,6 @@ import (
. "github.com/onsi/ginkgo" . "github.com/onsi/ginkgo"
. "github.com/onsi/gomega" . "github.com/onsi/gomega"
"github.com/getamis/go-ethereum/ethclient"
"github.com/getamis/istanbul-tools/container" "github.com/getamis/istanbul-tools/container"
"github.com/getamis/istanbul-tools/core" "github.com/getamis/istanbul-tools/core"
"github.com/getamis/istanbul-tools/core/genesis" "github.com/getamis/istanbul-tools/core/genesis"
@ -84,11 +83,11 @@ var _ = Describe("4 validators Istanbul", func() {
}) })
It("Blockchain creation", func() { It("Blockchain creation", func() {
for _, env := range envs { for _, geth := range geths {
cli, err := ethclient.Dial("http://localhost:" + fmt.Sprintf("%d", env.RpcPort)) client := geth.NewClient()
Expect(err).To(BeNil()) Expect(client).NotTo(BeNil())
block, err := cli.BlockByNumber(context.Background(), big.NewInt(0)) block, err := client.BlockByNumber(context.Background(), big.NewInt(0))
Expect(err).To(BeNil()) Expect(err).To(BeNil())
Expect(block).NotTo(BeNil()) Expect(block).NotTo(BeNil())
} }