How to use Test method of version Package

Best Gauge code snippet using version.Test

restmapper_test.go

Source:restmapper_test.go Github

copy

Full Screen

...16 "strings"17 "testing"18 "k8s.io/apimachinery/pkg/runtime/schema"19)20func TestRESTMapperVersionAndKindForResource(t *testing.T) {21 testGroup := "test.group"22 testVersion := "test"23 testGroupVersion := schema.GroupVersion{Group: testGroup, Version: testVersion}24 testCases := []struct {25 Resource schema.GroupVersionResource26 GroupVersionToRegister schema.GroupVersion27 ExpectedGVK schema.GroupVersionKind28 Err bool29 }{30 {Resource: schema.GroupVersionResource{Resource: "internalobjec"}, Err: true},31 {Resource: schema.GroupVersionResource{Resource: "internalObjec"}, Err: true},32 {Resource: schema.GroupVersionResource{Resource: "internalobject"}, ExpectedGVK: testGroupVersion.WithKind("InternalObject")},33 {Resource: schema.GroupVersionResource{Resource: "internalobjects"}, ExpectedGVK: testGroupVersion.WithKind("InternalObject")},34 }35 for i, testCase := range testCases {36 mapper := NewDefaultRESTMapper([]schema.GroupVersion{testGroupVersion})37 if len(testCase.ExpectedGVK.Kind) != 0 {38 mapper.Add(testCase.ExpectedGVK, RESTScopeNamespace)39 }40 actualGVK, err := mapper.KindFor(testCase.Resource)41 hasErr := err != nil42 if hasErr != testCase.Err {43 t.Errorf("%d: unexpected error behavior %t: %v", i, testCase.Err, err)44 continue45 }46 if err != nil {47 continue48 }49 if actualGVK != testCase.ExpectedGVK {50 t.Errorf("%d: unexpected version and kind: e=%s a=%s", i, testCase.ExpectedGVK, actualGVK)51 }52 }53}54func TestRESTMapperGroupForResource(t *testing.T) {55 testCases := []struct {56 Resource schema.GroupVersionResource57 GroupVersionKind schema.GroupVersionKind58 Err bool59 }{60 {Resource: schema.GroupVersionResource{Resource: "myObject"}, GroupVersionKind: schema.GroupVersionKind{Group: "testapi", Version: "test", Kind: "MyObject"}},61 {Resource: schema.GroupVersionResource{Resource: "myobject"}, GroupVersionKind: schema.GroupVersionKind{Group: "testapi2", Version: "test", Kind: "MyObject"}},62 {Resource: schema.GroupVersionResource{Resource: "myObje"}, Err: true, GroupVersionKind: schema.GroupVersionKind{Group: "testapi", Version: "test", Kind: "MyObject"}},63 {Resource: schema.GroupVersionResource{Resource: "myobje"}, Err: true, GroupVersionKind: schema.GroupVersionKind{Group: "testapi", Version: "test", Kind: "MyObject"}},64 }65 for i, testCase := range testCases {66 mapper := NewDefaultRESTMapper([]schema.GroupVersion{testCase.GroupVersionKind.GroupVersion()})67 mapper.Add(testCase.GroupVersionKind, RESTScopeNamespace)68 actualGVK, err := mapper.KindFor(testCase.Resource)69 if testCase.Err {70 if err == nil {71 t.Errorf("%d: expected error", i)72 }73 } else if err != nil {74 t.Errorf("%d: unexpected error: %v", i, err)75 } else if actualGVK != testCase.GroupVersionKind {76 t.Errorf("%d: expected group %q, got %q", i, testCase.GroupVersionKind, actualGVK)77 }78 }79}80func TestRESTMapperKindsFor(t *testing.T) {81 testCases := []struct {82 Name string83 PreferredOrder []schema.GroupVersion84 KindsToRegister []schema.GroupVersionKind85 PartialResourceToRequest schema.GroupVersionResource86 ExpectedKinds []schema.GroupVersionKind87 ExpectedKindErr string88 }{89 {90 // exact matches are preferred91 Name: "groups, with group exact",92 PreferredOrder: []schema.GroupVersion{93 {Group: "first-group-1", Version: "first-version"},94 {Group: "first-group", Version: "first-version"},95 },96 KindsToRegister: []schema.GroupVersionKind{97 {Group: "first-group-1", Version: "first-version", Kind: "my-kind"},98 {Group: "first-group", Version: "first-version", Kind: "my-kind"},99 },100 PartialResourceToRequest: schema.GroupVersionResource{Group: "first-group", Resource: "my-kind"},101 ExpectedKinds: []schema.GroupVersionKind{102 {Group: "first-group", Version: "first-version", Kind: "my-kind"},103 },104 },105 {106 // group prefixes work107 Name: "groups, with group prefix",108 PreferredOrder: []schema.GroupVersion{109 {Group: "second-group", Version: "first-version"},110 {Group: "first-group", Version: "first-version"},111 },112 KindsToRegister: []schema.GroupVersionKind{113 {Group: "first-group", Version: "first-version", Kind: "my-kind"},114 {Group: "second-group", Version: "first-version", Kind: "my-kind"},115 },116 PartialResourceToRequest: schema.GroupVersionResource{Group: "first", Resource: "my-kind"},117 ExpectedKinds: []schema.GroupVersionKind{118 {Group: "first-group", Version: "first-version", Kind: "my-kind"},119 },120 },121 {122 // group prefixes can be ambiguous123 Name: "groups, with ambiguous group prefix",124 PreferredOrder: []schema.GroupVersion{125 {Group: "first-group-1", Version: "first-version"},126 {Group: "first-group", Version: "first-version"},127 },128 KindsToRegister: []schema.GroupVersionKind{129 {Group: "first-group-1", Version: "first-version", Kind: "my-kind"},130 {Group: "first-group", Version: "first-version", Kind: "my-kind"},131 },132 PartialResourceToRequest: schema.GroupVersionResource{Group: "first", Resource: "my-kind"},133 ExpectedKinds: []schema.GroupVersionKind{134 {Group: "first-group-1", Version: "first-version", Kind: "my-kind"},135 {Group: "first-group", Version: "first-version", Kind: "my-kind"},136 },137 ExpectedKindErr: " matches multiple kinds ",138 },139 {140 Name: "ambiguous groups, with preference order",141 PreferredOrder: []schema.GroupVersion{142 {Group: "second-group", Version: "first-version"},143 {Group: "first-group", Version: "first-version"},144 },145 KindsToRegister: []schema.GroupVersionKind{146 {Group: "first-group", Version: "first-version", Kind: "my-kind"},147 {Group: "first-group", Version: "first-version", Kind: "your-kind"},148 {Group: "second-group", Version: "first-version", Kind: "my-kind"},149 {Group: "second-group", Version: "first-version", Kind: "your-kind"},150 },151 PartialResourceToRequest: schema.GroupVersionResource{Resource: "my-kinds"},152 ExpectedKinds: []schema.GroupVersionKind{153 {Group: "second-group", Version: "first-version", Kind: "my-kind"},154 {Group: "first-group", Version: "first-version", Kind: "my-kind"},155 },156 ExpectedKindErr: " matches multiple kinds ",157 },158 {159 Name: "ambiguous groups, with explicit group match",160 PreferredOrder: []schema.GroupVersion{161 {Group: "second-group", Version: "first-version"},162 {Group: "first-group", Version: "first-version"},163 },164 KindsToRegister: []schema.GroupVersionKind{165 {Group: "first-group", Version: "first-version", Kind: "my-kind"},166 {Group: "first-group", Version: "first-version", Kind: "your-kind"},167 {Group: "second-group", Version: "first-version", Kind: "my-kind"},168 {Group: "second-group", Version: "first-version", Kind: "your-kind"},169 },170 PartialResourceToRequest: schema.GroupVersionResource{Group: "first-group", Resource: "my-kinds"},171 ExpectedKinds: []schema.GroupVersionKind{172 {Group: "first-group", Version: "first-version", Kind: "my-kind"},173 },174 },175 {176 Name: "ambiguous groups, with ambiguous version match",177 PreferredOrder: []schema.GroupVersion{178 {Group: "first-group", Version: "first-version"},179 {Group: "second-group", Version: "first-version"},180 },181 KindsToRegister: []schema.GroupVersionKind{182 {Group: "first-group", Version: "first-version", Kind: "my-kind"},183 {Group: "first-group", Version: "first-version", Kind: "your-kind"},184 {Group: "second-group", Version: "first-version", Kind: "my-kind"},185 {Group: "second-group", Version: "first-version", Kind: "your-kind"},186 },187 PartialResourceToRequest: schema.GroupVersionResource{Version: "first-version", Resource: "my-kinds"},188 ExpectedKinds: []schema.GroupVersionKind{189 {Group: "first-group", Version: "first-version", Kind: "my-kind"},190 {Group: "second-group", Version: "first-version", Kind: "my-kind"},191 },192 ExpectedKindErr: " matches multiple kinds ",193 },194 }195 for _, testCase := range testCases {196 tcName := testCase.Name197 mapper := NewDefaultRESTMapper(testCase.PreferredOrder)198 for _, kind := range testCase.KindsToRegister {199 mapper.Add(kind, RESTScopeNamespace)200 }201 actualKinds, err := mapper.KindsFor(testCase.PartialResourceToRequest)202 if err != nil {203 t.Errorf("%s: unexpected error: %v", tcName, err)204 continue205 }206 if !reflect.DeepEqual(testCase.ExpectedKinds, actualKinds) {207 t.Errorf("%s: expected %v, got %v", tcName, testCase.ExpectedKinds, actualKinds)208 }209 singleKind, err := mapper.KindFor(testCase.PartialResourceToRequest)210 if err == nil && len(testCase.ExpectedKindErr) != 0 {211 t.Errorf("%s: expected error: %v", tcName, testCase.ExpectedKindErr)212 continue213 }214 if err != nil {215 if len(testCase.ExpectedKindErr) == 0 {216 t.Errorf("%s: unexpected error: %v", tcName, err)217 continue218 } else {219 if !strings.Contains(err.Error(), testCase.ExpectedKindErr) {220 t.Errorf("%s: expected %v, got %v", tcName, testCase.ExpectedKindErr, err)221 continue222 }223 }224 } else {225 if testCase.ExpectedKinds[0] != singleKind {226 t.Errorf("%s: expected %v, got %v", tcName, testCase.ExpectedKinds[0], singleKind)227 }228 }229 }230}231func TestRESTMapperResourcesFor(t *testing.T) {232 testCases := []struct {233 Name string234 PreferredOrder []schema.GroupVersion235 KindsToRegister []schema.GroupVersionKind236 PluralPartialResourceToRequest schema.GroupVersionResource237 SingularPartialResourceToRequest schema.GroupVersionResource238 ExpectedResources []schema.GroupVersionResource239 ExpectedResourceErr string240 }{241 {242 // exact matches are preferred243 Name: "groups, with group exact",244 PreferredOrder: []schema.GroupVersion{245 {Group: "first-group-1", Version: "first-version"},246 {Group: "first-group", Version: "first-version"},247 },248 KindsToRegister: []schema.GroupVersionKind{249 {Group: "first-group-1", Version: "first-version", Kind: "my-kind"},250 {Group: "first-group", Version: "first-version", Kind: "my-kind"},251 },252 PluralPartialResourceToRequest: schema.GroupVersionResource{Group: "first-group", Resource: "my-kinds"},253 SingularPartialResourceToRequest: schema.GroupVersionResource{Group: "first-group", Resource: "my-kind"},254 ExpectedResources: []schema.GroupVersionResource{255 {Group: "first-group", Version: "first-version", Resource: "my-kinds"},256 },257 },258 {259 // group prefixes work260 Name: "groups, with group prefix",261 PreferredOrder: []schema.GroupVersion{262 {Group: "second-group", Version: "first-version"},263 {Group: "first-group", Version: "first-version"},264 },265 KindsToRegister: []schema.GroupVersionKind{266 {Group: "first-group", Version: "first-version", Kind: "my-kind"},267 {Group: "second-group", Version: "first-version", Kind: "my-kind"},268 },269 PluralPartialResourceToRequest: schema.GroupVersionResource{Group: "first", Resource: "my-kinds"},270 SingularPartialResourceToRequest: schema.GroupVersionResource{Group: "first", Resource: "my-kind"},271 ExpectedResources: []schema.GroupVersionResource{272 {Group: "first-group", Version: "first-version", Resource: "my-kinds"},273 },274 },275 {276 // group prefixes can be ambiguous277 Name: "groups, with ambiguous group prefix",278 PreferredOrder: []schema.GroupVersion{279 {Group: "first-group-1", Version: "first-version"},280 {Group: "first-group", Version: "first-version"},281 },282 KindsToRegister: []schema.GroupVersionKind{283 {Group: "first-group-1", Version: "first-version", Kind: "my-kind"},284 {Group: "first-group", Version: "first-version", Kind: "my-kind"},285 },286 PluralPartialResourceToRequest: schema.GroupVersionResource{Group: "first", Resource: "my-kinds"},287 SingularPartialResourceToRequest: schema.GroupVersionResource{Group: "first", Resource: "my-kind"},288 ExpectedResources: []schema.GroupVersionResource{289 {Group: "first-group-1", Version: "first-version", Resource: "my-kinds"},290 {Group: "first-group", Version: "first-version", Resource: "my-kinds"},291 },292 ExpectedResourceErr: " matches multiple resources ",293 },294 {295 Name: "ambiguous groups, with preference order",296 PreferredOrder: []schema.GroupVersion{297 {Group: "second-group", Version: "first-version"},298 {Group: "first-group", Version: "first-version"},299 },300 KindsToRegister: []schema.GroupVersionKind{301 {Group: "first-group", Version: "first-version", Kind: "my-kind"},302 {Group: "first-group", Version: "first-version", Kind: "your-kind"},303 {Group: "second-group", Version: "first-version", Kind: "my-kind"},304 {Group: "second-group", Version: "first-version", Kind: "your-kind"},305 },306 PluralPartialResourceToRequest: schema.GroupVersionResource{Resource: "my-kinds"},307 SingularPartialResourceToRequest: schema.GroupVersionResource{Resource: "my-kind"},308 ExpectedResources: []schema.GroupVersionResource{309 {Group: "second-group", Version: "first-version", Resource: "my-kinds"},310 {Group: "first-group", Version: "first-version", Resource: "my-kinds"},311 },312 ExpectedResourceErr: " matches multiple resources ",313 },314 {315 Name: "ambiguous groups, with explicit group match",316 PreferredOrder: []schema.GroupVersion{317 {Group: "second-group", Version: "first-version"},318 {Group: "first-group", Version: "first-version"},319 },320 KindsToRegister: []schema.GroupVersionKind{321 {Group: "first-group", Version: "first-version", Kind: "my-kind"},322 {Group: "first-group", Version: "first-version", Kind: "your-kind"},323 {Group: "second-group", Version: "first-version", Kind: "my-kind"},324 {Group: "second-group", Version: "first-version", Kind: "your-kind"},325 },326 PluralPartialResourceToRequest: schema.GroupVersionResource{Group: "first-group", Resource: "my-kinds"},327 SingularPartialResourceToRequest: schema.GroupVersionResource{Group: "first-group", Resource: "my-kind"},328 ExpectedResources: []schema.GroupVersionResource{329 {Group: "first-group", Version: "first-version", Resource: "my-kinds"},330 },331 },332 {333 Name: "ambiguous groups, with ambiguous version match",334 PreferredOrder: []schema.GroupVersion{335 {Group: "first-group", Version: "first-version"},336 {Group: "second-group", Version: "first-version"},337 },338 KindsToRegister: []schema.GroupVersionKind{339 {Group: "first-group", Version: "first-version", Kind: "my-kind"},340 {Group: "first-group", Version: "first-version", Kind: "your-kind"},341 {Group: "second-group", Version: "first-version", Kind: "my-kind"},342 {Group: "second-group", Version: "first-version", Kind: "your-kind"},343 },344 PluralPartialResourceToRequest: schema.GroupVersionResource{Version: "first-version", Resource: "my-kinds"},345 SingularPartialResourceToRequest: schema.GroupVersionResource{Version: "first-version", Resource: "my-kind"},346 ExpectedResources: []schema.GroupVersionResource{347 {Group: "first-group", Version: "first-version", Resource: "my-kinds"},348 {Group: "second-group", Version: "first-version", Resource: "my-kinds"},349 },350 ExpectedResourceErr: " matches multiple resources ",351 },352 }353 for _, testCase := range testCases {354 tcName := testCase.Name355 for _, partialResource := range []schema.GroupVersionResource{testCase.PluralPartialResourceToRequest, testCase.SingularPartialResourceToRequest} {356 mapper := NewDefaultRESTMapper(testCase.PreferredOrder)357 for _, kind := range testCase.KindsToRegister {358 mapper.Add(kind, RESTScopeNamespace)359 }360 actualResources, err := mapper.ResourcesFor(partialResource)361 if err != nil {362 t.Errorf("%s: unexpected error: %v", tcName, err)363 continue364 }365 if !reflect.DeepEqual(testCase.ExpectedResources, actualResources) {366 t.Errorf("%s: expected %v, got %v", tcName, testCase.ExpectedResources, actualResources)367 }368 singleResource, err := mapper.ResourceFor(partialResource)369 if err == nil && len(testCase.ExpectedResourceErr) != 0 {370 t.Errorf("%s: expected error: %v", tcName, testCase.ExpectedResourceErr)371 continue372 }373 if err != nil {374 if len(testCase.ExpectedResourceErr) == 0 {375 t.Errorf("%s: unexpected error: %v", tcName, err)376 continue377 } else {378 if !strings.Contains(err.Error(), testCase.ExpectedResourceErr) {379 t.Errorf("%s: expected %v, got %v", tcName, testCase.ExpectedResourceErr, err)380 continue381 }382 }383 } else {384 if testCase.ExpectedResources[0] != singleResource {385 t.Errorf("%s: expected %v, got %v", tcName, testCase.ExpectedResources[0], singleResource)386 }387 }388 }389 }390}391func TestKindToResource(t *testing.T) {392 testCases := []struct {393 Kind string394 Plural, Singular string395 }{396 {Kind: "Pod", Plural: "pods", Singular: "pod"},397 {Kind: "ReplicationController", Plural: "replicationcontrollers", Singular: "replicationcontroller"},398 // Add "ies" when ending with "y"399 {Kind: "ImageRepository", Plural: "imagerepositories", Singular: "imagerepository"},400 // Add "es" when ending with "s"401 {Kind: "miss", Plural: "misses", Singular: "miss"},402 // Add "s" otherwise403 {Kind: "lowercase", Plural: "lowercases", Singular: "lowercase"},404 }405 for i, testCase := range testCases {406 version := schema.GroupVersion{}407 plural, singular := UnsafeGuessKindToResource(version.WithKind(testCase.Kind))408 if singular != version.WithResource(testCase.Singular) || plural != version.WithResource(testCase.Plural) {409 t.Errorf("%d: unexpected plural and singular: %v %v", i, plural, singular)410 }411 }412}413func TestRESTMapperResourceSingularizer(t *testing.T) {414 testGroupVersion := schema.GroupVersion{Group: "tgroup", Version: "test"}415 testCases := []struct {416 Kind string417 Plural string418 Singular string419 }{420 {Kind: "Pod", Plural: "pods", Singular: "pod"},421 {Kind: "ReplicationController", Plural: "replicationcontrollers", Singular: "replicationcontroller"},422 {Kind: "ImageRepository", Plural: "imagerepositories", Singular: "imagerepository"},423 {Kind: "Status", Plural: "statuses", Singular: "status"},424 {Kind: "lowercase", Plural: "lowercases", Singular: "lowercase"},425 // TODO this test is broken. This updates to reflect actual behavior. Kinds are expected to be singular426 // old (incorrect), comment: Don't add extra s if the original object is already plural427 {Kind: "lowercases", Plural: "lowercaseses", Singular: "lowercases"},428 }429 for i, testCase := range testCases {430 mapper := NewDefaultRESTMapper([]schema.GroupVersion{testGroupVersion})431 // create singular/plural mapping432 mapper.Add(testGroupVersion.WithKind(testCase.Kind), RESTScopeNamespace)433 singular, err := mapper.ResourceSingularizer(testCase.Plural)434 if err != nil {435 t.Errorf("%d: unexpected error: %v", i, err)436 }437 if singular != testCase.Singular {438 t.Errorf("%d: mismatched singular: got %v, expected %v", i, singular, testCase.Singular)439 }440 }441}442func TestRESTMapperRESTMapping(t *testing.T) {443 testGroup := "tgroup"444 testGroupVersion := schema.GroupVersion{Group: testGroup, Version: "test"}445 internalGroupVersion := schema.GroupVersion{Group: testGroup, Version: "test"}446 testCases := []struct {447 Kind string448 APIGroupVersions []schema.GroupVersion449 DefaultVersions []schema.GroupVersion450 Resource schema.GroupVersionResource451 ExpectedGroupVersion *schema.GroupVersion452 Err bool453 }{454 {Kind: "Unknown", Err: true},455 {Kind: "InternalObject", Err: true},456 {DefaultVersions: []schema.GroupVersion{testGroupVersion}, Kind: "Unknown", Err: true},457 {DefaultVersions: []schema.GroupVersion{testGroupVersion}, Kind: "InternalObject", APIGroupVersions: []schema.GroupVersion{{Group: testGroup, Version: "test"}}, Resource: testGroupVersion.WithResource("internalobjects")},458 {DefaultVersions: []schema.GroupVersion{testGroupVersion}, Kind: "InternalObject", APIGroupVersions: []schema.GroupVersion{{Group: testGroup, Version: "test"}}, Resource: testGroupVersion.WithResource("internalobjects")},459 {DefaultVersions: []schema.GroupVersion{testGroupVersion}, Kind: "InternalObject", APIGroupVersions: []schema.GroupVersion{{Group: testGroup, Version: "test"}}, Resource: testGroupVersion.WithResource("internalobjects")},460 {DefaultVersions: []schema.GroupVersion{testGroupVersion}, Kind: "InternalObject", APIGroupVersions: []schema.GroupVersion{}, Resource: internalGroupVersion.WithResource("internalobjects"), ExpectedGroupVersion: &schema.GroupVersion{Group: testGroup, Version: "test"}},461 {DefaultVersions: []schema.GroupVersion{testGroupVersion}, Kind: "InternalObject", APIGroupVersions: []schema.GroupVersion{{Group: testGroup, Version: "test"}}, Resource: testGroupVersion.WithResource("internalobjects")},462 // TODO: add test for a resource that exists in one version but not another463 }464 for i, testCase := range testCases {465 mapper := NewDefaultRESTMapper(testCase.DefaultVersions)466 mapper.Add(internalGroupVersion.WithKind("InternalObject"), RESTScopeNamespace)467 preferredVersions := []string{}468 for _, gv := range testCase.APIGroupVersions {469 preferredVersions = append(preferredVersions, gv.Version)470 }471 gk := schema.GroupKind{Group: testGroup, Kind: testCase.Kind}472 mapping, err := mapper.RESTMapping(gk, preferredVersions...)473 hasErr := err != nil474 if hasErr != testCase.Err {475 t.Errorf("%d: unexpected error behavior %t: %v", i, testCase.Err, err)476 }477 if hasErr {478 continue479 }480 if mapping.Resource != testCase.Resource {481 t.Errorf("%d: unexpected resource: %#v", i, mapping)482 }483 groupVersion := testCase.ExpectedGroupVersion484 if groupVersion == nil {485 groupVersion = &testCase.APIGroupVersions[0]486 }487 if mapping.GroupVersionKind.GroupVersion() != *groupVersion {488 t.Errorf("%d: unexpected version: %#v", i, mapping)489 }490 }491}492func TestRESTMapperRESTMappingSelectsVersion(t *testing.T) {493 expectedGroupVersion1 := schema.GroupVersion{Group: "tgroup", Version: "test1"}494 expectedGroupVersion2 := schema.GroupVersion{Group: "tgroup", Version: "test2"}495 expectedGroupVersion3 := schema.GroupVersion{Group: "tgroup", Version: "test3"}496 internalObjectGK := schema.GroupKind{Group: "tgroup", Kind: "InternalObject"}497 otherObjectGK := schema.GroupKind{Group: "tgroup", Kind: "OtherObject"}498 mapper := NewDefaultRESTMapper([]schema.GroupVersion{expectedGroupVersion1, expectedGroupVersion2})499 mapper.Add(expectedGroupVersion1.WithKind("InternalObject"), RESTScopeNamespace)500 mapper.Add(expectedGroupVersion2.WithKind("OtherObject"), RESTScopeNamespace)501 // pick default matching object kind based on search order502 mapping, err := mapper.RESTMapping(otherObjectGK)503 if err != nil {504 t.Fatalf("unexpected error: %v", err)505 }506 if mapping.Resource != expectedGroupVersion2.WithResource("otherobjects") || mapping.GroupVersionKind.GroupVersion() != expectedGroupVersion2 {507 t.Errorf("unexpected mapping: %#v", mapping)508 }509 mapping, err = mapper.RESTMapping(internalObjectGK)510 if err != nil {511 t.Fatalf("unexpected error: %v", err)512 }513 if mapping.Resource != expectedGroupVersion1.WithResource("internalobjects") || mapping.GroupVersionKind.GroupVersion() != expectedGroupVersion1 {514 t.Errorf("unexpected mapping: %#v", mapping)515 }516 // mismatch of version517 mapping, err = mapper.RESTMapping(internalObjectGK, expectedGroupVersion2.Version)518 if err == nil {519 t.Errorf("unexpected non-error")520 }521 mapping, err = mapper.RESTMapping(otherObjectGK, expectedGroupVersion1.Version)522 if err == nil {523 t.Errorf("unexpected non-error")524 }525 // not in the search versions526 mapping, err = mapper.RESTMapping(otherObjectGK, expectedGroupVersion3.Version)527 if err == nil {528 t.Errorf("unexpected non-error")529 }530 // explicit search order531 mapping, err = mapper.RESTMapping(otherObjectGK, expectedGroupVersion3.Version, expectedGroupVersion1.Version)532 if err == nil {533 t.Errorf("unexpected non-error")534 }535 mapping, err = mapper.RESTMapping(otherObjectGK, expectedGroupVersion3.Version, expectedGroupVersion2.Version)536 if err != nil {537 t.Fatalf("unexpected error: %v", err)538 }539 if mapping.Resource != expectedGroupVersion2.WithResource("otherobjects") || mapping.GroupVersionKind.GroupVersion() != expectedGroupVersion2 {540 t.Errorf("unexpected mapping: %#v", mapping)541 }542}543func TestRESTMapperRESTMappings(t *testing.T) {544 testGroup := "tgroup"545 testGroupVersion := schema.GroupVersion{Group: testGroup, Version: "v1"}546 testCases := []struct {547 Kind string548 APIGroupVersions []schema.GroupVersion549 DefaultVersions []schema.GroupVersion550 AddGroupVersionKind []schema.GroupVersionKind551 ExpectedRESTMappings []*RESTMapping552 Err bool553 }{554 {Kind: "Unknown", Err: true},555 {Kind: "InternalObject", Err: true},556 {DefaultVersions: []schema.GroupVersion{testGroupVersion}, Kind: "Unknown", Err: true},557 // ask for specific version - not available - thus error558 {DefaultVersions: []schema.GroupVersion{testGroupVersion}, Kind: "InternalObject", APIGroupVersions: []schema.GroupVersion{{Group: testGroup, Version: "v2"}}, Err: true},559 // ask for specific version - available - check ExpectedRESTMappings560 {561 DefaultVersions: []schema.GroupVersion{testGroupVersion},562 Kind: "InternalObject",563 APIGroupVersions: []schema.GroupVersion{{Group: testGroup, Version: "v2"}},564 AddGroupVersionKind: []schema.GroupVersionKind{schema.GroupVersion{Group: testGroup, Version: "v2"}.WithKind("InternalObject")},565 ExpectedRESTMappings: []*RESTMapping{{Resource: schema.GroupVersionResource{Group: testGroup, Version: "v2", Resource: "internalobjects"}, GroupVersionKind: schema.GroupVersionKind{Group: testGroup, Version: "v2", Kind: "InternalObject"}}},566 },567 // ask for specific versions - only one available - check ExpectedRESTMappings568 {569 DefaultVersions: []schema.GroupVersion{testGroupVersion},570 Kind: "InternalObject",571 APIGroupVersions: []schema.GroupVersion{{Group: testGroup, Version: "v3"}, {Group: testGroup, Version: "v2"}},572 AddGroupVersionKind: []schema.GroupVersionKind{schema.GroupVersion{Group: testGroup, Version: "v2"}.WithKind("InternalObject")},573 ExpectedRESTMappings: []*RESTMapping{{Resource: schema.GroupVersionResource{Group: testGroup, Version: "v2", Resource: "internalobjects"}, GroupVersionKind: schema.GroupVersionKind{Group: testGroup, Version: "v2", Kind: "InternalObject"}}},574 },575 // do not ask for specific version - search through default versions - check ExpectedRESTMappings576 {577 DefaultVersions: []schema.GroupVersion{testGroupVersion, {Group: testGroup, Version: "v2"}},578 Kind: "InternalObject",579 AddGroupVersionKind: []schema.GroupVersionKind{schema.GroupVersion{Group: testGroup, Version: "v1"}.WithKind("InternalObject"), schema.GroupVersion{Group: testGroup, Version: "v2"}.WithKind("InternalObject")},580 ExpectedRESTMappings: []*RESTMapping{581 {582 Resource: schema.GroupVersionResource{Group: testGroup, Version: "v1", Resource: "internalobjects"},583 GroupVersionKind: schema.GroupVersionKind{Group: testGroup, Version: "v1", Kind: "InternalObject"},584 },585 {586 Resource: schema.GroupVersionResource{Group: testGroup, Version: "v2", Resource: "internalobjects"},587 GroupVersionKind: schema.GroupVersionKind{Group: testGroup, Version: "v2", Kind: "InternalObject"},588 },589 },590 },591 }592 for i, testCase := range testCases {593 mapper := NewDefaultRESTMapper(testCase.DefaultVersions)594 for _, gvk := range testCase.AddGroupVersionKind {595 mapper.Add(gvk, RESTScopeNamespace)596 }597 preferredVersions := []string{}598 for _, gv := range testCase.APIGroupVersions {599 preferredVersions = append(preferredVersions, gv.Version)600 }601 gk := schema.GroupKind{Group: testGroup, Kind: testCase.Kind}602 mappings, err := mapper.RESTMappings(gk, preferredVersions...)603 hasErr := err != nil604 if hasErr != testCase.Err {605 t.Errorf("%d: unexpected error behavior %t: %v", i, testCase.Err, err)606 }607 if hasErr {608 continue609 }610 if len(mappings) != len(testCase.ExpectedRESTMappings) {611 t.Errorf("%d: unexpected number = %d of rest mappings was returned, expected = %d", i, len(mappings), len(testCase.ExpectedRESTMappings))612 }613 for j, mapping := range mappings {614 exp := testCase.ExpectedRESTMappings[j]615 if mapping.Resource != exp.Resource {616 t.Errorf("%d - %d: unexpected resource: %#v", i, j, mapping)617 }618 if mapping.GroupVersionKind != exp.GroupVersionKind {619 t.Errorf("%d - %d: unexpected GroupVersionKind: %#v", i, j, mapping)620 }621 }622 }623}624func TestRESTMapperReportsErrorOnBadVersion(t *testing.T) {625 expectedGroupVersion1 := schema.GroupVersion{Group: "tgroup", Version: "test1"}626 expectedGroupVersion2 := schema.GroupVersion{Group: "tgroup", Version: "test2"}627 internalObjectGK := schema.GroupKind{Group: "tgroup", Kind: "InternalObject"}628 mapper := NewDefaultRESTMapper([]schema.GroupVersion{expectedGroupVersion1, expectedGroupVersion2})629 mapper.Add(expectedGroupVersion1.WithKind("InternalObject"), RESTScopeNamespace)630 _, err := mapper.RESTMapping(internalObjectGK, "test3")631 if err == nil {632 t.Errorf("unexpected non-error")633 }634}...

Full Screen

Full Screen

json_test.go

Source:json_test.go Github

copy

Full Screen

...66 out.Interface = in.Interface67 out.gvk = in.gvk68 return69}70func TestDecode(t *testing.T) {71 testCases := []struct {72 creater runtime.ObjectCreater73 typer runtime.ObjectTyper74 yaml bool75 pretty bool76 strict bool77 data []byte78 defaultGVK *schema.GroupVersionKind79 into runtime.Object80 errFn func(error) bool81 expectedObject runtime.Object82 expectedGVK *schema.GroupVersionKind83 }{84 {85 data: []byte("{}"),86 expectedGVK: &schema.GroupVersionKind{},87 errFn: func(err error) bool { return strings.Contains(err.Error(), "Object 'Kind' is missing in") },88 },89 {90 data: []byte("{}"),91 defaultGVK: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"},92 creater: &mockCreater{err: fmt.Errorf("fake error")},93 expectedGVK: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"},94 errFn: func(err error) bool { return err.Error() == "fake error" },95 },96 {97 data: []byte("{}"),98 defaultGVK: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"},99 creater: &mockCreater{obj: &testDecodable{}},100 expectedObject: &testDecodable{},101 expectedGVK: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"},102 },103 // version without group is not defaulted104 {105 data: []byte(`{"apiVersion":"blah"}`),106 defaultGVK: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"},107 creater: &mockCreater{obj: &testDecodable{}},108 expectedObject: &testDecodable{},109 expectedGVK: &schema.GroupVersionKind{Kind: "Test", Group: "", Version: "blah"},110 },111 // group without version is defaulted112 {113 data: []byte(`{"apiVersion":"other/"}`),114 defaultGVK: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"},115 creater: &mockCreater{obj: &testDecodable{}},116 expectedObject: &testDecodable{},117 expectedGVK: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"},118 },119 // group version, kind is defaulted120 {121 data: []byte(`{"apiVersion":"other1/blah1"}`),122 defaultGVK: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"},123 creater: &mockCreater{obj: &testDecodable{}},124 expectedObject: &testDecodable{},125 expectedGVK: &schema.GroupVersionKind{Kind: "Test", Group: "other1", Version: "blah1"},126 },127 // gvk all provided then not defaulted at all128 {129 data: []byte(`{"kind":"Test","apiVersion":"other/blah"}`),130 defaultGVK: &schema.GroupVersionKind{Kind: "Test1", Group: "other1", Version: "blah1"},131 creater: &mockCreater{obj: &testDecodable{}},132 expectedObject: &testDecodable{},133 expectedGVK: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"},134 },135 //gvk defaulting if kind not provided in data and defaultGVK use into's kind136 {137 data: []byte(`{"apiVersion":"b1/c1"}`),138 into: &testDecodable{gvk: schema.GroupVersionKind{Kind: "a3", Group: "b1", Version: "c1"}},139 typer: &mockTyper{gvk: &schema.GroupVersionKind{Kind: "a3", Group: "b1", Version: "c1"}},140 defaultGVK: nil,141 creater: &mockCreater{obj: &testDecodable{}},142 expectedObject: &testDecodable{gvk: schema.GroupVersionKind{Kind: "a3", Group: "b1", Version: "c1"}},143 expectedGVK: &schema.GroupVersionKind{Kind: "a3", Group: "b1", Version: "c1"},144 },145 // accept runtime.Unknown as into and bypass creator146 {147 data: []byte(`{}`),148 into: &runtime.Unknown{},149 expectedGVK: &schema.GroupVersionKind{},150 expectedObject: &runtime.Unknown{151 Raw: []byte(`{}`),152 ContentType: runtime.ContentTypeJSON,153 },154 },155 {156 data: []byte(`{"test":"object"}`),157 into: &runtime.Unknown{},158 expectedGVK: &schema.GroupVersionKind{},159 expectedObject: &runtime.Unknown{160 Raw: []byte(`{"test":"object"}`),161 ContentType: runtime.ContentTypeJSON,162 },163 },164 {165 data: []byte(`{"test":"object"}`),166 into: &runtime.Unknown{},167 defaultGVK: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"},168 expectedGVK: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"},169 expectedObject: &runtime.Unknown{170 TypeMeta: runtime.TypeMeta{APIVersion: "other/blah", Kind: "Test"},171 Raw: []byte(`{"test":"object"}`),172 ContentType: runtime.ContentTypeJSON,173 },174 },175 // unregistered objects can be decoded into directly176 {177 data: []byte(`{"kind":"Test","apiVersion":"other/blah","value":1,"Other":"test"}`),178 into: &testDecodable{},179 typer: &mockTyper{err: runtime.NewNotRegisteredErrForKind("mock", schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"})},180 expectedGVK: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"},181 expectedObject: &testDecodable{182 Other: "test",183 Value: 1,184 },185 },186 // registered types get defaulted by the into object kind187 {188 data: []byte(`{"value":1,"Other":"test"}`),189 into: &testDecodable{},190 typer: &mockTyper{gvk: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"}},191 expectedGVK: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"},192 expectedObject: &testDecodable{193 Other: "test",194 Value: 1,195 },196 },197 // registered types get defaulted by the into object kind even without version, but return an error198 {199 data: []byte(`{"value":1,"Other":"test"}`),200 into: &testDecodable{},201 typer: &mockTyper{gvk: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: ""}},202 expectedGVK: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: ""},203 errFn: func(err error) bool { return strings.Contains(err.Error(), "Object 'apiVersion' is missing in") },204 expectedObject: &testDecodable{205 Other: "test",206 Value: 1,207 },208 },209 // runtime.VersionedObjects are decoded210 {211 data: []byte(`{"value":1,"Other":"test"}`),212 into: &runtime.VersionedObjects{Objects: []runtime.Object{}},213 creater: &mockCreater{obj: &testDecodable{}},214 typer: &mockTyper{gvk: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"}},215 defaultGVK: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"},216 expectedGVK: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"},217 expectedObject: &runtime.VersionedObjects{218 Objects: []runtime.Object{219 &testDecodable{220 Other: "test",221 Value: 1,222 },223 },224 },225 },226 // runtime.VersionedObjects with an object are decoded into227 {228 data: []byte(`{"Other":"test"}`),229 into: &runtime.VersionedObjects{Objects: []runtime.Object{&testDecodable{Value: 2}}},230 typer: &mockTyper{gvk: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"}},231 expectedGVK: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"},232 expectedObject: &runtime.VersionedObjects{233 Objects: []runtime.Object{234 &testDecodable{235 Other: "test",236 Value: 2,237 },238 },239 },240 },241 // Error on invalid number242 {243 data: []byte(`{"kind":"Test","apiVersion":"other/blah","interface":1e1000}`),244 creater: &mockCreater{obj: &testDecodable{}},245 expectedGVK: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"},246 errFn: func(err error) bool {247 return strings.Contains(err.Error(), `json_test.testDecodable.Interface: DecodeNumber: strconv.ParseFloat: parsing "1e1000": value out of range`)248 },249 },250 // Unmarshalling is case-sensitive251 {252 // "VaLue" should have been "value"253 data: []byte(`{"kind":"Test","apiVersion":"other/blah","VaLue":1,"Other":"test"}`),254 into: &testDecodable{},255 typer: &mockTyper{err: runtime.NewNotRegisteredErrForKind("mock", schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"})},256 expectedGVK: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"},257 expectedObject: &testDecodable{258 Other: "test",259 },260 },261 // Unmarshalling is case-sensitive for big struct.262 {263 // "b" should have been "B", "I" should have been "i"264 data: []byte(`{"kind":"Test","apiVersion":"other/blah","spec": {"A": 1, "b": 2, "h": 3, "I": 4}}`),265 into: &testDecodable{},266 typer: &mockTyper{err: runtime.NewNotRegisteredErrForKind("mock", schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"})},267 expectedGVK: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"},268 expectedObject: &testDecodable{269 Spec: DecodableSpec{A: 1, H: 3},270 },271 },272 // Unknown fields should return an error from the strict JSON deserializer.273 {274 data: []byte(`{"unknown": 1}`),275 into: &testDecodable{},276 typer: &mockTyper{gvk: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"}},277 expectedGVK: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"},278 errFn: func(err error) bool {279 return strings.Contains(err.Error(), "found unknown field")280 },281 strict: true,282 },283 // Unknown fields should return an error from the strict YAML deserializer.284 {285 data: []byte("unknown: 1\n"),286 into: &testDecodable{},287 typer: &mockTyper{gvk: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"}},288 expectedGVK: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"},289 errFn: func(err error) bool {290 return strings.Contains(err.Error(), "found unknown field")291 },292 yaml: true,293 strict: true,294 },295 // Duplicate fields should return an error from the strict JSON deserializer.296 {297 data: []byte(`{"value":1,"value":1}`),298 into: &testDecodable{},299 typer: &mockTyper{gvk: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"}},300 expectedGVK: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"},301 errFn: func(err error) bool {302 return strings.Contains(err.Error(), "already set in map")303 },304 strict: true,305 },306 // Duplicate fields should return an error from the strict YAML deserializer.307 {308 data: []byte("value: 1\n" +309 "value: 1\n"),310 into: &testDecodable{},311 typer: &mockTyper{gvk: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"}},312 expectedGVK: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"},313 errFn: func(err error) bool {314 return strings.Contains(err.Error(), "already set in map")315 },316 yaml: true,317 strict: true,318 },319 // Strict JSON decode should fail for untagged fields.320 {321 data: []byte(`{"kind":"Test","apiVersion":"other/blah","value":1,"Other":"test"}`),322 into: &testDecodable{},323 typer: &mockTyper{gvk: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"}},324 expectedGVK: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"},325 errFn: func(err error) bool {326 return strings.Contains(err.Error(), "found unknown field")327 },328 strict: true,329 },330 // Strict YAML decode should fail for untagged fields.331 {332 data: []byte("kind: Test\n" +333 "apiVersion: other/blah\n" +334 "value: 1\n" +335 "Other: test\n"),336 into: &testDecodable{},337 typer: &mockTyper{gvk: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"}},338 expectedGVK: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"},339 errFn: func(err error) bool {340 return strings.Contains(err.Error(), "found unknown field")341 },342 yaml: true,343 strict: true,344 },345 // Strict JSON decode into unregistered objects directly.346 {347 data: []byte(`{"kind":"Test","apiVersion":"other/blah","value":1,"Other":"test"}`),348 into: &testDecodable{},349 typer: &mockTyper{err: runtime.NewNotRegisteredErrForKind("mock", schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"})},350 expectedGVK: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"},351 expectedObject: &testDecodable{352 Other: "test",353 Value: 1,354 },355 strict: true,356 },357 // Strict YAML decode into unregistered objects directly.358 {359 data: []byte("kind: Test\n" +360 "apiVersion: other/blah\n" +361 "value: 1\n" +362 "Other: test\n"),363 into: &testDecodable{},364 typer: &mockTyper{err: runtime.NewNotRegisteredErrForKind("mock", schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"})},365 expectedGVK: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"},366 expectedObject: &testDecodable{367 Other: "test",368 Value: 1,369 },370 yaml: true,371 strict: true,372 },373 // Valid strict JSON decode without GVK.374 {375 data: []byte(`{"value":1234}`),376 into: &testDecodable{},377 typer: &mockTyper{gvk: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"}},378 expectedGVK: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"},379 expectedObject: &testDecodable{380 Value: 1234,381 },382 strict: true,383 },384 // Valid strict YAML decode without GVK.385 {386 data: []byte("value: 1234\n"),387 into: &testDecodable{},388 typer: &mockTyper{gvk: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"}},389 expectedGVK: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"},390 expectedObject: &testDecodable{391 Value: 1234,392 },393 yaml: true,394 strict: true,395 },396 }397 for i, test := range testCases {398 var s runtime.Serializer399 if test.yaml {400 s = json.NewSerializerWithOptions(json.DefaultMetaFactory, test.creater, test.typer, json.SerializerOptions{Yaml: test.yaml, Pretty: false, Strict: test.strict})401 } else {402 s = json.NewSerializerWithOptions(json.DefaultMetaFactory, test.creater, test.typer, json.SerializerOptions{Yaml: test.yaml, Pretty: test.pretty, Strict: test.strict})403 }...

Full Screen

Full Screen

version_test.go

Source:version_test.go Github

copy

Full Screen

...4 "github.com/stretchr/testify/require"5 "github.com/cosmos/cosmos-sdk/x/ibc/03-connection/types"6 ibctesting "github.com/cosmos/cosmos-sdk/x/ibc/testing"7)8func TestValidateVersion(t *testing.T) {9 testCases := []struct {10 name string11 version types.Version12 expPass bool13 }{14 {"valid version", types.DefaultIBCVersion, true},15 {"valid empty feature set", types.NewVersion(types.DefaultIBCVersionIdentifier, []string{}), true},16 {"empty version identifier", types.NewVersion(" ", []string{"ORDER_UNORDERED"}), false},17 {"empty feature", types.NewVersion(types.DefaultIBCVersionIdentifier, []string{"ORDER_UNORDERED", " "}), false},18 }19 for i, tc := range testCases {20 encodedVersion, err := tc.version.Encode()21 require.NoError(t, err, "test case %d failed to marshal version string: %s", i, tc.name)22 err = types.ValidateVersion(encodedVersion)23 if tc.expPass {24 require.NoError(t, err, "valid test case %d failed: %s", i, tc.name)25 } else {26 require.Error(t, err, "invalid test case %d passed: %s", i, tc.name)27 }28 }29}30func TestDecodeVersion(t *testing.T) {31 testCases := []struct {32 name string33 version string34 expVersion types.Version35 expPass bool36 }{37 {"valid version", ibctesting.ConnectionVersion, types.DefaultIBCVersion, true},38 {"invalid version", "not a proto encoded version", types.Version{}, false},39 {"empty string", " ", types.Version{}, false},40 }41 for i, tc := range testCases {42 version, err := types.DecodeVersion(tc.version)43 if tc.expPass {44 require.NoError(t, err, "valid test case %d failed: %s", i, tc.name)45 require.Equal(t, tc.expVersion, version)46 } else {47 require.Error(t, err, "invalid test case %d passed: %s", i, tc.name)48 }49 }50}51func TestFindSupportedVersion(t *testing.T) {52 testCases := []struct {53 name string54 version types.Version55 supportedVersions []types.Version56 expVersion types.Version57 expFound bool58 }{59 {"valid supported version", types.DefaultIBCVersion, types.GetCompatibleVersions(), types.DefaultIBCVersion, true},60 {"empty (invalid) version", types.Version{}, types.GetCompatibleVersions(), types.Version{}, false},61 {"empty supported versions", types.DefaultIBCVersion, []types.Version{}, types.Version{}, false},62 {"desired version is last", types.DefaultIBCVersion, []types.Version{types.NewVersion("1.1", nil), types.NewVersion("2", []string{"ORDER_UNORDERED"}), types.NewVersion("3", nil), types.DefaultIBCVersion}, types.DefaultIBCVersion, true},63 {"desired version identifier with different feature set", types.NewVersion(types.DefaultIBCVersionIdentifier, []string{"ORDER_DAG"}), types.GetCompatibleVersions(), types.DefaultIBCVersion, true},64 {"version not supported", types.NewVersion("2", []string{"ORDER_DAG"}), types.GetCompatibleVersions(), types.Version{}, false},65 }66 for i, tc := range testCases {67 version, found := types.FindSupportedVersion(tc.version, tc.supportedVersions)68 require.Equal(t, tc.expVersion.GetIdentifier(), version.GetIdentifier(), "test case %d: %s", i, tc.name)69 require.Equal(t, tc.expFound, found, "test case %d: %s", i, tc.name)70 }71}72func TestPickVersion(t *testing.T) {73 testCases := []struct {74 name string75 counterpartyVersions []types.Version76 expVer types.Version77 expPass bool78 }{79 {"valid default ibc version", types.GetCompatibleVersions(), types.DefaultIBCVersion, true},80 {"valid version in counterparty versions", []types.Version{types.NewVersion("version1", nil), types.NewVersion("2.0.0", []string{"ORDER_UNORDERED-ZK"}), types.DefaultIBCVersion}, types.DefaultIBCVersion, true},81 {"valid identifier match but empty feature set not allowed", []types.Version{types.NewVersion(types.DefaultIBCVersionIdentifier, []string{"DAG", "ORDERED-ZK", "UNORDERED-zk]"})}, types.NewVersion(types.DefaultIBCVersionIdentifier, nil), false},82 {"empty counterparty versions", []types.Version{}, types.Version{}, false},83 {"non-matching counterparty versions", []types.Version{types.NewVersion("2.0.0", nil)}, types.Version{}, false},84 }85 for i, tc := range testCases {86 encodedCounterpartyVersions, err := types.EncodeVersions(tc.counterpartyVersions)87 require.NoError(t, err)88 encodedVersion, err := types.PickVersion(encodedCounterpartyVersions)89 if tc.expPass {90 require.NoError(t, err, "valid test case %d failed: %s", i, tc.name)91 version, err := types.DecodeVersion(encodedVersion)92 require.NoError(t, err)93 require.Equal(t, tc.expVer, version, "valid test case %d falied: %s", i, tc.name)94 } else {95 require.Error(t, err, "invalid test case %d passed: %s", i, tc.name)96 require.Equal(t, "", encodedVersion, "invalid test case %d passed: %s", i, tc.name)97 }98 }99}100func TestVerifyProposedVersion(t *testing.T) {101 testCases := []struct {102 name string103 proposedVersion types.Version104 supportedVersion types.Version105 expPass bool106 }{107 {"entire feature set supported", types.DefaultIBCVersion, types.NewVersion("1", []string{"ORDER_ORDERED", "ORDER_UNORDERED", "ORDER_DAG"}), true},108 {"empty feature sets not supported", types.NewVersion("1", []string{}), types.DefaultIBCVersion, false},109 {"one feature missing", types.DefaultIBCVersion, types.NewVersion("1", []string{"ORDER_UNORDERED", "ORDER_DAG"}), false},110 {"both features missing", types.DefaultIBCVersion, types.NewVersion("1", []string{"ORDER_DAG"}), false},111 {"identifiers do not match", types.NewVersion("2", []string{"ORDER_UNORDERED", "ORDER_ORDERED"}), types.DefaultIBCVersion, false},112 }113 for i, tc := range testCases {114 err := tc.supportedVersion.VerifyProposedVersion(tc.proposedVersion)115 if tc.expPass {116 require.NoError(t, err, "test case %d: %s", i, tc.name)117 } else {118 require.Error(t, err, "test case %d: %s", i, tc.name)119 }120 }121}122func TestVerifySupportedFeature(t *testing.T) {123 nilFeatures, err := types.NewVersion(types.DefaultIBCVersionIdentifier, nil).Encode()124 require.NoError(t, err)125 testCases := []struct {126 name string127 version string128 feature string129 expPass bool130 }{131 {"check ORDERED supported", ibctesting.ConnectionVersion, "ORDER_ORDERED", true},132 {"check UNORDERED supported", ibctesting.ConnectionVersion, "ORDER_UNORDERED", true},133 {"check DAG unsupported", ibctesting.ConnectionVersion, "ORDER_DAG", false},134 {"check empty feature set returns false", nilFeatures, "ORDER_ORDERED", false},135 {"failed to unmarshal version", "not an encoded version", "ORDER_ORDERED", false},136 }...

Full Screen

Full Screen

Test

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 v1 := version.NewVersion("1.0.0")4 v2 := version.NewVersion("1.0.1")5 v3 := version.NewVersion("1.0.0")6 v4 := version.NewVersion("1.0.1")7 fmt.Println(v1.Test(v2))8 fmt.Println(v2.Test(v1))9 fmt.Println(v1.Test(v3))10 fmt.Println(v3.Test(v1))11 fmt.Println(v2.Test(v4))12 fmt.Println(v4.Test(v2))13}

Full Screen

Full Screen

Test

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 v1 := version.NewVersion(1, 0, 0, "alpha")4 v2 := version.NewVersion(1, 0, 0, "beta")5 v3 := version.NewVersion(1, 0, 0, "beta")6 v4 := version.NewVersion(1, 0, 0, "alpha")7 v5 := version.NewVersion(1, 0, 0, "alpha")8 v6 := version.NewVersion(1, 0, 0, "beta")9 v7 := version.NewVersion(1, 0, 0, "beta")10 v8 := version.NewVersion(1, 0, 0, "alpha")11 fmt.Println(v1.Test(v2))12 fmt.Println(v3.Test(v4))13 fmt.Println(v5.Test(v6))14 fmt.Println(v7.Test(v8))15}

Full Screen

Full Screen

Test

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 v := version.New(1, 2, 3)4 fmt.Println(v)5 fmt.Println(v.Test())6}

Full Screen

Full Screen

Test

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 v1 := version.NewVersion("1.0")4 v2 := version.NewVersion("1.1")5 v3 := version.NewVersion("1.0")6 fmt.Println(v1.Test(v2, version.LessThan))7 fmt.Println(v1.Test(v3, version.EqualTo))8 fmt.Println(v1.Test(v2, version.GreaterThan))9 fmt.Println(v1.Test(v2, version.NotEqualTo))10 fmt.Println(v1.Test(v3, version.LessThanOrEqualTo))11 fmt.Println(v1.Test(v3, version.GreaterThanOrEqualTo))12}

Full Screen

Full Screen

Test

Using AI Code Generation

copy

Full Screen

1import (2func Test(t *testing.T) {3 v := Version{1, 2, 3, "beta", 1}4 fmt.Println(v.String())5}6import (7func Test(t *testing.T) {8 v := Version{1, 2, 3, "beta", 1}9 fmt.Println(v.String())10}11import (12func Test(t *testing.T) {13 v := Version{1, 2, 3, "beta", 1}14 fmt.Println(v.String())15}

Full Screen

Full Screen

Test

Using AI Code Generation

copy

Full Screen

1import "fmt"2func main() {3 ver := Version{major: 1, minor: 2, patch: 3}4 ver.Test()5}6import "fmt"7func (v Version) Test() {8 fmt.Println("Major Version: ", v.major)9 fmt.Println("Minor Version: ", v.minor)10 fmt.Println("Patch Version: ", v.patch)11}12type Version struct {13}

Full Screen

Full Screen

Test

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 v := version.New()4 v.SetVersion(1, 2, 3)5 if v.Test(1, 2, 3) {6 fmt.Println("version is greater than 1.2.3")7 } else {8 fmt.Println("version is not greater than 1.2.3")9 }10 if v.Test(1, 2, 2) {11 fmt.Println("version is greater than 1.2.2")12 } else {13 fmt.Println("version is not greater than 1.2.2")14 }15 if v.Test(1, 2, 4) {16 fmt.Println("version is greater than 1.2.4")17 } else {18 fmt.Println("version is not greater than 1.2.4")19 }20 if v.Test(2, 2, 4) {21 fmt.Println("version is greater than 2.2.4")22 } else {23 fmt.Println("version is not greater than 2.2.4")24 }25 if v.Test(1, 3, 3) {26 fmt.Println("version is greater than 1.3.3")27 } else {28 fmt.Println("version is not greater than 1.3.3")29 }30 if v.Test(2, 2, 4) {31 fmt.Println("version is greater than 2.2.4")32 } else {33 fmt.Println("version is not greater than 2.2.4")34 }35}

Full Screen

Full Screen

Test

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 eq := equalfile.New(nil, equalfile.Options{})4 if eq.Test(path1, path2) {5 fmt.Println("The two directories are equal")6 } else {7 fmt.Println("The two directories are not equal")8 }9}

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful