|
| 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 TestClean_mount_paths(t *testing.T){ |
| 16 | + ids := ClientPathsvalidation() |
| 17 | + for _,strout := range ids{ |
| 18 | + client := &http.Client{} |
| 19 | + fmt.Println("http://127.0.0.1:50040/v1beta/e93b4c0934da416eb9c8d120c5d04d96/file/shares/"+strout) |
| 20 | + // Create request |
| 21 | + req, err := http.NewRequest("DELETE", "http://127.0.0.1:50040/v1beta/e93b4c0934da416eb9c8d120c5d04d96/file/shares/"+strout, nil) |
| 22 | + if err != nil { |
| 23 | + fmt.Println(err) |
| 24 | + return |
| 25 | + } |
| 26 | + // Fetch Request |
| 27 | + resp, err := client.Do(req) |
| 28 | + if err != nil { |
| 29 | + fmt.Println(err) |
| 30 | + return |
| 31 | + } |
| 32 | + defer resp.Body.Close() |
| 33 | + fmt.Println("response Status : ", resp.Status) |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +func ClientPathsvalidation()[]string{ |
| 38 | + res, err := http.Get("http://127.0.0.1:50040/v1beta/e93b4c0934da416eb9c8d120c5d04d96/file/shares") |
| 39 | + if err != nil{ |
| 40 | + log.Fatalln(err) |
| 41 | + } |
| 42 | + |
| 43 | + defer res.Body.Close() |
| 44 | + body, err := ioutil.ReadAll(res.Body) |
| 45 | + if err != nil{ |
| 46 | + log.Fatalln(err) |
| 47 | + } |
| 48 | + |
| 49 | + filesharelstmap := []map[string]interface{}{} |
| 50 | + if err := json.Unmarshal(body, &filesharelstmap); err != nil { |
| 51 | + panic(err) |
| 52 | + } |
| 53 | + |
| 54 | + if len(filesharelstmap) == 0{ |
| 55 | + fmt.Println("empty file share paths") |
| 56 | + } |
| 57 | + |
| 58 | + df := exec.Command("df") |
| 59 | + grep := exec.Command("grep", "-w", "/mnt") |
| 60 | + cut := exec.Command("awk", "{print $6}") |
| 61 | + result, _, err := Pipline(df, grep, cut) |
| 62 | + if err != nil { |
| 63 | + fmt.Println(err) |
| 64 | + return []string{} |
| 65 | + } |
| 66 | + |
| 67 | + out := string(result) |
| 68 | + result1 := strings.Split(out, "\n") |
| 69 | + var ids []string |
| 70 | + for _,k := range filesharelstmap{ |
| 71 | + exportlocations := fmt.Sprintf("%v", k["exportLocations"]) |
| 72 | + lstlocations := strings.Split(exportlocations, ":") |
| 73 | + mountpaths := TrimSuffix(lstlocations[1], "]") |
| 74 | + id := fmt.Sprintf("%v", k["id"]) |
| 75 | + strout := strings.Replace(mountpaths, "var", "mnt", -1) |
| 76 | + bool1 := contains(result1, strout) |
| 77 | + if !bool1 { |
| 78 | + fmt.Sprintf("this path doesn't exists %s", strout) |
| 79 | + }else{ |
| 80 | + ids = append(ids,id) |
| 81 | + } |
| 82 | + } |
| 83 | + return ids |
| 84 | +} |
| 85 | + |
| 86 | +func TrimSuffix(s, suffix string) string { |
| 87 | + if strings.HasSuffix(s, suffix) { |
| 88 | + s = s[:len(s)-len(suffix)] |
| 89 | + } |
| 90 | + return s |
| 91 | +} |
| 92 | + |
| 93 | +func contains(s []string, e string) bool { |
| 94 | + for _, a := range s { |
| 95 | + if a == e { |
| 96 | + return true |
| 97 | + } |
| 98 | + } |
| 99 | + return false |
| 100 | +} |
| 101 | + |
| 102 | + |
| 103 | +func Pipline(cmds ...*exec.Cmd) ([]byte, []byte, error) { |
| 104 | + // credits to https://studygolang.com/articles/5387 |
| 105 | + |
| 106 | + // At least one command |
| 107 | + if len(cmds) < 1 { |
| 108 | + return nil, nil, nil |
| 109 | + } |
| 110 | + |
| 111 | + var output bytes.Buffer |
| 112 | + var stderr bytes.Buffer |
| 113 | + var err error |
| 114 | + maxindex := len(cmds) - 1 |
| 115 | + cmds[maxindex].Stdout = &output |
| 116 | + cmds[maxindex].Stderr = &stderr |
| 117 | + |
| 118 | + for i, cmd := range cmds[:maxindex] { |
| 119 | + if i == maxindex { |
| 120 | + break |
| 121 | + } |
| 122 | + |
| 123 | + cmds[i+1].Stdin, err = cmd.StdoutPipe() |
| 124 | + if err != nil { |
| 125 | + return nil, nil, err |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + // Start each command |
| 130 | + for _, cmd := range cmds { |
| 131 | + err := cmd.Start() |
| 132 | + if err != nil { |
| 133 | + return output.Bytes(), stderr.Bytes(), err |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + // Wait for each command to complete |
| 138 | + for _, cmd := range cmds { |
| 139 | + err := cmd.Wait() |
| 140 | + if err != nil { |
| 141 | + return output.Bytes(), stderr.Bytes(), err |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + return output.Bytes(), stderr.Bytes(), nil |
| 146 | +} |
| 147 | + |
0 commit comments