/
collectorstest.go
93 lines (85 loc) · 1.71 KB
/
collectorstest.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
package collectors
import (
"bytes"
"compress/gzip"
"fmt"
"github.com/netapp/harvest/v2/pkg/tree"
"github.com/netapp/harvest/v2/pkg/tree/node"
"github.com/netapp/harvest/v2/third_party/tidwall/sjson"
"github.com/tidwall/gjson"
"io"
"os"
"path/filepath"
)
// Common unit testing helpers
var (
gsonCache = make(map[string][]gjson.Result)
)
func JSONToGson(path string, flatten bool) []gjson.Result {
var (
result []gjson.Result
err error
)
results, ok := gsonCache[path]
if ok {
return results
}
var reader io.Reader
if filepath.Ext(path) == ".gz" {
open, err := os.Open(path)
if err != nil {
panic(err)
}
defer open.Close()
reader, err = gzip.NewReader(open)
if err != nil {
panic(err)
}
} else {
data, err := os.ReadFile(path)
if err != nil {
return nil
}
reader = bytes.NewReader(data)
}
var b bytes.Buffer
_, err = io.Copy(&b, reader) //nolint:gosec
if err != nil {
return nil
}
bb := b.Bytes()
output := gjson.ParseBytes(bb)
data := output.Get("records")
numRecords := output.Get("num_records")
if !data.Exists() {
contentJSON := `{"records":[]}`
response, err := sjson.SetRawBytes([]byte(contentJSON), "records.-1", bb)
if err != nil {
panic(err)
}
value := gjson.GetBytes(response, "records")
result = append(result, value)
} else if numRecords.Int() > 0 {
if flatten {
result = append(result, data.Array()...)
} else {
result = append(result, data)
}
}
gsonCache[path] = result
return result
}
func Params(object string, path string) *node.Node {
yml := `
schedule:
- data: 9999h
objects:
%s: %s
`
yml = fmt.Sprintf(yml, object, path)
root, err := tree.LoadYaml([]byte(yml))
if err != nil {
panic(err)
}
return root
}