api/tests/validation_test.go

73 lines
1.9 KiB
Go

// Copyright Istio 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 crd
import (
"os"
"path/filepath"
"strings"
"testing"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"sigs.k8s.io/yaml"
)
type TestExpectation struct {
WantErr string `json:"_err,omitempty"`
}
func TestCRDs(t *testing.T) {
v := NewIstioValidator(t)
d, err := os.ReadDir("testdata")
if err != nil {
t.Fatal(err)
}
for _, f := range d {
t.Run(f.Name(), func(t *testing.T) {
f, err := os.ReadFile(filepath.Join("testdata", f.Name()))
if err != nil {
t.Fatal(err)
}
for _, item := range SplitString(string(f)) {
obj := &unstructured.Unstructured{}
if err := yaml.Unmarshal([]byte(item), obj); err != nil {
t.Fatal(err)
}
delete(obj.Object, "_err")
t.Run(obj.GetName(), func(t *testing.T) {
want := TestExpectation{}
if err := yaml.Unmarshal([]byte(item), &want); err != nil {
t.Fatal(err)
}
res := v.ValidateCustomResource(obj)
if want.WantErr == "" {
// Want no error
if res != nil {
t.Fatalf("configuration was invalid: %v", res)
}
} else {
if res == nil {
t.Fatalf("wanted error like %q, got none", want.WantErr)
}
if !strings.Contains(res.Error(), want.WantErr) {
t.Fatalf("wanted error like %q, got %q", want.WantErr, res)
}
}
})
}
})
}
}