fleet/server/datastore/datastore_software.go
Tomas Touceda 18fa2f6a02
Issue 1009 calculate diff software (#1305)
* First approach to diff

* Refactor things for better readability and testing

* Remove draft comment for algorithm

* Format things a bit better

* Remove unused and simplify code a bit

* Refactor for readability and testing

* Add changes file

* Implement new approach based on review comments

* Make sure to only delete from the current host

* Add single uninstall test and fix code

* Improve code based on review
2021-07-08 13:57:43 -03:00

96 lines
2.8 KiB
Go

package datastore
import (
"testing"
"time"
"github.com/fleetdm/fleet/v4/server/fleet"
"github.com/fleetdm/fleet/v4/server/test"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func testSaveHostSoftware(t *testing.T, ds fleet.Datastore) {
host1 := test.NewHost(t, ds, "host1", "", "host1key", "host1uuid", time.Now())
host2 := test.NewHost(t, ds, "host2", "", "host2key", "host2uuid", time.Now())
soft1 := fleet.HostSoftware{
Modified: true,
Software: []fleet.Software{
{Name: "foo", Version: "0.0.1", Source: "chrome_extensions"},
{Name: "foo", Version: "0.0.3", Source: "chrome_extensions"},
},
}
host1.HostSoftware = soft1
soft2 := fleet.HostSoftware{
Modified: true,
Software: []fleet.Software{
{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)
soft1 = fleet.HostSoftware{
Modified: true,
Software: []fleet.Software{
{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
soft2 = fleet.HostSoftware{
Modified: true,
Software: []fleet.Software{},
}
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)
soft1 = fleet.HostSoftware{
Modified: true,
Software: []fleet.Software{
{Name: "foo", Version: "0.0.3", Source: "chrome_extensions"},
{Name: "towel", Version: "42.0.0", Source: "apps"},
},
}
host1.HostSoftware = soft1
err = ds.SaveHostSoftware(host1)
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)
}