istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pkg/config/schema/resource/schema_test.go (about)

     1  // Copyright Istio Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package resource
    16  
    17  import (
    18  	"testing"
    19  
    20  	. "github.com/onsi/gomega"
    21  	"google.golang.org/protobuf/reflect/protoreflect"
    22  	"google.golang.org/protobuf/types/known/emptypb"
    23  
    24  	"istio.io/istio/pkg/config"
    25  	"istio.io/istio/pkg/test/util/assert"
    26  )
    27  
    28  func TestValidate(t *testing.T) {
    29  	cases := []struct {
    30  		name        string
    31  		b           Builder
    32  		expectError bool
    33  	}{
    34  		{
    35  			name: "valid",
    36  			b: Builder{
    37  				Kind:         "Empty",
    38  				Plural:       "Empties",
    39  				ProtoPackage: "github.com/gogo/protobuf/types",
    40  				Proto:        "google.protobuf.Empty",
    41  			},
    42  			expectError: false,
    43  		},
    44  		{
    45  			name: "invalid kind",
    46  			b: Builder{
    47  				Kind:         "",
    48  				Plural:       "Empties",
    49  				ProtoPackage: "github.com/gogo/protobuf/types",
    50  				Proto:        "google.protobuf.Empty",
    51  			},
    52  			expectError: true,
    53  		},
    54  		{
    55  			name: "invalid plural",
    56  			b: Builder{
    57  				Kind:         "Empty",
    58  				Plural:       "",
    59  				ProtoPackage: "github.com/gogo/protobuf/types",
    60  				Proto:        "google.protobuf.Empty",
    61  			},
    62  			expectError: true,
    63  		},
    64  		{
    65  			name: "invalid proto",
    66  			b: Builder{
    67  				Kind:         "Boo",
    68  				Plural:       "Boos",
    69  				ProtoPackage: "github.com/gogo/protobuf/types",
    70  				Proto:        "boo",
    71  			},
    72  			expectError: true,
    73  		},
    74  	}
    75  
    76  	for _, c := range cases {
    77  		t.Run(c.name, func(t *testing.T) {
    78  			g := NewWithT(t)
    79  
    80  			err := c.b.BuildNoValidate().Validate()
    81  			if c.expectError {
    82  				g.Expect(err).ToNot(BeNil())
    83  			} else {
    84  				g.Expect(err).To(BeNil())
    85  			}
    86  		})
    87  	}
    88  }
    89  
    90  func TestBuild(t *testing.T) {
    91  	cases := []struct {
    92  		name        string
    93  		b           Builder
    94  		expectError bool
    95  	}{
    96  		{
    97  			name: "valid",
    98  			b: Builder{
    99  				Kind:         "Empty",
   100  				Plural:       "Empties",
   101  				ProtoPackage: "github.com/gogo/protobuf/types",
   102  				Proto:        "google.protobuf.Empty",
   103  			},
   104  			expectError: false,
   105  		},
   106  		{
   107  			name: "invalid kind",
   108  			b: Builder{
   109  				Kind:         "",
   110  				Plural:       "Empties",
   111  				ProtoPackage: "github.com/gogo/protobuf/types",
   112  				Proto:        "google.protobuf.Empty",
   113  			},
   114  			expectError: true,
   115  		},
   116  		{
   117  			name: "invalid plural",
   118  			b: Builder{
   119  				Kind:         "Empty",
   120  				Plural:       "",
   121  				ProtoPackage: "github.com/gogo/protobuf/types",
   122  				Proto:        "google.protobuf.Empty",
   123  			},
   124  			expectError: true,
   125  		},
   126  		{
   127  			name: "invalid proto",
   128  			b: Builder{
   129  				Kind:         "Boo",
   130  				Plural:       "Boos",
   131  				ProtoPackage: "github.com/gogo/protobuf/types",
   132  				Proto:        "boo",
   133  			},
   134  			expectError: true,
   135  		},
   136  	}
   137  
   138  	for _, c := range cases {
   139  		t.Run(c.name, func(t *testing.T) {
   140  			g := NewWithT(t)
   141  
   142  			_, err := c.b.Build()
   143  			if c.expectError {
   144  				g.Expect(err).ToNot(BeNil())
   145  			} else {
   146  				g.Expect(err).To(BeNil())
   147  			}
   148  		})
   149  	}
   150  }
   151  
   152  func TestCanonicalName(t *testing.T) {
   153  	cases := []struct {
   154  		name     string
   155  		s        Schema
   156  		expected string
   157  	}{
   158  		{
   159  			name: "group",
   160  			s: Builder{
   161  				Group:        "g",
   162  				Version:      "v",
   163  				Kind:         "k",
   164  				Plural:       "ks",
   165  				ProtoPackage: "github.com/gogo/protobuf/types",
   166  				Proto:        "google.protobuf.Empty",
   167  			}.MustBuild(),
   168  			expected: "g/v/k",
   169  		},
   170  		{
   171  			name: "no group",
   172  			s: Builder{
   173  				Group:        "",
   174  				Version:      "v",
   175  				Kind:         "k",
   176  				Plural:       "ks",
   177  				ProtoPackage: "github.com/gogo/protobuf/types",
   178  				Proto:        "google.protobuf.Empty",
   179  			}.MustBuild(),
   180  			expected: "core/v/k",
   181  		},
   182  	}
   183  
   184  	for _, c := range cases {
   185  		t.Run(c.name, func(t *testing.T) {
   186  			g := NewWithT(t)
   187  			g.Expect(c.s.GroupVersionKind().String()).To(Equal(c.expected))
   188  		})
   189  	}
   190  }
   191  
   192  func TestNewProtoInstance(t *testing.T) {
   193  	s := Builder{
   194  		Kind:         "Empty",
   195  		Plural:       "Empties",
   196  		ProtoPackage: "google.golang.org/protobuf/types/known/emptypb",
   197  		Proto:        "google.protobuf.Empty",
   198  	}.MustBuild()
   199  
   200  	p, err := s.NewInstance()
   201  	assert.NoError(t, err)
   202  	assert.Equal(t, p, config.Spec(&emptypb.Empty{}))
   203  }
   204  
   205  func TestMustNewProtoInstance_Panic_Nil(t *testing.T) {
   206  	g := NewWithT(t)
   207  	defer func() {
   208  		r := recover()
   209  		g.Expect(r).NotTo(BeNil())
   210  	}()
   211  	old := protoMessageType
   212  	defer func() {
   213  		protoMessageType = old
   214  	}()
   215  	protoMessageType = func(message protoreflect.FullName) (protoreflect.MessageType, error) {
   216  		return nil, nil
   217  	}
   218  
   219  	s := Builder{
   220  		Kind:         "Empty",
   221  		ProtoPackage: "github.com/gogo/protobuf/types",
   222  		Proto:        "google.protobuf.Empty",
   223  	}.MustBuild()
   224  
   225  	_ = s.MustNewInstance()
   226  }
   227  
   228  func TestString(t *testing.T) {
   229  	g := NewWithT(t)
   230  
   231  	s := Builder{
   232  		Kind:         "Empty",
   233  		Plural:       "Empties",
   234  		ProtoPackage: "github.com/gogo/protobuf/types",
   235  		Proto:        "google.protobuf.Empty",
   236  	}.MustBuild()
   237  
   238  	g.Expect(s.String()).To(Equal(`[Schema](Empty, "github.com/gogo/protobuf/types", google.protobuf.Empty)`))
   239  }