-
Notifications
You must be signed in to change notification settings - Fork 44
/
util.go
156 lines (143 loc) · 3.39 KB
/
util.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// Copyright (C) 2016 Juniper Networks, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package gohanscript
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/cloudwan/gohan/job"
l "github.com/cloudwan/gohan/log"
"github.com/cloudwan/gohan/util"
"github.com/k0kubun/pp"
"github.com/nati/yaml"
)
var log = l.NewLoggerForModule("gohanscript")
func init() {
l.SetUpBasicLogging(os.Stderr, l.DefaultFormat)
}
//LoadYAMLFile loads YAML from file
func LoadYAMLFile(file string) (*yaml.Node, error) {
buf, err := ioutil.ReadFile(file)
if err != nil {
return nil, err
}
return LoadYAML(buf)
}
//LoadYAML loads YAML from byte buffer
func LoadYAML(buf []byte) (*yaml.Node, error) {
node, err := yaml.ParseYAML(buf)
if err != nil {
return nil, err
}
return node.Children[0], nil
}
func convertMapformat(d interface{}) interface{} {
switch data := d.(type) {
case map[interface{}]interface{}:
result := map[string]interface{}{}
for key, value := range data {
result[key.(string)] = convertMapformat(value)
}
return result
case []interface{}:
result := make([]interface{}, len(data))
for i, item := range data {
result[i] = convertMapformat(item)
}
return result
default:
return data
}
}
func pprint(obj interface{}) {
pp.Print(obj)
}
func forEachList(vm *VM, obj []interface{}, workerNum int, f func(item interface{}) error) error {
if obj == nil {
return nil
}
size := len(obj)
if size == 0 {
return nil
}
if workerNum == 0 {
for _, value := range obj {
select {
case f := <-vm.StopChan:
f()
return nil
default:
err := f(value)
if err != nil {
return err
}
}
}
return nil
}
queue := job.NewQueue(uint(workerNum))
for _, item := range obj {
localItem := item
queue.Add(job.NewJob(func() {
f(localItem)
}))
}
queue.Wait()
return nil
}
//RunTests find tests under a directory and it executes them.
func RunTests(dir string) (total, success, failed int) {
vm := NewVM()
abs, _ := filepath.Abs(dir)
filepath.Walk(abs,
func(path string, info os.FileInfo, err error) error {
if err != nil {
fmt.Println(err)
return err
}
if info == nil {
return nil
}
if info.IsDir() {
return nil
}
if !strings.HasSuffix(path, "_test.yaml") {
return nil
}
result, err := vm.RunFile(path)
if err != nil {
log.Warning("test failed: %s", err)
}
if result == nil {
log.Warning("No result")
}
resultMap := result.(map[string]interface{})
total += resultMap["count"].(int)
success += resultMap["success"].(int)
failed += resultMap["failed"].(int)
return nil
})
fmt.Printf("total: %d success: %d failed: %d \n", total, success, failed)
return
}
func extractID(value interface{}) string {
id := util.MaybeString(value)
id = strings.Replace(id, " ", "", -1)
id = strings.Replace(id, "{{", "", -1)
id = strings.Replace(id, "}}", "", -1)
return id
}