Skip to content

Commit 4aa0196

Browse files
Merge pull request #65 from macadmins/test_coverage
Fix test config file
2 parents 3a1bbc3 + 5ffaaf1 commit 4aa0196

22 files changed

+447
-90
lines changed

.testcoverage.yml

-9
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,8 @@ exclude:
5252
# Exclude files or packages matching their paths
5353
paths:
5454
- main.go
55-
- pkg/utils/exec.go
56-
- tables/fileline/file_line.go
57-
- tables/unifiedlog/unified_log.go
58-
- ^tables/networkquality$
59-
- ^tables/unifiedlog$
6055
- ^tables/puppet$
61-
- ^tables/unifiedlog$
6256
- ^tables/pendingappleupdates$
63-
- ^tables/fileline$
64-
- tables/unifiedlog/unified_log.go
65-
- tables/networkquality/networkquality.go
6657
- tables/pendingappleupdates/pendingappleupdates.go
6758
- tables/puppet/puppet_facts.go
6859
- tables/puppet/puppet_info.go

pkg/utils/BUILD.bazel

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ go_library(
77
"exec_mocks.go",
88
"osquery.go",
99
"utils.go",
10+
"utils_mocks.go",
1011
],
1112
importpath = "github.com/macadmins/osquery-extension/pkg/utils",
1213
visibility = ["//visibility:public"],

pkg/utils/exec_test.go

+29-18
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package utils
22

33
import (
44
"testing"
5+
6+
"github.com/stretchr/testify/assert"
57
)
68

79
func TestRunCmd(t *testing.T) {
@@ -14,32 +16,41 @@ func TestRunCmd(t *testing.T) {
1416
},
1517
}
1618
output, err := runner.RunCmd("echo", "test")
17-
if err != nil {
18-
t.Fatalf("RunCmd() error = %v, wantErr nil", err)
19-
return
20-
}
21-
got := string(output)
22-
if got != runner.Commands["echo test"].Output {
23-
t.Errorf("RunCmd() = %q, want %q", got, runner.Commands["echo test"].Output)
24-
}
19+
assert.NoError(t, err)
20+
assert.Equal(t, "test output", string(output))
2521
}
2622

2723
func TestRunCmdWithStdin(t *testing.T) {
2824
runner := MultiMockCmdRunner{
2925
Commands: map[string]MockCmdRunner{
30-
"echo": {
26+
"cat": {
3127
Output: "test output",
3228
Err: nil,
3329
},
3430
},
3531
}
36-
output, err := runner.RunCmdWithStdin("echo", "test")
37-
if err != nil {
38-
t.Fatalf("RunCmdWithStdin() error = %v, wantErr nil", err)
39-
return
40-
}
41-
got := string(output)
42-
if got != runner.Commands["echo"].Output {
43-
t.Errorf("RunCmdWithStdin() = %q, want %q", got, runner.Commands["echo"].Output)
44-
}
32+
output, err := runner.RunCmdWithStdin("cat", "test")
33+
assert.NoError(t, err)
34+
assert.Equal(t, "test output", string(output))
35+
}
36+
37+
func TestNewRunner(t *testing.T) {
38+
runner := NewRunner()
39+
40+
assert.NotNil(t, runner.Runner, "Expected Runner to be initialized, but got nil")
41+
assert.IsType(t, &ExecCmdRunner{}, runner.Runner, "Expected Runner to be of type *ExecCmdRunner")
42+
}
43+
44+
func TestExecCmdRunner_RunCmd(t *testing.T) {
45+
runner := &ExecCmdRunner{}
46+
output, err := runner.RunCmd("echo", "test")
47+
assert.NoError(t, err)
48+
assert.Equal(t, "test\n", string(output))
49+
}
50+
51+
func TestExecCmdRunner_RunCmdWithStdin(t *testing.T) {
52+
runner := &ExecCmdRunner{}
53+
output, err := runner.RunCmdWithStdin("cat", "test")
54+
assert.NoError(t, err)
55+
assert.Equal(t, "test", string(output))
4556
}

pkg/utils/utils.go

+13-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package utils
22

33
import "os"
44

5-
func FileExists(filename string) bool {
5+
func FileExists(fs FileSystem, filename string) bool {
66
info, err := os.Stat(filename)
77
if os.IsNotExist(err) {
88
return false
@@ -16,3 +16,15 @@ func BoolToString(b bool) string {
1616
}
1717
return "false"
1818
}
19+
20+
// FileSystem interface for os.Stat
21+
type FileSystem interface {
22+
Stat(name string) (os.FileInfo, error)
23+
}
24+
25+
// OSFileSystem is a concrete implementation of FileSystem using os package
26+
type OSFileSystem struct{}
27+
28+
func (OSFileSystem) Stat(name string) (os.FileInfo, error) {
29+
return os.Stat(name)
30+
}

pkg/utils/utils_mocks.go

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package utils
2+
3+
import "os"
4+
5+
// MockFileSystem is a mock implementation of FileSystem for testing
6+
type MockFileSystem struct {
7+
FileExists bool
8+
Err error
9+
}
10+
11+
func (m MockFileSystem) Stat(name string) (os.FileInfo, error) {
12+
if m.Err != nil {
13+
return nil, m.Err
14+
}
15+
if m.FileExists {
16+
return nil, nil
17+
}
18+
return nil, os.ErrNotExist
19+
}

pkg/utils/utils_test.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,24 @@ import (
99

1010
func TestFileExists(t *testing.T) {
1111
// Create a temporary file for testing
12+
fs := MockFileSystem{
13+
FileExists: true,
14+
Err: nil,
15+
}
1216
tempFile, err := os.CreateTemp("", "test")
1317
assert.NoError(t, err, "Failed to create temp file")
1418

1519
defer os.Remove(tempFile.Name())
1620
tempFile.Close()
1721

1822
// Test that FileExists returns true for an existing file
19-
assert.True(t, FileExists(tempFile.Name()), "Expected file to exist")
23+
assert.True(t, FileExists(fs, tempFile.Name()), "Expected file to exist")
2024

2125
// Delete the temporary file
2226
os.Remove(tempFile.Name())
2327

2428
// Test that FileExists returns false for a non-existing file
25-
assert.False(t, FileExists(tempFile.Name()), "Expected file to not exist")
29+
assert.False(t, FileExists(fs, tempFile.Name()), "Expected file to not exist")
2630
}
2731

2832
func TestBoolToString(t *testing.T) {

tables/fileline/BUILD.bazel

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
load("@io_bazel_rules_go//go:def.bzl", "go_library")
1+
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
22

33
go_library(
44
name = "fileline",
@@ -10,3 +10,13 @@ go_library(
1010
"@com_github_osquery_osquery_go//plugin/table",
1111
],
1212
)
13+
14+
go_test(
15+
name = "fileline_test",
16+
srcs = ["file_line_test.go"],
17+
embed = [":fileline"],
18+
deps = [
19+
"//pkg/utils",
20+
"@com_github_stretchr_testify//assert",
21+
],
22+
)

tables/fileline/file_line.go

+8-7
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ func FileLineGenerate(ctx context.Context, queryContext table.QueryContext) ([]m
4646
}
4747
}
4848
var results []map[string]string
49-
output, err := processFile(path, wildcard)
49+
fs := utils.OSFileSystem{}
50+
output, err := processFile(path, wildcard, fs)
5051
if err != nil {
5152
return results, err
5253
}
@@ -61,7 +62,7 @@ func FileLineGenerate(ctx context.Context, queryContext table.QueryContext) ([]m
6162
return results, nil
6263
}
6364

64-
func processFile(path string, wildcard bool) ([]FileLine, error) {
65+
func processFile(path string, wildcard bool, fs utils.FileSystem) ([]FileLine, error) {
6566

6667
var output []FileLine
6768

@@ -73,24 +74,24 @@ func processFile(path string, wildcard bool) ([]FileLine, error) {
7374
return nil, err
7475
}
7576
for _, file := range files {
76-
lines, _ := readLines(file)
77+
lines, _ := readLines(file, fs)
7778
output = append(output, lines...)
7879

7980
}
8081
} else {
81-
lines, _ := readLines(path)
82+
lines, _ := readLines(path, fs)
8283
output = append(output, lines...)
8384
}
8485

8586
return output, nil
8687

8788
}
8889

89-
func readLines(path string) ([]FileLine, error) {
90+
func readLines(path string, fs utils.FileSystem) ([]FileLine, error) {
9091
var output []FileLine
9192

92-
if !utils.FileExists(path) {
93-
err := errors.New("File does not exist")
93+
if !utils.FileExists(fs, path) {
94+
err := errors.New("file does not exist")
9495
return nil, err
9596
}
9697
file, err := os.Open(path)

tables/fileline/file_line_test.go

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package fileline
2+
3+
import (
4+
"os"
5+
"path/filepath"
6+
"testing"
7+
8+
"github.com/macadmins/osquery-extension/pkg/utils"
9+
"github.com/stretchr/testify/assert"
10+
)
11+
12+
func TestProcessFile(t *testing.T) {
13+
t.Run("processFile with wildcard", func(t *testing.T) {
14+
// Create temporary files for testing
15+
tmpFile1, err := os.CreateTemp("", "testfile1-*.txt")
16+
assert.NoError(t, err)
17+
defer os.Remove(tmpFile1.Name())
18+
19+
tmpFile2, err := os.CreateTemp("", "testfile2-*.txt")
20+
assert.NoError(t, err)
21+
defer os.Remove(tmpFile2.Name())
22+
23+
_, err = tmpFile1.WriteString("line1\nline2\n")
24+
assert.NoError(t, err)
25+
_, err = tmpFile2.WriteString("line3\nline4\n")
26+
assert.NoError(t, err)
27+
28+
tmpFile1.Close()
29+
tmpFile2.Close()
30+
31+
path := filepath.Join(filepath.Dir(tmpFile1.Name()), "testfile%-*.txt")
32+
fs := utils.MockFileSystem{FileExists: true, Err: nil}
33+
lines, err := processFile(path, true, fs)
34+
assert.NoError(t, err)
35+
assert.Len(t, lines, 4)
36+
})
37+
38+
t.Run("processFile without wildcard", func(t *testing.T) {
39+
// Create a temporary file for testing
40+
tmpFile, err := os.CreateTemp("", "testfile-*.txt")
41+
assert.NoError(t, err)
42+
defer os.Remove(tmpFile.Name())
43+
44+
_, err = tmpFile.WriteString("line1\nline2\n")
45+
assert.NoError(t, err)
46+
tmpFile.Close()
47+
48+
fs := utils.MockFileSystem{FileExists: true, Err: nil}
49+
50+
lines, err := processFile(tmpFile.Name(), false, fs)
51+
assert.NoError(t, err)
52+
assert.Len(t, lines, 2)
53+
})
54+
}
55+
56+
func TestReadLines(t *testing.T) {
57+
t.Run("readLines file exists", func(t *testing.T) {
58+
// Create a temporary file for testing
59+
tmpFile, err := os.CreateTemp("", "testfile-*.txt")
60+
assert.NoError(t, err)
61+
defer os.Remove(tmpFile.Name())
62+
63+
_, err = tmpFile.WriteString("line1\nline2\n")
64+
assert.NoError(t, err)
65+
tmpFile.Close()
66+
fs := utils.MockFileSystem{FileExists: true, Err: nil}
67+
lines, err := readLines(tmpFile.Name(), fs)
68+
assert.NoError(t, err)
69+
assert.Len(t, lines, 2)
70+
})
71+
72+
t.Run("readLines file does not exist", func(t *testing.T) {
73+
fs := utils.MockFileSystem{FileExists: false, Err: nil}
74+
lines, err := readLines("nonexistentfile.txt", fs)
75+
assert.Error(t, err)
76+
assert.Nil(t, lines)
77+
assert.Equal(t, "file does not exist", err.Error())
78+
})
79+
}

tables/mdm/BUILD.bazel

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ go_test(
1818
srcs = ["mdm_test.go"],
1919
embed = [":mdm"],
2020
deps = [
21+
"//pkg/utils",
2122
"@com_github_osquery_osquery_go//plugin/table",
2223
"@com_github_stretchr_testify//assert",
2324
],

0 commit comments

Comments
 (0)