go.ligato.io/vpp-agent/v3@v3.5.0/pkg/models/registry_test.go (about)

     1  //  Copyright (c) 2019 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 models_test
    16  
    17  import (
    18  	"testing"
    19  
    20  	. "github.com/onsi/gomega"
    21  	"google.golang.org/protobuf/proto"
    22  
    23  	. "go.ligato.io/vpp-agent/v3/pkg/models"
    24  	testmodel "go.ligato.io/vpp-agent/v3/pkg/models/testdata/proto"
    25  )
    26  
    27  func ResetDefaultRegistry() {
    28  	DefaultRegistry = NewRegistry()
    29  }
    30  
    31  func TestRegister(t *testing.T) {
    32  	g := NewGomegaWithT(t)
    33  	ResetDefaultRegistry()
    34  
    35  	basicModel := Register(&testmodel.Basic{}, Spec{
    36  		Module:  "module",
    37  		Version: "v1",
    38  		Type:    "basic",
    39  		Class:   "config",
    40  	})
    41  
    42  	registered := RegisteredModels()
    43  	g.Expect(registered).To(HaveLen(1))
    44  
    45  	g.Expect(proto.Equal(registered[0].Spec().Proto(), basicModel.Spec().Proto())).To(BeTrue())
    46  }
    47  
    48  func TestRegisterDuplicate(t *testing.T) {
    49  	g := NewGomegaWithT(t)
    50  	ResetDefaultRegistry()
    51  
    52  	g.Expect(Register(&testmodel.Basic{}, Spec{
    53  		Module:  "module",
    54  		Version: "v1",
    55  		Type:    "basic",
    56  		Class:   "config",
    57  	})).ToNot(BeNil())
    58  	g.Expect(func() {
    59  		Register(&testmodel.Basic{}, Spec{
    60  			Module:  "module",
    61  			Version: "v1",
    62  			Type:    "basic2",
    63  			Class:   "config",
    64  		})
    65  	}).To(Panic())
    66  }
    67  
    68  func TestRegisterClassFallback(t *testing.T) {
    69  	g := NewGomegaWithT(t)
    70  	ResetDefaultRegistry()
    71  
    72  	Register(&testmodel.Basic{}, Spec{
    73  		Module:  "module",
    74  		Version: "v1",
    75  		Type:    "basic",
    76  		// Class is not set
    77  	})
    78  
    79  	model, err := GetModelFor(&testmodel.Basic{})
    80  	g.Expect(err).ToNot(HaveOccurred())
    81  	g.Expect(model.Spec().Class).To(Equal("config"))
    82  }
    83  
    84  func TestRegisterWithOption(t *testing.T) {
    85  	t.Skip("TODO")
    86  
    87  	g := NewGomegaWithT(t)
    88  	ResetDefaultRegistry()
    89  
    90  	//Register(&testmodel.WithOption{}, /*model spec defined in the proto*/)
    91  
    92  	model, err := GetModelFor(&testmodel.WithOption{})
    93  	g.Expect(err).ToNot(HaveOccurred())
    94  	g.Expect(model.Spec().Type).To(Equal("woption"))
    95  }