Fix crash when a machine could not be created and when invalid data is returned

parent 1e3e4a29
......@@ -97,7 +97,7 @@ func (e *machineExecutor) Cleanup() {
}
// Release allocated machine
if e.data != "" {
if e.data != nil {
e.provider.Release(&e.config, e.data)
e.data = nil
}
......
......@@ -345,6 +345,7 @@ func (m *machineProvider) Use(config *common.RunnerConfig, data common.ExecutorD
if newData != nil {
m.Release(config, newData)
}
newData = nil
return
}
......@@ -374,7 +375,8 @@ func (m *machineProvider) Release(config *common.RunnerConfig, data common.Execu
}
// Remove machine if we already used it
if config.Machine.MaxBuilds > 0 && details.UsedCount >= config.Machine.MaxBuilds {
if config != nil && config.Machine != nil &&
config.Machine.MaxBuilds > 0 && details.UsedCount >= config.Machine.MaxBuilds {
err := m.remove(details.Name, "Too many builds")
if err == nil {
return nil
......
......@@ -529,3 +529,23 @@ func TestMachineUseOnDemand(t *testing.T) {
assert.Error(t, err, "fail to create a new machine on connect")
assertTotalMachines(t, p, 3, "it fails on no-connect, but we leave the machine created")
}
func TestMachineReleaseIfInvalidDataArePassed(t *testing.T) {
p, _ := testMachineProvider()
_, nd, err := p.Use(machineDefaultConfig, nil)
assert.NoError(t, err, "it create a new machine")
assert.NotNil(t, nd)
assertTotalMachines(t, p, 1, "it creates one machine")
err = p.Release(nil, nd)
assert.NoError(t, err, "it should not fail")
}
func TestMachineCreationIfFailedToConnect(t *testing.T) {
p, _ := testMachineProvider()
_, nd, err := p.Use(machineNoConnect, nil)
assert.Error(t, err, "it create a new machine")
assert.Nil(t, nd)
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment