Exemple #1
0
func TestGetServerNginx(t *testing.T) {
	if !nginxPrep() {
		t.SkipNow()
	}

	server, err := balance.GetServer(testService1.Id, "not-real")
	if err == nil {
		t.Errorf("Failed to fail GETTING server - %v", err)
		t.FailNow()
	}

	balance.SetService(&testService1)

	server, err = balance.GetServer(testService1.Id, "not-real")
	if err == nil {
		t.Errorf("Failed to fail GETTING server - %v", err)
		t.FailNow()
	}

	svc := testService1
	svc.Servers = append(svc.Servers, testServer2)
	balance.SetService(&svc)

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

	if server.Id != testServer2.Id {
		t.Errorf("Read server differs from written server")
	}
}
Exemple #2
0
func TestSetServicesNginx(t *testing.T) {
	if !nginxPrep() {
		t.SkipNow()
	}

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

	if err := balance.SetServices([]core.Service{testService2}); err != nil {
		t.Errorf("Failed to SET services - %v", err)
		t.FailNow()
	}

	_, err := balance.GetService(testService1.Id)
	if err == nil {
		t.Errorf("Failed to clear old services on PUT - %v", err)
		t.FailNow()
	}

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

	if service.Host != testService2.Host {
		t.Errorf("Read service differs from written service")
	}
}
Exemple #3
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")
	}
}
Exemple #4
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")
	}
}
Exemple #5
0
func TestSetServersNginx(t *testing.T) {
	if !nginxPrep() {
		t.SkipNow()
	}

	servers := []core.Server{testServer2}
	if err := balance.SetServers(testService1.Id, servers); err == nil {
		t.Errorf("Failed to fail SETTING servers - %v", err)
		t.FailNow()
	}

	balance.SetService(&testService1)

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

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

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

	if service.Servers[0].Host != svc.Servers[0].Host {
		t.Errorf("Failed to clear old servers on PUT")
	}
}
Exemple #6
0
func DeleteService(svcId string) error {
	// in case of failure
	oldService, err := database.GetService(svcId)
	if err != nil {
		if !strings.Contains(err.Error(), "No Service Found") {
			return err
		}
	}

	// delete backend rule
	err = balance.DeleteService(svcId)
	if err != nil {
		return err
	}

	if !database.CentralStore {
		// remove from backend
		err = database.DeleteService(svcId)
		if err != nil {
			// undo balance action
			if oldService != nil {
				if uerr := balance.SetService(oldService); uerr != nil {
					err = fmt.Errorf("%v - %v", err.Error(), uerr.Error())
				}
			}
			return err
		}
	}
	return nil
}
Exemple #7
0
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
}
Exemple #8
0
////////////////////////////////////////////////////////////////////////////////
// SERVICES
////////////////////////////////////////////////////////////////////////////////
func TestSetServiceNginx(t *testing.T) {
	if !nginxPrep() {
		t.SkipNow()
	}
	if err := balance.SetService(&testService1); err != nil {
		t.Errorf("Failed to SET service - %v", err)
		t.FailNow()
	}

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

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

	if service.Host != testService1.Host {
		t.Errorf("Read service differs from written service")
	}
}
Exemple #9
0
func TestGetServersNginx(t *testing.T) {
	if !nginxPrep() {
		t.SkipNow()
	}

	svc := testService1
	svc.Servers = append(svc.Servers, testServer2)
	balance.SetService(&svc)

	service, err := balance.GetService(testService1.Id)
	if err != nil {
		t.Errorf("Failed to GET servers - %v", err)
		t.FailNow()
	}

	if service.Servers[0].Id != testServer2.Id {
		t.Errorf("Read server differs from written server")
	}
}
Exemple #10
0
func TestDeleteServiceNginx(t *testing.T) {
	if !nginxPrep() {
		t.SkipNow()
	}
	if err := balance.DeleteService(testService2.Id); err != nil {
		t.Errorf("Failed to DELETE nonexistant service - %v", err)
	}

	balance.SetService(&testService2)

	if err := balance.DeleteService(testService2.Id); err != nil {
		t.Errorf("Failed to DELETE service - %v", err)
	}

	_, err := balance.GetService(testService2.Id)
	if err == nil {
		t.Error(err)
	}
}
Exemple #11
0
////////////////////////////////////////////////////////////////////////////////
// SERVICES
////////////////////////////////////////////////////////////////////////////////
func TestSetService(t *testing.T) {
	if skip {
		t.SkipNow()
	}
	if err := balance.SetService(&testService1); err != nil {
		t.Errorf("Failed to SET service - %v", err)
		t.FailNow()
	}

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

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