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), diff --git a/manager/controllers/template_systems_update.go b/manager/controllers/template_systems_update.go index 0af223e21..f42838b66 100644 --- a/manager/controllers/template_systems_update.go +++ b/manager/controllers/template_systems_update.go @@ -128,24 +128,30 @@ 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 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("ih.id in (?)", inventoryIDs).Find(&sysArchVersions).Error + if err != nil { + return err + } 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) - } + 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) } } + if err != nil { + err = errors2.Join(fmt.Errorf("template arch: %s, version: %s", template.Arch, template.Version), err) + } return err } 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",