github.com/clarenceb/holochain-proto@v0.0.1-alpha.0.20180918132555-dff351593ded/cmd/hcd/hcd_test.go (about)

     1  package main
     2  
     3  import (
     4  	holo "github.com/holochain/holochain-proto"
     5  	"github.com/holochain/holochain-proto/cmd"
     6  	. "github.com/smartystreets/goconvey/convey"
     7  	"github.com/urfave/cli"
     8  	"io/ioutil"
     9  	"os"
    10  	"path/filepath"
    11  	"testing"
    12  	"time"
    13  )
    14  
    15  func TestMain(m *testing.M) {
    16  	holo.InitializeHolochain()
    17  	os.Exit(m.Run())
    18  }
    19  
    20  func TestSetupApp(t *testing.T) {
    21  	app := setupApp()
    22  	Convey("it should create the cli App", t, func() {
    23  		So(app.Name, ShouldEqual, "hcd")
    24  	})
    25  }
    26  
    27  func TestWeb(t *testing.T) {
    28  	tmpTestDir, s, h, app := setupTestingApp("testApp")
    29  	defer os.RemoveAll(tmpTestDir)
    30  
    31  	Convey("it should run a webserver", t, func() {
    32  		out, err := cmd.RunAppWithStdoutCapture(app, []string{"hcd", "-path", s.Path, "testApp"}, 5*time.Second)
    33  		So(err, ShouldBeNil)
    34  
    35  		So(out, ShouldContainSubstring, h.DNAHash().String()+" on port 3141")
    36  		So(out, ShouldContainSubstring, "Serving holochain with DNA hash:")
    37  		So(out, ShouldNotContainSubstring, "running zyZome genesis")
    38  	})
    39  }
    40  
    41  func setupTestingApp(name string) (string, *holo.Service, *holo.Holochain, *cli.App) {
    42  	tmpTestDir, err := ioutil.TempDir("", "holochain.testing.hcd")
    43  	if err != nil {
    44  		panic(err)
    45  	}
    46  
    47  	err = os.Chdir(tmpTestDir)
    48  	if err != nil {
    49  		panic(err)
    50  	}
    51  
    52  	agent := "Fred Flintstone <fred@flintstone.com>"
    53  	s, err := holo.Init(filepath.Join(tmpTestDir, holo.DefaultDirectoryName), holo.AgentIdentity(agent), nil)
    54  	if err != nil {
    55  		panic(err)
    56  	}
    57  	root := filepath.Join(s.Path, name)
    58  	h, err := s.MakeTestingApp(root, "json", holo.InitializeDB, holo.CloneWithNewUUID, nil)
    59  	if err != nil {
    60  		panic(err)
    61  	}
    62  	h.GenChain()
    63  	app := setupApp()
    64  	return tmpTestDir, s, h, app
    65  }