Skip to content

Commit 6f9adb0

Browse files
feat: add more unit-tests to increase coverage
1 parent de2a26f commit 6f9adb0

File tree

2 files changed

+83
-24
lines changed

2 files changed

+83
-24
lines changed

internal/pkg/agent/cmd/container_init_linux.go

+1-5
Original file line numberDiff line numberDiff line change
@@ -162,12 +162,8 @@ func updateFileCapsFromBoundingSet(executablePath string) (updated bool, err err
162162
return false, fmt.Errorf("failed to chown %s: %w", executablePath, err)
163163
}
164164

165-
var fileSet interface {
166-
SetFile(pathString string) error
167-
}
168-
169165
// create a new set based on the capabilities of Bounding set
170-
fileSet, err = cap.FromText(capsText)
166+
fileSet, err := cap.FromText(capsText)
171167
if err != nil {
172168
return false, fmt.Errorf("failed to parse caps text: %w", err)
173169
}

internal/pkg/agent/cmd/container_init_test.go

+82-19
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ package cmd
99
import (
1010
"os"
1111
"path/filepath"
12+
"syscall"
1213
"testing"
1314

1415
"github.com/stretchr/testify/require"
@@ -26,12 +27,8 @@ func Test_chownPaths(t *testing.T) {
2627
defer os.RemoveAll(secondParentDir)
2728

2829
childDir := filepath.Join(secondParentDir, "child")
29-
err = os.MkdirAll(childDir, 0o777)
30-
require.NoError(t, err)
3130

3231
childChildDir := filepath.Join(childDir, "child")
33-
err = os.MkdirAll(childDir, 0o777)
34-
require.NoError(t, err)
3532

3633
pathsToChown := distinctPaths{}
3734
pathsToChown.addPath(childDir)
@@ -45,24 +42,80 @@ func Test_chownPaths(t *testing.T) {
4542
require.NoError(t, err)
4643
}
4744

45+
func Test_updateFileCapsFromBoundingSet(t *testing.T) {
46+
if os.Geteuid() == 0 {
47+
t.Skip("this test requires non-root user")
48+
return
49+
}
50+
51+
tmpDir, err := os.MkdirTemp("", "test_chown")
52+
require.NoError(t, err)
53+
defer os.RemoveAll(tmpDir)
54+
55+
executable := filepath.Join(tmpDir, "test_exec")
56+
57+
err = os.WriteFile(executable, []byte{}, 0o7777)
58+
require.NoError(t, err)
59+
60+
updated, err := updateFileCapsFromBoundingSet(executable)
61+
require.ErrorIs(t, err, syscall.EPERM)
62+
require.False(t, updated)
63+
}
64+
4865
func Test_getMissingBoundingCapsText(t *testing.T) {
4966
tc := []struct {
50-
name string
51-
fileCaps []cap.Value
52-
boundCaps []cap.Value
53-
capsText string
67+
name string
68+
fileCaps []cap.Value
69+
fileCapsErr error
70+
boundCaps []cap.Value
71+
boundCapsErr error
72+
capsText string
73+
expectedErr error
5474
}{
5575
{
56-
name: "no missing caps",
57-
fileCaps: []cap.Value{cap.CHOWN, cap.SETPCAP},
58-
boundCaps: []cap.Value{cap.CHOWN, cap.SETPCAP},
59-
capsText: "",
76+
name: "no missing caps",
77+
fileCaps: []cap.Value{cap.CHOWN, cap.SETPCAP},
78+
fileCapsErr: nil,
79+
boundCaps: []cap.Value{cap.CHOWN, cap.SETPCAP},
80+
boundCapsErr: nil,
81+
capsText: "",
82+
expectedErr: nil,
83+
},
84+
{
85+
name: "missing caps",
86+
fileCaps: []cap.Value{cap.CHOWN, cap.SETPCAP},
87+
fileCapsErr: nil,
88+
boundCaps: []cap.Value{cap.CHOWN, cap.SETPCAP, cap.DAC_OVERRIDE},
89+
boundCapsErr: nil,
90+
capsText: "cap_chown,cap_dac_override,cap_setpcap=eip",
91+
expectedErr: nil,
92+
},
93+
{
94+
name: "no data err",
95+
fileCaps: nil,
96+
fileCapsErr: syscall.ENODATA,
97+
boundCaps: []cap.Value{cap.CHOWN, cap.SETPCAP, cap.DAC_OVERRIDE},
98+
boundCapsErr: nil,
99+
capsText: "cap_chown,cap_dac_override,cap_setpcap=eip",
100+
expectedErr: nil,
60101
},
61102
{
62-
name: "missing caps",
63-
fileCaps: []cap.Value{cap.CHOWN, cap.SETPCAP},
64-
boundCaps: []cap.Value{cap.CHOWN, cap.SETPCAP, cap.DAC_OVERRIDE},
65-
capsText: "cap_chown,cap_dac_override,cap_setpcap=eip",
103+
name: "file caps permission err",
104+
fileCaps: nil,
105+
fileCapsErr: syscall.EPERM,
106+
boundCaps: []cap.Value{cap.CHOWN, cap.SETPCAP, cap.DAC_OVERRIDE},
107+
boundCapsErr: nil,
108+
capsText: "",
109+
expectedErr: syscall.EPERM,
110+
},
111+
{
112+
name: "bound caps permission err",
113+
fileCaps: nil,
114+
fileCapsErr: nil,
115+
boundCaps: []cap.Value{cap.CHOWN, cap.SETPCAP, cap.DAC_OVERRIDE},
116+
boundCapsErr: syscall.EPERM,
117+
capsText: "",
118+
expectedErr: syscall.EPERM,
66119
},
67120
}
68121

@@ -74,6 +127,10 @@ func Test_getMissingBoundingCapsText(t *testing.T) {
74127
for _, tt := range tc {
75128
t.Run(tt.name, func(t *testing.T) {
76129
capBound = func(val cap.Value) (bool, error) {
130+
if tt.boundCapsErr != nil {
131+
return false, tt.boundCapsErr
132+
}
133+
77134
for _, boundCap := range tt.boundCaps {
78135
if boundCap == val {
79136
return true, nil
@@ -82,17 +139,23 @@ func Test_getMissingBoundingCapsText(t *testing.T) {
82139
return false, nil
83140
}
84141
capGetFile = func(path string) (*cap.Set, error) {
85-
set := cap.NewSet()
142+
if tt.fileCapsErr != nil {
143+
return nil, tt.fileCapsErr
144+
}
86145

146+
set := cap.NewSet()
87147
if err := set.SetFlag(cap.Effective, true, tt.fileCaps...); err != nil {
88148
return nil, err
89149
}
90-
91150
return set, nil
92151
}
93152

94153
capsText, err := getMissingBoundingCapsText("non_existent")
95-
require.NoError(t, err)
154+
if tt.expectedErr != nil {
155+
require.ErrorIs(t, err, tt.expectedErr)
156+
} else {
157+
require.NoError(t, err)
158+
}
96159
require.Equal(t, tt.capsText, capsText)
97160
})
98161
}

0 commit comments

Comments
 (0)