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
|
/*
Copyright 2018 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package integration
import (
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"testing"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/gexec"
)
const (
testdataDir = "./testdata"
testPkgDir = "k8s.io/kube-openapi/test/integration/testdata"
inputDir = testPkgDir + "/listtype" +
"," + testPkgDir + "/maptype" +
"," + testPkgDir + "/structtype" +
"," + testPkgDir + "/dummytype" +
"," + testPkgDir + "/uniontype" +
"," + testPkgDir + "/enumtype" +
"," + testPkgDir + "/custom" +
"," + testPkgDir + "/defaults"
outputBase = "pkg"
outputPackage = "generated"
outputBaseFileName = "openapi_generated"
generatedSwaggerFileName = "generated.v2.json"
generatedReportFileName = "generated.v2.report"
goldenSwaggerFileName = "golden.v2.json"
goldenReportFileName = "golden.v2.report"
timeoutSeconds = 5.0
)
func TestGenerators(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Integration Test Suite")
}
var _ = Describe("Open API Definitions Generation", func() {
var (
workingDirectory string
tempDir string
terr error
openAPIGenPath string
)
testdataFile := func(filename string) string { return filepath.Join(testdataDir, filename) }
generatedFile := func(filename string) string { return filepath.Join(tempDir, filename) }
BeforeSuite(func() {
// Explicitly manage working directory
abs, err := filepath.Abs("")
Expect(err).ShouldNot(HaveOccurred())
workingDirectory = abs
// Create a temporary directory for generated swagger files.
tempDir, terr = ioutil.TempDir("./", "openapi")
Expect(terr).ShouldNot(HaveOccurred())
// Build the OpenAPI code generator.
By("building openapi-gen")
binary_path, berr := gexec.Build("../../cmd/openapi-gen/openapi-gen.go")
Expect(berr).ShouldNot(HaveOccurred())
openAPIGenPath = binary_path
// Run the OpenAPI code generator, creating OpenAPIDefinition code
// to be compiled into builder.
By("processing go idl with openapi-gen")
gr := generatedFile(generatedReportFileName)
command := exec.Command(openAPIGenPath,
"-i", inputDir,
"-o", outputBase,
"-p", outputPackage,
"-O", outputBaseFileName,
"-r", gr,
)
command.Dir = workingDirectory
session, err := gexec.Start(command, GinkgoWriter, GinkgoWriter)
Expect(err).ShouldNot(HaveOccurred())
Eventually(session, timeoutSeconds).Should(gexec.Exit(0))
By("writing swagger")
// Create the OpenAPI swagger builder.
binary_path, berr = gexec.Build("./builder/main.go")
Expect(berr).ShouldNot(HaveOccurred())
// Execute the builder, generating an OpenAPI swagger file with definitions.
gs := generatedFile(generatedSwaggerFileName)
By("writing swagger to " + gs)
command = exec.Command(binary_path, gs)
command.Dir = workingDirectory
session, err = gexec.Start(command, GinkgoWriter, GinkgoWriter)
Expect(err).ShouldNot(HaveOccurred())
Eventually(session, timeoutSeconds).Should(gexec.Exit(0))
})
AfterSuite(func() {
os.RemoveAll(tempDir)
gexec.CleanupBuildArtifacts()
})
Describe("openapi-gen --verify", func() {
It("Verifies that the existing files are correct", func() {
command := exec.Command(openAPIGenPath,
"-i", inputDir,
"-o", outputBase,
"-p", outputPackage,
"-O", outputBaseFileName,
"-r", testdataFile(goldenReportFileName),
"--verify-only",
)
command.Dir = workingDirectory
session, err := gexec.Start(command, GinkgoWriter, GinkgoWriter)
Expect(err).ShouldNot(HaveOccurred())
Eventually(session, timeoutSeconds).Should(gexec.Exit(0))
})
})
Describe("Validating OpenAPI Definition Generation", func() {
It("Generated OpenAPI swagger definitions should match golden files", func() {
// Diff the generated swagger against the golden swagger. Exit code should be zero.
command := exec.Command(
"diff",
testdataFile(goldenSwaggerFileName),
generatedFile(generatedSwaggerFileName),
)
command.Dir = workingDirectory
session, err := gexec.Start(command, GinkgoWriter, GinkgoWriter)
Expect(err).ShouldNot(HaveOccurred())
Eventually(session, timeoutSeconds).Should(gexec.Exit(0))
})
})
Describe("Validating API Rule Violation Reporting", func() {
It("Generated API rule violations should match golden report files", func() {
// Diff the generated report against the golden report. Exit code should be zero.
command := exec.Command(
"diff",
testdataFile(goldenReportFileName),
generatedFile(generatedReportFileName),
)
command.Dir = workingDirectory
session, err := gexec.Start(command, GinkgoWriter, GinkgoWriter)
Expect(err).ShouldNot(HaveOccurred())
Eventually(session, timeoutSeconds).Should(gexec.Exit(0))
})
})
})
|