コード例 #1
0
func TestStart(t *testing.T) {
	if !runUpstartTests {
		t.SkipNow()
	}

	opts := "name=" + jobName

	u, err := us.Dial()
	if err != nil {
		panic(err)
	}

	j, err := u.Job(jobName)
	if err != nil {
		panic(err)
	}

	defer j.Start()

	err = j.Stop()
	if err != nil {
		panic(err)
	}

	res, err := tachyon.RunAdhocTask("upstart/start", opts)
	if err != nil {
		panic(err)
	}

	if !res.Changed {
		t.Fatal("no change detected")
	}

	act, ok := res.Get("pid")
	if !ok {
		t.Fatal("pid not set")
	}

	pid, err := j.Pid()
	if err != nil {
		panic(err)
	}

	if pid != act.Read() {
		t.Fatal("job did not start?")
	}

	res, err = tachyon.RunAdhocTask("upstart/start", opts)
	if err != nil {
		panic(err)
	}

	if res.Changed {
		t.Fatal("change detected improperly")
	}

}
コード例 #2
0
func TestAptDryRun(t *testing.T) {
	if !runAptTests {
		return
	}

	res, err := tachyon.RunAdhocTask("apt", "pkg=acct dryrun=true")
	if err != nil {
		panic(err)
	}

	if !res.Changed {
		t.Error("No change detected")
	}

	if res.Data.Get("installed") != "" {
		t.Error("incorrectly found an installed version")
	}

	if res.Data.Get("candidate") == "" {
		t.Error("no candidate found")
	}

	if res.Data.Get("dryrun") != true {
		t.Error("dryrun not true")
	}
}
コード例 #3
0
func TestInstall(t *testing.T) {
	if !runUpstartTests {
		t.SkipNow()
	}

	dest := "/etc/init/upstart-test-daemon.conf"

	defer os.Remove(dest)

	opts := `name=upstart-test-daemon file=test-daemon.conf.sample`

	res, err := tachyon.RunAdhocTask("upstart/install", opts)
	if err != nil {
		panic(err)
	}

	if !res.Changed {
		t.Fatal("no change detected")
	}

	_, err = os.Stat(dest)
	if err != nil {
		t.Fatal(err)
	}
}
コード例 #4
0
func TestStop(t *testing.T) {
	if !runUpstartTests {
		t.SkipNow()
	}

	opts := "name=" + jobName

	u, err := us.Dial()
	if err != nil {
		panic(err)
	}

	j, err := u.Job(jobName)
	if err != nil {
		panic(err)
	}

	defer j.Start()

	res, err := tachyon.RunAdhocTask("upstart/stop", opts)
	if err != nil {
		panic(err)
	}

	if !res.Changed {
		t.Fatal("no change detected")
	}

	res, err = tachyon.RunAdhocTask("upstart/stop", opts)
	if err != nil {
		panic(err)
	}

	if res.Changed {
		t.Fatal("change detected improperly")
	}
}
コード例 #5
0
func TestDaemonScripts(t *testing.T) {
	if !runUpstartTests {
		t.SkipNow()
	}

	dest := "/etc/init/upstart-test-daemon.conf"

	defer os.Remove(dest)

	opts := `name=upstart-test-daemon command="date" [email protected] [email protected] [email protected] [email protected]`

	res, err := tachyon.RunAdhocTask("upstart/daemon", opts)
	if err != nil {
		panic(err)
	}

	if !res.Changed {
		t.Fatal("no change detected")
	}

	body, err := ioutil.ReadFile(dest)
	if err != nil {
		panic(err)
	}

	idx := bytes.Index(body, []byte("this is a prestart sample script"))
	if idx == -1 {
		t.Error("config didn't contain our script")
	}

	idx = bytes.Index(body, []byte("this is a poststart sample script"))
	if idx == -1 {
		t.Error("config didn't contain our script")
	}

	idx = bytes.Index(body, []byte("this is a prestop sample script"))
	if idx == -1 {
		t.Error("config didn't contain our script")
	}

	idx = bytes.Index(body, []byte("this is a poststop sample script"))
	if idx == -1 {
		t.Error("config didn't contain our script")
	}
}
コード例 #6
0
func TestRestart(t *testing.T) {
	if !runUpstartTests {
		t.SkipNow()
	}

	opts := "name=" + jobName

	u, err := us.Dial()
	if err != nil {
		panic(err)
	}

	j, err := u.Job(jobName)
	if err != nil {
		panic(err)
	}

	prev, err := j.Pid()
	if err != nil {
		panic(err)
	}

	res, err := tachyon.RunAdhocTask("upstart/restart", opts)
	if err != nil {
		panic(err)
	}

	if !res.Changed {
		t.Fatal("no change detected")
	}

	cur, err := j.Pid()
	if err != nil {
		panic(err)
	}

	if res.Data.Get("pid") != cur {
		t.Log(res.Data)
		t.Fatal("pid not set properly")
	}

	if prev == cur {
		t.Fatal("restart did not happen")
	}
}
コード例 #7
0
func TestDaemon(t *testing.T) {
	if !runUpstartTests {
		t.SkipNow()
	}

	defer os.Remove("/etc/init/upstart-test-daemon.conf")

	opts := `name=upstart-test-daemon command="date"`

	res, err := tachyon.RunAdhocTask("upstart/daemon", opts)
	if err != nil {
		panic(err)
	}

	if !res.Changed {
		t.Fatal("no change detected")
	}
}
コード例 #8
0
func TestAptInstallAndRemoves(t *testing.T) {
	if !runAptTests {
		return
	}

	defer removeAcct()

	res, err := tachyon.RunAdhocTask("apt", "pkg=acct")
	if err != nil {
		panic(err)
	}

	if !res.Changed {
		t.Fatal("No change detected")
	}

	grep := fmt.Sprintf(`apt-cache policy acct | grep "Installed: %s"`,
		res.Data.Get("installed"))

	_, err = exec.Command("sh", "-c", grep).CombinedOutput()

	if err != nil {
		t.Errorf("package did not install")
	}

	// Test that it skips too
	// Do this here instead of another test because installing is slow

	res2, err := tachyon.RunAdhocTask("apt", "pkg=acct")
	if err != nil {
		panic(err)
	}

	if res2.Changed {
		t.Fatal("acct was reinstalled incorrectly")
	}

	res3, err := tachyon.RunAdhocTask("apt", "pkg=acct state=absent")
	if err != nil {
		panic(err)
	}

	if !res3.Changed {
		t.Fatal("acct was not removed")
	}

	if res3.Data.Get("removed") != res.Data.Get("installed") {
		t.Fatalf("removed isn't set to the version removed: '%s '%s'",
			res3.Data.Get("removed"), res.Data.Get("installed"))
	}

	res4, err := tachyon.RunAdhocTask("apt", "pkg=acct state=absent")
	if err != nil {
		panic(err)
	}

	if res4.Changed {
		t.Fatal("acct was removed again")
	}

}
コード例 #9
0
func TestStartWithEnv(t *testing.T) {
	if !runUpstartTests {
		t.SkipNow()
	}

	opts := "name=" + jobName

	u, err := us.Dial()
	if err != nil {
		panic(err)
	}

	j, err := u.Job(jobName)
	if err != nil {
		panic(err)
	}

	defer j.Start()

	err = j.Stop()
	if err != nil {
		panic(err)
	}

	td := tachyon.TaskData{
		"upstart/start": map[interface{}]interface{}{
			"name": jobName,
			"env": map[interface{}]interface{}{
				"BAR": "foo",
			},
		},
	}

	res, err := tachyon.RunAdhocTaskVars(td)
	if err != nil {
		panic(err)
	}

	if !res.Changed {
		t.Fatal("no change detected")
	}

	act, ok := res.Get("pid")
	if !ok {
		t.Fatal("pid not set")
	}

	pid, err := j.Pid()
	if err != nil {
		panic(err)
	}

	if pid != act.Read() {
		t.Fatal("job did not start?")
	}

	res, err = tachyon.RunAdhocTask("upstart/start", opts)
	if err != nil {
		panic(err)
	}

	if res.Changed {
		t.Fatal("change detected improperly")
	}

}