Skip to content

Commit df56632

Browse files
author
Shruthi-1MN
committed
Validation of file share mount paths for integration test
1 parent 089dca6 commit df56632

1 file changed

Lines changed: 120 additions & 0 deletions

File tree

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
package integration
2+
3+
import (
4+
"bytes"
5+
"encoding/json"
6+
"fmt"
7+
"io/ioutil"
8+
"log"
9+
"net/http"
10+
"os/exec"
11+
"strings"
12+
"testing"
13+
)
14+
15+
func TestClientPathsvalidation(t *testing.T){
16+
res, err := http.Get("http://127.0.0.1:50040/v1beta/e93b4c0934da416eb9c8d120c5d04d96/file/shares")
17+
if err != nil{
18+
log.Fatalln(err)
19+
}
20+
21+
defer res.Body.Close()
22+
body, err := ioutil.ReadAll(res.Body)
23+
if err != nil{
24+
log.Fatalln(err)
25+
}
26+
27+
filesharelstmap := []map[string]interface{}{}
28+
if err := json.Unmarshal(body, &filesharelstmap); err != nil {
29+
panic(err)
30+
}
31+
32+
if len(filesharelstmap) == 0{
33+
t.Errorf("empty file share paths")
34+
}
35+
df := exec.Command("df")
36+
grep := exec.Command("grep", "-w", "/mnt")
37+
cut := exec.Command("awk", "{print $6}")//, "%", "-f", "1")
38+
39+
result, _, err := Pipline(df, grep, cut)
40+
if err != nil {
41+
fmt.Println(err)
42+
return
43+
}
44+
out := string(result)
45+
result1 := strings.Split(out, "\n")
46+
47+
for _,k := range filesharelstmap{
48+
exportlocations := fmt.Sprintf("%v", k["exportLocations"])
49+
lstlocations := strings.Split(exportlocations, ":")
50+
mountpaths := TrimSuffix(lstlocations[1], "]")
51+
strout := strings.Replace(mountpaths, "var", "mnt", -1)
52+
bool1 := contains(result1, strout)
53+
if !bool1 {
54+
t.Errorf("this path doesn't exists %s", strout)
55+
}
56+
}
57+
}
58+
59+
func TrimSuffix(s, suffix string) string {
60+
if strings.HasSuffix(s, suffix) {
61+
s = s[:len(s)-len(suffix)]
62+
}
63+
return s
64+
}
65+
66+
func contains(s []string, e string) bool {
67+
for _, a := range s {
68+
if a == e {
69+
return true
70+
}
71+
}
72+
return false
73+
}
74+
75+
76+
func Pipline(cmds ...*exec.Cmd) ([]byte, []byte, error) {
77+
// credits to https://studygolang.com/articles/5387
78+
79+
// At least one command
80+
if len(cmds) < 1 {
81+
return nil, nil, nil
82+
}
83+
84+
var output bytes.Buffer
85+
var stderr bytes.Buffer
86+
var err error
87+
maxindex := len(cmds) - 1
88+
cmds[maxindex].Stdout = &output
89+
cmds[maxindex].Stderr = &stderr
90+
91+
for i, cmd := range cmds[:maxindex] {
92+
if i == maxindex {
93+
break
94+
}
95+
96+
cmds[i+1].Stdin, err = cmd.StdoutPipe()
97+
if err != nil {
98+
return nil, nil, err
99+
}
100+
}
101+
102+
// Start each command
103+
for _, cmd := range cmds {
104+
err := cmd.Start()
105+
if err != nil {
106+
return output.Bytes(), stderr.Bytes(), err
107+
}
108+
}
109+
110+
// Wait for each command to complete
111+
for _, cmd := range cmds {
112+
err := cmd.Wait()
113+
if err != nil {
114+
return output.Bytes(), stderr.Bytes(), err
115+
}
116+
}
117+
118+
return output.Bytes(), stderr.Bytes(), nil
119+
}
120+

0 commit comments

Comments
 (0)