|
| 1 | +package inventory_views |
| 2 | + |
| 3 | +import ( |
| 4 | + "app/base/core" |
| 5 | + "app/base/database" |
| 6 | + "app/base/models" |
| 7 | + "app/base/utils" |
| 8 | + "testing" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | +) |
| 13 | + |
| 14 | +const rhAccountID = 1 |
| 15 | + |
| 16 | +func TestMakeInventoryViewsEvent(t *testing.T) { |
| 17 | + utils.SkipWithoutDB(t) |
| 18 | + core.SetupTestEnvironment() |
| 19 | + tx := database.DB.Begin() |
| 20 | + defer tx.Rollback() |
| 21 | + |
| 22 | + var rhAccount models.RhAccount |
| 23 | + assert.NoError(t, tx.Where("id = ?", rhAccountID).First(&rhAccount).Error) |
| 24 | + assert.NotEmpty(t, *rhAccount.OrgID) |
| 25 | + |
| 26 | + orgID := *rhAccount.OrgID |
| 27 | + |
| 28 | + var systems []models.SystemPlatform |
| 29 | + assert.NoError(t, tx.Where("rh_account_id = ? AND id in (1,3)", rhAccountID). |
| 30 | + Order("id").Find(&systems).Error) |
| 31 | + assert.Equal(t, 2, len(systems)) |
| 32 | + |
| 33 | + // Test MakeInventoryViewsEvent |
| 34 | + event, err := MakeInventoryViewsEvent(tx, orgID, systems) |
| 35 | + assert.NoError(t, err) |
| 36 | + assert.Equal(t, orgID, event.OrgID) |
| 37 | + assert.NotEmpty(t, event.Timestamp) |
| 38 | + |
| 39 | + // Verify timestamp is valid RFC3339 format |
| 40 | + _, err = time.Parse(time.RFC3339, event.Timestamp) |
| 41 | + assert.NoError(t, err) |
| 42 | + |
| 43 | + // Verify hosts |
| 44 | + assert.Equal(t, 2, len(event.Hosts)) |
| 45 | + |
| 46 | + // Check first system (with template) |
| 47 | + host1 := event.Hosts[0] |
| 48 | + assert.Equal(t, "00000000-0000-0000-0000-000000000001", host1.ID) |
| 49 | + assert.Equal(t, 2, host1.Data.ApplicableRhsaCount) |
| 50 | + assert.Equal(t, 3, host1.Data.ApplicableRhbaCount) |
| 51 | + assert.Equal(t, 3, host1.Data.ApplicableRheaCount) |
| 52 | + assert.Equal(t, 0, host1.Data.ApplicableOtherCount) |
| 53 | + assert.Equal(t, 2, host1.Data.InstallableRhsaCount) |
| 54 | + assert.Equal(t, 2, host1.Data.InstallableRhbaCount) |
| 55 | + assert.Equal(t, 1, host1.Data.InstallableRheaCount) |
| 56 | + assert.Equal(t, 0, host1.Data.InstallableOtherCount) |
| 57 | + assert.Equal(t, 0, host1.Data.PackagesInstalled) |
| 58 | + assert.Equal(t, 0, host1.Data.PackagesInstallable) |
| 59 | + assert.Equal(t, 0, host1.Data.PackagesApplicable) |
| 60 | + assert.Equal(t, "temp1-1", *host1.Data.TemplateName) |
| 61 | + assert.Equal(t, "99900000-0000-0000-0000-000000000001", *host1.Data.TemplateUUID) |
| 62 | + |
| 63 | + host2 := event.Hosts[1] |
| 64 | + assert.Equal(t, "00000000-0000-0000-0000-000000000003", host2.ID) |
| 65 | + assert.Equal(t, 0, host2.Data.ApplicableRhsaCount) |
| 66 | + assert.Equal(t, 0, host2.Data.ApplicableRhbaCount) |
| 67 | + assert.Equal(t, 1, host2.Data.ApplicableRheaCount) |
| 68 | + assert.Equal(t, 0, host2.Data.ApplicableOtherCount) // 1 - 1 - 0 - 0 |
| 69 | + assert.Equal(t, 0, host2.Data.InstallableRhsaCount) |
| 70 | + assert.Equal(t, 0, host2.Data.InstallableRhbaCount) |
| 71 | + assert.Equal(t, 1, host2.Data.InstallableRheaCount) |
| 72 | + assert.Equal(t, 0, host2.Data.InstallableOtherCount) // 1 - 1 - 0 - 0 |
| 73 | + assert.Equal(t, 0, host2.Data.PackagesInstalled) |
| 74 | + assert.Equal(t, 0, host2.Data.PackagesInstallable) |
| 75 | + assert.Equal(t, 0, host2.Data.PackagesApplicable) |
| 76 | + assert.Equal(t, "temp2-1", *host2.Data.TemplateName) |
| 77 | + assert.Equal(t, "99900000-0000-0000-0000-000000000002", *host2.Data.TemplateUUID) |
| 78 | + |
| 79 | +} |
| 80 | + |
| 81 | +func TestMakeInventoryViewsEventEmpty(t *testing.T) { |
| 82 | + utils.SkipWithoutDB(t) |
| 83 | + core.SetupTestEnvironment() |
| 84 | + |
| 85 | + tx := database.DB.Begin() |
| 86 | + defer tx.Rollback() |
| 87 | + |
| 88 | + event, err := MakeInventoryViewsEvent(tx, "test-org", []models.SystemPlatform{}) |
| 89 | + assert.NoError(t, err) |
| 90 | + assert.Equal(t, "test-org", event.OrgID) |
| 91 | + assert.Equal(t, 0, len(event.Hosts)) |
| 92 | + assert.NotEmpty(t, event.Timestamp) |
| 93 | +} |
| 94 | + |
| 95 | +func TestMakeInventoryViewsEventNoTemplate(t *testing.T) { |
| 96 | + utils.SkipWithoutDB(t) |
| 97 | + core.SetupTestEnvironment() |
| 98 | + |
| 99 | + tx := database.DB.Begin() |
| 100 | + defer tx.Rollback() |
| 101 | + |
| 102 | + var rhAccount models.RhAccount |
| 103 | + assert.NoError(t, tx.Where("id = ?", rhAccountID).First(&rhAccount).Error) |
| 104 | + assert.NotEmpty(t, *rhAccount.OrgID) |
| 105 | + orgID := *rhAccount.OrgID |
| 106 | + |
| 107 | + var systems []models.SystemPlatform |
| 108 | + assert.NoError(t, tx.Where("rh_account_id = ? AND id in (4)", rhAccountID). |
| 109 | + Order("id").Find(&systems).Error) |
| 110 | + assert.Equal(t, 1, len(systems)) |
| 111 | + |
| 112 | + // Should not error, but template fields should be nil |
| 113 | + event, err := MakeInventoryViewsEvent(tx, orgID, systems) |
| 114 | + assert.NoError(t, err) |
| 115 | + assert.Equal(t, 1, len(event.Hosts)) |
| 116 | + |
| 117 | + host := event.Hosts[0] |
| 118 | + assert.Equal(t, "00000000-0000-0000-0000-000000000004", host.ID) |
| 119 | + // Template fields should be nil when template is not found |
| 120 | + assert.Nil(t, host.Data.TemplateName) |
| 121 | + assert.Nil(t, host.Data.TemplateUUID) |
| 122 | +} |
0 commit comments