github.com/holochain/holochain-proto@v0.1.0-alpha-26.0.20200915073418-5c83169c9b5b/cmd/common_test.go (about)

     1  package cmd
     2  
     3  import (
     4  	"fmt"
     5  	. "github.com/smartystreets/goconvey/convey"
     6  	"os"
     7  	"os/user"
     8  	"path/filepath"
     9  	"testing"
    10  	"time"
    11  
    12  	holo "github.com/holochain/holochain-proto"
    13  )
    14  
    15  func TestIsAppDir(t *testing.T) {
    16  	for _, configExtension := range GetConfigExtensionList() {
    17  		Convey(fmt.Sprintf("it should test to see if dir is a holochain app (%v)", configExtension), t, func() {
    18  
    19  			d, s := holo.SetupTestService()
    20  
    21  			So(IsAppDir(d).Error(), ShouldEqual, "HC: Holochain App directory missing dna/dna.xyz config file")
    22  			h, err := s.MakeTestingApp(filepath.Join(s.Path, "test"), configExtension, holo.InitializeDB, holo.CloneWithNewUUID, nil)
    23  			if err != nil {
    24  				panic(err)
    25  			}
    26  			So(IsAppDir(h.RootPath()), ShouldBeNil)
    27  			holo.CleanupTestDir(d)
    28  		})
    29  	}
    30  
    31  }
    32  
    33  func TestGetHolochainRoot(t *testing.T) {
    34  	os.Setenv("HOLOPATH", "some/dir/in/the/env")
    35  	Convey("it should return the value passed in if not empty", t, func() {
    36  		root, err := GetHolochainRoot("some/dir")
    37  		So(err, ShouldBeNil)
    38  		So(root, ShouldEqual, "some/dir")
    39  	})
    40  	Convey("it should get the root from the env", t, func() {
    41  		root, err := GetHolochainRoot("")
    42  		So(err, ShouldBeNil)
    43  		So(root, ShouldEqual, "some/dir/in/the/env")
    44  	})
    45  	Convey("it should return the default directory if env and input are empty", t, func() {
    46  		os.Setenv("HOLOPATH", "")
    47  		root, err := GetHolochainRoot("")
    48  		So(err, ShouldBeNil)
    49  		u, err := user.Current()
    50  		if err != nil {
    51  			panic(err)
    52  		}
    53  		So(root, ShouldEqual, filepath.Join(u.HomeDir, holo.DefaultDirectoryName))
    54  	})
    55  }
    56  
    57  func TestGetService(t *testing.T) {
    58  	d := holo.SetupTestDir()
    59  	defer holo.CleanupTestDir(d)
    60  	Convey("it should fail to make a service if not initialized", t, func() {
    61  		service, err := GetService(d)
    62  		So(service, ShouldBeNil)
    63  		So(err, ShouldEqual, ErrServiceUninitialized)
    64  	})
    65  	Convey("it should make a service once initialized", t, func() {
    66  		holo.Init(d, holo.AgentIdentity("test@example.com"), nil)
    67  		service, err := GetService(d)
    68  		So(err, ShouldBeNil)
    69  		So(service.Path, ShouldEqual, d)
    70  	})
    71  }
    72  
    73  func TestGetHolochain(t *testing.T) {
    74  	d := holo.SetupTestDir()
    75  	defer holo.CleanupTestDir(d)
    76  
    77  	Convey("it should fail when service not initialized", t, func() {
    78  		h, err := GetHolochain("foobar", nil, "some-cmd")
    79  		So(err, ShouldEqual, ErrServiceUninitialized)
    80  		So(h, ShouldBeNil)
    81  	})
    82  
    83  	holo.Init(d, holo.AgentIdentity("test@example.com"), nil)
    84  	service, _ := GetService(d)
    85  	Convey("it should fail to get an non-existent holochain", t, func() {
    86  		h, err := GetHolochain("foobar", service, "some-cmd")
    87  		So(err.Error(), ShouldEqual, fmt.Sprintf("No DNA file in %s%sfoobar%sdna%s", d, string(os.PathSeparator), string(os.PathSeparator), string(os.PathSeparator)))
    88  		So(h, ShouldBeNil)
    89  	})
    90  
    91  	Convey("it should get an installed holochain", t, func() {
    92  		d, service, h := holo.PrepareTestChain("test")
    93  		defer holo.CleanupTestChain(h, d)
    94  
    95  		// finally run the test.
    96  		h, err := GetHolochain("test", service, "some-cmd")
    97  		So(err, ShouldBeNil)
    98  		So(h.Nucleus().DNA().Name, ShouldEqual, "test")
    99  		So(h.DHT(), ShouldNotBeNil)
   100  		So(h.Node(), ShouldNotBeNil)
   101  	})
   102  }
   103  
   104  func Test_OsExecFunctions_IsFile(t *testing.T) {
   105  	d := holo.MakeTestDirName()
   106  	os.MkdirAll(d, 0770)
   107  	defer holo.CleanupTestDir(d)
   108  
   109  	testFile := filepath.Join(d, "common_test.go.Test_OsExecPipes.aFile")
   110  
   111  	Convey("it should when there is no touched file", t, func() {
   112  		So(IsFile(testFile), ShouldEqual, false)
   113  	})
   114  
   115  	Convey("it should when there is a touched file", t, func() {
   116  		OsExecPipes("touch", testFile)
   117  		So(IsFile(testFile), ShouldEqual, true)
   118  		OsExecSilent("rm", testFile)
   119  		So(IsFile(testFile), ShouldEqual, false)
   120  	})
   121  }
   122  
   123  func Test_TimestampFunctions(t *testing.T) {
   124  	Convey("check second adder", t, func() {
   125  		now := time.Now().Unix()
   126  		So(GetUnixTimestamp_secondsFromNow(10), ShouldBeGreaterThanOrEqualTo, now+10)
   127  	})
   128  	Convey("check duration from now timestamp", t, func() {
   129  		targetTime := GetUnixTimestamp_secondsFromNow(10)
   130  		durationUntil := GetDuration_fromUnixTimestamp(targetTime)
   131  		// fmt.Printf("now, target, durationUntil: %v, %v, %v\n\n", time.Now(), targetTime, durationUntil)
   132  		So(time.Now().Add(durationUntil).After(time.Now().Add(8*time.Second)), ShouldEqual, true)
   133  		So(time.Now().Add(durationUntil).Before(time.Now().Add(12*time.Second)), ShouldEqual, true)
   134  	})
   135  
   136  }