-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
125 lines (101 loc) · 2.51 KB
/
main_test.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
package main
import (
"bytes"
"fmt"
"os"
"os/exec"
"regexp"
"strconv"
"strings"
"testing"
)
type version struct {
major, minor, patch int
}
// Only one version 0.0.1 not 10.10.10
func newVersion(name string) *version {
s := regexp.MustCompile(`(?m)(\d)\.(\d)\.(\d)`).FindStringSubmatch(name)
v := &version{}
if len(s) >= 4 {
v.major, _ = strconv.Atoi(s[1]) //no lint
v.minor, _ = strconv.Atoi(s[2]) //no lint
v.patch, _ = strconv.Atoi(s[3]) //no lint
} else {
return nil
}
return v
}
func TestVersionInChangeLog(t *testing.T) {
t.Parallel()
changeLogFile := "change.log"
shaOut, err := exec.Command("git", "rev-list", "--tags", "--max-count=1").Output()
if err != nil {
t.Error(string(shaOut))
t.Fatal("git rev-list: ", err)
}
sha := strings.TrimSuffix(string(shaOut), "\n")
out, err := exec.Command("git", "describe", "--tags", "--always", sha).Output()
if err != nil {
t.Error(string(out))
t.Fatal("git describe: ", err)
}
tag := newVersion(string(out))
if tag == nil {
t.Fatal("not found version")
}
//Read change log
f, err := os.Open(changeLogFile)
if err != nil {
t.Fatal("read change log")
}
defer f.Close()
_, err = os.Stat(changeLogFile)
if err != nil {
t.Fatal("not found change log")
}
var buf bytes.Buffer
_, err = buf.ReadFrom(f)
if err != nil {
t.Fatal("read change log")
}
allVersions := regexp.MustCompile(`\n(\d)\.(\d)\.(\d)`).FindAllString(buf.String(), -1)
// Last version in change log
code := newVersion(allVersions[len(allVersions)-1])
if code == nil {
t.Fatal("not found version in changelog")
}
switch {
case code.major > tag.major: // 2.*.* > 1.*.*
break
case code.major < tag.major: // 1.*.* < 2.*.* bad
t.Error("Bad MAJOR version")
case code.minor > tag.minor: // 1.1.* > 1.0.*
break
case code.minor < tag.minor: // 1.0.* < 1.1.* bad
t.Error("Bad MINOR version")
case code.patch < tag.patch: // 1.0.0 < 1.0.1 bad
t.Error("Bad PATCH version")
}
}
func TestStartMain(t *testing.T) {
configFile := "/tmp/config.main"
os.Args = []string{"cmd", fmt.Sprintf("--cfgFile=%s", configFile)}
file, err := os.Create(configFile)
if err != nil {
t.Fatalf("Error creating test config file: %v", err)
}
defer file.Close()
_, err = file.Write([]byte(`firewall=iptables`))
if err != nil {
t.Fatalf("Error writing test config file: %v", err)
}
err = file.Sync()
if err != nil {
t.Fatalf("Error syncing test config file: %v", err)
}
main()
err = os.Remove(configFile)
if err != nil {
t.Fatalf("Failed to remove file: %v", err)
}
}