github.com/vektra/tachyon@v0.0.0-20150921164542-0da4f3861aef/procmgmt/upstart/upstart_test.go (about)

     1  package upstart
     2  
     3  import (
     4  	"bytes"
     5  	"github.com/vektra/tachyon"
     6  	us "github.com/vektra/tachyon/upstart"
     7  	"io/ioutil"
     8  	"os"
     9  	"os/exec"
    10  	"testing"
    11  )
    12  
    13  var jobName string = "atd"
    14  var runUpstartTests = false
    15  
    16  func init() {
    17  	if s := os.Getenv("TEST_JOB"); s != "" {
    18  		jobName = s
    19  	}
    20  
    21  	c := exec.Command("which", "initctl")
    22  	c.Run()
    23  	runUpstartTests = c.ProcessState.Success()
    24  }
    25  
    26  func TestInstall(t *testing.T) {
    27  	if !runUpstartTests {
    28  		t.SkipNow()
    29  	}
    30  
    31  	dest := "/etc/init/upstart-test-daemon.conf"
    32  
    33  	defer os.Remove(dest)
    34  
    35  	opts := `name=upstart-test-daemon file=test-daemon.conf.sample`
    36  
    37  	res, err := tachyon.RunAdhocTask("upstart/install", opts)
    38  	if err != nil {
    39  		panic(err)
    40  	}
    41  
    42  	if !res.Changed {
    43  		t.Fatal("no change detected")
    44  	}
    45  
    46  	_, err = os.Stat(dest)
    47  	if err != nil {
    48  		t.Fatal(err)
    49  	}
    50  }
    51  
    52  func TestDaemon(t *testing.T) {
    53  	if !runUpstartTests {
    54  		t.SkipNow()
    55  	}
    56  
    57  	defer os.Remove("/etc/init/upstart-test-daemon.conf")
    58  
    59  	opts := `name=upstart-test-daemon command="date"`
    60  
    61  	res, err := tachyon.RunAdhocTask("upstart/daemon", opts)
    62  	if err != nil {
    63  		panic(err)
    64  	}
    65  
    66  	if !res.Changed {
    67  		t.Fatal("no change detected")
    68  	}
    69  }
    70  
    71  func TestDaemonScripts(t *testing.T) {
    72  	if !runUpstartTests {
    73  		t.SkipNow()
    74  	}
    75  
    76  	dest := "/etc/init/upstart-test-daemon.conf"
    77  
    78  	defer os.Remove(dest)
    79  
    80  	opts := `name=upstart-test-daemon command="date" pre_start=@prestart.sample post_start=@poststart.sample pre_stop=@prestop.sample post_stop=@poststop.sample`
    81  
    82  	res, err := tachyon.RunAdhocTask("upstart/daemon", opts)
    83  	if err != nil {
    84  		panic(err)
    85  	}
    86  
    87  	if !res.Changed {
    88  		t.Fatal("no change detected")
    89  	}
    90  
    91  	body, err := ioutil.ReadFile(dest)
    92  	if err != nil {
    93  		panic(err)
    94  	}
    95  
    96  	idx := bytes.Index(body, []byte("this is a prestart sample script"))
    97  	if idx == -1 {
    98  		t.Error("config didn't contain our script")
    99  	}
   100  
   101  	idx = bytes.Index(body, []byte("this is a poststart sample script"))
   102  	if idx == -1 {
   103  		t.Error("config didn't contain our script")
   104  	}
   105  
   106  	idx = bytes.Index(body, []byte("this is a prestop sample script"))
   107  	if idx == -1 {
   108  		t.Error("config didn't contain our script")
   109  	}
   110  
   111  	idx = bytes.Index(body, []byte("this is a poststop sample script"))
   112  	if idx == -1 {
   113  		t.Error("config didn't contain our script")
   114  	}
   115  }
   116  
   117  func TestTaskScripts(t *testing.T) {
   118  	if !runUpstartTests {
   119  		t.SkipNow()
   120  	}
   121  
   122  	dest := "/etc/init/upstart-test-daemon.conf"
   123  
   124  	defer os.Remove(dest)
   125  
   126  	opts := `name=upstart-test-daemon command="date" pre_start=@prestart.sample post_start=@poststart.sample pre_stop=@prestop.sample post_stop=@poststop.sample`
   127  
   128  	res, err := tachyon.RunAdhocTask("upstart/task", opts)
   129  	if err != nil {
   130  		panic(err)
   131  	}
   132  
   133  	if !res.Changed {
   134  		t.Fatal("no change detected")
   135  	}
   136  
   137  	body, err := ioutil.ReadFile(dest)
   138  	if err != nil {
   139  		panic(err)
   140  	}
   141  
   142  	idx := bytes.Index(body, []byte("this is a prestart sample script"))
   143  	if idx == -1 {
   144  		t.Error("config didn't contain our script")
   145  	}
   146  
   147  	idx = bytes.Index(body, []byte("this is a poststart sample script"))
   148  	if idx == -1 {
   149  		t.Error("config didn't contain our script")
   150  	}
   151  
   152  	idx = bytes.Index(body, []byte("this is a prestop sample script"))
   153  	if idx == -1 {
   154  		t.Error("config didn't contain our script")
   155  	}
   156  
   157  	idx = bytes.Index(body, []byte("this is a poststop sample script"))
   158  	if idx == -1 {
   159  		t.Error("config didn't contain our script")
   160  	}
   161  }
   162  
   163  func TestTask(t *testing.T) {
   164  	if !runUpstartTests {
   165  		t.SkipNow()
   166  	}
   167  
   168  	defer os.Remove("/etc/init/upstart-test-task.conf")
   169  
   170  	opts := `name=upstart-test-task command="date"`
   171  
   172  	res, err := tachyon.RunAdhocTask("upstart/task", opts)
   173  	if err != nil {
   174  		panic(err)
   175  	}
   176  
   177  	if !res.Changed {
   178  		t.Fatal("no change detected")
   179  	}
   180  }
   181  
   182  func TestRestart(t *testing.T) {
   183  	if !runUpstartTests {
   184  		t.SkipNow()
   185  	}
   186  
   187  	opts := "name=" + jobName
   188  
   189  	u, err := us.Dial()
   190  	if err != nil {
   191  		panic(err)
   192  	}
   193  
   194  	j, err := u.Job(jobName)
   195  	if err != nil {
   196  		panic(err)
   197  	}
   198  
   199  	prev, err := j.Pid()
   200  	if err != nil {
   201  		panic(err)
   202  	}
   203  
   204  	res, err := tachyon.RunAdhocTask("upstart/restart", opts)
   205  	if err != nil {
   206  		panic(err)
   207  	}
   208  
   209  	if !res.Changed {
   210  		t.Fatal("no change detected")
   211  	}
   212  
   213  	cur, err := j.Pid()
   214  	if err != nil {
   215  		panic(err)
   216  	}
   217  
   218  	if res.Data.Get("pid") != cur {
   219  		t.Log(res.Data)
   220  		t.Fatal("pid not set properly")
   221  	}
   222  
   223  	if prev == cur {
   224  		t.Fatal("restart did not happen")
   225  	}
   226  }
   227  
   228  func TestStop(t *testing.T) {
   229  	if !runUpstartTests {
   230  		t.SkipNow()
   231  	}
   232  
   233  	opts := "name=" + jobName
   234  
   235  	u, err := us.Dial()
   236  	if err != nil {
   237  		panic(err)
   238  	}
   239  
   240  	j, err := u.Job(jobName)
   241  	if err != nil {
   242  		panic(err)
   243  	}
   244  
   245  	defer j.Start()
   246  
   247  	res, err := tachyon.RunAdhocTask("upstart/stop", opts)
   248  	if err != nil {
   249  		panic(err)
   250  	}
   251  
   252  	if !res.Changed {
   253  		t.Fatal("no change detected")
   254  	}
   255  
   256  	res, err = tachyon.RunAdhocTask("upstart/stop", opts)
   257  	if err != nil {
   258  		panic(err)
   259  	}
   260  
   261  	if res.Changed {
   262  		t.Fatal("change detected improperly")
   263  	}
   264  }
   265  
   266  func TestStart(t *testing.T) {
   267  	if !runUpstartTests {
   268  		t.SkipNow()
   269  	}
   270  
   271  	opts := "name=" + jobName
   272  
   273  	u, err := us.Dial()
   274  	if err != nil {
   275  		panic(err)
   276  	}
   277  
   278  	j, err := u.Job(jobName)
   279  	if err != nil {
   280  		panic(err)
   281  	}
   282  
   283  	defer j.Start()
   284  
   285  	err = j.Stop()
   286  	if err != nil {
   287  		panic(err)
   288  	}
   289  
   290  	res, err := tachyon.RunAdhocTask("upstart/start", opts)
   291  	if err != nil {
   292  		panic(err)
   293  	}
   294  
   295  	if !res.Changed {
   296  		t.Fatal("no change detected")
   297  	}
   298  
   299  	act, ok := res.Get("pid")
   300  	if !ok {
   301  		t.Fatal("pid not set")
   302  	}
   303  
   304  	pid, err := j.Pid()
   305  	if err != nil {
   306  		panic(err)
   307  	}
   308  
   309  	if pid != act.Read() {
   310  		t.Fatal("job did not start?")
   311  	}
   312  
   313  	res, err = tachyon.RunAdhocTask("upstart/start", opts)
   314  	if err != nil {
   315  		panic(err)
   316  	}
   317  
   318  	if res.Changed {
   319  		t.Fatal("change detected improperly")
   320  	}
   321  
   322  }
   323  
   324  func TestStartWithEnv(t *testing.T) {
   325  	if !runUpstartTests {
   326  		t.SkipNow()
   327  	}
   328  
   329  	opts := "name=" + jobName
   330  
   331  	u, err := us.Dial()
   332  	if err != nil {
   333  		panic(err)
   334  	}
   335  
   336  	j, err := u.Job(jobName)
   337  	if err != nil {
   338  		panic(err)
   339  	}
   340  
   341  	defer j.Start()
   342  
   343  	err = j.Stop()
   344  	if err != nil {
   345  		panic(err)
   346  	}
   347  
   348  	td := tachyon.TaskData{
   349  		"upstart/start": map[interface{}]interface{}{
   350  			"name": jobName,
   351  			"env": map[interface{}]interface{}{
   352  				"BAR": "foo",
   353  			},
   354  		},
   355  	}
   356  
   357  	res, err := tachyon.RunAdhocTaskVars(td)
   358  	if err != nil {
   359  		panic(err)
   360  	}
   361  
   362  	if !res.Changed {
   363  		t.Fatal("no change detected")
   364  	}
   365  
   366  	act, ok := res.Get("pid")
   367  	if !ok {
   368  		t.Fatal("pid not set")
   369  	}
   370  
   371  	pid, err := j.Pid()
   372  	if err != nil {
   373  		panic(err)
   374  	}
   375  
   376  	if pid != act.Read() {
   377  		t.Fatal("job did not start?")
   378  	}
   379  
   380  	res, err = tachyon.RunAdhocTask("upstart/start", opts)
   381  	if err != nil {
   382  		panic(err)
   383  	}
   384  
   385  	if res.Changed {
   386  		t.Fatal("change detected improperly")
   387  	}
   388  
   389  }