Skip to content

Commit f5dd08b

Browse files
committed
RHINENG-22325: inventory views tests
1 parent f63f3e0 commit f5dd08b

2 files changed

Lines changed: 96 additions & 1 deletion

File tree

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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+
var systems []models.SystemPlatform
27+
assert.NoError(t, tx.Where("rh_account_id = ? AND id in (1,3)", rhAccountID).
28+
Order("id").Find(&systems).Error)
29+
assert.Equal(t, 2, len(systems))
30+
31+
event, err := MakeInventoryViewsEvent(tx, *rhAccount.OrgID, systems)
32+
assert.NoError(t, err)
33+
assert.Equal(t, *rhAccount.OrgID, event.OrgID)
34+
assert.NotEmpty(t, event.Timestamp)
35+
_, err = time.Parse(time.RFC3339, event.Timestamp)
36+
assert.NoError(t, err)
37+
38+
// Verify hosts
39+
assert.Equal(t, 2, len(event.Hosts))
40+
41+
assert.Equal(t, InventoryViewsHost{
42+
ID: "00000000-0000-0000-0000-000000000001",
43+
Data: InventoryViewsHostData{2, 3, 3, 0, 2, 2, 1, 0, 0, 0, 0,
44+
utils.PtrString("temp1-1"), utils.PtrString("99900000-0000-0000-0000-000000000001")},
45+
}, event.Hosts[0])
46+
47+
assert.Equal(t, InventoryViewsHost{
48+
ID: "00000000-0000-0000-0000-000000000003",
49+
Data: InventoryViewsHostData{0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0,
50+
utils.PtrString("temp2-1"), utils.PtrString("99900000-0000-0000-0000-000000000002")},
51+
}, event.Hosts[1])
52+
}
53+
54+
func TestMakeInventoryViewsEventEmpty(t *testing.T) {
55+
utils.SkipWithoutDB(t)
56+
core.SetupTestEnvironment()
57+
58+
tx := database.DB.Begin()
59+
defer tx.Rollback()
60+
61+
event, err := MakeInventoryViewsEvent(tx, "test-org", []models.SystemPlatform{})
62+
assert.NoError(t, err)
63+
assert.Equal(t, "test-org", event.OrgID)
64+
assert.Equal(t, 0, len(event.Hosts))
65+
assert.NotEmpty(t, event.Timestamp)
66+
}
67+
68+
func TestMakeInventoryViewsEventNoTemplate(t *testing.T) {
69+
utils.SkipWithoutDB(t)
70+
core.SetupTestEnvironment()
71+
72+
tx := database.DB.Begin()
73+
defer tx.Rollback()
74+
75+
var rhAccount models.RhAccount
76+
assert.NoError(t, tx.Where("id = ?", rhAccountID).First(&rhAccount).Error)
77+
assert.NotEmpty(t, *rhAccount.OrgID)
78+
orgID := *rhAccount.OrgID
79+
80+
var systems []models.SystemPlatform
81+
assert.NoError(t, tx.Where("rh_account_id = ? AND id in (4)", rhAccountID).
82+
Order("id").Find(&systems).Error)
83+
assert.Equal(t, 1, len(systems))
84+
85+
// Should not error, but template fields should be nil
86+
event, err := MakeInventoryViewsEvent(tx, orgID, systems)
87+
assert.NoError(t, err)
88+
assert.Equal(t, 1, len(event.Hosts))
89+
90+
host := event.Hosts[0]
91+
assert.Equal(t, "00000000-0000-0000-0000-000000000004", host.ID)
92+
// Template fields should be nil when template is not found
93+
assert.Nil(t, host.Data.TemplateName)
94+
assert.Nil(t, host.Data.TemplateUUID)
95+
}

conf/test.env

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ DB_PASSWD=passwd
1010
LIMIT_PAGE_SIZE=false
1111

1212
# don't put "" or '' around the text otherwise they'll be included into content
13-
POD_CONFIG=label=upload;vmaas_call_max_retries=100;template_change_eval=false;update_users;update_db_config;use_testing_db
13+
POD_CONFIG=label=upload;vmaas_call_max_retries=100;template_change_eval=false;update_users;update_db_config;use_testing_db;inventory_views
1414

1515
KESSEL_URL=platform:9005
1616
KESSEL_INSECURE=true

0 commit comments

Comments
 (0)