Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added UTs for admit/netdel.go #114

Merged
merged 1 commit into from
Jul 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/confman/confman.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func GetTenantConfig(danmClient danmclientset.Interface) (*danmtypes.TenantConfi
return nil, err
}
if reply == nil || len(reply.Items) == 0 {
return nil, errors.New("TenantNetworks cannot be created without provisioning a TenantConfig first!")
return nil, errors.New("no TenantConfigs exist int the cluster")
}
//TODO: do a namespace based selection later if one generic config does not suffice
return &reply.Items[0], nil
Expand Down
31 changes: 9 additions & 22 deletions test/stubs/danm/client_stub.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,32 @@ package danm

import (
client "github.com/nokia/danm/crd/client/clientset/versioned/typed/danm/v1"
danmtypes "github.com/nokia/danm/crd/apis/danm/v1"
"github.com/nokia/danm/test/utils"
rest "k8s.io/client-go/rest"
)

type TestArtifacts struct {
TestNets []danmtypes.DanmNet
TestEps []danmtypes.DanmEp
ReservedIps []ReservedIpsList
TestTconfs []danmtypes.TenantConfig
}

type ReservedIpsList struct {
NetworkId string
Reservations []Reservation
}

type Reservation struct {
Ip string
Set bool
}

type ClientStub struct {
Objects TestArtifacts
Objects utils.TestArtifacts
NetClient *NetClientStub
TconfClient *TconfClientStub
}

func (client *ClientStub) DanmNets(namespace string) client.DanmNetInterface {
if client.NetClient == nil {
client.NetClient = newNetClientStub(client.Objects.TestNets, client.Objects.ReservedIps)
}
return client.NetClient
return client.NetClient
}

func (client *ClientStub) DanmEps(namespace string) client.DanmEpInterface {
return newEpClientStub(client.Objects.TestEps)
}

func (client *ClientStub) TenantConfigs() client.TenantConfigInterface {
return newTconfClientStub(client.Objects.TestTconfs)
if client.TconfClient == nil {
client.TconfClient = newTconfClientStub(client.Objects.TestTconfs, client.Objects.ReservedVnis)
}
return client.TconfClient
}

func (client *ClientStub) TenantNetworks(namespace string) client.TenantNetworkInterface {
Expand All @@ -55,7 +42,7 @@ func (c *ClientStub) RESTClient() rest.Interface {
return nil
}

func newClientStub(ta TestArtifacts) *ClientStub {
func newClientStub(ta utils.TestArtifacts) *ClientStub {
return &ClientStub {
Objects: ta,
}
Expand Down
3 changes: 2 additions & 1 deletion test/stubs/danm/clientset_stub.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package danm
import (
discovery "k8s.io/client-go/discovery"
danmv1 "github.com/nokia/danm/crd/client/clientset/versioned/typed/danm/v1"
"github.com/nokia/danm/test/utils"
)

type ClientSetStub struct {
Expand All @@ -21,7 +22,7 @@ func (c *ClientSetStub) Discovery() discovery.DiscoveryInterface {
return nil
}

func NewClientSetStub(objects TestArtifacts) *ClientSetStub {
func NewClientSetStub(objects utils.TestArtifacts) *ClientSetStub {
var clientSet ClientSetStub
clientSet.DanmClient = newClientStub(objects)
return &clientSet
Expand Down
8 changes: 5 additions & 3 deletions test/stubs/danm/netclient_stub.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,20 @@ import (
"github.com/nokia/danm/pkg/bitarray"
"github.com/nokia/danm/pkg/datastructs"
"github.com/nokia/danm/pkg/ipam"
"github.com/nokia/danm/test/utils"
)

const (
magicVersion = "42"
)

type NetClientStub struct{
TestNets []danmtypes.DanmNet
ReservedIpsList []ReservedIpsList
ReservedIpsList []utils.ReservedIpsList
TimesUpdateWasCalled int
}

func newNetClientStub(nets []danmtypes.DanmNet, ips []ReservedIpsList) *NetClientStub {
func newNetClientStub(nets []danmtypes.DanmNet, ips []utils.ReservedIpsList) *NetClientStub {
return &NetClientStub{TestNets: nets, ReservedIpsList: ips}
}

Expand Down Expand Up @@ -102,6 +104,6 @@ func (netClient *NetClientStub) Patch(name string, pt types.PatchType, data []by
return nil, nil
}

func (netClient *NetClientStub) AddReservedIpsList(reservedIps []ReservedIpsList) {
func (netClient *NetClientStub) AddReservedIpsList(reservedIps []utils.ReservedIpsList) {
netClient.ReservedIpsList = reservedIps
}
60 changes: 42 additions & 18 deletions test/stubs/danm/tconfclient_stub.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,66 +2,90 @@ package danm

import (
"errors"
"strconv"
"strings"
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
danmtypes "github.com/nokia/danm/crd/apis/danm/v1"
"github.com/nokia/danm/pkg/bitarray"
"github.com/nokia/danm/test/utils"
types "k8s.io/apimachinery/pkg/types"
watch "k8s.io/apimachinery/pkg/watch"
)

type TconfClientStub struct{
testTconfs []danmtypes.TenantConfig
TestTconfs []danmtypes.TenantConfig
ReservedVnis []utils.ReservedVnisList
TimesUpdateWasCalled int
}

func newTconfClientStub(tconfs []danmtypes.TenantConfig) TconfClientStub {
return TconfClientStub{testTconfs: tconfs}
func newTconfClientStub(tconfs []danmtypes.TenantConfig, vnis []utils.ReservedVnisList) *TconfClientStub {
return &TconfClientStub{TestTconfs: tconfs, ReservedVnis: vnis}
}
func (tconfClient TconfClientStub) Create(obj *danmtypes.TenantConfig) (*danmtypes.TenantConfig, error) {

func (tconfClient *TconfClientStub) Create(obj *danmtypes.TenantConfig) (*danmtypes.TenantConfig, error) {
return nil, nil
}

func (tconfClient TconfClientStub) Update(obj *danmtypes.TenantConfig) (*danmtypes.TenantConfig, error) {
func (tconfClient *TconfClientStub) Update(obj *danmtypes.TenantConfig) (*danmtypes.TenantConfig, error) {
tconfClient.TimesUpdateWasCalled++
for _, vniReservation := range tconfClient.ReservedVnis {
for index, hostProfile := range obj.HostDevices {
if hostProfile.Name == vniReservation.ProfileName && hostProfile.VniType == vniReservation.VniType {
ba := bitarray.NewBitArrayFromBase64(hostProfile.Alloc)
for _, reservation := range vniReservation.Reservations {
if !ba.Get(uint32(reservation.Vni)) && reservation.Set {
return nil, errors.New("Reservation failure, VNI:" + strconv.Itoa(reservation.Vni) + " should have been reserved in TenantConfig:" + obj.ObjectMeta.Name + " profile no:" + strconv.Itoa(index))
}
if ba.Get(uint32(reservation.Vni)) && !reservation.Set {
return nil, errors.New("Reservation failure, VNI:" + strconv.Itoa(reservation.Vni) + " should have been free in TenantConfig:" + obj.ObjectMeta.Name + " profile no.:" + strconv.Itoa(index))
}
}
} else {
if hostProfile.Alloc != utils.ExhaustedAllocFor5k {
return nil, errors.New("Unexpected VNI was freed in Interface profile named:" + hostProfile.Name)
}
}
}
}
if strings.HasPrefix(obj.ObjectMeta.Name,"error") {
return nil, errors.New("here you go")
}
return &danmtypes.TenantConfig{}, nil
}

func (tconfClient TconfClientStub) Delete(name string, options *meta_v1.DeleteOptions) error {
func (tconfClient *TconfClientStub) Delete(name string, options *meta_v1.DeleteOptions) error {
return nil
}

func (tconfClient TconfClientStub) DeleteCollection(options *meta_v1.DeleteOptions, listOptions meta_v1.ListOptions) error {
func (tconfClient *TconfClientStub) DeleteCollection(options *meta_v1.DeleteOptions, listOptions meta_v1.ListOptions) error {
return nil
}

func (tconfClient TconfClientStub) Get(tconfNameName string, options meta_v1.GetOptions) (*danmtypes.TenantConfig, error) {
for _, tconf := range tconfClient.testTconfs {
func (tconfClient *TconfClientStub) Get(tconfNameName string, options meta_v1.GetOptions) (*danmtypes.TenantConfig, error) {
for _, tconf := range tconfClient.TestTconfs {
if tconf.ObjectMeta.Name == tconfNameName {
return &tconf, nil
}
}
return nil, nil
}

func (tconfClient TconfClientStub) Watch(opts meta_v1.ListOptions) (watch.Interface, error) {
func (tconfClient *TconfClientStub) Watch(opts meta_v1.ListOptions) (watch.Interface, error) {
watch := watch.NewEmptyWatch()
return watch, nil
}

func (tconfClient TconfClientStub) List(opts meta_v1.ListOptions) (*danmtypes.TenantConfigList, error) {
if tconfClient.testTconfs == nil {
func (tconfClient *TconfClientStub) List(opts meta_v1.ListOptions) (*danmtypes.TenantConfigList, error) {
if tconfClient.TestTconfs == nil {
return nil, nil
}
if strings.HasPrefix(tconfClient.testTconfs[0].ObjectMeta.Name,"error") {
if strings.HasPrefix(tconfClient.TestTconfs[0].ObjectMeta.Name,"error") && !strings.Contains(tconfClient.TestTconfs[0].ObjectMeta.Name,"update"){
return nil, errors.New("error happened")
}
tconfList := danmtypes.TenantConfigList{Items: tconfClient.testTconfs }
tconfList := danmtypes.TenantConfigList{Items: tconfClient.TestTconfs }
return &tconfList, nil
}

func (tconfClient TconfClientStub) Patch(name string, pt types.PatchType, data []byte, subresources ...string) (result *danmtypes.TenantConfig, err error) {
func (tconfClient *TconfClientStub) Patch(name string, pt types.PatchType, data []byte, subresources ...string) (result *danmtypes.TenantConfig, err error) {
return nil, nil
}

Loading