Skip to content

Commit 91ba5cf

Browse files
committed
Handle unknown instance ID during a config apply operation
1 parent 28adb68 commit 91ba5cf

File tree

5 files changed

+19
-5
lines changed

5 files changed

+19
-5
lines changed

api/grpc/mpi/v1/command.pb.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/grpc/mpi/v1/common.pb.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/grpc/mpi/v1/files.pb.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/resource/resource_service.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,10 @@ func (r *ResourceService) ApplyConfig(ctx context.Context, instanceID string) er
158158
var instance *mpi.Instance
159159
operator := r.instanceOperators[instanceID]
160160

161+
if operator == nil {
162+
return fmt.Errorf("no instance operator found for instance %s", instanceID)
163+
}
164+
161165
for _, resourceInstance := range r.resource.GetInstances() {
162166
if resourceInstance.GetInstanceMeta().GetInstanceId() == instanceID {
163167
instance = resourceInstance

internal/resource/resource_service_test.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,29 +240,40 @@ func TestResourceService_ApplyConfig(t *testing.T) {
240240
ctx := context.Background()
241241

242242
tests := []struct {
243+
instanceID string
243244
reloadErr error
244245
validateErr error
245246
expected error
246247
name string
247248
}{
248249
{
249250
name: "Test 1: Successful reload",
251+
instanceID: protos.GetNginxOssInstance([]string{}).GetInstanceMeta().GetInstanceId(),
250252
reloadErr: nil,
251253
validateErr: nil,
252254
expected: nil,
253255
},
254256
{
255257
name: "Test 2: Failed reload",
258+
instanceID: protos.GetNginxOssInstance([]string{}).GetInstanceMeta().GetInstanceId(),
256259
reloadErr: fmt.Errorf("something went wrong"),
257260
validateErr: nil,
258261
expected: fmt.Errorf("failed to reload NGINX %w", fmt.Errorf("something went wrong")),
259262
},
260263
{
261264
name: "Test 3: Failed validate",
265+
instanceID: protos.GetNginxOssInstance([]string{}).GetInstanceMeta().GetInstanceId(),
262266
reloadErr: nil,
263267
validateErr: fmt.Errorf("something went wrong"),
264268
expected: fmt.Errorf("failed validating config %w", fmt.Errorf("something went wrong")),
265269
},
270+
{
271+
name: "Test 4: Unknown instance ID",
272+
instanceID: "unknown",
273+
reloadErr: nil,
274+
validateErr: nil,
275+
expected: fmt.Errorf("no instance operator found for instance unknown"),
276+
},
266277
}
267278

268279
for _, test := range tests {
@@ -283,8 +294,7 @@ func TestResourceService_ApplyConfig(t *testing.T) {
283294
}
284295
resourceService.resource.Instances = instances
285296

286-
reloadError := resourceService.ApplyConfig(ctx,
287-
protos.GetNginxOssInstance([]string{}).GetInstanceMeta().GetInstanceId())
297+
reloadError := resourceService.ApplyConfig(ctx, test.instanceID)
288298
assert.Equal(t, test.expected, reloadError)
289299
})
290300
}

0 commit comments

Comments
 (0)