Пример #1
0
func TestSetServerNginx(t *testing.T) {
	if !nginxPrep() {
		t.SkipNow()
	}

	balance.SetService(&testService1)

	if err := balance.SetServer(testService1.Id, &testServer1); err != nil {
		t.Errorf("Failed to SET server - %v", err)
		t.FailNow()
	}

	if err := balance.SetServer(testService1.Id, &testServer1); err != nil {
		t.Errorf("Failed to SET server - %v", err)
		t.FailNow()
	}

	service, err := balance.GetService(testService1.Id)
	if err != nil {
		t.Error(err)
	}

	svc := testService1
	svc.Servers = append(svc.Servers, testServer1)

	if service.Servers[0].Host != svc.Servers[0].Host {
		t.Errorf("Read service differs from written service")
	}
}
Пример #2
0
func TestDeleteServerNginx(t *testing.T) {
	if !nginxPrep() {
		t.SkipNow()
	}

	err := balance.DeleteServer("not-real-thing", testServer2.Id)
	if err == nil {
		t.Errorf("Failed to DELETE nonexistant server - %v", err)
	}

	balance.SetService(&testService1)

	err = balance.DeleteServer(testService1.Id, testServer2.Id)
	if err != nil {
		t.Errorf("Failed to DELETE nonexistant server - %v", err)
	}

	balance.SetServer(testService1.Id, &testServer2)

	err = balance.DeleteServer(testService1.Id, testServer2.Id)
	if err != nil {
		t.Errorf("Failed to DELETE server - %v", err)
	}

	// todo: read from ipvsadm
	service, err := balance.GetService(testService1.Id)
	if err != nil {
		t.Error(err)
		t.FailNow()
	}

	if service.Id != testService1.Id {
		t.Errorf("Read service differs from written service")
	}
}
Пример #3
0
func DeleteServer(svcId, srvId string) error {
	// in case of failure
	srv, err := database.GetServer(svcId, srvId)
	if err != nil {
		if !strings.Contains(err.Error(), "No Server Found") {
			return err
		}
	}

	// delete rule from balancer
	if err = balance.DeleteServer(svcId, srvId); err != nil {
		if !strings.Contains(err.Error(), "No Server Found") {
			return err
		}
	}

	if !database.CentralStore {
		// remove from backend
		if err = database.DeleteServer(svcId, srvId); err != nil && !strings.Contains(err.Error(), "No Server Found") {
			// undo balance action
			if uerr := balance.SetServer(svcId, srv); uerr != nil {
				err = fmt.Errorf("%v - %v", err.Error(), uerr.Error())
			}
			return err
		}
	}
	return nil
}
Пример #4
0
func SetServer(svcId string, server *core.Server) error {
	// // idempotent additions (don't update server on post)
	// if srv, _ := database.GetServer(svcId, server.Id); srv != nil {
	//  return nil
	// }

	// apply to balancer
	err := balance.SetServer(svcId, server)
	if err != nil {
		return err
	}

	if !database.CentralStore {
		// save to backend
		err = database.SetServer(svcId, server)
		if err != nil {
			// undo balance action
			if uerr := balance.DeleteServer(svcId, server.Id); uerr != nil {
				err = fmt.Errorf("%v - %v", err.Error(), uerr.Error())
			}
			return err
		}
	}
	return nil
}