go.mondoo.com/cnquery@v0.0.0-20231005093811-59568235f6ea/providers/os/connection/filesystem_test.go (about) 1 // Copyright (c) Mondoo, Inc. 2 // SPDX-License-Identifier: BUSL-1.1 3 4 package connection_test 5 6 import ( 7 "testing" 8 9 "github.com/spf13/afero" 10 "github.com/stretchr/testify/assert" 11 "github.com/stretchr/testify/require" 12 "go.mondoo.com/cnquery/providers-sdk/v1/inventory" 13 "go.mondoo.com/cnquery/providers/os/connection" 14 "go.mondoo.com/cnquery/providers/os/connection/fs/fsutil" 15 "go.mondoo.com/cnquery/providers/os/detector" 16 ) 17 18 func TestOsDetection(t *testing.T) { 19 conn, err := connection.NewFileSystemConnection(0, &inventory.Config{ 20 Path: "./fs/testdata/centos8", 21 }, nil) 22 require.NoError(t, err) 23 24 pf, detected := detector.DetectOS(conn) 25 require.True(t, detected) 26 27 assert.Equal(t, "centos", pf.Name) 28 assert.Equal(t, "8.2.2004", pf.Version) 29 } 30 31 func TestMountedDirectoryFile(t *testing.T) { 32 conn, err := connection.NewFileSystemConnection(0, &inventory.Config{ 33 Path: "./fs/testdata/centos8", 34 }, nil) 35 require.NoError(t, err) 36 37 f, err := conn.FileSystem().Open("/etc/os-release") 38 assert.Nil(t, err, "should open without error") 39 assert.NotNil(t, f) 40 defer f.Close() 41 42 afutil := afero.Afero{Fs: conn.FileSystem()} 43 afutil.Exists(f.Name()) 44 45 p := f.Name() 46 assert.Equal(t, "/etc/os-release", p, "path should be correct") 47 48 stat, err := f.Stat() 49 assert.Equal(t, int64(417), stat.Size(), "should read file size") 50 assert.Nil(t, err, "should execute without error") 51 52 content, err := afutil.ReadFile(f.Name()) 53 assert.Equal(t, nil, err, "should execute without error") 54 assert.Equal(t, 417, len(content), "should read the full content") 55 56 // reset reader 57 f.Seek(0, 0) 58 sha, err := fsutil.Sha256(f) 59 assert.Equal(t, "1d272eeae89e45470abf750cdc037eb72b216686cf8c105e5b9925df21ec1043", sha, "sha256 output should be correct") 60 assert.Nil(t, err, "should execute without error") 61 62 // reset reader 63 f.Seek(0, 0) 64 md5, err := fsutil.Md5(f) 65 assert.Equal(t, "f5a898d54907811ccc54cd35dcb991d1", md5, "md5 output should be correct") 66 assert.Nil(t, err, "should execute without error") 67 } 68 69 func TestRunCommandReturnsErr(t *testing.T) { 70 conn, err := connection.NewFileSystemConnection(0, &inventory.Config{ 71 Path: "./fs/testdata/centos8", 72 }, nil) 73 require.NoError(t, err) 74 75 _, err = conn.RunCommand("aa-status") 76 require.Error(t, err) 77 assert.Equal(t, "provider does not implement RunCommand", err.Error()) 78 }