...

Source file src/github.com/linkerd/linkerd2/pkg/profiles/openapi_test.go

Documentation: github.com/linkerd/linkerd2/pkg/profiles

     1  package profiles
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/go-openapi/spec"
     7  	sp "github.com/linkerd/linkerd2/controller/gen/apis/serviceprofile/v1alpha2"
     8  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
     9  )
    10  
    11  func TestSwaggerToServiceProfile(t *testing.T) {
    12  	namespace := "myns"
    13  	name := "mysvc"
    14  	clusterDomain := "mycluster.local"
    15  
    16  	swagger := spec.Swagger{
    17  		SwaggerProps: spec.SwaggerProps{
    18  			Paths: &spec.Paths{
    19  				Paths: map[string]spec.PathItem{
    20  					"/authors/{id}": {
    21  						PathItemProps: spec.PathItemProps{
    22  							Post: &spec.Operation{
    23  								OperationProps: spec.OperationProps{
    24  									Responses: &spec.Responses{
    25  										ResponsesProps: spec.ResponsesProps{
    26  											StatusCodeResponses: map[int]spec.Response{
    27  												500: {},
    28  											},
    29  										},
    30  									},
    31  								},
    32  								VendorExtensible: spec.VendorExtensible{
    33  									Extensions: spec.Extensions{xLinkerdRetryable: true, xLinkerdTimeout: "60s"},
    34  								},
    35  							},
    36  						},
    37  					},
    38  					"/path/with/trailing/slash/": {
    39  						PathItemProps: spec.PathItemProps{
    40  							Get: &spec.Operation{},
    41  						},
    42  					},
    43  				},
    44  			},
    45  		},
    46  	}
    47  
    48  	expectedServiceProfile := sp.ServiceProfile{
    49  		TypeMeta: ServiceProfileMeta,
    50  		ObjectMeta: metav1.ObjectMeta{
    51  			Name:      name + "." + namespace + ".svc." + clusterDomain,
    52  			Namespace: namespace,
    53  		},
    54  		Spec: sp.ServiceProfileSpec{
    55  			Routes: []*sp.RouteSpec{
    56  				{
    57  					Name: "POST /authors/{id}",
    58  					Condition: &sp.RequestMatch{
    59  						PathRegex: "/authors/[^/]*",
    60  						Method:    "POST",
    61  					},
    62  					ResponseClasses: []*sp.ResponseClass{
    63  						{
    64  							Condition: &sp.ResponseMatch{
    65  								Status: &sp.Range{
    66  									Min: 500,
    67  									Max: 500,
    68  								},
    69  							},
    70  							IsFailure: true,
    71  						},
    72  					},
    73  					IsRetryable: true,
    74  					Timeout:     "60s",
    75  				},
    76  				{
    77  					Name: "GET /path/with/trailing/slash/",
    78  					Condition: &sp.RequestMatch{
    79  						PathRegex: "/path/with/trailing/slash/",
    80  						Method:    "GET",
    81  					},
    82  				},
    83  			},
    84  		},
    85  	}
    86  
    87  	actualServiceProfile := swaggerToServiceProfile(swagger, namespace, name, clusterDomain)
    88  
    89  	err := ServiceProfileYamlEquals(actualServiceProfile, expectedServiceProfile)
    90  	if err != nil {
    91  		t.Fatalf("ServiceProfiles are not equal: %v", err)
    92  	}
    93  }
    94  

View as plain text