|
| 1 | +// +build integration |
| 2 | + |
| 3 | +package integration |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "fmt" |
| 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 | + "github.com/samba-in-kubernetes/samba-operator/tests/utils/smbclient" |
| 15 | +) |
| 16 | + |
| 17 | +type MountPathSuite struct { |
| 18 | + suite.Suite |
| 19 | + |
| 20 | + auths []smbclient.Auth |
| 21 | + commonSources []kube.FileSource |
| 22 | + smbshareSetupSources []kube.FileSource |
| 23 | + smbshareSources []kube.FileSource |
| 24 | + smbShareSetupResource types.NamespacedName |
| 25 | + setupServerLabelPattern string |
| 26 | + smbShareResource types.NamespacedName |
| 27 | + serverLabelPattern string |
| 28 | + tc *kube.TestClient |
| 29 | +} |
| 30 | + |
| 31 | +func (s *MountPathSuite) getPodIP(labelPattern string) (string, error) { |
| 32 | + pods, err := s.tc.FetchPods( |
| 33 | + context.TODO(), |
| 34 | + kube.PodFetchOptions{ |
| 35 | + Namespace: testNamespace, |
| 36 | + LabelSelector: labelPattern, |
| 37 | + MaxFound: 1, |
| 38 | + }) |
| 39 | + if err != nil { |
| 40 | + return "", err |
| 41 | + } |
| 42 | + for _, pod := range pods { |
| 43 | + if kube.PodIsReady(&pod) { |
| 44 | + return pod.Status.PodIP, nil |
| 45 | + } |
| 46 | + } |
| 47 | + return "", fmt.Errorf("no pods ready when fetching IP") |
| 48 | +} |
| 49 | + |
| 50 | +func (s *MountPathSuite) waitForPods(labelPattern string) { |
| 51 | + require := s.Require() |
| 52 | + ctx, cancel := context.WithDeadline( |
| 53 | + context.TODO(), |
| 54 | + time.Now().Add(waitForPodsTime)) |
| 55 | + defer cancel() |
| 56 | + opts := kube.PodFetchOptions{ |
| 57 | + Namespace: testNamespace, |
| 58 | + LabelSelector: labelPattern, |
| 59 | + } |
| 60 | + require.NoError( |
| 61 | + kube.WaitForAnyPodExists(ctx, kube.NewTestClient(""), opts), |
| 62 | + "pod does not exist", |
| 63 | + ) |
| 64 | + require.NoError( |
| 65 | + kube.WaitForAnyPodReady(ctx, kube.NewTestClient(""), opts), |
| 66 | + "pod not ready", |
| 67 | + ) |
| 68 | +} |
| 69 | + |
| 70 | +func (s *MountPathSuite) SetupSuite() { |
| 71 | + s.tc = kube.NewTestClient("") |
| 72 | + require := s.Require() |
| 73 | + createFromFiles(require, s.tc, append(s.commonSources, s.smbshareSetupSources...)) |
| 74 | + // ensure the smbserver test pod exists and is ready |
| 75 | + s.waitForPods(s.setupServerLabelPattern) |
| 76 | + serverIP, err := s.getPodIP(s.setupServerLabelPattern) |
| 77 | + require.NoError(err) |
| 78 | + share := smbclient.Share{ |
| 79 | + Host: smbclient.Host(serverIP), |
| 80 | + Name: s.smbShareSetupResource.Name, |
| 81 | + } |
| 82 | + |
| 83 | + // Create folders over smbclient |
| 84 | + smbclient := smbclient.MustPodExec(s.tc, testNamespace, |
| 85 | + "smbclient", "client") |
| 86 | + err = smbclient.CacheFlush(context.TODO()) |
| 87 | + require.NoError(err) |
| 88 | + auth := s.auths[0] |
| 89 | + cmds := []string{ |
| 90 | + "mkdir testmnt1", |
| 91 | + "mkdir testmnt2", |
| 92 | + "mkdir testmnt1/mnt1", |
| 93 | + "mkdir testmnt2/mnt2", |
| 94 | + } |
| 95 | + err = smbclient.Command( |
| 96 | + context.TODO(), |
| 97 | + share, |
| 98 | + auth, |
| 99 | + cmds) |
| 100 | + require.NoError(err) |
| 101 | + |
| 102 | + // Delete the smbshare created |
| 103 | + deleteFromFiles(require, s.tc, s.smbshareSetupSources) |
| 104 | + |
| 105 | + // Create smbshare with Spec.Storage.PVC.Path specified |
| 106 | + createFromFiles(require, s.tc, append(s.commonSources, s.smbshareSources...)) |
| 107 | + s.waitForPods(s.serverLabelPattern) |
| 108 | +} |
| 109 | + |
| 110 | +func (s *MountPathSuite) TearDownSuite() { |
| 111 | + deleteFromFiles(s.Require(), s.tc, s.smbshareSetupSources) |
| 112 | + deleteFromFiles(s.Require(), s.tc, s.smbshareSources) |
| 113 | + deleteFromFiles(s.Require(), s.tc, s.commonSources) |
| 114 | +} |
| 115 | + |
| 116 | +func (s *MountPathSuite) TestMountPath() { |
| 117 | + require := s.Require() |
| 118 | + |
| 119 | + serverIP, err := s.getPodIP(s.serverLabelPattern) |
| 120 | + require.NoError(err) |
| 121 | + share := smbclient.Share{ |
| 122 | + Host: smbclient.Host(serverIP), |
| 123 | + Name: s.smbShareResource.Name, |
| 124 | + } |
| 125 | + |
| 126 | + // Test if correct path mounted using smbclient |
| 127 | + smbclient := smbclient.MustPodExec(s.tc, testNamespace, |
| 128 | + "smbclient", "client") |
| 129 | + err = smbclient.CacheFlush(context.TODO()) |
| 130 | + require.NoError(err) |
| 131 | + auth := s.auths[0] |
| 132 | + out, err := smbclient.CommandOutput( |
| 133 | + context.TODO(), |
| 134 | + share, |
| 135 | + auth, |
| 136 | + []string{"ls"}) |
| 137 | + require.NoError(err) |
| 138 | + require.Contains(string(out), "mnt1") |
| 139 | +} |
0 commit comments