github.com/masahide/goansible@v0.0.0-20160116054156-01eac649e9f2/procmgmt/upstart/upstart_test.go (about)

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