| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 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 |
|
| 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 |
|
| 78 |
abs, err := filepath.Abs("") |
| 79 |
Expect(err).ShouldNot(HaveOccurred()) |
| 80 |
workingDirectory = abs |
| 81 |
|
| 82 |
|
| 83 |
tempDir, terr = os.MkdirTemp("./", "openapi") |
| 84 |
Expect(terr).ShouldNot(HaveOccurred()) |
| 85 |
|
| 86 |
|
| 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 |
|
| 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 |
|
| 109 |
binaryPath, berr = gexec.Build("./builder/main.go") |
| 110 |
Expect(berr).ShouldNot(HaveOccurred()) |
| 111 |
|
| 112 |
|
| 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 |
|
| 123 |
binaryPath, berr = gexec.Build("./builder3/main.go") |
| 124 |
Expect(berr).ShouldNot(HaveOccurred()) |
| 125 |
|
| 126 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
}) |