This repository has been archived by the owner on Jan 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
function_test.go
130 lines (109 loc) · 3.07 KB
/
function_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
package function
import (
"net/http"
"os"
"testing"
"github.com/qiyihuang/messenger"
"github.com/stretchr/testify/require"
)
func TestBuildStatusString(t *testing.T) {
t.Run("Returns correct status", func(t *testing.T) {
require.Equal(t, "SUCCESS", Success.string(), "Status string incorrect")
require.Equal(t, "FAILURE", Failure.string(), "Status string incorrect")
require.Equal(t, "CANCELLED", Cancelled.string(), "Status string incorrect")
require.Equal(t, "TIMEOUT", Timeout.string(), "Status string incorrect")
require.Equal(t, "FAILED", Failed.string(), "Status string incorrect")
})
}
func TestNotifyParams(t *testing.T) {
t.Run("Green", func(t *testing.T) {
m := pubsubMessage{
Message: message{
Attributes: attributes{
Status: "SUCCESS",
},
},
}
desc, color := notifyParams(m)
require.Equal(t, "Build status: SUCCESS.", desc, "Incorrect description")
require.Equal(t, GREEN, color, "Incorrect color")
})
t.Run("Red", func(t *testing.T) {
msgs := []pubsubMessage{
{
Message: message{
Attributes: attributes{
Status: "FAILURE",
},
},
},
{
Message: message{
Attributes: attributes{
Status: "CANCELLED",
},
},
},
{
Message: message{
Attributes: attributes{
Status: "TIMEOUT",
},
},
},
{
Message: message{
Attributes: attributes{
Status: "FAILED",
},
},
},
}
for _, m := range msgs {
desc, color := notifyParams(m)
require.Equal(t, "Build status: "+m.Message.Attributes.Status+".", desc, "Incorrect description")
require.Equal(t, RED, color, "Incorrect color")
}
})
}
func resetClient() {
webhookClient = nil
}
func TestClient(t *testing.T) {
t.Run("httpClient is nil", func(t *testing.T) {
resetClient()
defer resetClient()
os.Setenv("DISCORD_WEBHOOK_URL", "https://discord.com/api/webhooks/somehook")
defer os.Unsetenv("ARTIFACT_BUCKET_NAME")
c, _ := client()
require.NotEmpty(t, c, "Incorrect httpClient")
})
t.Run("httpClient exists", func(t *testing.T) {
webhookClient, _ = messenger.NewClient(http.DefaultClient, "https://discord.com/api/webhooks/somehook")
defer resetClient()
c, _ := client()
require.NotEmpty(t, c, "Incorrect httpClient")
})
t.Run("Return error", func(t *testing.T) {
os.Setenv("DISCORD_WEBHOOK_URL", "wrong url")
defer os.Unsetenv("DISCORD_WEBHOOK_URL")
_, err := client()
require.Error(t, err, "Return error failed")
})
}
// Todo check how to set up google creds for GitHub.
// Todo using storage.NewClient requires proper creds.
// func TestBucket(t *testing.T) {
// t.Run("bucketHandle is nil", func(t *testing.T) {
// bucketHandle = nil
// // c, _ := storage.NewClient(context.Background())
// n := "name"
// os.Setenv("ARTIFACT_BUCKET_NAME", n)
// defer os.Unsetenv("ARTIFACT_BUCKET_NAME")
// // expectedBucket := c.Bucket(n)
// b, err := bucket()
// // require.Equal(t, expectedBucket, b, "Incorrect bucketHandle")
// require.Equal(t, nil, err, "Incorrect bucketHandle")
// require.NotEqual(t, nil, b, "Incorrect bucketHandle")
// })
// }