166 lines
4.1 KiB
Go
166 lines
4.1 KiB
Go
// Copyright 2016-2020, Pulumi Corporation.
|
|
//
|
|
// 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 schema
|
|
|
|
import (
|
|
"encoding/json"
|
|
"io/ioutil"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func readSchemaFile(file string) (pkgSpec PackageSpec) {
|
|
// Read in, decode, and import the schema.
|
|
schemaBytes, err := ioutil.ReadFile(filepath.Join("..", "internal", "test", "testdata", file))
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
if err = json.Unmarshal(schemaBytes, &pkgSpec); err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
return pkgSpec
|
|
}
|
|
|
|
func TestImportSpec(t *testing.T) {
|
|
// Read in, decode, and import the schema.
|
|
pkgSpec := readSchemaFile("kubernetes.json")
|
|
|
|
pkg, err := ImportSpec(pkgSpec, nil)
|
|
if err != nil {
|
|
t.Errorf("ImportSpec() error = %v", err)
|
|
}
|
|
|
|
for _, r := range pkg.Resources {
|
|
assert.NotNil(t, r.Package, "expected resource %s to have an associated Package", r.Token)
|
|
}
|
|
}
|
|
|
|
var enumTests = []struct {
|
|
filename string
|
|
shouldError bool
|
|
expected *EnumType
|
|
}{
|
|
{"bad-enum-1.json", true, nil},
|
|
{"bad-enum-2.json", true, nil},
|
|
{"bad-enum-3.json", true, nil},
|
|
{"bad-enum-4.json", true, nil},
|
|
{"good-enum-1.json", false, &EnumType{
|
|
Token: "fake-provider:module1:Color",
|
|
ElementType: stringType,
|
|
Elements: []*Enum{
|
|
{Value: "Red"},
|
|
{Value: "Orange"},
|
|
{Value: "Yellow"},
|
|
{Value: "Green"},
|
|
},
|
|
}},
|
|
{"good-enum-2.json", false, &EnumType{
|
|
Token: "fake-provider:module1:Number",
|
|
ElementType: intType,
|
|
Elements: []*Enum{
|
|
{Value: int32(1), Name: "One"},
|
|
{Value: int32(2), Name: "Two"},
|
|
{Value: int32(3), Name: "Three"},
|
|
{Value: int32(6), Name: "Six"},
|
|
},
|
|
}},
|
|
{"good-enum-3.json", false, &EnumType{
|
|
Token: "fake-provider:module1:Boolean",
|
|
ElementType: boolType,
|
|
Elements: []*Enum{
|
|
{Value: true, Name: "One"},
|
|
{Value: false, Name: "Zero"},
|
|
},
|
|
}},
|
|
{"good-enum-4.json", false, &EnumType{
|
|
Token: "fake-provider:module1:Number2",
|
|
ElementType: numberType,
|
|
Comment: "what a great description",
|
|
Elements: []*Enum{
|
|
{Value: float64(1), Comment: "one", Name: "One"},
|
|
{Value: float64(2), Comment: "two", Name: "Two"},
|
|
{Value: 3.4, Comment: "3.4", Name: "ThreePointFour"},
|
|
{Value: float64(6), Comment: "six", Name: "Six"},
|
|
},
|
|
}},
|
|
}
|
|
|
|
func TestEnums(t *testing.T) {
|
|
for _, tt := range enumTests {
|
|
t.Run(tt.filename, func(t *testing.T) {
|
|
pkgSpec := readSchemaFile(filepath.Join("schema", tt.filename))
|
|
|
|
pkg, err := ImportSpec(pkgSpec, nil)
|
|
if tt.shouldError {
|
|
assert.Error(t, err)
|
|
} else {
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
result := pkg.Types[0]
|
|
assert.Equal(t, tt.expected, result)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestImportResourceRef(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
schemaFile string
|
|
wantErr bool
|
|
validator func(pkg *Package)
|
|
}{
|
|
{
|
|
"valid",
|
|
"simple-resource-schema/schema.json",
|
|
false,
|
|
func(pkg *Package) {
|
|
for _, r := range pkg.Resources {
|
|
if r.Token == "example::OtherResource" {
|
|
for _, p := range r.Properties {
|
|
if p.Name == "foo" {
|
|
assert.IsType(t, &ResourceType{}, p.Type)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
},
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
// Read in, decode, and import the schema.
|
|
schemaBytes, err := ioutil.ReadFile(
|
|
filepath.Join("..", "internal", "test", "testdata", tt.schemaFile))
|
|
assert.NoError(t, err)
|
|
|
|
var pkgSpec PackageSpec
|
|
err = json.Unmarshal(schemaBytes, &pkgSpec)
|
|
assert.NoError(t, err)
|
|
|
|
pkg, err := ImportSpec(pkgSpec, nil)
|
|
if (err != nil) != tt.wantErr {
|
|
t.Errorf("ImportSpec() error = %v, wantErr %v", err, tt.wantErr)
|
|
return
|
|
}
|
|
tt.validator(pkg)
|
|
})
|
|
}
|
|
}
|