-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconf_test.go
132 lines (103 loc) · 3.64 KB
/
conf_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
126
127
128
129
130
131
132
package rcc
import (
"context"
"encoding/json"
"fmt"
"reflect"
"testing"
"time"
. "github.com/agiledragon/gomonkey/v2"
"github.com/baidu/brcc/brcc-go-sdk/logutil"
. "github.com/smartystreets/goconvey/convey"
)
// TestNewConf test conf initialize
func TestNewConf(t *testing.T) {
// initial log
logutil.InitLog()
Convey("Test for conf initailize", t, func() {
Convey("Test load from config success", func() {
conf, err := NewConf(TestConfFile)
So(err, ShouldBeNil)
So(conf, ShouldNotBeNil)
So(conf.ProjectName, ShouldEqual, projectName)
So(conf.EnvName, ShouldEqual, envName)
So(conf.VersionName, ShouldEqual, versionName)
So(conf.ApiPassword, ShouldEqual, apiPassword)
So(conf.ServerUrl, ShouldEqual, serverUrl)
So(conf.EnableCallback, ShouldEqual, enableCache)
So(conf.CallbackIntervalInt, ShouldEqual, callbackInterval)
So(conf.RequestTimeoutInt, ShouldEqual, requestTimeout)
So(conf.EnableCache, ShouldEqual, enableCache)
So(conf.RequestTimeout, ShouldEqual, requestTimeout*time.Second)
So(conf.CallbackInterval, ShouldEqual, callbackInterval*time.Second)
})
Convey("test load from non-exist config", func() {
conf, err := NewConf("fake.toml")
So(err, ShouldNotBeNil)
So(conf, ShouldBeNil)
})
})
}
// TestNormalize test internal method 'normalize' of Conf
func TestNormalize(t *testing.T) {
Convey("Test internal method 'normalize' ", t, func() {
var token string = "akfjsldfjsjdss"
Convey("test with mock refreshToken and acquireEnvironment", func() {
// do mock here
patches := ApplyMethod(reflect.TypeOf((*httpRequester)(nil)), "Post", func(_ *httpRequester, url string, _ interface{}, respData interface{}) error {
bodyBytes := []byte(fmt.Sprintf(`{"token":"%s"}`, token))
json.Unmarshal(bodyBytes, respData)
return nil
})
defer patches.Reset()
// do mock here
patches2 := ApplyMethod(reflect.TypeOf((*httpRequester)(nil)), "Get", func(_ *httpRequester, url string, respData interface{}) error {
bodyBytes := []byte(`{"environmentId":1}`)
json.Unmarshal(bodyBytes, respData)
return nil
})
defer patches2.Reset()
conf, err := NewConf(TestConfFile)
So(err, ShouldBeNil)
err = conf.normalize(context.Background())
So(err, ShouldBeNil)
})
})
}
// TestAcquireEnvironment test internal method 'refreshToken' of Conf
func TestRefreshToken(t *testing.T) {
Convey("Test internal method 'acquireEnvironment'", t, func() {
var token string = "sfsdfdsfdsfsdf"
Convey("test with mock http request", func() {
// do mock here
patches := ApplyMethod(reflect.TypeOf((*httpRequester)(nil)), "Post", func(_ *httpRequester, url string, _ interface{}, respData interface{}) error {
bodyBytes := []byte(fmt.Sprintf(`{"token":"%s"}`, token))
json.Unmarshal(bodyBytes, respData)
return nil
})
defer patches.Reset()
conf, err := NewConf(TestConfFile)
So(err, ShouldBeNil)
conf.refreshToken(context.Background())
So(conf.Token, ShouldEqual, token)
})
})
}
// TestAcquireEnvironment test internal method 'acquireEnvironment' of Conf
func TestAcquireEnvironment(t *testing.T) {
Convey("Test internal method 'acquireEnvironment'", t, func() {
Convey("test with mock http request", func() {
// do mock here
patches := ApplyMethod(reflect.TypeOf((*httpRequester)(nil)), "Get", func(_ *httpRequester, url string, respData interface{}) error {
bodyBytes := []byte(`{"environmentId":1}`)
json.Unmarshal(bodyBytes, respData)
return nil
})
defer patches.Reset()
conf, err := NewConf(TestConfFile)
So(err, ShouldBeNil)
conf.acquireEnvironment(context.Background())
So(conf.EnvId, ShouldEqual, 1)
})
})
}