mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-26 05:03:09 +00:00
Merge pull request #7560 from wojtek-t/conversion_test
Test whether auto-generated conversions weren't manually edited
This commit is contained in:
commit
dd976a27fb
@ -24,6 +24,7 @@ import (
|
|||||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/conversion"
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/conversion"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// AUTO-GENERATED FUNCTIONS START HERE
|
||||||
func convert_v1beta3_AWSElasticBlockStoreVolumeSource_To_api_AWSElasticBlockStoreVolumeSource(in *AWSElasticBlockStoreVolumeSource, out *newer.AWSElasticBlockStoreVolumeSource, s conversion.Scope) error {
|
func convert_v1beta3_AWSElasticBlockStoreVolumeSource_To_api_AWSElasticBlockStoreVolumeSource(in *AWSElasticBlockStoreVolumeSource, out *newer.AWSElasticBlockStoreVolumeSource, s conversion.Scope) error {
|
||||||
out.VolumeID = in.VolumeID
|
out.VolumeID = in.VolumeID
|
||||||
out.FSType = in.FSType
|
out.FSType = in.FSType
|
||||||
@ -2914,6 +2915,8 @@ func convert_api_VolumeSource_To_v1beta3_VolumeSource(in *newer.VolumeSource, ou
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// AUTO-GENERATED FUNCTIONS END HERE
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
err := newer.Scheme.AddGeneratedConversionFuncs(
|
err := newer.Scheme.AddGeneratedConversionFuncs(
|
||||||
convert_api_AWSElasticBlockStoreVolumeSource_To_v1beta3_AWSElasticBlockStoreVolumeSource,
|
convert_api_AWSElasticBlockStoreVolumeSource_To_v1beta3_AWSElasticBlockStoreVolumeSource,
|
||||||
|
151
pkg/conversion/code_generation_test.go
Normal file
151
pkg/conversion/code_generation_test.go
Normal file
@ -0,0 +1,151 @@
|
|||||||
|
/*
|
||||||
|
Copyright 2015 Google Inc. All rights reserved.
|
||||||
|
|
||||||
|
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 conversion_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"bytes"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"os"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
||||||
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/conversion"
|
||||||
|
|
||||||
|
"github.com/golang/glog"
|
||||||
|
)
|
||||||
|
|
||||||
|
func generateConversions(t *testing.T, version string) (bytes.Buffer, bytes.Buffer) {
|
||||||
|
g := conversion.NewGenerator(api.Scheme.Raw())
|
||||||
|
g.OverwritePackage(version, "")
|
||||||
|
g.OverwritePackage("api", "newer")
|
||||||
|
for _, knownType := range api.Scheme.KnownTypes(version) {
|
||||||
|
if err := g.GenerateConversionsForType(version, knownType); err != nil {
|
||||||
|
glog.Errorf("error while generating conversion functions for %v: %v", knownType, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var functions bytes.Buffer
|
||||||
|
functionsWriter := bufio.NewWriter(&functions)
|
||||||
|
if err := g.WriteConversionFunctions(functionsWriter); err != nil {
|
||||||
|
t.Fatalf("couldn't generate conversion functions: %v", err)
|
||||||
|
}
|
||||||
|
if err := functionsWriter.Flush(); err != nil {
|
||||||
|
t.Fatalf("error while flushing writer")
|
||||||
|
}
|
||||||
|
|
||||||
|
var names bytes.Buffer
|
||||||
|
namesWriter := bufio.NewWriter(&names)
|
||||||
|
if err := g.WriteConversionFunctionNames(namesWriter); err != nil {
|
||||||
|
t.Fatalf("couldn't generate conversion function names: %v", err)
|
||||||
|
}
|
||||||
|
if err := namesWriter.Flush(); err != nil {
|
||||||
|
t.Fatalf("error while flushing writer")
|
||||||
|
}
|
||||||
|
|
||||||
|
return functions, names
|
||||||
|
}
|
||||||
|
|
||||||
|
func readLinesUntil(t *testing.T, reader *bufio.Reader, stop string, buffer *bytes.Buffer) error {
|
||||||
|
for {
|
||||||
|
line, err := reader.ReadString('\n')
|
||||||
|
if err == io.EOF {
|
||||||
|
return fmt.Errorf("'%s' line not found", stop)
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("error while reading file: %v", err)
|
||||||
|
}
|
||||||
|
if line == stop {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if buffer != nil {
|
||||||
|
if _, err := buffer.WriteString(line); err != nil {
|
||||||
|
t.Fatalf("error while buffering line")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func bufferExistingConversions(t *testing.T, fileName string) (bytes.Buffer, bytes.Buffer) {
|
||||||
|
file, err := os.Open(fileName)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("couldn't open file %s", fileName)
|
||||||
|
}
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
|
reader := bufio.NewReader(file)
|
||||||
|
|
||||||
|
functionsPrefix := "// AUTO-GENERATED FUNCTIONS START HERE\n"
|
||||||
|
functionsSuffix := "// AUTO-GENERATED FUNCTIONS END HERE\n"
|
||||||
|
if err := readLinesUntil(t, reader, functionsPrefix, nil); err != nil {
|
||||||
|
t.Fatalf("error while parsing file: %v", err)
|
||||||
|
}
|
||||||
|
var functions bytes.Buffer
|
||||||
|
if err := readLinesUntil(t, reader, functionsSuffix, &functions); err != nil {
|
||||||
|
t.Fatalf("error while parsing file: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
functionNamesPrefix := "\terr := newer.Scheme.AddGeneratedConversionFuncs(\n"
|
||||||
|
functionNamesSuffix := "\t)\n"
|
||||||
|
if err := readLinesUntil(t, reader, functionNamesPrefix, nil); err != nil {
|
||||||
|
t.Fatalf("error while parsing file: %v", err)
|
||||||
|
}
|
||||||
|
var names bytes.Buffer
|
||||||
|
if err := readLinesUntil(t, reader, functionNamesSuffix, &names); err != nil {
|
||||||
|
t.Fatalf("error while parsing file: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return functions, names
|
||||||
|
}
|
||||||
|
|
||||||
|
func compareBuffers(t *testing.T, existing, generated bytes.Buffer) {
|
||||||
|
for {
|
||||||
|
existingLine, existingErr := existing.ReadString('\n')
|
||||||
|
generatedLine, generatedErr := generated.ReadString('\n')
|
||||||
|
if existingErr == io.EOF && generatedErr == io.EOF {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if existingErr != generatedErr {
|
||||||
|
t.Errorf("reading errors: existing %v generated %v", existingErr, generatedErr)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if existingErr != nil {
|
||||||
|
t.Errorf("error while reading: %v", existingErr)
|
||||||
|
}
|
||||||
|
if existingLine != generatedLine {
|
||||||
|
diff := fmt.Sprintf("first difference: expected %s, got %s", generatedLine, existingLine)
|
||||||
|
t.Errorf("please regenerate conversion functions; %s", diff)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestNoManualChangesToGenerateConversions(t *testing.T) {
|
||||||
|
versions := []string{"v1beta3"}
|
||||||
|
|
||||||
|
for _, version := range versions {
|
||||||
|
fileName := fmt.Sprintf("../../pkg/api/%s/conversion.go", version)
|
||||||
|
|
||||||
|
existingFunctions, existingNames := bufferExistingConversions(t, fileName)
|
||||||
|
generatedFunctions, generatedNames := generateConversions(t, version)
|
||||||
|
|
||||||
|
compareBuffers(t, existingFunctions, generatedFunctions)
|
||||||
|
compareBuffers(t, existingNames, generatedNames)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user