From 8e269c911cf5d0a07037258e81bdfdd5795f8baf Mon Sep 17 00:00:00 2001 From: Michael Mraka Date: Tue, 18 Jun 2024 13:10:59 +0200 Subject: [PATCH 1/7] RHINENG-10240: check db query for error --- manager/controllers/template_systems_update.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/manager/controllers/template_systems_update.go b/manager/controllers/template_systems_update.go index 0af223e21..50b124cb1 100644 --- a/manager/controllers/template_systems_update.go +++ b/manager/controllers/template_systems_update.go @@ -134,9 +134,12 @@ func templateArchVersionMatch( Version string }{} var err error - database.Systems(db, acc, groups). + err = database.Systems(db, acc, groups). Select("ih.id as inventory_id, ih.system_profile->'operating_system'->>'major' as version, sp.arch as arch"). - Where("id in (?)", inventoryIDs).Find(&sysArchVersions) + Where("id in (?)", inventoryIDs).Find(&sysArchVersions).Error + if err != nil { + return err + } for _, sys := range sysArchVersions { if sys.Version != template.Version && sys.Arch != template.Arch { From 1361086b547c2e29f7a2a4f5d5feb49dc407a8fe Mon Sep 17 00:00:00 2001 From: Michael Mraka Date: Tue, 18 Jun 2024 13:12:07 +0200 Subject: [PATCH 2/7] RHINENG-10240: fix column reference "id" is ambiguous (SQLSTATE 42702) --- manager/controllers/template_systems_update.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/manager/controllers/template_systems_update.go b/manager/controllers/template_systems_update.go index 50b124cb1..b83e31169 100644 --- a/manager/controllers/template_systems_update.go +++ b/manager/controllers/template_systems_update.go @@ -136,7 +136,7 @@ func templateArchVersionMatch( var err error err = database.Systems(db, acc, groups). Select("ih.id as inventory_id, ih.system_profile->'operating_system'->>'major' as version, sp.arch as arch"). - Where("id in (?)", inventoryIDs).Find(&sysArchVersions).Error + Where("ih.id in (?)", inventoryIDs).Find(&sysArchVersions).Error if err != nil { return err } From 3014b95bf0c87f8d55c0bed77ff0cb689cf2adbe Mon Sep 17 00:00:00 2001 From: Michael Mraka Date: Tue, 18 Jun 2024 13:13:23 +0200 Subject: [PATCH 3/7] RHINENG-10240: move check outside of the loop --- manager/controllers/template_systems_update.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/manager/controllers/template_systems_update.go b/manager/controllers/template_systems_update.go index b83e31169..e9c622402 100644 --- a/manager/controllers/template_systems_update.go +++ b/manager/controllers/template_systems_update.go @@ -143,12 +143,12 @@ func templateArchVersionMatch( for _, sys := range sysArchVersions { if sys.Version != template.Version && sys.Arch != template.Arch { - if err == nil { - err = fmt.Errorf("template arch: %s, version: %s", template.Arch, template.Version) - } systemErr := fmt.Errorf("system uuid: %s, arch: %s, version: %s", sys.InventoryID, sys.Arch, sys.Version) err = errors2.Join(err, systemErr) } } + if err != nil { + err = errors2.Join(fmt.Errorf("template arch: %s, version: %s", template.Arch, template.Version), err) + } return err } From d206f1d68f0392ffedeb46ca066f1c23de8bb4dd Mon Sep 17 00:00:00 2001 From: Michael Mraka Date: Tue, 18 Jun 2024 13:15:21 +0200 Subject: [PATCH 4/7] RHINENG-10240: both version and arch must match --- manager/controllers/template_systems_update.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/manager/controllers/template_systems_update.go b/manager/controllers/template_systems_update.go index e9c622402..7b06a6206 100644 --- a/manager/controllers/template_systems_update.go +++ b/manager/controllers/template_systems_update.go @@ -142,7 +142,7 @@ func templateArchVersionMatch( } for _, sys := range sysArchVersions { - if sys.Version != template.Version && sys.Arch != template.Arch { + if sys.Version != template.Version || sys.Arch != template.Arch { systemErr := fmt.Errorf("system uuid: %s, arch: %s, version: %s", sys.InventoryID, sys.Arch, sys.Version) err = errors2.Join(err, systemErr) } From 01ae2ebdee3ca5dcc76a6e1669c5651bba028268 Mon Sep 17 00:00:00 2001 From: Michael Mraka Date: Tue, 18 Jun 2024 13:25:58 +0200 Subject: [PATCH 5/7] RHINENG-10240: don't check version and arch when unassigning systems --- manager/controllers/template_systems_update.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/manager/controllers/template_systems_update.go b/manager/controllers/template_systems_update.go index 7b06a6206..f42838b66 100644 --- a/manager/controllers/template_systems_update.go +++ b/manager/controllers/template_systems_update.go @@ -128,6 +128,9 @@ func assignTemplateSystems(c *gin.Context, db *gorm.DB, accountID int, template func templateArchVersionMatch( db *gorm.DB, inventoryIDs []string, template *models.Template, acc int, groups map[string]string, ) error { + if template == nil { + return nil + } var sysArchVersions = []struct { InventoryID string Arch string From 87eb151b68197367b23063a51800f2dcf8ab2245 Mon Sep 17 00:00:00 2001 From: Michael Mraka Date: Tue, 18 Jun 2024 13:41:00 +0200 Subject: [PATCH 6/7] RHINENG-10240: add arch into test data --- dev/test_data.sql | 46 +++++++++++++++++++++++----------------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/dev/test_data.sql b/dev/test_data.sql index f525dcc05..39bf59068 100644 --- a/dev/test_data.sql +++ b/dev/test_data.sql @@ -25,29 +25,29 @@ INSERT INTO baseline (id, rh_account_id, name, config, description) VALUES (3, 1, 'baseline_1-3', '{"to_time": "2000-01-01T00:00:00+00:00"}', NULL), (4, 3, 'baseline_3-4', '{"to_time": "2000-01-01T00:00:00+00:00"}', NULL); -INSERT INTO template (id, rh_account_id, uuid, environment_id, name, description, config, creator) VALUES -(1, 1, '99900000-0000-0000-0000-000000000001', '99900000000000000000000000000001', 'temp1-1', 'desc1', '{"to_time": "2010-09-22T00:00:00+00:00"}', 'user1'), -(2, 1, '99900000-0000-0000-0000-000000000002', '99900000000000000000000000000002', 'temp2-1', 'desc2', '{"to_time": "2021-01-01T00:00:00+00:00"}', 'user2'), -(3, 1, '99900000-0000-0000-0000-000000000003', '99900000000000000000000000000003', 'temp3-1', NULL, '{"to_time": "2000-01-01T00:00:00+00:00"}', 'user3'), -(4, 3, '99900000-0000-0000-0000-000000000004', '99900000000000000000000000000004', 'temp4-3', 'desc4', '{"to_time": "2000-01-01T00:00:00+00:00"}', 'user4'); - -INSERT INTO system_platform (id, inventory_id, display_name, rh_account_id, reporter_id, vmaas_json, json_checksum, last_evaluation, last_upload, packages_installed, packages_installable, packages_applicable, third_party, baseline_id, baseline_uptodate, template_id) VALUES -(1, '00000000-0000-0000-0000-000000000001','00000000-0000-0000-0000-000000000001', 1, 1, '{ "package_list": [ "kernel-2.6.32-696.20.1.el6.x86_64" ], "repository_list": [ "rhel-6-server-rpms" ] }', '1', '2018-09-22 12:00:00-04', '2020-09-22 12:00:00-04',0,0,0, true, 1, true, 1), -(2, '00000000-0000-0000-0000-000000000002','00000000-0000-0000-0000-000000000002', 1, 1, '{ "package_list": [ "kernel-2.6.32-696.20.1.el6.x86_64" ], "repository_list": [ "rhel-6-server-rpms" ] }', '1', '2018-09-22 12:00:00-04', '2018-09-22 12:00:00-04',0,0,0, false, 1, true, 1), -(3, '00000000-0000-0000-0000-000000000003','00000000-0000-0000-0000-000000000003', 1, 1, '{ "package_list": [ "kernel-2.6.32-696.20.1.el6.x86_64" ], "repository_list": [ "rhel-6-server-rpms" ] }', '1', '2018-09-22 12:00:00-04', '2018-09-18 12:00:00-04',0,0,0, false, 2, false, 2), -(4, '00000000-0000-0000-0000-000000000004','00000000-0000-0000-0000-000000000004', 1, 1, '{ "package_list": [ "kernel-2.6.32-696.20.1.el6.x86_64" ], "repository_list": [ "rhel-6-server-rpms" ] }', '1', '2018-09-22 12:00:00-04', '2018-09-18 12:00:00-04',0,0,0, false, NULL, NULL, NULL), -(5, '00000000-0000-0000-0000-000000000005','00000000-0000-0000-0000-000000000005', 1, 1, '{ "package_list": [ "kernel-2.6.32-696.20.1.el6.x86_64" ], "repository_list": [ "rhel-6-server-rpms" ] }', '1', '2018-09-22 12:00:00-04', '2018-09-18 12:00:00-04',0,0,0, false, NULL, NULL, NULL), -(6, '00000000-0000-0000-0000-000000000006','00000000-0000-0000-0000-000000000006', 1, 1, '{ "package_list": [ "kernel-2.6.32-696.20.1.el6.x86_64" ], "repository_list": [ "rhel-6-server-rpms" ] }', '1', '2018-09-22 12:00:00-04', '2018-08-26 12:00:00-04',0,0,0, false, NULL, NULL, NULL); - -INSERT INTO system_platform (id, inventory_id, display_name, rh_account_id, vmaas_json, json_checksum, last_updated, unchanged_since, last_upload, packages_installed, packages_installable, packages_applicable) VALUES -(7, '00000000-0000-0000-0000-000000000007','00000000-0000-0000-0000-000000000007', 1, '{ "package_list": [ "kernel-2.6.32-696.20.1.el6.x86_64" ], "repository_list": [ "rhel-6-server-rpms" ] }', '1', '2018-10-04 14:13:12-04', '2018-09-22 12:00:00-04', '2018-08-26 12:00:00-04',0,0,0); - -INSERT INTO system_platform (id, inventory_id, display_name, rh_account_id, vmaas_json, json_checksum, last_evaluation, last_upload, packages_installed, packages_installable, packages_applicable) VALUES -(8, '00000000-0000-0000-0000-000000000008','00000000-0000-0000-0000-000000000008', 1, '{ "package_list": [ "kernel-2.6.32-696.20.1.el6.x86_64" ], "repository_list": [ "rhel-6-server-rpms" ] }', '1', '2018-09-22 12:00:00-04', '2018-08-26 12:00:00-04',0,0,0), -(9, '00000000-0000-0000-0000-000000000009','00000000-0000-0000-0000-000000000009', 2, '{ "package_list": [ "kernel-2.6.32-696.20.1.el6.x86_64" ], "repository_list": [ "rhel-6-server-rpms" ] }', '1', '2018-09-22 12:00:00-04', '2018-01-22 12:00:00-04',0,0,0), -(10, '00000000-0000-0000-0000-000000000010','00000000-0000-0000-0000-000000000010', 2, '{ "package_list": [ "kernel-2.6.32-696.20.1.el6.x86_64" ], "repository_list": [ "rhel-6-server-rpms" ] }', '1', '2018-09-22 12:00:00-04', '2018-01-22 12:00:00-04',0,0,0), -(11, '00000000-0000-0000-0000-000000000011','00000000-0000-0000-0000-000000000011', 2, '{ "package_list": [ "kernel-2.6.32-696.20.1.el6.x86_64" ], "repository_list": [ "rhel-6-server-rpms" ] }', '1', '2018-09-22 12:00:00-04', '2018-01-22 12:00:00-04',0,0,0), -(12, '00000000-0000-0000-0000-000000000012','00000000-0000-0000-0000-000000000012', 3, '{ "package_list": [ "kernel-2.6.32-696.20.1.el6.x86_64" ], "repository_list": [ "rhel-6-server-rpms" ] }', '1', '2018-09-22 12:00:00-04', '2018-01-22 12:00:00-04',2,2,2); +INSERT INTO template (id, rh_account_id, uuid, environment_id, name, description, config, arch, version, creator) VALUES +(1, 1, '99900000-0000-0000-0000-000000000001', '99900000000000000000000000000001', 'temp1-1', 'desc1', '{"to_time": "2010-09-22T00:00:00+00:00"}', 'x86_64', '8', 'user1'), +(2, 1, '99900000-0000-0000-0000-000000000002', '99900000000000000000000000000002', 'temp2-1', 'desc2', '{"to_time": "2021-01-01T00:00:00+00:00"}', 'x86_64', '8', 'user2'), +(3, 1, '99900000-0000-0000-0000-000000000003', '99900000000000000000000000000003', 'temp3-1', NULL, '{"to_time": "2000-01-01T00:00:00+00:00"}', 'x86_64', '8', 'user3'), +(4, 3, '99900000-0000-0000-0000-000000000004', '99900000000000000000000000000004', 'temp4-3', 'desc4', '{"to_time": "2000-01-01T00:00:00+00:00"}', 'x86_64', '8', 'user4'); + +INSERT INTO system_platform (id, inventory_id, display_name, rh_account_id, reporter_id, vmaas_json, json_checksum, last_evaluation, last_upload, packages_installed, packages_installable, packages_applicable, third_party, baseline_id, baseline_uptodate, template_id, arch) VALUES +(1, '00000000-0000-0000-0000-000000000001','00000000-0000-0000-0000-000000000001', 1, 1, '{ "package_list": [ "kernel-2.6.32-696.20.1.el6.x86_64" ], "repository_list": [ "rhel-6-server-rpms" ] }', '1', '2018-09-22 12:00:00-04', '2020-09-22 12:00:00-04',0,0,0, true, 1, true, 1, 'x86_64'), +(2, '00000000-0000-0000-0000-000000000002','00000000-0000-0000-0000-000000000002', 1, 1, '{ "package_list": [ "kernel-2.6.32-696.20.1.el6.x86_64" ], "repository_list": [ "rhel-6-server-rpms" ] }', '1', '2018-09-22 12:00:00-04', '2018-09-22 12:00:00-04',0,0,0, false, 1, true, 1, 'x86_64'), +(3, '00000000-0000-0000-0000-000000000003','00000000-0000-0000-0000-000000000003', 1, 1, '{ "package_list": [ "kernel-2.6.32-696.20.1.el6.x86_64" ], "repository_list": [ "rhel-6-server-rpms" ] }', '1', '2018-09-22 12:00:00-04', '2018-09-18 12:00:00-04',0,0,0, false, 2, false, 2, 'x86_64'), +(4, '00000000-0000-0000-0000-000000000004','00000000-0000-0000-0000-000000000004', 1, 1, '{ "package_list": [ "kernel-2.6.32-696.20.1.el6.x86_64" ], "repository_list": [ "rhel-6-server-rpms" ] }', '1', '2018-09-22 12:00:00-04', '2018-09-18 12:00:00-04',0,0,0, false, NULL, NULL, NULL, 'x86_64'), +(5, '00000000-0000-0000-0000-000000000005','00000000-0000-0000-0000-000000000005', 1, 1, '{ "package_list": [ "kernel-2.6.32-696.20.1.el6.x86_64" ], "repository_list": [ "rhel-6-server-rpms" ] }', '1', '2018-09-22 12:00:00-04', '2018-09-18 12:00:00-04',0,0,0, false, NULL, NULL, NULL, 'x86_64'), +(6, '00000000-0000-0000-0000-000000000006','00000000-0000-0000-0000-000000000006', 1, 1, '{ "package_list": [ "kernel-2.6.32-696.20.1.el6.x86_64" ], "repository_list": [ "rhel-6-server-rpms" ] }', '1', '2018-09-22 12:00:00-04', '2018-08-26 12:00:00-04',0,0,0, false, NULL, NULL, NULL, 'x86_64'); + +INSERT INTO system_platform (id, inventory_id, display_name, rh_account_id, vmaas_json, json_checksum, last_updated, unchanged_since, last_upload, packages_installed, packages_installable, packages_applicable, arch) VALUES +(7, '00000000-0000-0000-0000-000000000007','00000000-0000-0000-0000-000000000007', 1, '{ "package_list": [ "kernel-2.6.32-696.20.1.el6.x86_64" ], "repository_list": [ "rhel-6-server-rpms" ] }', '1', '2018-10-04 14:13:12-04', '2018-09-22 12:00:00-04', '2018-08-26 12:00:00-04',0,0,0, 'x86_64'); + +INSERT INTO system_platform (id, inventory_id, display_name, rh_account_id, vmaas_json, json_checksum, last_evaluation, last_upload, packages_installed, packages_installable, packages_applicable, arch) VALUES +(8, '00000000-0000-0000-0000-000000000008','00000000-0000-0000-0000-000000000008', 1, '{ "package_list": [ "kernel-2.6.32-696.20.1.el6.x86_64" ], "repository_list": [ "rhel-6-server-rpms" ] }', '1', '2018-09-22 12:00:00-04', '2018-08-26 12:00:00-04',0,0,0, 'x86_64'), +(9, '00000000-0000-0000-0000-000000000009','00000000-0000-0000-0000-000000000009', 2, '{ "package_list": [ "kernel-2.6.32-696.20.1.el6.x86_64" ], "repository_list": [ "rhel-6-server-rpms" ] }', '1', '2018-09-22 12:00:00-04', '2018-01-22 12:00:00-04',0,0,0, 'x86_64'), +(10, '00000000-0000-0000-0000-000000000010','00000000-0000-0000-0000-000000000010', 2, '{ "package_list": [ "kernel-2.6.32-696.20.1.el6.x86_64" ], "repository_list": [ "rhel-6-server-rpms" ] }', '1', '2018-09-22 12:00:00-04', '2018-01-22 12:00:00-04',0,0,0, 'x86_64'), +(11, '00000000-0000-0000-0000-000000000011','00000000-0000-0000-0000-000000000011', 2, '{ "package_list": [ "kernel-2.6.32-696.20.1.el6.x86_64" ], "repository_list": [ "rhel-6-server-rpms" ] }', '1', '2018-09-22 12:00:00-04', '2018-01-22 12:00:00-04',0,0,0, 'x86_64'), +(12, '00000000-0000-0000-0000-000000000012','00000000-0000-0000-0000-000000000012', 3, '{ "package_list": [ "kernel-2.6.32-696.20.1.el6.x86_64" ], "repository_list": [ "rhel-6-server-rpms" ] }', '1', '2018-09-22 12:00:00-04', '2018-01-22 12:00:00-04',2,2,2, 'x86_64'); INSERT INTO system_platform (id, inventory_id, display_name, rh_account_id, vmaas_json, json_checksum, last_evaluation, last_upload, packages_installed, packages_installable, packages_applicable, yum_updates) VALUES (13, '00000000-0000-0000-0000-000000000013','00000000-0000-0000-0000-000000000013', 3, '{ "package_list": [ "kernel-2.6.32-696.20.1.el6.x86_64" ], "repository_list": [ "rhel-6-server-rpms" ] }', '1', '2018-09-22 12:00:00-04', '2018-01-22 12:00:00-04', 1,0,0, NULL), From 51eb4f5b54c3de0ecf05395d66cf7ed16b9091f4 Mon Sep 17 00:00:00 2001 From: Michael Mraka Date: Tue, 18 Jun 2024 15:16:47 +0200 Subject: [PATCH 7/7] RHINENG-10240: add test for invalid system in the template --- .../template_systems_update_test.go | 29 +++++++++++++++---- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/manager/controllers/template_systems_update_test.go b/manager/controllers/template_systems_update_test.go index 5ae4bd611..132ba0699 100644 --- a/manager/controllers/template_systems_update_test.go +++ b/manager/controllers/template_systems_update_test.go @@ -17,7 +17,6 @@ var templatePath = "/:template_id/systems" var templateAccount = 1 var templateSystems = []string{ "00000000-0000-0000-0000-000000000004", - "00000000-0000-0000-0000-000000000006", "00000000-0000-0000-0000-000000000007", "00000000-0000-0000-0000-000000000008", } @@ -27,7 +26,6 @@ func TestUpdateTemplateSystems(t *testing.T) { data := `{ "systems": [ "00000000-0000-0000-0000-000000000004", - "00000000-0000-0000-0000-000000000006", "00000000-0000-0000-0000-000000000008" ] }` @@ -43,6 +41,29 @@ func TestUpdateTemplateSystems(t *testing.T) { database.DeleteTemplate(t, templateAccount, templateUUID) } +func TestUpdateTemplateInvalidVersion(t *testing.T) { + core.SetupTest(t) + data := `{ + "systems": [ + "00000000-0000-0000-0000-000000000006" + ] + }` + + database.CreateTemplate(t, templateAccount, templateUUID, []string{ + "00000000-0000-0000-0000-000000000007", + }) + w := CreateRequestRouterWithParams("PUT", templatePath, templateUUID, "", bytes.NewBufferString(data), "", + TemplateSystemsUpdateHandler, templateAccount) + + var errResp utils.ErrorResponse + CheckResponse(t, w, http.StatusBadRequest, &errResp) + // 00000000-0000-0000-0000-000000000006 is RHEL 7 + assert.Equal(t, "Incompatible template and system version or architecture: template arch: x86_64, version: 8\n"+ + "system uuid: 00000000-0000-0000-0000-000000000006, arch: x86_64, version: 7", + errResp.Error) + database.DeleteTemplate(t, templateAccount, templateUUID) +} + func TestUpdateTemplateInvalidSystem(t *testing.T) { core.SetupTest(t) @@ -102,8 +123,7 @@ func TestReassignTemplateSystems2(t *testing.T) { data := `{ "systems": [ "00000000-0000-0000-0000-000000000004", - "00000000-0000-0000-0000-000000000005", - "00000000-0000-0000-0000-000000000006" + "00000000-0000-0000-0000-000000000005" ] }` w := CreateRequestRouterWithParams("PUT", templatePath, template2, "", bytes.NewBufferString(data), "", @@ -114,7 +134,6 @@ func TestReassignTemplateSystems2(t *testing.T) { database.CheckTemplateSystems(t, templateAccount, template2, []string{ "00000000-0000-0000-0000-000000000004", "00000000-0000-0000-0000-000000000005", - "00000000-0000-0000-0000-000000000006", }) database.CheckTemplateSystems(t, templateAccount, templateUUID, []string{ "00000000-0000-0000-0000-000000000007",