2021-04-26 15:44:22 +00:00
|
|
|
package datastore
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
"github.com/fleetdm/fleet/server/fleet"
|
2021-04-26 15:44:22 +00:00
|
|
|
"github.com/fleetdm/fleet/server/test"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
func testSaveHostSoftware(t *testing.T, ds fleet.Datastore) {
|
2021-04-26 15:44:22 +00:00
|
|
|
host1 := test.NewHost(t, ds, "host1", "", "host1key", "host1uuid", time.Now())
|
|
|
|
host2 := test.NewHost(t, ds, "host2", "", "host2key", "host2uuid", time.Now())
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
soft1 := fleet.HostSoftware{
|
2021-04-26 15:44:22 +00:00
|
|
|
Modified: true,
|
2021-06-06 22:07:29 +00:00
|
|
|
Software: []fleet.Software{
|
2021-04-26 15:44:22 +00:00
|
|
|
{Name: "foo", Version: "0.0.1", Source: "chrome_extensions"},
|
|
|
|
{Name: "foo", Version: "0.0.3", Source: "chrome_extensions"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
host1.HostSoftware = soft1
|
2021-06-06 22:07:29 +00:00
|
|
|
soft2 := fleet.HostSoftware{
|
2021-04-26 15:44:22 +00:00
|
|
|
Modified: true,
|
2021-06-06 22:07:29 +00:00
|
|
|
Software: []fleet.Software{
|
2021-04-26 15:44:22 +00:00
|
|
|
{Name: "foo", Version: "0.0.2", Source: "chrome_extensions"},
|
|
|
|
{Name: "foo", Version: "0.0.3", Source: "chrome_extensions"},
|
|
|
|
{Name: "bar", Version: "0.0.3", Source: "deb_packages"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
host2.HostSoftware = soft2
|
|
|
|
|
|
|
|
err := ds.SaveHostSoftware(host1)
|
|
|
|
require.NoError(t, err)
|
|
|
|
err = ds.SaveHostSoftware(host2)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = ds.LoadHostSoftware(host1)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.False(t, host1.HostSoftware.Modified)
|
|
|
|
test.ElementsMatchSkipID(t, soft1.Software, host1.HostSoftware.Software)
|
|
|
|
|
|
|
|
err = ds.LoadHostSoftware(host2)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.False(t, host2.HostSoftware.Modified)
|
|
|
|
test.ElementsMatchSkipID(t, soft2.Software, host2.HostSoftware.Software)
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
soft1 = fleet.HostSoftware{
|
2021-04-26 15:44:22 +00:00
|
|
|
Modified: true,
|
2021-06-06 22:07:29 +00:00
|
|
|
Software: []fleet.Software{
|
2021-04-26 15:44:22 +00:00
|
|
|
{Name: "foo", Version: "0.0.1", Source: "chrome_extensions"},
|
|
|
|
{Name: "foo", Version: "0.0.3", Source: "chrome_extensions"},
|
|
|
|
{Name: "towel", Version: "42.0.0", Source: "apps"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
host1.HostSoftware = soft1
|
2021-06-06 22:07:29 +00:00
|
|
|
soft2 = fleet.HostSoftware{
|
2021-04-26 15:44:22 +00:00
|
|
|
Modified: true,
|
2021-06-06 22:07:29 +00:00
|
|
|
Software: []fleet.Software{},
|
2021-04-26 15:44:22 +00:00
|
|
|
}
|
|
|
|
host2.HostSoftware = soft2
|
|
|
|
|
|
|
|
err = ds.SaveHostSoftware(host1)
|
|
|
|
require.NoError(t, err)
|
|
|
|
err = ds.SaveHostSoftware(host2)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = ds.LoadHostSoftware(host1)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.False(t, host1.HostSoftware.Modified)
|
|
|
|
test.ElementsMatchSkipID(t, soft1.Software, host1.HostSoftware.Software)
|
|
|
|
|
|
|
|
err = ds.LoadHostSoftware(host2)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.False(t, host2.HostSoftware.Modified)
|
|
|
|
test.ElementsMatchSkipID(t, soft2.Software, host2.HostSoftware.Software)
|
|
|
|
}
|