knative.dev/pkg@v0.0.0-20260602142205-ac97e43f6622/apis/duck/v1/source_types_test.go (about)

     1  /*
     2  Copyright 2021 The Knative Authors
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package v1
    18  
    19  import (
    20  	"context"
    21  	"testing"
    22  
    23  	"knative.dev/pkg/apis"
    24  )
    25  
    26  func TestSourceValidate(t *testing.T) {
    27  	for _, tt := range []struct {
    28  		name string
    29  		src  *Source
    30  		want *apis.FieldError
    31  	}{{
    32  		name: "nil source validation",
    33  		src:  nil,
    34  		want: nil,
    35  	}, {
    36  		name: "nil source spec validation",
    37  		src:  &Source{},
    38  		want: apis.ErrGeneric("expected at least one, got none", "spec.sink.ref", "spec.sink.uri"),
    39  	}, {
    40  		name: "empty source spec validation",
    41  		src:  &Source{Spec: SourceSpec{}},
    42  		want: apis.ErrGeneric("expected at least one, got none", "spec.sink.ref", "spec.sink.uri"),
    43  	}, {
    44  		name: "empty source ceOverrides extensions validation",
    45  		src: &Source{Spec: SourceSpec{
    46  			Sink: Destination{
    47  				URI: apis.HTTP("localhost"),
    48  			},
    49  			CloudEventOverrides: &CloudEventOverrides{Extensions: map[string]string{}},
    50  		}},
    51  		want: nil,
    52  	}, {
    53  		name: "empty extension name error",
    54  		src: &Source{Spec: SourceSpec{
    55  			Sink: Destination{
    56  				URI: apis.HTTP("localhost"),
    57  			},
    58  			CloudEventOverrides: &CloudEventOverrides{Extensions: map[string]string{"": "test"}},
    59  		}},
    60  		want: apis.ErrInvalidKeyName(
    61  			"",
    62  			"spec.ceOverrides.extensions",
    63  			"keys MUST NOT be empty",
    64  		),
    65  	}, {
    66  		name: "long extension key name is valid",
    67  		src: &Source{Spec: SourceSpec{
    68  			Sink: Destination{
    69  				URI: apis.HTTP("localhost"),
    70  			},
    71  			CloudEventOverrides: &CloudEventOverrides{
    72  				Extensions: map[string]string{"nameLongerThan20Characters": "test"},
    73  			},
    74  		}},
    75  		want: nil,
    76  	}, {
    77  		name: "invalid extension name",
    78  		src: &Source{Spec: SourceSpec{
    79  			Sink: Destination{
    80  				URI: apis.HTTP("localhost"),
    81  			},
    82  			CloudEventOverrides: &CloudEventOverrides{Extensions: map[string]string{"invalid_name": "test"}},
    83  		}},
    84  		want: apis.ErrInvalidKeyName(
    85  			"invalid_name",
    86  			"spec.ceOverrides.extensions",
    87  			"keys are expected to be alphanumeric",
    88  		),
    89  	}, {
    90  		name: "valid extension name",
    91  		src: &Source{Spec: SourceSpec{
    92  			Sink: Destination{
    93  				URI: apis.HTTP("localhost"),
    94  			},
    95  			CloudEventOverrides: &CloudEventOverrides{
    96  				Extensions: map[string]string{"validName": "test"},
    97  			},
    98  		}},
    99  		want: nil,
   100  	}} {
   101  		t.Run(tt.name, func(t *testing.T) {
   102  			tt := tt
   103  			t.Parallel()
   104  			got := tt.src.Validate(context.TODO())
   105  			if tt.want != nil && got.Error() != tt.want.Error() {
   106  				t.Errorf("Unexpected error want:\n%+s\ngot:\n%+s", tt.want, got)
   107  			}
   108  
   109  			if tt.want == nil && got != nil {
   110  				t.Errorf("Unexpected error want:\nnil\ngot:\n%+s", got)
   111  			}
   112  		})
   113  	}
   114  }