2021-09-09 05:34:12 +00:00
package main
import (
2021-12-30 01:32:55 +00:00
"fmt"
2021-09-09 05:34:12 +00:00
"os"
2021-12-30 01:32:55 +00:00
"path/filepath"
2022-07-11 12:49:13 +00:00
"runtime"
2021-09-09 05:34:12 +00:00
"testing"
2021-09-20 18:09:38 +00:00
2022-01-31 13:41:11 +00:00
"github.com/fleetdm/fleet/v4/orbit/pkg/packaging"
"github.com/fleetdm/fleet/v4/orbit/pkg/update"
2022-05-10 14:52:33 +00:00
"github.com/fleetdm/fleet/v4/pkg/nettest"
2021-09-20 18:09:38 +00:00
"github.com/stretchr/testify/require"
2021-09-09 05:34:12 +00:00
)
func TestPackage ( t * testing . T ) {
2022-05-10 14:52:33 +00:00
nettest . Run ( t )
2021-09-09 05:34:12 +00:00
2022-01-31 13:41:11 +00:00
updateOpt := update . DefaultOptions
updateOpt . RootDirectory = t . TempDir ( )
updatesData , err := packaging . InitializeUpdates ( updateOpt )
require . NoError ( t , err )
2021-09-09 05:34:12 +00:00
// --type is required
runAppCheckErr ( t , [ ] string { "package" , "deb" } , "Required flag \"type\" not set" )
// if you provide -fleet-url & --enroll-secret are required together
runAppCheckErr ( t , [ ] string { "package" , "--type=deb" , "--fleet-url=https://localhost:8080" } , "--enroll-secret and --fleet-url must be provided together" )
runAppCheckErr ( t , [ ] string { "package" , "--type=deb" , "--enroll-secret=foobar" } , "--enroll-secret and --fleet-url must be provided together" )
// --insecure and --fleet-certificate are mutually exclusive
runAppCheckErr ( t , [ ] string { "package" , "--type=deb" , "--insecure" , "--fleet-certificate=test123" } , "--insecure and --fleet-certificate may not be provided together" )
2021-12-30 01:32:55 +00:00
// Test invalid PEM file provided in --fleet-certificate.
certDir := t . TempDir ( )
fleetCertificate := filepath . Join ( certDir , "fleet.pem" )
2022-10-14 14:14:18 +00:00
err = os . WriteFile ( fleetCertificate , [ ] byte ( "undefined" ) , os . FileMode ( 0 o644 ) )
2021-12-30 01:32:55 +00:00
require . NoError ( t , err )
2023-04-27 11:44:39 +00:00
runAppCheckErr ( t , [ ] string { "package" , "--type=deb" , fmt . Sprintf ( "--fleet-certificate=%s" , fleetCertificate ) } , fmt . Sprintf ( "failed to read fleet server certificate %q: invalid PEM file" , fleetCertificate ) )
2021-12-30 01:32:55 +00:00
2022-07-11 12:49:13 +00:00
if runtime . GOOS != "linux" {
2022-10-14 14:14:18 +00:00
runAppCheckErr ( t , [ ] string { "package" , "--type=msi" , "--native-tooling" } , "native tooling is only available in Linux" )
2022-07-11 12:49:13 +00:00
}
2022-01-31 13:41:11 +00:00
t . Run ( "deb" , func ( t * testing . T ) {
2022-05-10 14:52:33 +00:00
runAppForTest ( t , [ ] string { "package" , "--type=deb" , "--insecure" , "--disable-open-folder" } )
2022-01-31 13:41:11 +00:00
info , err := os . Stat ( fmt . Sprintf ( "fleet-osquery_%s_amd64.deb" , updatesData . OrbitVersion ) )
require . NoError ( t , err )
require . Greater ( t , info . Size ( ) , int64 ( 0 ) ) // TODO verify contents
} )
2023-04-05 18:02:18 +00:00
t . Run ( "--use-sytem-configuration can't be used on installers that aren't pkg" , func ( t * testing . T ) {
for _ , p := range [ ] string { "deb" , "msi" , "rpm" , "" } {
runAppCheckErr (
t ,
[ ] string { "package" , fmt . Sprintf ( "--type=%s" , p ) , "--use-system-configuration" } ,
"--use-system-configuration is only available for pkg installers" ,
)
}
2022-01-31 13:41:11 +00:00
} )
2021-11-19 01:43:52 +00:00
// fleet-osquery.msi
// runAppForTest(t, []string{"package", "--type=msi", "--insecure"}) TODO: this is currently failing on Github runners due to permission issues
// info, err = os.Stat("orbit-osquery_0.0.3.msi")
// require.NoError(t, err)
// require.Greater(t, info.Size(), int64(0))
2021-09-09 05:34:12 +00:00
2021-11-19 01:43:52 +00:00
// runAppForTest(t, []string{"package", "--type=pkg", "--insecure"}) TODO: had a hard time getting xar installed on Ubuntu
2021-09-09 05:34:12 +00:00
}