Skip to content
This repository has been archived by the owner on Aug 3, 2020. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
ibuildthecloud committed Jul 15, 2015
1 parent 4f2dab2 commit a79e4bd
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 5 deletions.
15 changes: 12 additions & 3 deletions rancher/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -364,19 +364,19 @@ func (r *RancherService) getLbLinks() ([]interface{}, error) {
func (r *RancherService) getLbLinkPorts(name string) ([]string, error) {
labelName := "io.rancher.loadbalancer.target." + name
v := r.serviceConfig.Labels.MapParts()[labelName]
if len(v) == 0 {
if v == "" {
if len(r.serviceConfig.Ports) != 1 {
return nil, fmt.Errorf("Failed to find target ports for %s, add label %s", name, labelName)
}
parts := strings.SplitN(r.serviceConfig.Ports[0], ",", 2)
parts := TrimSplit(TrimSplit(r.serviceConfig.Ports[0], "/", 2)[0], ":", 2)
if len(parts) == 2 {
return []string{parts[1]}, nil
} else {
return []string{parts[0]}, nil
}
}

return strings.Split(v, ","), nil
return TrimSplit(v, ",", -1), nil
}

func (r *RancherService) getServiceLinks() ([]interface{}, error) {
Expand Down Expand Up @@ -710,3 +710,12 @@ func (r *RancherService) DependentServices() []project.ServiceRelationship {
func (r *RancherService) Pull() error {
return nil
}

func TrimSplit(str, sep string, count int) []string {
result := []string{}
for _, i := range strings.SplitN(strings.TrimSpace(str), sep, count) {
result = append(result, strings.TrimSpace(i))
}

return result
}
125 changes: 123 additions & 2 deletions tests/integration/cattletest/core/test_compose.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,8 +379,129 @@ def test_lb(client, compose):
project = find_one(client.list_environment, name=project_name)
assert len(project.services()) == 3
lb = _get_service(project.services(), 'lb')
_get_service(project.services(), 'web')
_get_service(project.services(), 'web2')
web = _get_service(project.services(), 'web')
web2 = _get_service(project.services(), 'web2')

maps = client.list_service_consume_map(serviceId=lb.id)
assert len(maps) == 2

for map in maps:
if map.consumedServiceId == web.id:
assert map.ports == ['80']
elif map.consumedServiceId == web2.id:
assert map.ports == ['80']
else:
assert False

assert lb.type == 'loadBalancerService'


def test_lb_default_port_http(client, compose):
template = '''
lb:
image: rancher/load-balancer-service
ports:
- 7900:80/tcp
links:
- web
web:
image: nginx
'''

project_name = create_project(compose, input=template)

project = find_one(client.list_environment, name=project_name)
assert len(project.services()) == 2
lb = _get_service(project.services(), 'lb')
web = _get_service(project.services(), 'web')
assert lb.launchConfig.ports == ['7900:80/tcp']

map = find_one(client.list_service_consume_map, serviceId=lb.id)
assert map.consumedServiceId == web.id
assert map.ports == ['80']


def test_lb_default_port_with_mapped_tcp(client, compose):
template = '''
lb:
image: rancher/load-balancer-service
ports:
- 80:8080/tcp
links:
- web
web:
image: nginx
'''

project_name = create_project(compose, input=template)

project = find_one(client.list_environment, name=project_name)
assert len(project.services()) == 2
lb = _get_service(project.services(), 'lb')
assert lb.launchConfig.ports == ['80:8080/tcp']

web = _get_service(project.services(), 'web')

map = find_one(client.list_service_consume_map, serviceId=lb.id)
assert map.consumedServiceId == web.id
assert map.ports == ['8080']


def test_lb_default_port_with_tcp(client, compose):
template = '''
lb:
image: rancher/load-balancer-service
ports:
- 80/tcp
links:
- web
web:
image: nginx
'''

project_name = create_project(compose, input=template)

project = find_one(client.list_environment, name=project_name)
assert len(project.services()) == 2
lb = _get_service(project.services(), 'lb')
web = _get_service(project.services(), 'web')

map = find_one(client.list_service_consume_map, serviceId=lb.id)
assert map.consumedServiceId == web.id
assert map.ports == ['80']


def test_lb_path_space_target(client, compose):
template = '''
lb:
image: rancher/load-balancer-service
ports:
- 80:8080
labels:
io.rancher.loadbalancer.target.web: "6000:hostname/path,
7000"
links:
- web
web:
image: nginx
'''

project_name = create_project(compose, input=template)

project = find_one(client.list_environment, name=project_name)
assert len(project.services()) == 2
lb = _get_service(project.services(), 'lb')
web = _get_service(project.services(), 'web')

maps = client.list_service_consume_map(serviceId=lb.id)
assert len(maps) == 1

for map in maps:
if map.consumedServiceId == web.id:
assert map.ports == ['6000:hostname/path',
'7000']
else:
assert False

assert lb.type == 'loadBalancerService'

Expand Down

0 comments on commit a79e4bd

Please sign in to comment.