|
| 1 | +/******************************************************************************* |
| 2 | + * IBM Confidential |
| 3 | + * OCO Source Materials |
| 4 | + * IBM Cloud Kubernetes Service, 5737-D43 |
| 5 | + * (C) Copyright IBM Corp. 2025 All Rights Reserved. |
| 6 | + * The source code for this program is not published or otherwise divested of |
| 7 | + * its trade secrets, irrespective of what has been deposited with |
| 8 | + * the U.S. Copyright Office. |
| 9 | + ******************************************************************************/ |
| 10 | + |
| 11 | +package driver |
| 12 | + |
| 13 | +import ( |
| 14 | + "errors" |
| 15 | + "os" |
| 16 | + "testing" |
| 17 | + |
| 18 | + "github.com/stretchr/testify/assert" |
| 19 | +) |
| 20 | + |
| 21 | +// mockSocketPermission implements the socketPermission interface |
| 22 | +type mockSocketPermission struct { |
| 23 | + chownCalled int |
| 24 | + chmodCalled int |
| 25 | + |
| 26 | + chownArgs struct { |
| 27 | + name string |
| 28 | + uid int |
| 29 | + gid int |
| 30 | + } |
| 31 | + chmodArgs struct { |
| 32 | + name string |
| 33 | + mode os.FileMode |
| 34 | + } |
| 35 | + |
| 36 | + chownErr error |
| 37 | + chmodErr error |
| 38 | +} |
| 39 | + |
| 40 | +func (m *mockSocketPermission) Chown(name string, uid, gid int) error { |
| 41 | + m.chownCalled++ |
| 42 | + m.chownArgs = struct { |
| 43 | + name string |
| 44 | + uid int |
| 45 | + gid int |
| 46 | + }{name, uid, gid} |
| 47 | + return m.chownErr |
| 48 | +} |
| 49 | + |
| 50 | +func (m *mockSocketPermission) Chmod(name string, mode os.FileMode) error { |
| 51 | + m.chmodCalled++ |
| 52 | + m.chmodArgs = struct { |
| 53 | + name string |
| 54 | + mode os.FileMode |
| 55 | + }{name, mode} |
| 56 | + return m.chmodErr |
| 57 | +} |
| 58 | + |
| 59 | +func TestSetupSidecar(t *testing.T) { |
| 60 | + tests := []struct { |
| 61 | + name string |
| 62 | + groupID string |
| 63 | + expectedErr bool |
| 64 | + chownErr error |
| 65 | + chmodErr error |
| 66 | + expectedChownCalls int |
| 67 | + expectedChmodCalls int |
| 68 | + expectedGroupID int |
| 69 | + }{ |
| 70 | + { |
| 71 | + name: "ValidGroupID", |
| 72 | + groupID: "2121", |
| 73 | + expectedErr: false, |
| 74 | + chownErr: nil, |
| 75 | + chmodErr: nil, |
| 76 | + expectedChownCalls: 1, |
| 77 | + expectedChmodCalls: 1, |
| 78 | + expectedGroupID: 2121, |
| 79 | + }, |
| 80 | + { |
| 81 | + name: "InvalidGroupID", |
| 82 | + groupID: "notanint", |
| 83 | + expectedErr: true, |
| 84 | + chownErr: nil, |
| 85 | + chmodErr: nil, |
| 86 | + expectedChownCalls: 0, |
| 87 | + expectedChmodCalls: 0, |
| 88 | + }, |
| 89 | + { |
| 90 | + name: "EmptyGroupID", |
| 91 | + groupID: "", |
| 92 | + expectedErr: false, |
| 93 | + chownErr: nil, |
| 94 | + chmodErr: nil, |
| 95 | + expectedChownCalls: 1, |
| 96 | + expectedChmodCalls: 1, |
| 97 | + expectedGroupID: 0, // Default to 0 if SIDECAR_GROUP_ID is empty |
| 98 | + }, |
| 99 | + { |
| 100 | + name: "ChownError", |
| 101 | + groupID: "1000", |
| 102 | + expectedErr: true, |
| 103 | + chownErr: errors.New("chown error"), |
| 104 | + chmodErr: nil, |
| 105 | + expectedChownCalls: 1, |
| 106 | + expectedChmodCalls: 0, // No chmod expected if chown fails |
| 107 | + expectedGroupID: 1000, |
| 108 | + }, |
| 109 | + { |
| 110 | + name: "ChmodError", |
| 111 | + groupID: "1000", |
| 112 | + expectedErr: true, |
| 113 | + chownErr: nil, |
| 114 | + chmodErr: errors.New("chmod error"), |
| 115 | + expectedChownCalls: 1, |
| 116 | + expectedChmodCalls: 1, |
| 117 | + expectedGroupID: 1000, |
| 118 | + }, |
| 119 | + } |
| 120 | + |
| 121 | + for _, tc := range tests { |
| 122 | + t.Run(tc.name, func(t *testing.T) { |
| 123 | + // Set SIDECAR_GROUP_ID environment variable |
| 124 | + if tc.groupID != "" { |
| 125 | + err := os.Setenv("SIDECAR_GROUP_ID", tc.groupID) |
| 126 | + assert.NoError(t, err) |
| 127 | + } else { |
| 128 | + err := os.Unsetenv("SIDECAR_GROUP_ID") |
| 129 | + assert.NoError(t, err) |
| 130 | + } |
| 131 | + defer os.Unsetenv("SIDECAR_GROUP_ID") // nolint:errcheck |
| 132 | + |
| 133 | + mock := &mockSocketPermission{ |
| 134 | + chownErr: tc.chownErr, |
| 135 | + chmodErr: tc.chmodErr, |
| 136 | + } |
| 137 | + |
| 138 | + // Creating test logger |
| 139 | + logger, teardown := GetTestLogger(t) |
| 140 | + defer teardown() |
| 141 | + |
| 142 | + // Call the function under test |
| 143 | + err := setupSidecar("/mock/socket", mock, logger) |
| 144 | + |
| 145 | + // Verify the result |
| 146 | + if tc.expectedErr { |
| 147 | + assert.Error(t, err) |
| 148 | + } else { |
| 149 | + assert.NoError(t, err) |
| 150 | + } |
| 151 | + |
| 152 | + // Verify the number of times Chown and Chmod were called |
| 153 | + assert.Equal(t, tc.expectedChownCalls, mock.chownCalled) |
| 154 | + assert.Equal(t, tc.expectedChmodCalls, mock.chmodCalled) |
| 155 | + |
| 156 | + if tc.expectedChownCalls > 0 { |
| 157 | + assert.Equal(t, -1, mock.chownArgs.uid) |
| 158 | + assert.Equal(t, tc.expectedGroupID, mock.chownArgs.gid) |
| 159 | + assert.Equal(t, "/mock/socket", mock.chownArgs.name) |
| 160 | + } |
| 161 | + }) |
| 162 | + } |
| 163 | +} |
0 commit comments