Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ driver: deps buildimage

.PHONY: build
build:
CGO_ENABLED=0 GOOS=linux go build -mod=vendor -v -ldflags '-X main.version=${git_commit_id} -extldflags "-static"' -o ${GOPATH}/bin/${EXE_DRIVER_NAME} ./cmd/$*
CGO_ENABLED=0 GOOS=linux go build -mod=mod -v -ldflags '-X main.version=${git_commit_id} -extldflags "-static"' -o ${GOPATH}/bin/${EXE_DRIVER_NAME} ./cmd/$*


.PHONY: buildimage
Expand Down
2 changes: 1 addition & 1 deletion pkg/driver/fileOps.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* IBM Confidential
* OCO Source Materials
* IBM Cloud Kubernetes Service, 5737-D43
* (C) Copyright IBM Corp. 2023 All Rights Reserved.
* (C) Copyright IBM Corp. 2025 All Rights Reserved.
* The source code for this program is not published or otherwise divested of
* its trade secrets, irrespective of what has been deposited with
* the U.S. Copyright Office.
Expand Down
163 changes: 163 additions & 0 deletions pkg/driver/fileOps_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
/*******************************************************************************
* IBM Confidential
* OCO Source Materials
* IBM Cloud Kubernetes Service, 5737-D43
* (C) Copyright IBM Corp. 2025 All Rights Reserved.
* The source code for this program is not published or otherwise divested of
* its trade secrets, irrespective of what has been deposited with
* the U.S. Copyright Office.
******************************************************************************/

package driver

import (
"errors"
"os"
"testing"

"github.com/stretchr/testify/assert"
)

// mockSocketPermission implements the socketPermission interface
type mockSocketPermission struct {
chownCalled int
chmodCalled int

chownArgs struct {
name string
uid int
gid int
}
chmodArgs struct {
name string
mode os.FileMode
}

chownErr error
chmodErr error
}

func (m *mockSocketPermission) Chown(name string, uid, gid int) error {
m.chownCalled++
m.chownArgs = struct {
name string
uid int
gid int
}{name, uid, gid}
return m.chownErr
}

func (m *mockSocketPermission) Chmod(name string, mode os.FileMode) error {
m.chmodCalled++
m.chmodArgs = struct {
name string
mode os.FileMode
}{name, mode}
return m.chmodErr
}

func TestSetupSidecar(t *testing.T) {
tests := []struct {
name string
groupID string
expectedErr bool
chownErr error
chmodErr error
expectedChownCalls int
expectedChmodCalls int
expectedGroupID int
}{
{
name: "ValidGroupID",
groupID: "2121",
expectedErr: false,
chownErr: nil,
chmodErr: nil,
expectedChownCalls: 1,
expectedChmodCalls: 1,
expectedGroupID: 2121,
},
{
name: "InvalidGroupID",
groupID: "notanint",
expectedErr: true,
chownErr: nil,
chmodErr: nil,
expectedChownCalls: 0,
expectedChmodCalls: 0,
},
{
name: "EmptyGroupID",
groupID: "",
expectedErr: false,
chownErr: nil,
chmodErr: nil,
expectedChownCalls: 1,
expectedChmodCalls: 1,
expectedGroupID: 0, // Default to 0 if SIDECAR_GROUP_ID is empty
},
{
name: "ChownError",
groupID: "1000",
expectedErr: true,
chownErr: errors.New("chown error"),
chmodErr: nil,
expectedChownCalls: 1,
expectedChmodCalls: 0, // No chmod expected if chown fails
expectedGroupID: 1000,
},
{
name: "ChmodError",
groupID: "1000",
expectedErr: true,
chownErr: nil,
chmodErr: errors.New("chmod error"),
expectedChownCalls: 1,
expectedChmodCalls: 1,
expectedGroupID: 1000,
},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
// Set SIDECAR_GROUP_ID environment variable
if tc.groupID != "" {
err := os.Setenv("SIDECAR_GROUP_ID", tc.groupID)
assert.NoError(t, err)
} else {
err := os.Unsetenv("SIDECAR_GROUP_ID")
assert.NoError(t, err)
}
defer os.Unsetenv("SIDECAR_GROUP_ID") // nolint:errcheck

mock := &mockSocketPermission{
chownErr: tc.chownErr,
chmodErr: tc.chmodErr,
}

// Creating test logger
logger, teardown := GetTestLogger(t)
defer teardown()

// Call the function under test
err := setupSidecar("/mock/socket", mock, logger)

// Verify the result
if tc.expectedErr {
assert.Error(t, err)
} else {
assert.NoError(t, err)
}

// Verify the number of times Chown and Chmod were called
assert.Equal(t, tc.expectedChownCalls, mock.chownCalled)
assert.Equal(t, tc.expectedChmodCalls, mock.chmodCalled)

if tc.expectedChownCalls > 0 {
assert.Equal(t, -1, mock.chownArgs.uid)
assert.Equal(t, tc.expectedGroupID, mock.chownArgs.gid)
assert.Equal(t, "/mock/socket", mock.chownArgs.name)
}
})
}
}