gopkg.in/dedis/onet.v2@v2.0.0-20181115163211-c8f3724038a7/simul/platform/platform_test.go (about)

     1  package platform
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"path/filepath"
     7  	"testing"
     8  	"time"
     9  
    10  	"gopkg.in/dedis/onet.v2/log"
    11  )
    12  
    13  var testfile = `Machines = 8
    14  RunWait = "33s"
    15  App = "sign"
    16  
    17  Ppm, Rounds, Other
    18  2, 30,string 1
    19  4, 30,	string 2`
    20  
    21  var testfile2 = `Machines = 8
    22  App = "sign"`
    23  
    24  func TestReadRunfile(t *testing.T) {
    25  	tplat := &TPlat{}
    26  
    27  	tmpfile, err := ioutil.TempFile("", "testrun.toml")
    28  	log.ErrFatal(err)
    29  	_, err = tmpfile.Write([]byte(testfile))
    30  	if err != nil {
    31  		log.Fatal("Couldn't write to tmp-file:", err)
    32  	}
    33  	tmpfile.Close()
    34  
    35  	tests := ReadRunFile(tplat, tmpfile.Name())
    36  	log.ErrFatal(os.Remove(tmpfile.Name()))
    37  	log.Lvl2(tplat)
    38  	log.Lvlf2("%+v\n", tests[0])
    39  	if tplat.App != "sign" {
    40  		log.Fatal("App should be 'sign'")
    41  	}
    42  	if len(tests) != 2 {
    43  		log.Fatal("There should be 2 tests")
    44  	}
    45  	if tests[0].Get("machines") != "8" {
    46  		log.Fatal("Machines = 8 has not been copied into RunConfig")
    47  	}
    48  	if tests[0].Get("other") != "string 1" {
    49  		log.Fatal("other copied into RunConfig")
    50  	}
    51  	dt, err := tests[0].GetDuration("runwait")
    52  	if err != nil {
    53  		t.Fatal("unexpected runwait err", err)
    54  	}
    55  	if dt != 33*time.Second {
    56  		t.Fatal("unexpected runwait")
    57  	}
    58  }
    59  
    60  func TestReadRunfile2(t *testing.T) {
    61  	tplat := &TPlat{}
    62  
    63  	tmpfile, err := ioutil.TempFile("", "testrun.toml")
    64  	log.ErrFatal(err)
    65  	_, err = tmpfile.Write([]byte(testfile2))
    66  	if err != nil {
    67  		log.Fatal("Couldn't write to tmp-file:", err)
    68  	}
    69  	tmpfile.Close()
    70  
    71  	ReadRunFile(tplat, tmpfile.Name())
    72  	log.ErrFatal(os.Remove(tmpfile.Name()))
    73  	if tplat.App != "sign" {
    74  		log.Fatal("App should be 'sign'")
    75  	}
    76  	if tplat.Machines != 8 {
    77  		log.Fatal("Machines should be 8")
    78  	}
    79  }
    80  
    81  type TPlat struct {
    82  	App      string
    83  	Machines int
    84  	RunWait  duration
    85  }
    86  
    87  func (t *TPlat) Configure(pc *Config)                {}
    88  func (t *TPlat) Build(s string, arg ...string) error { return nil }
    89  func (t *TPlat) Deploy(rc *RunConfig) error          { return nil }
    90  func (t *TPlat) Start(...string) error               { return nil }
    91  func (t *TPlat) Stop() error                         { return nil }
    92  func (t *TPlat) Cleanup() error                      { return nil }
    93  func (t *TPlat) Wait() error                         { return nil }
    94  
    95  type duration struct {
    96  	time.Duration
    97  }
    98  
    99  func (d *duration) UnmarshalText(text []byte) error {
   100  	var err error
   101  	d.Duration, err = time.ParseDuration(string(text))
   102  	return err
   103  }
   104  
   105  func TestCross(t *testing.T) {
   106  	t.Skip("Test not useful in automated context. Use it manually if you want.")
   107  
   108  	log.SetDebugVisible(4)
   109  	dir, err := ioutil.TempDir("", "build")
   110  	if err != nil {
   111  		t.Error(err)
   112  	}
   113  	defer os.RemoveAll(dir)
   114  
   115  	hello := []byte(`
   116  package main
   117  func main() {
   118    println("hello")
   119  }
   120  `)
   121  	err = ioutil.WriteFile(filepath.Join(dir, "hello.go"), hello, 0600)
   122  	if err != nil {
   123  		t.Error(err)
   124  	}
   125  
   126  	wd, _ := os.Getwd()
   127  	os.Chdir(dir)
   128  	defer os.Chdir(wd)
   129  
   130  	_, err = Build(".", "out", "386", "freebsd")
   131  	if err != nil {
   132  		t.Error(err)
   133  	}
   134  }