Validate port label for host address mode

Also skip getting an address for script checks which don't use them.

Fixed a weird invalid reserved port in a TaskRunner test helper as well
as a problem with our mock Alloc/Job. Hopefully the latter doesn't cause
other tests to fail, but we were referencing an invalid PortLabel and
just not catching it before.
This commit is contained in:
Michael Schurter
2017-12-07 21:58:15 -08:00
parent f11360acf8
commit b999358892
4 changed files with 23 additions and 6 deletions

View File

@@ -119,9 +119,6 @@ func testTaskRunnerFromAlloc(t *testing.T, restarts bool, alloc *structs.Allocat
upd := &MockTaskStateUpdater{}
task := alloc.Job.TaskGroups[0].Tasks[0]
// Initialize the port listing. This should be done by the offer process but
// we have a mock so that doesn't happen.
task.Resources.Networks[0].ReservedPorts = []structs.Port{{Label: "", Value: 80}}
allocDir := allocdir.NewAllocDir(testLogger(), filepath.Join(conf.AllocDir, alloc.ID))
if err := allocDir.Build(); err != nil {
@@ -347,7 +344,7 @@ func TestTaskRunner_Update(t *testing.T) {
task.Services[0].Checks[0] = &structs.ServiceCheck{
Name: "http-check",
Type: "http",
PortLabel: "web",
PortLabel: "http",
Path: "${NOMAD_META_foo}",
}
task.Driver = "mock_driver"

View File

@@ -632,6 +632,13 @@ func (c *ServiceClient) checkRegs(ops *operations, allocID, serviceID string, se
ops.scripts = append(ops.scripts, newScriptCheck(
allocID, task.Name, checkID, check, exec, c.client, c.logger, c.shutdownCh))
// Skip getAddress for script checks
checkReg, err := createCheckReg(serviceID, checkID, check, "", 0)
if err != nil {
return nil, fmt.Errorf("failed to add script check %q: %v", check.Name, err)
}
ops.regChecks = append(ops.regChecks, checkReg)
continue
}
// Default to the service's port but allow check to override
@@ -1098,6 +1105,9 @@ func getAddress(addrMode, portLabel string, networks structs.Networks, driverNet
case structs.AddressModeHost:
// Default path: use host ip:port
ip, port := networks.Port(portLabel)
if ip == "" && port == 0 {
return "", 0, fmt.Errorf("invalid port %q: port label not found", portLabel)
}
return ip, port, nil
case structs.AddressModeDriver:

View File

@@ -1541,6 +1541,12 @@ func TestGetAddress(t *testing.T) {
},
ErrContains: "invalid port",
},
{
Name: "HostBadPort",
Mode: structs.AddressModeHost,
PortLabel: "bad-port-label",
ErrContains: "invalid port",
},
{
Name: "InvalidMode",
Mode: "invalid-mode",
@@ -1574,7 +1580,11 @@ func TestGetAddress(t *testing.T) {
if tc.ErrContains == "" {
assert.Nil(t, err)
} else {
assert.Contains(t, err.Error(), tc.ErrContains)
if err == nil {
t.Fatalf("expected error containing %q but err=nil", tc.ErrContains)
} else {
assert.Contains(t, err.Error(), tc.ErrContains)
}
}
})
}

View File

@@ -119,7 +119,7 @@ func Job() *structs.Job {
},
{
Name: "${TASK}-admin",
PortLabel: "admin",
PortLabel: "main",
},
},
LogConfig: structs.DefaultLogConfig(),