This repository was archived by the owner on Mar 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_test.go
More file actions
176 lines (160 loc) · 4.15 KB
/
Copy pathmain_test.go
File metadata and controls
176 lines (160 loc) · 4.15 KB
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
166
167
168
169
170
171
172
173
174
175
176
package main
import (
"encoding/json"
"flag"
"fmt"
"net/http"
"net/http/httptest"
"os"
"testing"
)
func TestExitCodes(t *testing.T) {
old := os.Args
t.Cleanup(func() { os.Args = old })
tests := map[string]struct {
env map[string]string
args []string
code int
}{
"NoArgs": {args: []string{"cmd"}, code: 2},
"NoSuchFile": {args: []string{"cmd", "testdata/nonexistent.elf"}, code: 74},
"OnlyMeasure": {args: []string{"cmd", "--only-measure", "testdata/rgctl.elf"}, code: 0},
"DryRun": {env: map[string]string{
ghActionSha: "7eb306e0a1a8c77922acb685a21e4f9854a6bce3",
ghActionRepo: "zoftko/felf",
ghActionRefName: "dev",
},
args: []string{"cmd", "--dry-run", "testdata/rgctl.elf"},
code: 0,
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
os.Args = test.args
flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError)
for key, val := range test.env {
t.Setenv(key, val)
}
if status := cli(); status != test.code {
t.Errorf("%s: expected exit code %d, got %d", name, test.code, status)
}
})
}
}
func TestPush(t *testing.T) {
old := os.Args
t.Cleanup(func() { os.Args = old })
ref := "main"
repo := "zoftko/felf-cli"
token := "magicalmisterytoken"
path := "/api/measurements"
sha := "7eb306e0a1a8c77922acb685a21e4f9854a6bce3"
tests := map[string]struct {
env map[string]string
response int
code int
}{
"MissingToken": {
env: map[string]string{
ghActionSha: sha,
ghActionRefName: ref,
ghActionRepo: repo,
},
response: 200,
code: 4,
},
"MissingGithubSha": {
env: map[string]string{
ghActionSha: "",
ghActionRefName: ref,
},
response: 200,
code: 3,
},
"MissingGithubRef": {
env: map[string]string{
ghActionRefName: "",
},
response: -1,
code: 3,
},
"MissingGithubRepo": {
env: map[string]string{
ghActionRefName: ref,
ghActionSha: sha,
ghActionRepo: "",
},
response: -1,
code: 3,
},
"ResponseNon200": {
env: map[string]string{
ghActionSha: sha,
ghActionRefName: ref,
ghActionRepo: repo,
envToken: token,
},
response: 401,
code: 1,
},
"Response200": {
env: map[string]string{
ghActionSha: sha,
ghActionRefName: ref,
ghActionRepo: repo,
envToken: token,
},
response: 200,
code: 0,
},
"MissingURL": {
env: map[string]string{
ghActionSha: sha,
ghActionRefName: ref,
ghActionRepo: repo,
envToken: token,
envUrl: "",
},
response: -1,
code: 5,
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
os.Args = []string{"cmd", "testdata/rgctl.elf"}
flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError)
server := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
if content := request.Header.Get("Content-Type"); content != "application/json" {
t.Errorf("expected Content-Type: %s, got: %s", "application/json", content)
}
if method := request.Method; method != http.MethodPost {
t.Errorf("expected %s, got %s", method, http.MethodPost)
}
if repoHeader := request.Header.Get(headerRepo); repoHeader != repo {
t.Errorf("expected %s: %s, got %s", headerRepo, repo, repoHeader)
}
expectedAuth := fmt.Sprintf("Bearer %s", token)
if authorization := request.Header.Get("Authorization"); authorization != expectedAuth {
t.Errorf("expected Authorization: %s, got: %s", expectedAuth, authorization)
}
var payload Payload
_ = json.NewDecoder(request.Body).Decode(&payload)
if payload.Sha != sha {
t.Errorf("expected sha: %s, got :%s", sha, payload.Sha)
}
if payload.Ref != ref {
t.Errorf("expected ref: %s, got :%s", ref, payload.Ref)
}
writer.WriteHeader(test.response)
}))
t.Cleanup(func() { server.Close() })
t.Setenv("FELF_URL", server.URL+path)
for env, val := range test.env {
t.Setenv(env, val)
}
if code := cli(); code != test.code {
t.Errorf("expected exit code %d, got: %d", test.code, code)
}
})
}
}