Skip to content

Commit

Permalink
test: adding extra tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bl4ko committed Mar 12, 2024
1 parent 63ee1e0 commit 10c85b9
Show file tree
Hide file tree
Showing 12 changed files with 427 additions and 242 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ require (
github.com/jinzhu/copier v0.4.0 // indirect
github.com/magefile/mage v1.15.0 // indirect
github.com/rogpeppe/go-internal v1.12.0 // indirect
golang.org/x/net v0.21.0 // indirect
golang.org/x/sys v0.17.0 // indirect
golang.org/x/net v0.22.0 // indirect
golang.org/x/sys v0.18.0 // indirect
gopkg.in/djherbis/times.v1 v1.3.0 // indirect
)
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE=
golang.org/x/net v0.21.0 h1:AQyQV4dYCvJ7vGmJyKki9+PBdyvhkSd8EIx/qb0AYv4=
golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44=
golang.org/x/net v0.22.0 h1:9sGLhx7iRIHEiX0oAJ3MRZMUCElJgy7Br1nO+AMN3Tc=
golang.org/x/net v0.22.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
Expand All @@ -69,8 +69,8 @@ golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y=
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
Expand Down
32 changes: 32 additions & 0 deletions internal/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,35 @@ const (
MaxVID = 4094
TaggedVID = 4095
)

// Here all mappings are defined so we don't hardcode api paths of objects
// in our code.
const (
ContactGroupsAPIPath = "/api/tenancy/contact-groups/"
ContactRolesAPIPath = "/api/tenancy/contact-roles/"
ContactsAPIPath = "/api/tenancy/contacts/"
TenantsAPIPath = "/api/tenancy/tenants/"
ContactAssignmentsAPIPath = "/api/tenancy/contact-assignments/"

PrefixesAPIPath = "/api/ipam/prefixes/"
VlanGroupsAPIPath = "/api/ipam/vlan-groups/"
VlansAPIPath = "/api/ipam/vlans/"
IPAddressesAPIPath = "/api/ipam/ip-addresses/"

ClusterTypesAPIPath = "/api/virtualization/cluster-types/"
ClusterGroupsAPIPath = "/api/virtualization/cluster-groups/"
ClustersAPIPath = "/api/virtualization/clusters/"
VirtualMachinesAPIPath = "/api/virtualization/virtual-machines/"
VMInterfacesAPIPath = "/api/virtualization/interfaces/"

DevicesAPIPath = "/api/dcim/devices/"
DeviceRolesAPIPath = "/api/dcim/device-roles/"
DeviceTypesAPIPath = "/api/dcim/device-types/"
InterfacesAPIPath = "/api/dcim/interfaces/"
SitesAPIPath = "/api/dcim/sites/"
ManufacturersAPIPath = "/api/dcim/manufacturers/"
PlatformsAPIPath = "/api/dcim/platforms/"

CustomFieldsAPIPath = "/api/extras/custom-fields/"
TagsAPIPath = "/api/extras/tags/"
)
35 changes: 18 additions & 17 deletions internal/netbox/inventory/add_items.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package inventory
import (
"context"

"github.com/bl4ko/netbox-ssot/internal/constants"
"github.com/bl4ko/netbox-ssot/internal/netbox/objects"
"github.com/bl4ko/netbox-ssot/internal/netbox/service"
"github.com/bl4ko/netbox-ssot/internal/utils"
Expand Down Expand Up @@ -176,7 +177,7 @@ func (nbi *NetboxInventory) AddContact(ctx context.Context, newContact *objects.
defer nbi.ContactsLock.Unlock()
if _, ok := nbi.ContactsIndexByName[newContact.Name]; ok {
oldContact := nbi.ContactsIndexByName[newContact.Name]
delete(nbi.OrphanManager[service.ContactsAPIPath], oldContact.ID)
delete(nbi.OrphanManager[constants.ContactsAPIPath], oldContact.ID)
diffMap, err := utils.JSONDiffMapExceptID(newContact, oldContact, false, nbi.SourcePriority)
if err != nil {
return nil, err
Expand Down Expand Up @@ -219,7 +220,7 @@ func (nbi *NetboxInventory) AddContactAssignment(ctx context.Context, newCA *obj
newCA.Tags = append(newCA.Tags, nbi.SsotTag)
if _, ok := nbi.ContactAssignmentsIndexByContentTypeAndObjectIDAndContactIDAndRoleID[newCA.ContentType][newCA.ObjectID][newCA.Contact.ID][newCA.Role.ID]; ok {
oldCA := nbi.ContactAssignmentsIndexByContentTypeAndObjectIDAndContactIDAndRoleID[newCA.ContentType][newCA.ObjectID][newCA.Contact.ID][newCA.Role.ID]
delete(nbi.OrphanManager[service.ContactAssignmentsAPIPath], oldCA.ID)
delete(nbi.OrphanManager[constants.ContactAssignmentsAPIPath], oldCA.ID)
diffMap, err := utils.JSONDiffMapExceptID(newCA, oldCA, false, nbi.SourcePriority)
if err != nil {
return nil, err
Expand Down Expand Up @@ -282,7 +283,7 @@ func (nbi *NetboxInventory) AddClusterGroup(ctx context.Context, newCg *objects.
if _, ok := nbi.ClusterGroupsIndexByName[newCg.Name]; ok {
// Remove id from orphan manager, because it still exists in the sources
oldCg := nbi.ClusterGroupsIndexByName[newCg.Name]
delete(nbi.OrphanManager[service.ClusterGroupsAPIPath], oldCg.ID)
delete(nbi.OrphanManager[constants.ClusterGroupsAPIPath], oldCg.ID)
diffMap, err := utils.JSONDiffMapExceptID(newCg, oldCg, false, nbi.SourcePriority)
if err != nil {
return nil, err
Expand Down Expand Up @@ -316,7 +317,7 @@ func (nbi *NetboxInventory) AddClusterType(ctx context.Context, newClusterType *
if _, ok := nbi.ClusterTypesIndexByName[newClusterType.Name]; ok {
// Remove id from orphan manager, because it still exists in the sources
oldClusterType := nbi.ClusterTypesIndexByName[newClusterType.Name]
delete(nbi.OrphanManager[service.ClusterTypesAPIPath], oldClusterType.ID)
delete(nbi.OrphanManager[constants.ClusterTypesAPIPath], oldClusterType.ID)
diffMap, err := utils.JSONDiffMapExceptID(newClusterType, oldClusterType, false, nbi.SourcePriority)
if err != nil {
return nil, err
Expand Down Expand Up @@ -351,7 +352,7 @@ func (nbi *NetboxInventory) AddCluster(ctx context.Context, newCluster *objects.
if _, ok := nbi.ClustersIndexByName[newCluster.Name]; ok {
// Remove id from orphan manager, because it still exists in the sources
oldCluster := nbi.ClustersIndexByName[newCluster.Name]
delete(nbi.OrphanManager[service.ClustersAPIPath], oldCluster.ID)
delete(nbi.OrphanManager[constants.ClustersAPIPath], oldCluster.ID)
diffMap, err := utils.JSONDiffMapExceptID(newCluster, oldCluster, false, nbi.SourcePriority)
if err != nil {
return nil, err
Expand Down Expand Up @@ -384,7 +385,7 @@ func (nbi *NetboxInventory) AddDeviceRole(ctx context.Context, newDeviceRole *ob
if _, ok := nbi.DeviceRolesIndexByName[newDeviceRole.Name]; ok {
// Remove id from orphan manager, because it still exists in the sources
oldDeviceRole := nbi.DeviceRolesIndexByName[newDeviceRole.Name]
delete(nbi.OrphanManager[service.DeviceRolesAPIPath], nbi.DeviceRolesIndexByName[newDeviceRole.Name].ID)
delete(nbi.OrphanManager[constants.DeviceRolesAPIPath], nbi.DeviceRolesIndexByName[newDeviceRole.Name].ID)
diffMap, err := utils.JSONDiffMapExceptID(newDeviceRole, oldDeviceRole, false, nbi.SourcePriority)
if err != nil {
return nil, err
Expand Down Expand Up @@ -417,7 +418,7 @@ func (nbi *NetboxInventory) AddManufacturer(ctx context.Context, newManufacturer
if _, ok := nbi.ManufacturersIndexByName[newManufacturer.Name]; ok {
// Remove id from orphan manager, because it still exists in the sources
oldManufacturer := nbi.ManufacturersIndexByName[newManufacturer.Name]
delete(nbi.OrphanManager[service.ManufacturersAPIPath], oldManufacturer.ID)
delete(nbi.OrphanManager[constants.ManufacturersAPIPath], oldManufacturer.ID)
diffMap, err := utils.JSONDiffMapExceptID(newManufacturer, oldManufacturer, false, nbi.SourcePriority)
if err != nil {
return nil, err
Expand Down Expand Up @@ -450,7 +451,7 @@ func (nbi *NetboxInventory) AddDeviceType(ctx context.Context, newDeviceType *ob
if _, ok := nbi.DeviceTypesIndexByModel[newDeviceType.Model]; ok {
// Remove id from orphan manager, because it still exists in the sources
oldDeviceType := nbi.DeviceTypesIndexByModel[newDeviceType.Model]
delete(nbi.OrphanManager[service.DeviceTypesAPIPath], oldDeviceType.ID)
delete(nbi.OrphanManager[constants.DeviceTypesAPIPath], oldDeviceType.ID)
diffMap, err := utils.JSONDiffMapExceptID(newDeviceType, oldDeviceType, false, nbi.SourcePriority)
if err != nil {
return nil, err
Expand Down Expand Up @@ -483,7 +484,7 @@ func (nbi *NetboxInventory) AddPlatform(ctx context.Context, newPlatform *object
if _, ok := nbi.PlatformsIndexByName[newPlatform.Name]; ok {
// Remove id from orphan manager, because it still exists in the sources
oldPlatform := nbi.PlatformsIndexByName[newPlatform.Name]
delete(nbi.OrphanManager[service.PlatformsAPIPath], oldPlatform.ID)
delete(nbi.OrphanManager[constants.PlatformsAPIPath], oldPlatform.ID)
diffMap, err := utils.JSONDiffMapExceptID(newPlatform, oldPlatform, false, nbi.SourcePriority)
if err != nil {
return nil, err
Expand Down Expand Up @@ -515,7 +516,7 @@ func (nbi *NetboxInventory) AddDevice(ctx context.Context, newDevice *objects.De
newDevice.Tags = append(newDevice.Tags, nbi.SsotTag)
if _, ok := nbi.DevicesIndexByNameAndSiteID[newDevice.Name][newDevice.Site.ID]; ok {
oldDevice := nbi.DevicesIndexByNameAndSiteID[newDevice.Name][newDevice.Site.ID]
delete(nbi.OrphanManager[service.DevicesAPIPath], oldDevice.ID)
delete(nbi.OrphanManager[constants.DevicesAPIPath], oldDevice.ID)
diffMap, err := utils.JSONDiffMapExceptID(newDevice, oldDevice, false, nbi.SourcePriority)
if err != nil {
return nil, err
Expand Down Expand Up @@ -551,7 +552,7 @@ func (nbi *NetboxInventory) AddVlanGroup(ctx context.Context, newVlanGroup *obje
if _, ok := nbi.VlanGroupsIndexByName[newVlanGroup.Name]; ok {
// Remove id from orphan manager, because it still exists in the sources
oldVlanGroup := nbi.VlanGroupsIndexByName[newVlanGroup.Name]
delete(nbi.OrphanManager[service.VlanGroupsAPIPath], oldVlanGroup.ID)
delete(nbi.OrphanManager[constants.VlanGroupsAPIPath], oldVlanGroup.ID)
diffMap, err := utils.JSONDiffMapExceptID(newVlanGroup, oldVlanGroup, false, nbi.SourcePriority)
if err != nil {
return nil, err
Expand Down Expand Up @@ -584,7 +585,7 @@ func (nbi *NetboxInventory) AddVlan(ctx context.Context, newVlan *objects.Vlan)
if _, ok := nbi.VlansIndexByVlanGroupIDAndVID[newVlan.Group.ID][newVlan.Vid]; ok {
// Remove id from orphan manager, because it still exists in the sources
oldVlan := nbi.VlansIndexByVlanGroupIDAndVID[newVlan.Group.ID][newVlan.Vid]
delete(nbi.OrphanManager[service.VlansAPIPath], oldVlan.ID)
delete(nbi.OrphanManager[constants.VlansAPIPath], oldVlan.ID)
diffMap, err := utils.JSONDiffMapExceptID(newVlan, oldVlan, false, nbi.SourcePriority)
if err != nil {
return nil, err
Expand Down Expand Up @@ -619,7 +620,7 @@ func (nbi *NetboxInventory) AddInterface(ctx context.Context, newInterface *obje
newInterface.Tags = append(newInterface.Tags, nbi.SsotTag)
if _, ok := nbi.InterfacesIndexByDeviceIDAndName[newInterface.Device.ID][newInterface.Name]; ok {
// Remove id from orphan manager, because it still exists in the sources
delete(nbi.OrphanManager[service.InterfacesAPIPath], nbi.InterfacesIndexByDeviceIDAndName[newInterface.Device.ID][newInterface.Name].ID)
delete(nbi.OrphanManager[constants.InterfacesAPIPath], nbi.InterfacesIndexByDeviceIDAndName[newInterface.Device.ID][newInterface.Name].ID)
diffMap, err := utils.JSONDiffMapExceptID(newInterface, nbi.InterfacesIndexByDeviceIDAndName[newInterface.Device.ID][newInterface.Name], false, nbi.SourcePriority)
oldIntf := nbi.InterfacesIndexByDeviceIDAndName[newInterface.Device.ID][newInterface.Name]
if err != nil {
Expand Down Expand Up @@ -655,7 +656,7 @@ func (nbi *NetboxInventory) AddVM(ctx context.Context, newVM *objects.VM) (*obje
newVM.Tags = append(newVM.Tags, nbi.SsotTag)
if _, ok := nbi.VMsIndexByName[newVM.Name]; ok {
// Remove id from orphan manager, because it still exists in the sources
delete(nbi.OrphanManager[service.VirtualMachinesAPIPath], nbi.VMsIndexByName[newVM.Name].ID)
delete(nbi.OrphanManager[constants.VirtualMachinesAPIPath], nbi.VMsIndexByName[newVM.Name].ID)
diffMap, err := utils.JSONDiffMapExceptID(newVM, nbi.VMsIndexByName[newVM.Name], false, nbi.SourcePriority)
oldVM := nbi.VMsIndexByName[newVM.Name]
if err != nil {
Expand Down Expand Up @@ -689,7 +690,7 @@ func (nbi *NetboxInventory) AddVMInterface(ctx context.Context, newVMInterface *
defer nbi.VMInterfacesLock.Unlock()
if _, ok := nbi.VMInterfacesIndexByVMIdAndName[newVMInterface.VM.ID][newVMInterface.Name]; ok {
// Remove id from orphan manager, because it still exists in the sources
delete(nbi.OrphanManager[service.VMInterfacesAPIPath], nbi.VMInterfacesIndexByVMIdAndName[newVMInterface.VM.ID][newVMInterface.Name].ID)
delete(nbi.OrphanManager[constants.VMInterfacesAPIPath], nbi.VMInterfacesIndexByVMIdAndName[newVMInterface.VM.ID][newVMInterface.Name].ID)
diffMap, err := utils.JSONDiffMapExceptID(newVMInterface, nbi.VMInterfacesIndexByVMIdAndName[newVMInterface.VM.ID][newVMInterface.Name], false, nbi.SourcePriority)
oldVMIface := nbi.VMInterfacesIndexByVMIdAndName[newVMInterface.VM.ID][newVMInterface.Name]
if err != nil {
Expand Down Expand Up @@ -725,7 +726,7 @@ func (nbi *NetboxInventory) AddIPAddress(ctx context.Context, newIPAddress *obje
defer nbi.IPAddressesLock.Unlock()
if _, ok := nbi.IPAdressesIndexByAddress[newIPAddress.Address]; ok {
// Delete id from orphan manager, because it still exists in the sources
delete(nbi.OrphanManager[service.IPAddressesAPIPath], nbi.IPAdressesIndexByAddress[newIPAddress.Address].ID)
delete(nbi.OrphanManager[constants.IPAddressesAPIPath], nbi.IPAdressesIndexByAddress[newIPAddress.Address].ID)
diffMap, err := utils.JSONDiffMapExceptID(newIPAddress, nbi.IPAdressesIndexByAddress[newIPAddress.Address], false, nbi.SourcePriority)
oldIPAddress := nbi.IPAdressesIndexByAddress[newIPAddress.Address]
if err != nil {
Expand Down Expand Up @@ -759,7 +760,7 @@ func (nbi *NetboxInventory) AddPrefix(ctx context.Context, newPrefix *objects.Pr
defer nbi.PrefixesLock.Unlock()
if _, ok := nbi.PrefixesIndexByPrefix[newPrefix.Prefix]; ok {
// Delete id from orphan manager, because it still exists in the sources
delete(nbi.OrphanManager[service.PrefixesAPIPath], nbi.PrefixesIndexByPrefix[newPrefix.Prefix].ID)
delete(nbi.OrphanManager[constants.PrefixesAPIPath], nbi.PrefixesIndexByPrefix[newPrefix.Prefix].ID)
diffMap, err := utils.JSONDiffMapExceptID(newPrefix, nbi.PrefixesIndexByPrefix[newPrefix.Prefix], false, nbi.SourcePriority)
oldPrefix := nbi.PrefixesIndexByPrefix[newPrefix.Prefix]
if err != nil {
Expand Down
32 changes: 22 additions & 10 deletions internal/netbox/inventory/add_items_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,24 @@ package inventory

import (
"context"
"encoding/json"
"log"
"os"
"reflect"
"sync"
"testing"

"github.com/bl4ko/netbox-ssot/internal/constants"
"github.com/bl4ko/netbox-ssot/internal/logger"
"github.com/bl4ko/netbox-ssot/internal/netbox/objects"
"github.com/bl4ko/netbox-ssot/internal/netbox/service"
)

var MockInventory = &NetboxInventory{
Logger: &logger.Logger{Logger: log.New(os.Stdout, "", log.LstdFlags)},
TagsIndexByName: make(map[string]*objects.Tag),
TagsLock: sync.Mutex{},
NetboxAPI: nil, // TODO
NetboxAPI: service.MockNetboxClient,
}

func TestNetboxInventory_AddTag(t *testing.T) {
Expand All @@ -28,26 +31,35 @@ func TestNetboxInventory_AddTag(t *testing.T) {
name string
nbi *NetboxInventory
args args
want *objects.Tag
want string
wantErr bool
}{
// {
// name: "Test new tag add",
// nbi: MockInventory,
// args: args{ctx: context.WithValue(context.Background(), constants.CtxSourceKey, "test"), newTag: &objects.Tag{Name: "new tag", Description: "New Tag", Color: objects.ColorBlack, Slug: "new_tag"}},
// want: &objects.Tag{Name: "new tag", Description: "New Tag", Color: objects.ColorBlack, Slug: "new_tag"},
// },
{
name: "Test new tag add",
nbi: MockInventory,
args: args{ctx: context.WithValue(context.Background(), constants.CtxSourceKey, "test"), newTag: &objects.Tag{Name: "new tag", Description: "New Tag", Color: objects.ColorBlack, Slug: "new_tag"}},
want: service.TagCreateResponse,
},
}

mockServer := service.CreateMockServer()
defer mockServer.Close()

for _, tt := range tests {
service.MockNetboxClient.BaseURL = mockServer.URL
t.Run(tt.name, func(t *testing.T) {
got, err := tt.nbi.AddTag(tt.args.ctx, tt.args.newTag)
if (err != nil) != tt.wantErr {
t.Errorf("NetboxInventory.AddTag() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("NetboxInventory.AddTag() = %v, want %v", got, tt.want)
var wantTag objects.Tag
err = json.Unmarshal([]byte(tt.want), &wantTag)
if err != nil {
t.Errorf("unmarshal test data: %s", err)
}
if !reflect.DeepEqual(got, &wantTag) {
t.Errorf("NetboxInventory.AddTag() = %v, want %v", got, wantTag)
}
})
}
Expand Down
Loading

0 comments on commit 10c85b9

Please sign in to comment.