Skip to content

Commit

Permalink
add main.TestStart()
Browse files Browse the repository at this point in the history
  • Loading branch information
Songmu committed Feb 24, 2016
1 parent 53fb9d3 commit 469fff0
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 6 deletions.
4 changes: 0 additions & 4 deletions commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/Songmu/prompter"
"github.com/mackerelio/mackerel-agent/command"
"github.com/mackerelio/mackerel-agent/config"
"github.com/mackerelio/mackerel-agent/logging"
"github.com/mackerelio/mackerel-agent/mackerel"
"github.com/mackerelio/mackerel-agent/version"
)
Expand All @@ -27,9 +26,6 @@ func doMain(fs *flag.FlagSet, argv []string) error {
if err != nil {
return fmt.Errorf("failed to load config: %s", err)
}
if conf.Verbose {
logging.SetLogLevel(logging.DEBUG)
}
return start(conf, make(chan struct{}))
}

Expand Down
7 changes: 5 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,11 @@ func removePidFile(pidfile string) {
}

func start(conf *config.Config, termCh chan struct{}) error {
logger.Infof("Starting mackerel-agent version:%s, rev:%s, apibase:%s",
version.VERSION, version.GITCOMMIT, conf.Apibase)
if conf.Verbose {
logging.SetLogLevel(logging.DEBUG)
}
logger.Infof("Starting mackerel-agent version:%s, rev:%s, apibase:%s", version.VERSION, version.GITCOMMIT, conf.Apibase)

if err := createPidFile(conf.Pidfile); err != nil {
return fmt.Errorf("createPidFile(%q) failed: %s", conf.Pidfile, err)
}
Expand Down
101 changes: 101 additions & 0 deletions start_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package main

import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"testing"
"time"

"github.com/mackerelio/mackerel-agent/mackerel"
)

func respJSON(w http.ResponseWriter, data map[string]interface{}) {
respJSON, _ := json.Marshal(data)
w.Header().Set("Content-Type", "application/json")
fmt.Fprint(w, string(respJSON))
}

func TestStart(t *testing.T) {
hostID := "xxx1234567890"
mux := http.NewServeMux()
mux.HandleFunc("/api/v0/hosts/"+hostID, func(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "GET":
respJSON(w, map[string]interface{}{
"host": mackerel.Host{
ID: hostID,
Name: "host.example.com",
Status: "standby",
},
})
case "PUT":
respJSON(w, map[string]interface{}{
"result": "OK",
})
default:
t.Errorf("request method should be PUT or GET but :%s", r.Method)
}
})
mux.HandleFunc("/api/v0/tsdb", func(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "POST":
payload := []mackerel.CreatingMetricsValue{}
err := json.NewDecoder(r.Body).Decode(&payload)
if err != nil {
t.Errorf("decode failed: %s", err)
}

respJSON(w, map[string]interface{}{
"success": true,
})
default:
t.Errorf("request method should be POST but: %s", r.Method)
}
})
ts := httptest.NewServer(mux)
defer ts.Close()

root, err := ioutil.TempDir("", "mackerel-config-test")
if err != nil {
t.Fatalf("Could not create temprary dir for test")
}
defer os.RemoveAll(root)

confFile, err := os.Create(filepath.Join(root, "mackerel-agent.conf"))
if err != nil {
t.Fatalf("Could not create temprary file for test")
}
confFile.WriteString(`apikey="DUMMYAPIKEY"` + "\n")
confFile.Sync()
confFile.Close()
argv := []string{
"-conf=" + confFile.Name(),
"-apibase=" + ts.URL,
"-pidfile=" + root + "/pid",
"-root=" + root,
"-verbose",
}
conf, err := resolveConfig(&flag.FlagSet{}, argv)
if err != nil {
t.Errorf("err should be nil, but got: %s", err)
}
conf.SaveHostID(hostID)
termCh := make(chan struct{})
done := make(chan struct{})
go func() {
err = start(conf, termCh)
done <- struct{}{}
}()
time.Sleep(5 * time.Second)
termCh <- struct{}{}
<-done
if err != nil {
t.Errorf("err should be nil but: %s", err)
}
}

0 comments on commit 469fff0

Please sign in to comment.