-
Notifications
You must be signed in to change notification settings - Fork 4
/
generate_openapi_test.go
57 lines (45 loc) · 1.7 KB
/
generate_openapi_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
package main
import (
"os"
"testing"
"github.com/rancher-sandbox/cluster-api-provider-elemental/internal/api"
"github.com/swaggest/openapi-go/openapi3"
"github.com/swaggest/rest/gorillamux"
)
const outputFile = "elemental-openapi.yaml"
func TestGenerateOpenAPI(t *testing.T) {
server := api.Server{}
router := server.NewRouter()
// Setup OpenAPI schema.
refl := openapi3.NewReflector()
refl.SpecSchema().SetTitle("Elemental API")
refl.SpecSchema().SetVersion("v0.0.1")
refl.SpecSchema().SetDescription(`This API can be used to interact with the Cluster API Elemental operator.<br />
This API is for <b>Internal</b> use by the <a href="https://github.com/rancher-sandbox/cluster-api-provider-elemental/tree/main/cmd/agent">Elemental CAPI agent</a> and it's not supported for public use.<br />
Use it at your own risk.<br />
<br />
The schemas are mapping the related <a href="https://github.com/rancher-sandbox/cluster-api-provider-elemental/tree/main/api/v1beta1">Elemental CAPI resources</a>.<br />`)
// Walk the router with OpenAPI collector.
c := gorillamux.NewOpenAPICollector(refl)
if err := router.Walk(c.Walker); err != nil {
t.Fatalf("Could not walk API routes: %s", err.Error())
}
// Get the resulting schema.
if yaml, err := refl.Spec.MarshalYAML(); err != nil {
t.Fatalf("Could not marshal OpenAPI YAML: %s", err.Error())
} else {
writeOpenAPISpecFile(t, yaml)
}
}
func writeOpenAPISpecFile(t *testing.T, spec []byte) {
t.Helper()
f, err := os.Create(outputFile)
if err != nil {
t.Fatalf("Could not create file '%s': %s", outputFile, err.Error())
}
defer f.Close()
_, err = f.Write(spec)
if err != nil {
t.Fatalf("Could not write file '%s': %s", outputFile, err.Error())
}
}