github.com/crowdsecurity/crowdsec@v1.6.1/pkg/cwhub/iteminstall_test.go (about) 1 package cwhub 2 3 import ( 4 "os" 5 "testing" 6 7 "github.com/stretchr/testify/assert" 8 "github.com/stretchr/testify/require" 9 ) 10 11 func testInstall(hub *Hub, t *testing.T, item *Item) { 12 // Install the parser 13 _, err := item.downloadLatest(false, false) 14 require.NoError(t, err, "failed to download %s", item.Name) 15 16 err = hub.localSync() 17 require.NoError(t, err, "failed to run localSync") 18 19 assert.True(t, item.State.UpToDate, "%s should be up-to-date", item.Name) 20 assert.False(t, item.State.Installed, "%s should not be installed", item.Name) 21 assert.False(t, item.State.Tainted, "%s should not be tainted", item.Name) 22 23 err = item.enable() 24 require.NoError(t, err, "failed to enable %s", item.Name) 25 26 err = hub.localSync() 27 require.NoError(t, err, "failed to run localSync") 28 29 assert.True(t, item.State.Installed, "%s should be installed", item.Name) 30 } 31 32 func testTaint(hub *Hub, t *testing.T, item *Item) { 33 assert.False(t, item.State.Tainted, "%s should not be tainted", item.Name) 34 35 // truncate the file 36 f, err := os.Create(item.State.LocalPath) 37 require.NoError(t, err) 38 f.Close() 39 40 // Local sync and check status 41 err = hub.localSync() 42 require.NoError(t, err, "failed to run localSync") 43 44 assert.True(t, item.State.Tainted, "%s should be tainted", item.Name) 45 } 46 47 func testUpdate(hub *Hub, t *testing.T, item *Item) { 48 assert.False(t, item.State.UpToDate, "%s should not be up-to-date", item.Name) 49 50 // Update it + check status 51 _, err := item.downloadLatest(true, true) 52 require.NoError(t, err, "failed to update %s", item.Name) 53 54 // Local sync and check status 55 err = hub.localSync() 56 require.NoError(t, err, "failed to run localSync") 57 58 assert.True(t, item.State.UpToDate, "%s should be up-to-date", item.Name) 59 assert.False(t, item.State.Tainted, "%s should not be tainted anymore", item.Name) 60 } 61 62 func testDisable(hub *Hub, t *testing.T, item *Item) { 63 assert.True(t, item.State.Installed, "%s should be installed", item.Name) 64 65 // Remove 66 _, err := item.disable(false, false) 67 require.NoError(t, err, "failed to disable %s", item.Name) 68 69 // Local sync and check status 70 err = hub.localSync() 71 require.NoError(t, err, "failed to run localSync") 72 require.Empty(t, hub.Warnings) 73 74 assert.False(t, item.State.Tainted, "%s should not be tainted anymore", item.Name) 75 assert.False(t, item.State.Installed, "%s should not be installed anymore", item.Name) 76 assert.True(t, item.State.Downloaded, "%s should still be downloaded", item.Name) 77 78 // Purge 79 _, err = item.disable(true, false) 80 require.NoError(t, err, "failed to purge %s", item.Name) 81 82 // Local sync and check status 83 err = hub.localSync() 84 require.NoError(t, err, "failed to run localSync") 85 require.Empty(t, hub.Warnings) 86 87 assert.False(t, item.State.Installed, "%s should not be installed anymore", item.Name) 88 assert.False(t, item.State.Downloaded, "%s should not be downloaded", item.Name) 89 } 90 91 func TestInstallParser(t *testing.T) { 92 /* 93 - install a random parser 94 - check its status 95 - taint it 96 - check its status 97 - force update it 98 - check its status 99 - remove it 100 */ 101 hub := envSetup(t) 102 103 // map iteration is random by itself 104 for _, it := range hub.GetItemMap(PARSERS) { 105 testInstall(hub, t, it) 106 testTaint(hub, t, it) 107 testUpdate(hub, t, it) 108 testDisable(hub, t, it) 109 110 break 111 } 112 } 113 114 func TestInstallCollection(t *testing.T) { 115 /* 116 - install a random parser 117 - check its status 118 - taint it 119 - check its status 120 - force update it 121 - check its status 122 - remove it 123 */ 124 hub := envSetup(t) 125 126 // map iteration is random by itself 127 for _, it := range hub.GetItemMap(COLLECTIONS) { 128 testInstall(hub, t, it) 129 testTaint(hub, t, it) 130 testUpdate(hub, t, it) 131 testDisable(hub, t, it) 132 133 break 134 } 135 }