forked from coreos/coretest
/
helpers.go
112 lines (98 loc) · 1.95 KB
/
helpers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package coretest
import (
"bufio"
"crypto/sha256"
"encoding/hex"
"fmt"
"io/ioutil"
"net"
"net/http"
"os"
"strings"
"time"
)
func CheckPort(network, address string, timeout time.Duration) error {
errc := make(chan error, 1)
go func() {
_, err := net.Dial(network, address)
errc <- err
}()
select {
case <-time.After(timeout):
return fmt.Errorf("%s:%s timed out after %s seconds.",
network, address, timeout)
case err := <-errc:
return err
}
}
func IsLink(f os.FileInfo) bool {
return f.Mode()&os.ModeSymlink != 0
}
func CheckHttpStatus(url string, timeout time.Duration) error {
errc := make(chan error, 1)
go func() {
tr := &http.Transport{}
req, err := http.NewRequest("GET", url, nil)
if err != nil {
errc <- err
return
}
resp, err := tr.RoundTrip(req)
if err != nil {
errc <- err
return
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
errc <- fmt.Errorf("%s failed with status %d.", url, resp.StatusCode)
return
}
errc <- nil
}()
select {
case <-time.After(timeout):
return fmt.Errorf("%s timed out after %s seconds.",
url, timeout)
case err := <-errc:
return err
}
}
type MountTable struct {
Device string
MountPoint string
FsType string
Options []string
}
func GetMountTable() (mounts []MountTable, err error) {
f, err := os.Open("/proc/mounts")
if err != nil {
return
}
scanner := bufio.NewScanner(f)
for scanner.Scan() {
line := strings.Split(scanner.Text(), " ")
if len(line) < 6 {
continue
}
mounts = append(mounts, MountTable{
Device: line[0],
MountPoint: line[1],
FsType: line[2],
Options: strings.Split(line[3], ","),
})
}
return
}
func Sha256File(fileName string) (hash string, err error) {
f, err := os.Open(fileName)
if err != nil {
return
}
bytes, err := ioutil.ReadAll(f)
if err != nil {
return
}
fileHasher := sha256.New()
fileHasher.Write(bytes)
return hex.EncodeToString(fileHasher.Sum(nil)), nil
}