-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstate_test.go
131 lines (98 loc) · 2.67 KB
/
state_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
package main
import (
"io/ioutil"
"os"
"testing"
"github.com/tactycal/agent/stubUtils"
)
var testStatePath = "testdata/state"
func TestStateReset_Ok(t *testing.T) {
tmpFile, _ := ioutil.TempFile("", "tactycal-agent-ut")
defer os.Remove(tmpFile.Name()) // try to delete just in case
state := NewState(tmpFile.Name())
// call reset
err := state.Reset()
// check error
if err != nil {
t.Errorf("Expected error to be nil; got %v", err)
}
// tmp file should be gone
if _, err := os.Stat(tmpFile.Name()); !os.IsNotExist(err) {
t.Errorf("Expected file to be removed")
}
}
func TestStateReset_Err(t *testing.T) {
// create state with unexisting file
state := NewState("/should/not/exists")
err := state.Reset()
// check error
if err == nil {
t.Error("Excected error; to nil")
}
}
func TestStateGetToken_Ok(t *testing.T) {
stub := stubUtils.NewStubs(t,
&stubUtils.ReadFileStub{Path: testStatePath, Output: []byte(`{"token": "TOKEN"}`)})
defer stub.Close()
s := NewState(testStatePath)
token, err := s.GetToken()
// check error
if err != nil {
t.Errorf("Expected error to be nil; got %v", err)
}
// check token
if token != "TOKEN" {
t.Errorf("Expected \"TOKEN\"; got \"%s\"", token)
}
}
func TestStateGetToken_InvalidJson(t *testing.T) {
stub := stubUtils.NewStubs(t,
&stubUtils.ReadFileStub{Path: testStatePath, Output: []byte("How to break JSON?")})
defer stub.Close()
s := NewState(testStatePath)
token, err := s.GetToken()
// check error
if err == nil {
t.Errorf("Expected error not to be nil")
}
// check token
if token != "" {
t.Errorf("Expected token to be empty; got %v", token)
}
}
func TestStateGetToken_SomeError(t *testing.T) {
stub := stubUtils.NewStubs(t,
&stubUtils.ReadFileStub{Path: testStatePath, Err: stubUtils.OhNoErr})
defer stub.Close()
s := NewState(testStatePath)
token, err := s.GetToken()
// check error
if err != stubUtils.OhNoErr {
t.Errorf("Expected error %v; got %v", stubUtils.OhNoErr, err)
}
// check token
if token != "" {
t.Errorf("Expected token to be empty; got %v", token)
}
}
func TestStateSetToken_Ok(t *testing.T) {
stub := stubUtils.NewStubs(t,
&stubUtils.WriteFileStub{Path: testStatePath, Data: []byte(`{"Token":"TOKEN"}`), Mode: 0600})
defer stub.Close()
state := NewState(testStatePath)
err := state.SetToken("TOKEN")
// check error
if err != nil {
t.Errorf("Expected error to be nil; got %v", err)
}
}
func TestStateSetToken_Error(t *testing.T) {
stub := stubUtils.NewStubs(t,
&stubUtils.WriteFileStub{Err: stubUtils.OhNoErr})
defer stub.Close()
state := NewState(testStatePath)
err := state.SetToken("TOKEN")
if err == nil {
t.Errorf("Expected error to be set")
}
}