github.com/creichlin/pentaconta@v0.1.1-0.20170602155716-6b53e3be0bdb/main_test.go (about)

     1  package main
     2  
     3  import (
     4  	"github.com/ghodss/yaml"
     5  	"io/ioutil"
     6  	"log"
     7  	"os"
     8  	"path/filepath"
     9  	"testing"
    10  	"time"
    11  )
    12  
    13  func TestReadDefaultConfigName(t *testing.T) {
    14  	oldArgs := os.Args
    15  	defer func() {
    16  		os.Args = oldArgs
    17  	}()
    18  
    19  	os.Args = []string{oldArgs[0]}
    20  	name, _ := readConfigParams()
    21  	if name != "pentaconta.test" {
    22  		t.Errorf("Default config name should be name of executable but is %v", name)
    23  	}
    24  }
    25  
    26  func TestReadCustomConfigName(t *testing.T) {
    27  	oldArgs := os.Args
    28  	defer func() {
    29  		os.Args = oldArgs
    30  	}()
    31  
    32  	os.Args = []string{oldArgs[0], "-config", "custom-name"}
    33  	name, _ := readConfigParams()
    34  	if name != "custom-name" {
    35  		t.Errorf("Custom config name should be custom-name but is %v", name)
    36  	}
    37  }
    38  
    39  
    40  func TestCustomSignals(t *testing.T) {
    41  	sr := NewServiceRunner("testdata/integration/custom_signal.yaml")
    42  	defer sr.Close()
    43  	ioutil.WriteFile(filepath.Join(sr.Dir, "aborted.txt"), []byte("data"), 0644)
    44  	go sr.Start(time.Millisecond * 1500)
    45  	time.Sleep(time.Millisecond * 500)
    46  	ioutil.WriteFile(filepath.Join(sr.Dir, "aborted.txt"), []byte("datax"), 0644)
    47  	time.Sleep(time.Millisecond * 500)
    48  
    49  }
    50  
    51  
    52  
    53  func TestRuns(t *testing.T) {
    54  	sr := NewServiceRunner("testdata/integration/1s.yaml")
    55  	defer sr.Close()
    56  	sr.Start(time.Second * 2)
    57  	stats, err := ioutil.ReadFile(filepath.Join(sr.Dir, "stats.json"))
    58  	if err != nil {
    59  		panic(err)
    60  	}
    61  
    62  	expected := `{
    63    "Samples": 2,
    64    "Services": {
    65      "pc_stable": {
    66        "Errors": 0,
    67        "Crashes": 0,
    68        "Terminations": 1,
    69        "Logs": 2
    70      }
    71    }
    72  }`
    73  
    74  	if string(stats) != expected {
    75  		t.Errorf("Stats is not as expected:\nCurrent\n%v\nexpected:\n%v", string(stats), expected)
    76  	}
    77  }
    78  
    79  
    80  type ServiceRunner struct {
    81  	data interface{}
    82  	oldWorkingDir string
    83  	Dir string
    84  }
    85  
    86  func (s *ServiceRunner)Start(duration time.Duration) {
    87  	runWithDeclaration(s.data, duration)
    88  }
    89  
    90  func (s *ServiceRunner)Close() {
    91  	os.RemoveAll(s.Dir)
    92  	os.Chdir(s.oldWorkingDir)
    93  }
    94  
    95  func NewServiceRunner(configPath string) *ServiceRunner {
    96  	dir, err := ioutil.TempDir("", "pentacontatest")
    97  	if err != nil {
    98  		log.Fatal(err)
    99  	}
   100  
   101  	cd, err := ioutil.ReadFile(configPath)
   102  	if err != nil {
   103  		panic(err)
   104  	}
   105  
   106  	data := interface{}(nil)
   107  
   108  	err = yaml.Unmarshal(cd, &data)
   109  	if err != nil {
   110  		panic(err)
   111  	}
   112  
   113  	oldWorkingDir, _ := os.Getwd()
   114  	os.Chdir(dir)
   115  
   116  	return &ServiceRunner{
   117  		data: data,
   118  		oldWorkingDir: oldWorkingDir,
   119  		Dir: dir,
   120  	}
   121  }