|
| 1 | +// +build integration |
| 2 | + |
| 3 | +package integration |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "strings" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/stretchr/testify/suite" |
| 11 | + "k8s.io/apimachinery/pkg/types" |
| 12 | + |
| 13 | + "github.com/samba-in-kubernetes/samba-operator/tests/utils/kube" |
| 14 | +) |
| 15 | + |
| 16 | +type MountPathPermissionsSuite struct { |
| 17 | + suite.Suite |
| 18 | + |
| 19 | + commonSources []kube.FileSource |
| 20 | + smbshareSources []kube.FileSource |
| 21 | + smbShareResource types.NamespacedName |
| 22 | + serverLabelPattern string |
| 23 | + tc *kube.TestClient |
| 24 | +} |
| 25 | + |
| 26 | +func (s *MountPathPermissionsSuite) waitForPods(labelPattern string) { |
| 27 | + require := s.Require() |
| 28 | + ctx, cancel := context.WithDeadline( |
| 29 | + context.TODO(), |
| 30 | + time.Now().Add(waitForPodsTime)) |
| 31 | + defer cancel() |
| 32 | + opts := kube.PodFetchOptions{ |
| 33 | + Namespace: testNamespace, |
| 34 | + LabelSelector: labelPattern, |
| 35 | + } |
| 36 | + require.NoError( |
| 37 | + kube.WaitForAnyPodExists(ctx, kube.NewTestClient(""), opts), |
| 38 | + "pod does not exist", |
| 39 | + ) |
| 40 | + require.NoError( |
| 41 | + kube.WaitForAnyPodReady(ctx, kube.NewTestClient(""), opts), |
| 42 | + "pod not ready", |
| 43 | + ) |
| 44 | +} |
| 45 | + |
| 46 | +func (s *MountPathPermissionsSuite) SetupSuite() { |
| 47 | + s.tc = kube.NewTestClient("") |
| 48 | + require := s.Require() |
| 49 | + |
| 50 | + // Create smbshare with Spec.Storage.PVC.Path specified |
| 51 | + createFromFiles(require, s.tc, append(s.commonSources, s.smbshareSources...)) |
| 52 | + s.waitForPods(s.serverLabelPattern) |
| 53 | +} |
| 54 | + |
| 55 | +func (s *MountPathPermissionsSuite) TearDownSuite() { |
| 56 | + deleteFromFiles(s.Require(), s.tc, s.smbshareSources) |
| 57 | + deleteFromFiles(s.Require(), s.tc, s.commonSources) |
| 58 | +} |
| 59 | + |
| 60 | +// The test checks the first directory within /mnt for the xattr. |
| 61 | +// We may need to change this if multiple mount paths are supported |
| 62 | +func (s *MountPathPermissionsSuite) TestMountPathPermissions() { |
| 63 | + require := s.Require() |
| 64 | + cmd := []string{ |
| 65 | + "/bin/python3", |
| 66 | + "-c", |
| 67 | + "import xattr,os;mp='/mnt/' + os.listdir('/mnt')[0]; print(xattr.get(mp, 'user.share-perms-status'))", |
| 68 | + } |
| 69 | + |
| 70 | + pods, err := s.tc.FetchPods( |
| 71 | + context.TODO(), |
| 72 | + kube.PodFetchOptions{ |
| 73 | + Namespace: s.smbShareResource.Namespace, |
| 74 | + LabelSelector: s.serverLabelPattern, |
| 75 | + MaxFound: 1, |
| 76 | + }) |
| 77 | + require.NoError(err) |
| 78 | + |
| 79 | + pc := kube.PodCommand{ |
| 80 | + Command: cmd, |
| 81 | + Namespace: s.smbShareResource.Namespace, |
| 82 | + PodName: pods[0].Name, |
| 83 | + ContainerName: "samba", |
| 84 | + } |
| 85 | + bch := kube.NewBufferedCommandHandler() |
| 86 | + err = kube.NewTestExec(s.tc).Call(pc, bch) |
| 87 | + require.NoError(err) |
| 88 | + out := strings.TrimSpace(string(bch.GetStdout())) |
| 89 | + s.Require().NotEmpty(out) |
| 90 | +} |
0 commit comments