-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain_test.go
68 lines (61 loc) · 2.25 KB
/
main_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
package main
import (
"bytes"
"io"
"net/http"
"net/http/httptest"
"os"
"testing"
)
func TestValidConfig(t *testing.T) {
infile, err := os.Open("./test/fcos-config.yaml")
if err != nil {
t.Fatalf("failed to open fcos config: %v\n", err)
}
defer infile.Close()
dataIn, err := io.ReadAll(infile)
if err != nil {
t.Fatalf("failed to read %s: %v\n", infile.Name(), err)
}
req, err := http.NewRequest("POST", "/api/v1/transpile", bytes.NewBuffer(dataIn))
if err != nil {
t.Fatal(err)
}
req.Header.Set("Content-Type", "text/x-yaml")
rr := httptest.NewRecorder()
handler := http.HandlerFunc(transpile)
handler.ServeHTTP(rr, req)
if status := rr.Code; status != http.StatusOK {
t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusOK)
}
expected := `{"ignition":{"version":"3.5.0"},"storage":{"files":[{"path":"/etc/test.cfg","contents":{"compression":"","source":"data:,test-test-test"},"mode":384}]},"systemd":{"units":[{"contents":"[Unit]\nDescription=Test\nAfter=network-online.target\nWants=network.target\n\n[Service]\nExecStart=/usr/bin/test\n\n[Install]\nWantedBy=multi-user.target\n","enabled":true,"name":"test.service"},{"mask":true,"name":"docker.service"}]}}` + "\n"
if rr.Body.String() != expected {
t.Errorf("handler returned unexpected body: got %v want %v", rr.Body.String(), expected)
}
}
func TestInvalidConfig(t *testing.T) {
infile, err := os.Open("./test/fcos-error.yaml")
if err != nil {
t.Fatalf("failed to open fcos config: %v\n", err)
}
defer infile.Close()
dataIn, err := io.ReadAll(infile)
if err != nil {
t.Fatalf("failed to read %s: %v\n", infile.Name(), err)
}
req, err := http.NewRequest("POST", "/api/v1/transpile?strict=true", bytes.NewBuffer(dataIn))
if err != nil {
t.Fatal(err)
}
req.Header.Set("Content-Type", "text/x-yaml")
rr := httptest.NewRecorder()
handler := http.HandlerFunc(transpile)
handler.ServeHTTP(rr, req)
if status := rr.Code; status != http.StatusBadRequest {
t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusBadRequest)
}
expected := `{"error":"config produced warnings and strict was specified"}`
if rr.Body.String() != expected {
t.Errorf("handler returned unexpected body: got %v want %v", rr.Body.String(), expected)
}
}