From 99e84abc6644ad780bc0b3e213a39f64ed9e6ef6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20Sch=C3=BCller?= Date: Mon, 19 Feb 2024 18:09:10 +0100 Subject: [PATCH] api: add error message for package without name RHEL-16006 --- internal/blueprint/blueprint.go | 12 +++++++ internal/client/blueprints_test.go | 53 ++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) diff --git a/internal/blueprint/blueprint.go b/internal/blueprint/blueprint.go index b7a428edf..1602ea714 100644 --- a/internal/blueprint/blueprint.go +++ b/internal/blueprint/blueprint.go @@ -101,6 +101,18 @@ func (b *Blueprint) Initialize() error { return fmt.Errorf("Error hashing passwords: %s", err.Error()) } + for i, p := range b.Packages { + if len(p.Name) == 0 { + var errMsg string + if len(p.Version) == 0 { + errMsg = fmt.Sprintf("Entry #%d has neither version nor name.", i+1) + } else { + errMsg = fmt.Sprintf("Entry #%d has version '%v' but no name.", i+1, p.Version) + } + return fmt.Errorf("All package entries need to contain the name of the package. %s", errMsg) + } + } + return nil } diff --git a/internal/client/blueprints_test.go b/internal/client/blueprints_test.go index 8cfa9a7be..18ab2026e 100644 --- a/internal/client/blueprints_test.go +++ b/internal/client/blueprints_test.go @@ -1229,3 +1229,56 @@ func TestBlueprintChangeV1(t *testing.T) { require.NotNil(t, bp, "GET blueprint change failed: missing blueprint") assert.Equal(t, bp.Version, "0.0.1") } + +func TestEmptyPackageNameBlueprintJsonV0(t *testing.T) { + bp := `{ + "name": "test-emptypackage-blueprint-v0", + "description": "TestEmptyPackageNameBlueprintV0", + "version": "0.0.1", + "packages": [{"name": "", "version": "1.32.1"}] + }` + + // Push a blueprint + resp, err := PostJSONBlueprintV0(testState.socket, bp) + require.NoError(t, err, "failed with a client error") + require.False(t, resp.Status, "Negative status expected.") + require.Equal(t, len(resp.Errors), 1, "There should be exactly one error") + assert.True(t, strings.HasPrefix(resp.Errors[0].String(), "BlueprintsError"), "I expect a BlueprintsError") +} + +func TestEmptyPackageNameBlueprintV0(t *testing.T) { + bp := `name = "EMPTY-PACKAGE-NAME" + description = "empty package name" + version = "0.0.1" + modules = [] + groups = [] + distro = "test-distro" + + [[packages]] + version = "*" + ` + + resp, err := PostTOMLBlueprintV0(testState.socket, bp) + require.NoError(t, err, "failed with a client error") + require.False(t, resp.Status, "Negative status expected.") + require.Equal(t, len(resp.Errors), 1, "There should be exactly one error") + assert.True(t, strings.HasPrefix(resp.Errors[0].String(), "BlueprintsError"), "I expect a BlueprintsError") +} + +func TestEmptyPackageNameAndVersionBlueprintV0(t *testing.T) { + bp := `name = "EMPTY-PACKAGE-NAME" + description = "empty package name" + version = "0.0.1" + modules = [] + groups = [] + distro = "test-distro" + + [[packages]] + ` + + resp, err := PostTOMLBlueprintV0(testState.socket, bp) + require.NoError(t, err, "failed with a client error") + require.False(t, resp.Status, "Negative status expected.") + require.Equal(t, len(resp.Errors), 1, "There should be exactly one error") + assert.True(t, strings.HasPrefix(resp.Errors[0].String(), "BlueprintsError"), "I expect a BlueprintsError") +}