示例#1
0
func TestSetService(t *testing.T) {
	config.DatabaseConnection = "scribble:///tmp/scribbleTest"
	config.Log = lumber.NewConsoleLogger(lumber.LvlInt("FATAL"))

	// Backend = &database.ScribbleDatabase{}
	database.Init()

	if err := database.SetService(&testService1); err != nil {
		t.Errorf("Failed to SET service - %v", err)
	}

	service, err := ioutil.ReadFile("/tmp/scribbleTest/services/tcp-192_168_0_15-80.json")
	if err != nil {
		t.Error(err)
	}

	jService, err := toJson(testService1)
	if err != nil {
		t.Error(err)
	}

	if string(service) != string(jService) {
		t.Errorf("Read service differs from written service")
	}
}
示例#2
0
文件: redis.go 项目: nanopack/portal
// SetService tells all members to add the service to their database.
// rolls back on failure
func (r *Redis) SetService(service *core.Service) error {
	conn := pool.Get()
	defer conn.Close()

	// publishJson to others
	err := r.publishJson(conn, "set-service", service)
	if err != nil {
		// nothing to rollback yet (nobody received)
		return err
	}

	actionHash := fmt.Sprintf("%x", md5.Sum([]byte(fmt.Sprintf("set-service %s", *service))))

	// ensure all members applied action
	err = r.waitForMembers(conn, actionHash)
	if err != nil {
		uActionHash := fmt.Sprintf("%x", md5.Sum([]byte(fmt.Sprintf("delete-service %s", service.Id))))
		// cleanup rollback cruft. clear actionHash ensures no mistakes on re-submit
		defer conn.Do("DEL", uActionHash, actionHash)
		// attempt rollback - no need to waitForMembers here
		uerr := r.publishString(conn, "delete-service", service.Id)
		if uerr != nil {
			err = fmt.Errorf("%v - %v", err, uerr)
		}
		return err
	}

	if database.CentralStore {
		return database.SetService(service)
	}

	return nil
}
示例#3
0
文件: common.go 项目: nanopack/portal
func SetService(service *core.Service) error {
	// in case of failure
	oldServices, err := database.GetServices()
	if err != nil {
		return err
	}

	// apply to balancer
	err = balance.SetService(service)
	if err != nil {
		return err
	}

	if !database.CentralStore {
		// save to backend
		err = database.SetService(service)
		if err != nil {
			// undo balancer action
			if uerr := balance.SetServices(oldServices); uerr != nil {
				err = fmt.Errorf("%v - %v", err.Error(), uerr.Error())
			}
			return err
		}
	}
	return nil
}
示例#4
0
文件: none.go 项目: nanopack/portal
func (n None) SetService(service *core.Service) error {
	err := common.SetService(service)
	if err != nil {
		return err
	}
	if database.CentralStore {
		return database.SetService(service)
	}
	return nil
}
示例#5
0
func TestSetServer(t *testing.T) {
	database.SetService(&testService1)
	if err := database.SetServer(testService1.Id, &testServer1); err != nil {
		t.Errorf("Failed to SET server - %v", err)
	}

	service, err := ioutil.ReadFile("/tmp/scribbleTest/services/tcp-192_168_0_15-80.json")
	if err != nil {
		t.Error(err)
	}

	svc := testService1
	svc.Servers = append(svc.Servers, testServer1)
	jService, err := toJson(svc)
	if err != nil {
		t.Error(err)
	}

	if string(service) != string(jService) {
		t.Errorf("Read service differs from written service")
	}
}