Skip to content

Commit 36829b5

Browse files
synaretephlogistonjohn
authored andcommitted
tests: ensure samba-metrics sidecar is responsive
Issue a curl from samba container to samba-metrics sidecar container in order to verify samba-metrics is responsive with valid output. Execute the curl command within the pod itself. Enabled only if 'samba-metrics' container exists in pod. Signed-off-by: Shachar Sharon <[email protected]>
1 parent 391cfc9 commit 36829b5

File tree

1 file changed

+63
-3
lines changed

1 file changed

+63
-3
lines changed

tests/integration/smb_share_test.go

Lines changed: 63 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"context"
77
"fmt"
88
"path"
9+
"strings"
910
"time"
1011

1112
"github.com/stretchr/testify/suite"
@@ -76,6 +77,14 @@ func (s *SmbShareSuite) getPodFetchOptions() kube.PodFetchOptions {
7677
}
7778

7879
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) {
7988
l := fmt.Sprintf(
8089
"samba-operator.samba.org/service=%s", s.smbShareResource.Name)
8190
pods, err := s.tc.FetchPods(
@@ -86,14 +95,14 @@ func (s *SmbShareSuite) getPodIP() (string, error) {
8695
MaxFound: s.maxPods,
8796
})
8897
if err != nil {
89-
return "", err
98+
return nil, err
9099
}
91100
for _, pod := range pods {
92101
if kube.PodIsReady(&pod) {
93-
return pod.Status.PodIP, nil
102+
return &pod, nil
94103
}
95104
}
96-
return "", fmt.Errorf("no pods ready when fetching IP")
105+
return nil, fmt.Errorf("no pods ready")
97106
}
98107

99108
func (s *SmbShareSuite) TestPodsReady() {
@@ -273,6 +282,57 @@ func (s *SmbShareWithExternalNetSuite) TestServiceIsLoadBalancer() {
273282
)
274283
}
275284

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+
276336
func allSmbShareSuites() map[string]suite.TestingSuite {
277337
m := map[string]suite.TestingSuite{}
278338
m["users1"] = &SmbShareSuite{

0 commit comments

Comments
 (0)