aboutsummaryrefslogtreecommitdiff
path: root/clients/netbox/config_file_client_test.go
blob: bce51658fad532b65b5c429606fc345d80ee5584 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package netbox

import (
	"context"
	"net"
	"testing"
	"testing/fstest"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/suite"
)

var (
	ipNet1 *net.IPNet
	ipNet2 *net.IPNet
)

func init() {
	_, ipNet1, _ = net.ParseCIDR("127.0.0.1/8")
	_, ipNet2, _ = net.ParseCIDR("10.0.10.0/24")
}

var testFs fstest.MapFS

var jsonMissingTopSiteTags = []byte(`{
	"netbox": { }
}`)

var jsonInvalidFormat = []byte(`{
	"netbox": {
		"sites": {
			"site1": {
				"invalid-tag": { "foo": "bar" }
			}
		},
		"tags": {
			"invalid-tag": { "foo": "bar" }
		}
	}
}`)

var jsonTestConfig = []byte(`{
	"netbox": {
		"sites": {
			"site1": {
				"tag1": [ "127.0.0.1/8", "10.0.10.0/24" ]
			}
		},
		"tags": {
			"tag1": [ "127.0.0.1/8", "10.0.10.0/24" ]
		}
	}
}`)

var yamlTestConfig = []byte(`
netbox:
  sites:
    site1:
      tag1: [ "127.0.0.1/8", "10.0.10.0/24" ]
  tags:
    tag1: [ "127.0.0.1/8", "10.0.10.0/24" ]
`)

func init() {
	testFs = fstest.MapFS{}
	testFs["netbox.foo"] = &fstest.MapFile{Data: jsonTestConfig}
	testFs["netbox.json"] = &fstest.MapFile{Data: jsonTestConfig}
	testFs["netbox.yaml"] = &fstest.MapFile{Data: yamlTestConfig}
	testFs["netbox.yml"] = &fstest.MapFile{Data: yamlTestConfig}
	testFs["no-site-tags.json"] = &fstest.MapFile{Data: jsonMissingTopSiteTags}
	testFs["invalid-format.json"] = &fstest.MapFile{Data: jsonInvalidFormat}
}

type ConfigFileNetboxClientSuite struct {
	suite.Suite
	c   NetboxClient
	nst NetboxClient
	inv NetboxClient
}

func (s *ConfigFileNetboxClientSuite) SetupTest() {
	var err error
	s.c, err = NewConfigFileClient(testFs, "netbox.yaml", "netbox")
	assert.NoError(s.T(), err)

	s.nst, err = NewConfigFileClient(testFs, "no-site-tags.json", "netbox")
	assert.NoError(s.T(), err)

	s.inv, err = NewConfigFileClient(testFs, "invalid-format.json", "netbox")
	assert.NoError(s.T(), err)
}

func (s *ConfigFileNetboxClientSuite) TestFromJSON() {
	_, err := NewConfigFileClient(testFs, "netbox.json", "netbox")
	assert.NoError(s.T(), err)
}

func (s *ConfigFileNetboxClientSuite) TestFromYAML() {
	_, err := NewConfigFileClient(testFs, "netbox.yaml", "netbox")
	assert.NoError(s.T(), err)
	_, err = NewConfigFileClient(testFs, "netbox.yml", "netbox")
	assert.NoError(s.T(), err)
}

func (s *ConfigFileNetboxClientSuite) TestFromInvalidExtension() {
	_, err := NewConfigFileClient(testFs, "netbox.foo", "netbox")
	assert.ErrorContains(s.T(), err, "extension .foo are not supported")
}

func (s *ConfigFileNetboxClientSuite) TestFromInvalidFile() {
	_, err := NewConfigFileClient(testFs, "netbox.foo-", "netbox")
	assert.ErrorContains(s.T(), err, "file does not exist")
}

func (s *ConfigFileNetboxClientSuite) TestFromInvalidKey() {
	_, err := NewConfigFileClient(testFs, "netbox.json", "netboks")
	assert.ErrorContains(s.T(), err, "Key netboks does not exist")
}

func (s *ConfigFileNetboxClientSuite) TestGetSitePrefixesWithTag() {
	ips, err := s.c.GetSitePrefixesWithTag(context.TODO(), "site1", "tag1")
	assert.NoError(s.T(), err)
	assert.Equal(s.T(), ipNet1, ips[0])
	assert.Equal(s.T(), ipNet2, ips[1])
}

func (s *ConfigFileNetboxClientSuite) TestGetSitePrefixesWithTagMissing() {
	_, err := s.nst.GetSitePrefixesWithTag(context.TODO(), "site1", "tag1")
	assert.ErrorContains(s.T(), err, "No key 'sites'")

	_, err = s.c.GetSitePrefixesWithTag(context.TODO(), "site2", "tag1")
	assert.ErrorContains(s.T(), err, "Site site2 not in sites")

	_, err = s.c.GetSitePrefixesWithTag(context.TODO(), "site1", "tag2")
	assert.ErrorContains(s.T(), err, "Tag tag2 not in tags")
}

func (s *ConfigFileNetboxClientSuite) TestGetSitePrefixesWithTagInvalid() {
	_, err := s.inv.GetSitePrefixesWithTag(context.TODO(), "site1", "invalid-tag")
	assert.ErrorContains(s.T(), err, "Key 'sites' not in correct format")
}

func (s *ConfigFileNetboxClientSuite) TestGetPrefixesWithTag() {
	ips, err := s.c.GetPrefixesWithTag(context.TODO(), "tag1")
	assert.NoError(s.T(), err)
	assert.Equal(s.T(), ipNet1, ips[0])
	assert.Equal(s.T(), ipNet2, ips[1])
}

func (s *ConfigFileNetboxClientSuite) TestGetPrefixesWithTagMissingTags() {
	_, err := s.nst.GetPrefixesWithTag(context.TODO(), "tag1")
	assert.ErrorContains(s.T(), err, "No key 'tags'")

	_, err = s.c.GetPrefixesWithTag(context.TODO(), "tag2")
	assert.ErrorContains(s.T(), err, "Tag tag2 not in tags")
}

func (s *ConfigFileNetboxClientSuite) TestGetPrefixesWithTagMissingInvalid() {
	_, err := s.inv.GetPrefixesWithTag(context.TODO(), "invalid-tag")
	assert.ErrorContains(s.T(), err, "Key 'tags' not in correct format")
}

func TestConfigFileNetboxClientSuite(t *testing.T) {
	suite.Run(t, &ConfigFileNetboxClientSuite{})
}