Forráskód Böngészése

add tests for post-hooks

z4yx 5 éve
szülő
commit
bc416a6088
1 módosított fájl, 68 hozzáadás és 0 törlés
  1. 68 0
      worker/job_test.go

+ 68 - 0
worker/job_test.go

@@ -112,6 +112,74 @@ func TestMirrorJob(t *testing.T) {
 
 		})
 
+		Convey("When running long jobs with post-fail hook", func(ctx C) {
+			scriptContent := `#!/bin/bash
+echo '++++++'
+echo $TUNASYNC_WORKING_DIR
+echo $0 sleeping
+sleep 3
+echo $TUNASYNC_WORKING_DIR
+echo '------'
+			`
+			err = ioutil.WriteFile(scriptFile, []byte(scriptContent), 0755)
+			So(err, ShouldBeNil)
+
+			hookScriptFile := filepath.Join(tmpDir, "hook.sh")
+			err = ioutil.WriteFile(hookScriptFile, []byte(scriptContent), 0755)
+			So(err, ShouldBeNil)
+
+			h, err := newExecPostHook(provider, execOnFailure, hookScriptFile)
+			So(err, ShouldBeNil)
+			provider.AddHook(h)
+
+			managerChan := make(chan jobMessage, 10)
+			semaphore := make(chan empty, 1)
+			job := newMirrorJob(provider)
+
+			Convey("If we kill it", func(ctx C) {
+				go job.Run(managerChan, semaphore)
+				job.ctrlChan <- jobStart
+
+				time.Sleep(1 * time.Second)
+				msg := <-managerChan
+				So(msg.status, ShouldEqual, PreSyncing)
+				msg = <-managerChan
+				So(msg.status, ShouldEqual, Syncing)
+
+				job.ctrlChan <- jobStop
+
+				msg = <-managerChan
+				So(msg.status, ShouldEqual, Failed)
+
+				job.ctrlChan <- jobDisable
+				<-job.disabled
+			})
+
+			Convey("If we kill it then start it", func(ctx C) {
+				go job.Run(managerChan, semaphore)
+				job.ctrlChan <- jobStart
+
+				time.Sleep(1 * time.Second)
+				msg := <-managerChan
+				So(msg.status, ShouldEqual, PreSyncing)
+				msg = <-managerChan
+				So(msg.status, ShouldEqual, Syncing)
+
+				job.ctrlChan <- jobStop
+
+				time.Sleep(2 * time.Second)
+				logger.Debugf("Now starting...\n")
+				job.ctrlChan <- jobStart
+
+				msg = <-managerChan
+				So(msg.status, ShouldEqual, Failed)
+
+				job.ctrlChan <- jobDisable
+				<-job.disabled
+			})
+
+		})
+
 		Convey("When running long jobs", func(ctx C) {
 			scriptContent := `#!/bin/bash
 echo $TUNASYNC_WORKING_DIR