Example #1
0
// confファイルをパースする
func parseConfig(str string, baseConfig *Config) (*Config, error) {
	config := *baseConfig // 設定をコピー

	err := gcfg.ReadStringInto(&config, str)
	if err != nil {
		return nil, err
	}

	// 列挙値のチェック
	notifyType := config.Config.NotifyType
	if !util.ContainsStr(notifyType, NotifyTypes) {
		return nil, errors.New("invalid NotifyType:" + notifyType)
	}

	notifyWhen := config.Config.NotifyWhen
	if !util.ContainsStr(notifyWhen, NotifyWhens) {
		return nil, errors.New("invalid NotifyWhen:" + notifyWhen)
	}

	return &config, nil
}
Example #2
0
// Statuses APIのテスト
func TestResponseStatuses(t *testing.T) {
	// sleepを実行するstubに差し替える
	goron.SystemCommand = func(cmd string, args ...string) ([]byte, int, error) {
		if util.ContainsStr("sleep 3", args) {
			time.Sleep(time.Second * 3)
		}
		return []byte(""), 0, nil
	}

	server, wc, wsc := createTestServer(t)
	err := server.Start(wc, wsc)
	if err != nil {
		t.Error(err)
	}

	resp, err := http.Get("http://localhost:16777/statuses")
	if err != nil {
		t.Error(err)
	}
	if resp.StatusCode != 200 {
		t.Errorf("(expected) status: %d != %d", resp.StatusCode, 200)
	}
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		t.Error(err)
	}
	expected := `{"webapi_test.conf":{"root /bin/echo hello":"waiting","root sleep 3":"waiting"}}`
	if string(body) != expected {
		t.Errorf("\n\t(expected)%s\n\t(actual)  %s", expected, string(body))
	}
	resp.Body.Close()

	// cronをスタート
	server.context.Start()

	time.Sleep(time.Second)

	// 実行中の状態を取得
	resp2, err := http.Get("http://localhost:16777/statuses")
	if err != nil {
		t.Error(err)
	}
	if resp2.StatusCode != 200 {
		t.Errorf("(expected) status: %d != %d", resp2.StatusCode, 200)
	}
	body2, err := ioutil.ReadAll(resp2.Body)
	if err != nil {
		t.Error(err)
	}
	expected = `{"webapi_test.conf":{"root /bin/echo hello":"waiting","root sleep 3":"running"}}`
	if string(body2) != expected {
		t.Errorf("\n\t(expected)%s\n\t(actual)  %s", expected, string(body2))
	}
	resp2.Body.Close()

	server.context.Stop()

	pid := os.Getpid()
	syscall.Kill(pid, syscall.SIGTERM)
	err = <-wsc
}