Debian Codegraph @

test/integration/integration_suite_test.go

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 /*
2 Copyright 2018 The Kubernetes Authors.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17 package integration
18
19 import (
20 "os"
21 "os/exec"
22 "path"
23 "path/filepath"
24 "testing"
25
26 . "github.com/onsi/ginkgo/v2"
27 . "github.com/onsi/gomega"
28 "github.com/onsi/gomega/gexec"
29 )
30
31 const (
32 headerFilePath = "../../boilerplate/boilerplate.go.txt"
33 testdataDir = "./testdata"
34 testPkgRoot = "k8s.io/kube-openapi/test/integration/testdata"
35 outputPkg = testPkgRoot + "/pkg/generated"
36 generatedCodeFileName = "openapi_generated.go"
37 goldenCodeFilePath = "pkg/generated/" + generatedCodeFileName
38 generatedSwaggerFileName = "generated.v2.json"
39 goldenSwaggerFileName = "golden.v2.json"
40 generatedReportFileName = "generated.v2.report"
41 goldenReportFileName = "golden.v2.report"
42 generatedOpenAPIv3FileName = "generated.v3.json"
43 goldenOpenAPIv3Filename = "golden.v3.json"
44
•45 timeoutSeconds = 60.0
46 )
47
48 var (
49 workingDirectory string
50 tempDir string
51 terr error
52 openAPIGenPath string
53
54 inputDirs = []string{
55 // `path` vs. `filepath` because packages use '/'
56 path.Join(testPkgRoot, "listtype"),
57 path.Join(testPkgRoot, "maptype"),
58 path.Join(testPkgRoot, "structtype"),
59 path.Join(testPkgRoot, "dummytype"),
60 path.Join(testPkgRoot, "uniontype"),
61 path.Join(testPkgRoot, "enumtype"),
62 path.Join(testPkgRoot, "custom"),
63 path.Join(testPkgRoot, "valuevalidation"),
64 path.Join(testPkgRoot, "defaults"),
65 }
66 )
67
68 func generatedFile(filename string) string { return filepath.Join(tempDir, filename) }
69 func testdataFile(filename string) string { return filepath.Join(testdataDir, filename) }
70
71 func TestGenerators(t *testing.T) {
72 RegisterFailHandler(Fail)
73 RunSpecs(t, "Integration Test Suite")
74 }
75
76 var _ = BeforeSuite(func() {
77 // Explicitly manage working directory
78 abs, err := filepath.Abs("")
79 Expect(err).ShouldNot(HaveOccurred())
80 workingDirectory = abs
81
82 // Create a temporary directory for generated swagger files.
83 tempDir, terr = os.MkdirTemp("./", "openapi")
84 Expect(terr).ShouldNot(HaveOccurred())
85
86 // Build the OpenAPI code generator.
87 By("building openapi-gen")
88 binaryPath, berr := gexec.Build("../../cmd/openapi-gen/openapi-gen.go")
89 Expect(berr).ShouldNot(HaveOccurred())
90 openAPIGenPath = binaryPath
91
92 // Run the OpenAPI code generator.
93 By("running openapi-gen")
94 args := append([]string{
95 "--output-dir", tempDir,
96 "--output-pkg", outputPkg,
97 "--output-file", generatedCodeFileName,
98 "--report-filename", generatedFile(generatedReportFileName),
99 "--go-header-file", headerFilePath,
100 }, inputDirs...)
101 command := exec.Command(openAPIGenPath, args...)
102 command.Dir = workingDirectory
103 session, err := gexec.Start(command, GinkgoWriter, GinkgoWriter)
104 Expect(err).ShouldNot(HaveOccurred())
105 Eventually(session, timeoutSeconds).Should(gexec.Exit(0))
106
107 By("writing swagger v2.0")
108 // Create the OpenAPI swagger builder.
109 binaryPath, berr = gexec.Build("./builder/main.go")
110 Expect(berr).ShouldNot(HaveOccurred())
111
112 // Execute the builder, generating an OpenAPI swagger file with definitions.
113 gs := generatedFile(generatedSwaggerFileName)
114 By("writing swagger to " + gs)
115 command = exec.Command(binaryPath, gs)
116 command.Dir = workingDirectory
117 session, err = gexec.Start(command, GinkgoWriter, GinkgoWriter)
118 Expect(err).ShouldNot(HaveOccurred())
119 Eventually(session, timeoutSeconds).Should(gexec.Exit(0))
120
121 By("writing OpenAPI v3.0")
122 // Create the OpenAPI swagger builder.
123 binaryPath, berr = gexec.Build("./builder3/main.go")
124 Expect(berr).ShouldNot(HaveOccurred())
125
126 // Execute the builder, generating an OpenAPI swagger file with definitions.
127 gov3 := generatedFile(generatedOpenAPIv3FileName)
128 By("writing swagger to " + gov3)
129 command = exec.Command(binaryPath, gov3)
130 command.Dir = workingDirectory
131 session, err = gexec.Start(command, GinkgoWriter, GinkgoWriter)
132 Expect(err).ShouldNot(HaveOccurred())
133 Eventually(session, timeoutSeconds).Should(gexec.Exit(0))
134 })
135
136 var _ = AfterSuite(func() {
137 os.RemoveAll(tempDir)
138 gexec.CleanupBuildArtifacts()
139 })
140
141 var _ = Describe("Open API Definitions Generation", func() {
142 Describe("Validating generated code", func() {
143 It("Generated code should match golden files", func() {
144 // Diff the generated code against the golden code. Exit code should be zero.
145 command := exec.Command(
146 "diff", "-u",
147 goldenCodeFilePath,
148 generatedFile(generatedCodeFileName),
149 )
150 command.Dir = workingDirectory
151 session, err := gexec.Start(command, GinkgoWriter, GinkgoWriter)
152 Expect(err).ShouldNot(HaveOccurred())
153 Eventually(session, timeoutSeconds).Should(gexec.Exit(0))
154 })
155 })
156
157 Describe("Validating OpenAPI V2 Definition Generation", func() {
158 It("Generated OpenAPI swagger definitions should match golden files", func() {
159 // Diff the generated swagger against the golden swagger. Exit code should be zero.
160 command := exec.Command(
161 "diff", "-u",
162 testdataFile(goldenSwaggerFileName),
163 generatedFile(generatedSwaggerFileName),
164 )
165 command.Dir = workingDirectory
166 session, err := gexec.Start(command, GinkgoWriter, GinkgoWriter)
167 Expect(err).ShouldNot(HaveOccurred())
168 Eventually(session, timeoutSeconds).Should(gexec.Exit(0))
169 })
170 })
171
172 Describe("Validating OpenAPI V3 Definition Generation", func() {
173 It("Generated OpenAPI swagger definitions should match golden files", func() {
174 // Diff the generated swagger against the golden swagger. Exit code should be zero.
175 command := exec.Command(
176 "diff", "-u",
177 testdataFile(goldenOpenAPIv3Filename),
178 generatedFile(generatedOpenAPIv3FileName),
179 )
180 command.Dir = workingDirectory
181 session, err := gexec.Start(command, GinkgoWriter, GinkgoWriter)
182 Expect(err).ShouldNot(HaveOccurred())
183 Eventually(session, timeoutSeconds).Should(gexec.Exit(0))
184 })
185 })
186
187 Describe("Validating API Rule Violation Reporting", func() {
188 It("Generated API rule violations should match golden report files", func() {
189 // Diff the generated report against the golden report. Exit code should be zero.
190 command := exec.Command(
191 "diff", "-u",
192 testdataFile(goldenReportFileName),
193 generatedFile(generatedReportFileName),
194 )
195 command.Dir = workingDirectory
196 session, err := gexec.Start(command, GinkgoWriter, GinkgoWriter)
197 Expect(err).ShouldNot(HaveOccurred())
198 Eventually(session, timeoutSeconds).Should(gexec.Exit(0))
199 })
200 })
201 })