func TestThatReloadCommandExecutesOnFsChanges(t *testing.T) {
	tmpDir := setup(t)
	defer os.RemoveAll(tmpDir)

	// start the utility in background
	go main()

	status = sync.GetStatusInstance()
	if time.Since(status.LastSync).Seconds() < 2 {
		t.Fatal("sync should not happen immediately" + time.Since(status.LastSync).String())
	}
	if time.Since(status.LastReload).Seconds() > 2 {
		t.Fatal("LastReload should be current but was " + time.Since(status.LastReload).String())
	}
	if time.Since(status.LastFSChangeDetected).Seconds() > 2 {
		t.Fatal("LastFSChangeDetected should be current but was " + time.Since(status.LastFSChangeDetected).String())
	}

	// wait for some time to init
	time.Sleep(500 * time.Millisecond)

	//modifyFS: create a new directory and file
	dir, err := ioutil.TempDir(tmpDir, "new-dir-")
	if err != nil {
		t.Fatal(err)
	}
	f1, err := createFile(t, dir, "new-file-content")
	if err != nil {
		t.Fatal(err)
	}
	defer os.Remove(f1.Name()) // clean up

	// use a reload command to change the content of the new file added
	// so that we later test its content, expecting to find this change
	reload_cmd := "sed -i.old s/new/reload_cmd/ " + f1.Name()
	reloadCmd = &reload_cmd

	// wait for some time to track the changes
	time.Sleep(1000 * time.Millisecond)

	// check that reload cmd has been executed
	if time.Since(status.LastSync).Seconds() > 1 {
		t.Fatal("sync should have executed earlier than 1.5 but was executed " + time.Since(status.LastSync).String())
	}
	if time.Since(status.LastReload).Seconds() > 1 {
		t.Fatal("reload should have executed earlier than 1.5s but was executed " + time.Since(status.LastReload).String())
	}
	if time.Since(status.LastFSChangeDetected).Seconds() > 1 {
		t.Fatal("FS changes should have been detected earlier than 1.5s but was detected " + time.Since(status.LastFSChangeDetected).String())
	}

	c, err := ioutil.ReadFile(f1.Name())
	if !strings.HasPrefix(string(c), "reload_cmd-file-content") {
		t.Fatal("reload cmd did not run correctly. File content was:" + string(c))
	}

	//reset the reload command
	reload_cmd = "echo reload-cmd not defined"
	reloadCmd = &reload_cmd
}
コード例 #2
0
package ws

import (
	"encoding/json"
	"github.com/adobe-apiplatform/api-gateway-config-supervisor/sync"
	"log"
	"net/http"
)

var status = sync.GetStatusInstance()

func healthCheckHandler(w http.ResponseWriter, r *http.Request) {
	w.Header().Add("Content-type", "application/json")
	status.Status = "OK"
	json.NewEncoder(w).Encode(status)
}

// Starts a web server to expose health-check endpoints
// the assumption is that as long as the server works, the sync works too
// We may add more checks to make sure the sync executed correctly
func RunWS(httpAddr string) {
	log.Println("Starting HTTP on port", httpAddr)
	http.HandleFunc("/health-check", healthCheckHandler)
	http.ListenAndServe(httpAddr, nil)
}