|
| 1 | +/* |
| 2 | +Copyright 2024 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package ibmcsidriver |
| 18 | + |
| 19 | +import ( |
| 20 | + "errors" |
| 21 | + "os" |
| 22 | + "testing" |
| 23 | + |
| 24 | + cloudProvider "github.com/IBM/ibm-csi-common/pkg/ibmcloudprovider" |
| 25 | + "github.com/kubernetes-sigs/ibm-vpc-block-csi-driver/pkg/ibmcsidriver/ibmcsidriverfakes" |
| 26 | + "github.com/stretchr/testify/assert" |
| 27 | +) |
| 28 | + |
| 29 | +func TestSetupSidecar(t *testing.T) { |
| 30 | + tests := []struct { |
| 31 | + name string |
| 32 | + groupID string |
| 33 | + expectedErr bool |
| 34 | + chownErr error |
| 35 | + chmodErr error |
| 36 | + expectedChownCalls int |
| 37 | + expectedChmodCalls int |
| 38 | + expectedGroupID int |
| 39 | + }{ |
| 40 | + { |
| 41 | + name: "ValidGroupID", |
| 42 | + groupID: "2121", |
| 43 | + expectedErr: false, |
| 44 | + chownErr: nil, |
| 45 | + chmodErr: nil, |
| 46 | + expectedChownCalls: 1, |
| 47 | + expectedChmodCalls: 1, |
| 48 | + expectedGroupID: 2121, |
| 49 | + }, |
| 50 | + { |
| 51 | + name: "EmptyGroupID", |
| 52 | + groupID: "", |
| 53 | + expectedErr: false, |
| 54 | + chownErr: nil, |
| 55 | + chmodErr: nil, |
| 56 | + expectedChownCalls: 1, |
| 57 | + expectedChmodCalls: 1, |
| 58 | + expectedGroupID: 0, // Default to 0 if SIDECAR_GROUP_ID is empty |
| 59 | + }, |
| 60 | + { |
| 61 | + name: "ChownError", |
| 62 | + groupID: "1000", |
| 63 | + expectedErr: true, |
| 64 | + chownErr: errors.New("chown error"), |
| 65 | + chmodErr: nil, |
| 66 | + expectedChownCalls: 1, |
| 67 | + expectedChmodCalls: 0, // No chmod expected if chown fails |
| 68 | + expectedGroupID: 1000, |
| 69 | + }, |
| 70 | + { |
| 71 | + name: "ChmodError", |
| 72 | + groupID: "1000", |
| 73 | + expectedErr: true, |
| 74 | + chownErr: nil, |
| 75 | + chmodErr: errors.New("chmod error"), |
| 76 | + expectedChownCalls: 1, |
| 77 | + expectedChmodCalls: 1, |
| 78 | + expectedGroupID: 1000, |
| 79 | + }, |
| 80 | + } |
| 81 | + |
| 82 | + for _, tc := range tests { |
| 83 | + t.Run(tc.name, func(t *testing.T) { |
| 84 | + // Set SIDECAR_GROUP_ID environment variable |
| 85 | + if tc.groupID != "" { |
| 86 | + os.Setenv("SIDECAR_GROUP_ID", tc.groupID) |
| 87 | + } else { |
| 88 | + os.Unsetenv("SIDECAR_GROUP_ID") |
| 89 | + } |
| 90 | + defer os.Unsetenv("SIDECAR_GROUP_ID") |
| 91 | + |
| 92 | + // Create the fake object generated by counterfeiter |
| 93 | + fakeSocketPermission := new(ibmcsidriverfakes.FakeSocketPermission) |
| 94 | + |
| 95 | + // Set return values for chown and chmod methods |
| 96 | + fakeSocketPermission.ChownReturns(tc.chownErr) |
| 97 | + fakeSocketPermission.ChmodReturns(tc.chmodErr) |
| 98 | + |
| 99 | + // Creating test logger |
| 100 | + logger, teardown := cloudProvider.GetTestLogger(t) |
| 101 | + defer teardown() |
| 102 | + |
| 103 | + // Call the function under test |
| 104 | + err := setupSidecar("/path/to/socket", fakeSocketPermission, logger) |
| 105 | + |
| 106 | + // Verify the result |
| 107 | + if tc.expectedErr { |
| 108 | + assert.Error(t, err) |
| 109 | + } else { |
| 110 | + assert.NoError(t, err) |
| 111 | + } |
| 112 | + |
| 113 | + // Verify the number of times Chown and Chmod were called |
| 114 | + assert.Equal(t, tc.expectedChownCalls, fakeSocketPermission.ChownCallCount()) |
| 115 | + assert.Equal(t, tc.expectedChmodCalls, fakeSocketPermission.ChmodCallCount()) |
| 116 | + |
| 117 | + // Verify the group ID passed to chown |
| 118 | + if tc.expectedChownCalls > 0 { |
| 119 | + _, _, actualGroupID := fakeSocketPermission.ChownArgsForCall(0) |
| 120 | + assert.Equal(t, tc.expectedGroupID, actualGroupID) |
| 121 | + } |
| 122 | + }) |
| 123 | + } |
| 124 | +} |
0 commit comments