6
6
"context"
7
7
"fmt"
8
8
"path"
9
+ "strings"
9
10
"time"
10
11
11
12
"github.com/stretchr/testify/suite"
@@ -76,6 +77,14 @@ func (s *SmbShareSuite) getPodFetchOptions() kube.PodFetchOptions {
76
77
}
77
78
78
79
func (s * SmbShareSuite ) getPodIP () (string , error ) {
80
+ pod , err := s .getReadyPod ()
81
+ if err != nil {
82
+ return "" , err
83
+ }
84
+ return pod .Status .PodIP , nil
85
+ }
86
+
87
+ func (s * SmbShareSuite ) getReadyPod () (* corev1.Pod , error ) {
79
88
l := fmt .Sprintf (
80
89
"samba-operator.samba.org/service=%s" , s .smbShareResource .Name )
81
90
pods , err := s .tc .FetchPods (
@@ -86,14 +95,14 @@ func (s *SmbShareSuite) getPodIP() (string, error) {
86
95
MaxFound : s .maxPods ,
87
96
})
88
97
if err != nil {
89
- return "" , err
98
+ return nil , err
90
99
}
91
100
for _ , pod := range pods {
92
101
if kube .PodIsReady (& pod ) {
93
- return pod . Status . PodIP , nil
102
+ return & pod , nil
94
103
}
95
104
}
96
- return "" , fmt .Errorf ("no pods ready when fetching IP " )
105
+ return nil , fmt .Errorf ("no pods ready" )
97
106
}
98
107
99
108
func (s * SmbShareSuite ) TestPodsReady () {
@@ -273,6 +282,57 @@ func (s *SmbShareWithExternalNetSuite) TestServiceIsLoadBalancer() {
273
282
)
274
283
}
275
284
285
+ func (s * SmbShareSuite ) TestMetricsOnPod () {
286
+ pod , cont , err := s .getMetricsContainer ()
287
+ s .Require ().NoError (err )
288
+ if cont == nil {
289
+ s .T ().Skipf ("no metrics container present" )
290
+ }
291
+ // Issue a curl command from samba container to samba-metrics container
292
+ // within smbd pod.
293
+ curl := fmt .Sprintf ("curl -s http://%s:8080/metrics" , pod .GetName ())
294
+ pc := kube.PodCommand {
295
+ Command : []string {"sh" , "-c" , curl },
296
+ Namespace : pod .GetNamespace (),
297
+ PodName : pod .GetName (),
298
+ ContainerName : "samba" ,
299
+ }
300
+ bch := kube .NewBufferedCommandHandler ()
301
+ err = kube .NewTestExec (s .tc ).Call (pc , bch )
302
+ s .Require ().NoError (err )
303
+ out := strings .TrimSpace (string (bch .GetStdout ()))
304
+ s .Require ().NotEmpty (out )
305
+
306
+ // Ensure that we get at least minimal output
307
+ hasLocksTotal := false
308
+ hasSharesTotal := false
309
+ for _ , line := range strings .Split (out , "\n " ) {
310
+ if ! strings .HasPrefix (line , "#" ) {
311
+ if strings .HasPrefix (line , "smb_locks_total " ) {
312
+ hasLocksTotal = true
313
+ } else if strings .HasPrefix (line , "smb_shares_total " ) {
314
+ hasSharesTotal = true
315
+ }
316
+ }
317
+ }
318
+ s .Require ().True (hasLocksTotal )
319
+ s .Require ().True (hasSharesTotal )
320
+ }
321
+
322
+ func (s * SmbShareSuite ) getMetricsContainer () (
323
+ * corev1.Pod , * corev1.Container , error ) {
324
+ pod , err := s .getReadyPod ()
325
+ if err != nil {
326
+ return nil , nil , err
327
+ }
328
+ for _ , cont := range pod .Spec .Containers {
329
+ if strings .Contains (cont .Name , "metrics" ) {
330
+ return pod , & cont , nil
331
+ }
332
+ }
333
+ return nil , nil , nil // Case running without metrics
334
+ }
335
+
276
336
func allSmbShareSuites () map [string ]suite.TestingSuite {
277
337
m := map [string ]suite.TestingSuite {}
278
338
m ["users1" ] = & SmbShareSuite {
0 commit comments