go.ligato.io/vpp-agent/v3@v3.5.0/plugins/vpp/handlers_test.go (about)

     1  //  Copyright (c) 2021 Cisco and/or its affiliates.
     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 vpp_test
    16  
    17  import (
    18  	"testing"
    19  
    20  	. "github.com/onsi/gomega"
    21  
    22  	"go.ligato.io/vpp-agent/v3/plugins/vpp"
    23  	"go.ligato.io/vpp-agent/v3/plugins/vpp/vppmock"
    24  )
    25  
    26  type testHandlerAPI interface {
    27  	A() error
    28  }
    29  
    30  type testHandler struct{}
    31  
    32  func (t *testHandler) A() error {
    33  	return nil
    34  }
    35  
    36  func TestRegisterHandler(t *testing.T) {
    37  	c := vppmock.SetupTestCtx(t)
    38  	vpp.ClearRegisteredHandlers()
    39  
    40  	handler := vpp.RegisterHandler(vpp.HandlerDesc{
    41  		Name:       "handlerA",
    42  		HandlerAPI: (*testHandlerAPI)(nil),
    43  	})
    44  
    45  	Expect(handler).ToNot(BeNil())
    46  	Expect(handler.Versions()).To(BeEmpty())
    47  	Expect(handler.FindCompatibleVersion(c.MockVPPClient)).To(BeNil())
    48  }
    49  
    50  func TestRegisterHandlerVersions(t *testing.T) {
    51  	c := vppmock.SetupTestCtx(t)
    52  	vpp.ClearRegisteredHandlers()
    53  
    54  	const (
    55  		version vpp.Version = "21.06-test"
    56  	)
    57  
    58  	handler := vpp.RegisterHandler(vpp.HandlerDesc{
    59  		Name:       "handlerA",
    60  		HandlerAPI: (*testHandlerAPI)(nil),
    61  	})
    62  	handler.AddVersion(vpp.HandlerVersion{
    63  		Version: version,
    64  		Check: func(client vpp.Client) error {
    65  			return nil
    66  		},
    67  		NewHandler: func(client vpp.Client, i ...interface{}) vpp.HandlerAPI {
    68  			return &testHandler{}
    69  		},
    70  	})
    71  
    72  	Expect(handler.Versions()).ToNot(BeEmpty())
    73  
    74  	ver := handler.FindCompatibleVersion(c.MockVPPClient)
    75  	Expect(ver).ToNot(BeNil())
    76  	Expect(ver.Version).To(Equal(version))
    77  	Expect(ver.NewHandler(c.MockVPPClient)).To(BeAssignableToTypeOf(&testHandler{}))
    78  }