...

Source file src/sigs.k8s.io/kustomize/kyaml/fn/framework/parser/schema_test.go

Documentation: sigs.k8s.io/kustomize/kyaml/fn/framework/parser

     1  // Copyright 2021 The Kubernetes Authors.
     2  // SPDX-License-Identifier: Apache-2.0
     3  
     4  package parser_test
     5  
     6  import (
     7  	_ "embed"
     8  	iofs "io/fs"
     9  	"os"
    10  	"testing"
    11  
    12  	"github.com/stretchr/testify/assert"
    13  	"github.com/stretchr/testify/require"
    14  	"sigs.k8s.io/kustomize/kyaml/fn/framework/parser"
    15  )
    16  
    17  //go:embed testdata/schema1.json
    18  var schema1String string
    19  
    20  //go:embed testdata/schema2.json
    21  var schema2String string
    22  
    23  func TestSchemaFiles(t *testing.T) {
    24  	tests := []struct {
    25  		name          string
    26  		paths         []string
    27  		fs            iofs.FS
    28  		expectedCount int
    29  		wantErr       string
    30  	}{
    31  		{
    32  			name:          "parses schema from file",
    33  			paths:         []string{"testdata/schema1.json"},
    34  			expectedCount: 1,
    35  		},
    36  		{
    37  			name:          "accepts multiple inputs",
    38  			paths:         []string{"testdata/schema1.json", "testdata/schema2.json"},
    39  			expectedCount: 2,
    40  		},
    41  		{
    42  			name:          "parses templates from directory",
    43  			paths:         []string{"testdata"},
    44  			expectedCount: 2,
    45  		},
    46  		{
    47  			name:          "can be configured with an alternative FS",
    48  			fs:            os.DirFS("testdata"), // changes the root of the input paths
    49  			paths:         []string{"schema1.json"},
    50  			expectedCount: 1,
    51  		},
    52  		{
    53  			name:    "rejects non-.template.yaml files",
    54  			paths:   []string{"testdata/ignore.yaml"},
    55  			wantErr: "file testdata/ignore.yaml does not have any of permitted extensions [.json]",
    56  		},
    57  	}
    58  	for _, tt := range tests {
    59  		t.Run(tt.name, func(t *testing.T) {
    60  			p := parser.SchemaFiles(tt.paths...)
    61  			if tt.fs != nil {
    62  				p = p.FromFS(tt.fs)
    63  			}
    64  			schemas, err := p.Parse()
    65  			if tt.wantErr != "" {
    66  				require.EqualError(t, err, tt.wantErr)
    67  				return
    68  			}
    69  			require.NoError(t, err)
    70  			assert.Equal(t, tt.expectedCount, len(schemas))
    71  		})
    72  	}
    73  }
    74  
    75  func TestSchemaStrings(t *testing.T) {
    76  	tests := []struct {
    77  		name          string
    78  		data          []string
    79  		expectedCount int
    80  	}{
    81  		{
    82  			name:          "parses templates from strings",
    83  			data:          []string{schema1String},
    84  			expectedCount: 1,
    85  		},
    86  		{
    87  			name:          "accepts multiple inputs",
    88  			data:          []string{schema1String, schema2String},
    89  			expectedCount: 2,
    90  		},
    91  	}
    92  	for _, tt := range tests {
    93  		t.Run(tt.name, func(t *testing.T) {
    94  			p := parser.SchemaStrings(tt.data...)
    95  			schemas, err := p.Parse()
    96  			require.NoError(t, err)
    97  			assert.Equal(t, tt.expectedCount, len(schemas))
    98  		})
    99  	}
   100  }
   101  

View as plain text