-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
45 additions
and
185 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,88 +1,52 @@ | ||
package openapi | ||
|
||
import ( | ||
"fmt" | ||
"context" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/hashicorp/go-multierror" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"github.com/deckhouse/d8-lint/pkg/errors" | ||
"github.com/deckhouse/d8-lint/pkg/manager" | ||
) | ||
|
||
func ValidateOpenAPI(path string) error { | ||
apiFiles, err := GetOpenAPIYAMLFiles(path) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
resultC := RunOpenAPIValidator(path, apiFiles) | ||
// OpenAPI linter | ||
type OpenAPI struct{} | ||
|
||
for result := range resultC { | ||
assert.NoError(t, result.validationError, "File '%s' has invalid spec", strings.TrimPrefix(result.filePath, deckhousePath)) | ||
func (*OpenAPI) Run(_ context.Context, m manager.Module) (errors.LintRuleErrorsList, error) { | ||
apiFiles, err := GetOpenAPIYAMLFiles(m.Path) | ||
if err != nil { | ||
return errors.LintRuleErrorsList{}, err | ||
} | ||
} | ||
|
||
// TestValidators test that validation hooks are working | ||
func TestValidators(t *testing.T) { | ||
apiFiles := []string{deckhousePath + "testing/openapi_validation/openapi_testdata/values.yaml"} | ||
|
||
filesC := make(chan fileValidation, len(apiFiles)) | ||
resultC := RunOpenAPIValidator(filesC) | ||
|
||
for _, apiFile := range apiFiles { | ||
filesC <- fileValidation{ | ||
filePath: apiFile, | ||
rootPath: m.Path, | ||
} | ||
} | ||
close(filesC) | ||
|
||
var result errors.LintRuleErrorsList | ||
for res := range resultC { | ||
assert.Error(t, res.validationError) | ||
err, ok := res.validationError.(*multierror.Error) | ||
require.True(t, ok) | ||
require.Len(t, err.Errors, 6) | ||
|
||
// we can't guarantee order here, thats why test contains | ||
assert.Contains(t, res.validationError.Error(), "properties.https is invalid: must have no default value") | ||
assert.Contains(t, res.validationError.Error(), "Enum 'properties.https.properties.mode.enum' is invalid: value 'disabled' must start with Capital letter") | ||
assert.Contains(t, res.validationError.Error(), "Enum 'properties.https.properties.mode.enum' is invalid: value: 'Cert-Manager' must be in CamelCase") | ||
assert.Contains(t, res.validationError.Error(), "Enum 'properties.https.properties.mode.enum' is invalid: value: 'Some:Thing' must be in CamelCase") | ||
assert.Contains(t, res.validationError.Error(), "Enum 'properties.https.properties.mode.enum' is invalid: value: 'Any.Thing' must be in CamelCase") | ||
assert.Contains(t, res.validationError.Error(), "properties.highAvailability is invalid: must have no default value") | ||
} | ||
} | ||
|
||
func TestCRDValidators(t *testing.T) { | ||
apiFiles := []string{deckhousePath + "testing/openapi_validation/openapi_testdata/crd.yaml"} | ||
|
||
filesC := make(chan fileValidation, len(apiFiles)) | ||
resultC := RunOpenAPIValidator(filesC) | ||
|
||
for _, apiFile := range apiFiles { | ||
filesC <- fileValidation{ | ||
filePath: apiFile, | ||
if res.validationError != nil { | ||
result.Add(errors.LintRuleError{ | ||
Text: res.validationError.Error(), | ||
ID: "openapi", | ||
ObjectID: strings.TrimPrefix(res.filePath, m.Path), | ||
Value: res.validationError, | ||
}) | ||
} | ||
} | ||
close(filesC) | ||
|
||
for res := range resultC { | ||
assert.Error(t, res.validationError) | ||
err, ok := res.validationError.(*multierror.Error) | ||
require.True(t, ok) | ||
require.Len(t, err.Errors, 1) | ||
return errors.LintRuleErrorsList{}, nil | ||
} | ||
|
||
// we can't guarantee order here, thats why test contains | ||
assert.Contains(t, res.validationError.Error(), "file validation error: wrong property") | ||
} | ||
func (*OpenAPI) Name() string { | ||
return "OpenAPI Linter" | ||
} | ||
|
||
func TestModulesVersionsValidation(t *testing.T) { | ||
mv, err := modulesVersions(deckhousePath) | ||
require.NoError(t, err) | ||
for m, v := range mv { | ||
message := fmt.Sprintf("conversions version(%d) and spec version(%d) for module %s are not equal", | ||
v.conversionsVersion, v.specVersion, m) | ||
assert.Equal(t, true, v.conversionsVersion == v.specVersion, message) | ||
} | ||
func (*OpenAPI) Desc() string { | ||
return "OpenAPI will check all openapi files in the module" | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.