-
Notifications
You must be signed in to change notification settings - Fork 2
/
client_test.go
165 lines (114 loc) · 2.93 KB
/
client_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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package salt
import (
"context"
"os"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestClient(t *testing.T) {
var server, username, password, method string
var values = map[string]*string{
"SALTAPI_URL": &server,
"SALTAPI_USER": &username,
"SALTAPI_PASS": &password,
"SALTAPI_EAUTH": &method,
}
for k, v := range values {
*v = os.Getenv(k)
if *v == "" {
t.Skipf("Skip: %s undefined", k)
}
}
c := New(server)
ctx := context.Background()
t.Run("Login", func(t *testing.T) {
assert.Nil(t, c.Login(ctx, username, password, method))
assert.NotEmpty(t, c.Token)
})
t.Run("Jobs", func(t *testing.T) {
var jobs []string
fn := func(id string, job Response) error {
t.Logf("Job: ID = %s, function = %s", id, job.Get("Function"))
jobs = append(jobs, id)
return nil
}
assert.Nil(t, c.Jobs.All(ctx, fn))
assert.NotEmpty(t, jobs)
t.Logf("Seen %d jobs.", len(jobs))
})
t.Run("Keys", func(t *testing.T) {
var minions []string
fn := func(name string) error {
minions = append(minions, name)
return nil
}
assert.Nil(t, c.Keys.ListAccepted(ctx, fn))
assert.NotEmpty(t, minions)
t.Logf("Seen %d minion keys: %s", len(minions), minions)
})
t.Run("Minions", func(t *testing.T) {
var minions []string
fn := func(id string, grains Response) error {
t.Logf("Minion: ID = %s, osfinger = %s", id, grains.Get("osfinger"))
minions = append(minions, id)
return nil
}
assert.Nil(t, c.Minions.All(ctx, fn))
assert.NotEmpty(t, minions)
t.Logf("Seen %d minions.", len(minions))
})
t.Run("Ping", func(t *testing.T) {
err := c.Ping(ctx, "*", func(id string, ok bool) error {
t.Logf("Ping: ID = %s, OK = %v", id, ok)
return nil
})
assert.Nil(t, err)
})
t.Run("Run", func(t *testing.T) {
type TestArgReturn struct {
Arguments []string `json:"args"`
Keywords Object `json:"kwargs"`
}
cmd := Command{
Arguments: []string{"a1", "a2"},
Function: "test.arg_clean",
Keywords: Object{"k1": 1, "k2": false},
Target: "*",
Timeout: 10,
}
exp := TestArgReturn{
Arguments: []string{"a1", "a2"},
Keywords: Object{"k1": float64(1), "k2": false},
}
err := c.Run(ctx, &cmd, func(id string, response Response) error {
var ret TestArgReturn
if err := response.Decode(&ret); err != nil {
return err
}
t.Logf("Run: ID = %s, return = %v", id, ret)
assert.Equal(t, exp, ret)
return nil
})
assert.Nil(t, err)
})
t.Run("Events", func(t *testing.T) {
var seen int
ctx, cancel := context.WithTimeout(ctx, time.Second*15)
defer cancel()
c.Events.Stream(ctx, func(event Response) error {
t.Logf("Events: tag = %s", event.Get("tag"))
seen++
return nil
})
assert.NotEmpty(t, seen)
})
_ = time.Now()
t.Run("Logout", func(t *testing.T) {
assert.Nil(t, c.Logout(ctx))
err := c.Minions.All(ctx, func(_ string, _ Response) error {
return nil
})
assert.NotNil(t, err)
})
}