示例#1
0
func TestMain(m *testing.M) {
	// open database in memory for testing
	mailbox.OpenMemDB()
	err := mailbox.CreateDB()
	if err != nil {
		panic(err)
	}

	// create a default mailbox to use
	mb, err = mailbox.Create("mb")
	if err != nil {
		panic(err)
	}

	// create an access token for the default mailbox
	accessKey = &mailbox.AccessKey{FullAccess: true}
	accessKey.Create()

	// create a postmasterClient
	pmClient = client.Client{
		Host:          "localhost:4111",
		Mailbox:       mb.Id,
		AccessKeyName: accessKey.Name,
		AccessKey:     accessKey.Secret,
	}

	// Start up a test server to use
	server.EnableLongPolling = false
	go server.Start(":4111")
	retCode := m.Run()

	// cleanup
	mailbox.CloseDB()
	os.Exit(retCode)
}
示例#2
0
func TestMain(m *testing.M) {
	mailbox.OpenMemDB()
	mailbox.CreateDB()

	retCode := m.Run()

	mailbox.CloseDB()
	os.Exit(retCode)
}
示例#3
0
文件: server.go 项目: 5Sigma/Conduit
// Start loads the web server and begins listening.
func Start(addr string) error {
	if serverRunning == true {
		return nil
	}

	if mailbox.DB == nil {
		mailbox.OpenDB()
		if _, err := os.Stat("mailboxes.db"); os.IsNotExist(err) {
			err := mailbox.CreateDB()
			if err != nil {
				panic(err)
			}
		}
	}

	cleanupTicker := time.Tick(1 * time.Hour)
	go func() {
		for {
			select {
			case <-cleanupTicker:
				err := cleanupFiles()
				if err != nil {
					log.Warn(err.Error())
				}
			}
		}
	}()

	endpoints := EndPointHandler{}
	svr := &http.Server{
		Addr:         addr,
		ReadTimeout:  12 * time.Minute,
		WriteTimeout: 12 * time.Minute,
	}

	endpoints.Add("GET", `/upgrade`, sendConduitBinary)
	endpoints.Add("POST", `/get`, getMessage)
	endpoints.Add("POST", "/put", putMessage)
	endpoints.Add("POST", "/stats/clients", clientStats)
	endpoints.Add("POST", "/stats", systemStats)
	endpoints.Add("POST", "/delete", deleteMessage)
	endpoints.Add("POST", "/deploy/list", deployInfo)
	endpoints.Add("POST", "/deploy/respond", deployRespond)
	endpoints.Add("POST", "/register", register)
	endpoints.Add("POST", "/deregister", deregister)
	endpoints.Add("POST", "/upload", acceptFile)
	endpoints.Add("POST", "/checkfile", checkfile)
	endpoints.Add("POST", "/asset", getAsset)
	http.Handle("/", &endpoints)
	serverRunning = true
	err := svr.ListenAndServe()
	return err
}
示例#4
0
func TestMain(m *testing.M) {
	mailbox.OpenMemDB()
	mailbox.CreateDB()

	server.EnableLongPolling = false
	server.ThrottleDelay = 0

	go server.Start(":4111")

	retCode := m.Run()

	mailbox.CloseDB()
	os.Exit(retCode)
}
示例#5
0
func TestDeploy(t *testing.T) {
	mailbox.OpenMemDB()
	mailbox.CreateDB()
	os.Create("test.js")
	file, err := os.OpenFile("test.js", os.O_APPEND|os.O_WRONLY, 0644)
	file.WriteString("console.log('test');")
	file.Close()

	mb, err := mailbox.Create("test.test")
	if err != nil {
		t.Fatal(err)
	}
	if err != nil {
		t.Fatal(err)
	}

	key := mailbox.AccessKey{FullAccess: true}
	err = key.Create()
	if err != nil {
		t.Fatal(err)
	}

	viper.Set("host", ":5112")
	viper.Set("mailbox", mb.Id)
	viper.Set("access_key", key.Secret)
	viper.Set("access_key_name", key.Name)
	viper.Set("show_requests", true)

	go serverCmd.Run(serverCmd, []string{})
	deployCmd.ParseFlags([]string{"-x"})
	deployCmd.Run(deployCmd, []string{"test.js", "test.test"})

	os.Remove("test.js")

	msg, err := mb.GetMessage()
	if err != nil {
		t.Fatal(err)
	}
	if msg == nil {
		t.Fatal("No message waiting")
	}
}
示例#6
0
func TestAsset(t *testing.T) {

	mailbox.OpenMemDB()
	mailbox.CreateDB()

	mb, err := mailbox.Create("test.asset")
	if err != nil {
		t.Fatal(err)
	}
	if err != nil {
		t.Fatal(err)
	}

	key := mailbox.AccessKey{FullAccess: true}
	err = key.Create()
	if err != nil {
		t.Fatal(err)
	}

	viper.Set("host", ":5112")
	viper.Set("mailbox", mb.Id)
	viper.Set("access_key", key.Secret)
	viper.Set("access_key_name", key.Name)
	viper.Set("show_requests", true)
	client, err := ClientFromConfig()
	if err != nil {
		t.Fatal(err)
	}

	resp, err := client.CheckRemoteFile("324e63777cae0113d708633836c9cb18")
	if err != nil {
		t.Fatal(err)
	}

	if resp != false {
		t.Fatal("Check file came back true before upload.")
	}

	os.Create("test.js")
	file, err := os.OpenFile("test.js", os.O_APPEND|os.O_WRONLY, 0644)
	file.WriteString("console.log('test');")
	file.Close()

	go serverCmd.Run(serverCmd, []string{})
	deployCmd.ParseFlags([]string{"-x", "-a", "test.js"})
	deployCmd.Run(deployCmd, []string{"test.js", "test.asset"})

	defer os.Remove("test.js")

	resp, err = client.CheckRemoteFile("324e63777cae0113d708633836c9cb18")
	if err != nil {
		t.Fatal(err)
	}

	if resp != true {
		t.Fatal("File check came back false after upload")
	}

	fname, err := client.DownloadAsset("324e63777cae0113d708633836c9cb18")
	if err != nil {
		t.Fatal(err)
	}

	defer os.Remove(fname)

	fData, err := ioutil.ReadFile(fname)
	if err != nil {
		t.Fatal(err)
	}
	if string(fData) != "console.log('test');" {
		t.Fatal("File data is wrong")
	}
}