Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validation of file share mount paths for integration test #998

Open
wants to merge 1 commit into
base: development
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
147 changes: 147 additions & 0 deletions test/e2e/paths_validation_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
package e2e

import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os/exec"
"strings"
"testing"
)

func TestClean_mount_paths(t *testing.T){
ids := ClientPathsvalidation()
for _,strout := range ids{
client := &http.Client{}
fmt.Println("http://127.0.0.1:50040/v1beta/e93b4c0934da416eb9c8d120c5d04d96/file/shares/"+strout)
// Create request
req, err := http.NewRequest("DELETE", "http://127.0.0.1:50040/v1beta/e93b4c0934da416eb9c8d120c5d04d96/file/shares/"+strout, nil)
if err != nil {
fmt.Println(err)
return
}
// Fetch Request
resp, err := client.Do(req)
if err != nil {
fmt.Println(err)
return
}
defer resp.Body.Close()
fmt.Println("response Status : ", resp.Status)
}
}

func ClientPathsvalidation()[]string{
res, err := http.Get("http://127.0.0.1:50040/v1beta/e93b4c0934da416eb9c8d120c5d04d96/file/shares")
if err != nil{
log.Fatalln(err)
}

defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil{
log.Fatalln(err)
}

filesharelstmap := []map[string]interface{}{}
if err := json.Unmarshal(body, &filesharelstmap); err != nil {
panic(err)
}

if len(filesharelstmap) == 0{
fmt.Println("empty file share paths")
}

df := exec.Command("df")
grep := exec.Command("grep", "-w", "/mnt")
cut := exec.Command("awk", "{print $6}")
result, _, err := Pipline(df, grep, cut)
if err != nil {
fmt.Println(err)
return []string{}
}

out := string(result)
result1 := strings.Split(out, "\n")
var ids []string
for _,k := range filesharelstmap{
exportlocations := fmt.Sprintf("%v", k["exportLocations"])
lstlocations := strings.Split(exportlocations, ":")
mountpaths := TrimSuffix(lstlocations[1], "]")
id := fmt.Sprintf("%v", k["id"])
strout := strings.Replace(mountpaths, "var", "mnt", -1)
bool1 := contains(result1, strout)
if !bool1 {
fmt.Sprintf("this path doesn't exists %s", strout)
}else{
ids = append(ids,id)
}
}
return ids
}

func TrimSuffix(s, suffix string) string {
if strings.HasSuffix(s, suffix) {
s = s[:len(s)-len(suffix)]
}
return s
}

func contains(s []string, e string) bool {
for _, a := range s {
if a == e {
return true
}
}
return false
}


func Pipline(cmds ...*exec.Cmd) ([]byte, []byte, error) {
// credits to https://studygolang.com/articles/5387

// At least one command
if len(cmds) < 1 {
return nil, nil, nil
}

var output bytes.Buffer
var stderr bytes.Buffer
var err error
maxindex := len(cmds) - 1
cmds[maxindex].Stdout = &output
cmds[maxindex].Stderr = &stderr

for i, cmd := range cmds[:maxindex] {
if i == maxindex {
break
}

cmds[i+1].Stdin, err = cmd.StdoutPipe()
if err != nil {
return nil, nil, err
}
}

// Start each command
for _, cmd := range cmds {
err := cmd.Start()
if err != nil {
return output.Bytes(), stderr.Bytes(), err
}
}

// Wait for each command to complete
for _, cmd := range cmds {
err := cmd.Wait()
if err != nil {
return output.Bytes(), stderr.Bytes(), err
}
}

return output.Bytes(), stderr.Bytes(), nil
}