Skip to content

Commit

Permalink
fix: check if the image was found (#223)
Browse files Browse the repository at this point in the history
A nil check was missing before using the image object.

Closes #222
  • Loading branch information
jooola authored Aug 14, 2024
1 parent 73f10e4 commit da3b49b
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
10 changes: 10 additions & 0 deletions builder/hcloud/step_create_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,23 @@ func (s *stepCreateServer) Run(ctx context.Context, state multistep.StateBag) mu
if err != nil {
return errorHandler(state, ui, "Could not find image", err)
}
if image == nil {
return errorHandler(state, ui, "", fmt.Errorf("Could not find image"))
}
} else {
image, err = getImageWithSelectors(ctx, client, c, serverType)
if err != nil {
return errorHandler(state, ui, "Could not find image", err)
}
}
ui.Message(fmt.Sprintf("Using image '%d'", image.ID))
if image.IsDeprecated() {
ui.Errorf(
"The image '%d' is deprecated since the %s and will soon be unavailable",
image.ID, image.Deprecated.Format("2006-01-02"),
)
}

state.Put(StateSourceImageID, image.ID)

var networks []*hcloud.Network
Expand Down
30 changes: 30 additions & 0 deletions builder/hcloud/step_create_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,36 @@ func TestStepCreateServer(t *testing.T) {
assert.Regexp(t, "Could not fetch primary ip .*", err.Error())
},
},
{
Name: "fail to get image",
Step: &stepCreateServer{},
SetupConfigFunc: func(c *Config) {},
SetupStateFunc: func(state multistep.StateBag) {
state.Put(StateSSHKeyID, int64(1))
state.Put(StateServerType, &hcloud.ServerType{ID: 9, Name: "cpx11", Architecture: "x86"})
},
WantRequests: []mockutil.Request{
{Method: "GET", Path: "/ssh_keys/1",
Status: 200,
JSONRaw: `{
"ssh_key": { "id": 1 }
}`,
},
{Method: "GET", Path: "/images?architecture=x86&include_deprecated=true&name=debian-12",
Status: 200,
JSONRaw: `{
"images": []
}`,
},
},
WantStepAction: multistep.ActionHalt,
WantStateFunc: func(t *testing.T, state multistep.StateBag) {
err, ok := state.Get(StateError).(error)
assert.True(t, ok)
assert.NotNil(t, err)
assert.EqualError(t, err, "Could not find image")
},
},
})
}

Expand Down

0 comments on commit da3b49b

Please sign in to comment.