File: export_test.go

package info (click to toggle)
ez-ipam 0.3.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,544 kB
  • sloc: makefile: 6
file content (187 lines) | stat: -rw-r--r-- 5,458 bytes parent folder | download
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package export

import (
	"strings"
	"testing"

	"github.com/plumber-cd/ez-ipam/internal/domain"
)

func TestRenderMarkdownEmpty(t *testing.T) {
	c := domain.NewCatalog()
	c.Put(&domain.StaticFolder{Base: domain.Base{ID: "Networks"}, Index: 0})
	c.Put(&domain.StaticFolder{Base: domain.Base{ID: "Zones"}, Index: 1})
	c.Put(&domain.StaticFolder{Base: domain.Base{ID: "VLANs"}, Index: 2})
	c.Put(&domain.StaticFolder{Base: domain.Base{ID: "WiFi SSIDs"}, Index: 3})
	c.Put(&domain.StaticFolder{Base: domain.Base{ID: "Equipment"}, Index: 4})
	c.Put(&domain.StaticFolder{Base: domain.Base{ID: "DNS"}, Index: 5})

	md, err := RenderMarkdown(c)
	if err != nil {
		t.Fatalf("RenderMarkdown() error: %v", err)
	}

	// Should contain the title.
	if !strings.Contains(md, "# EZ-IPAM") {
		t.Error("expected markdown to contain title")
	}

	// Always-present sections.
	for _, section := range []string{"## Overview", "## Networks", "## Detailed Networks"} {
		if !strings.Contains(md, section) {
			t.Errorf("expected markdown to contain %q", section)
		}
	}

	// Conditional sections should NOT appear when empty.
	for _, section := range []string{"## VLANs", "## DNS Records", "## WiFi SSIDs", "## Zones", "## Equipment"} {
		if strings.Contains(md, section) {
			t.Errorf("empty catalog should not contain %q", section)
		}
	}
}

func TestRenderMarkdownWithData(t *testing.T) {
	c := domain.NewCatalog()
	c.Put(&domain.StaticFolder{Base: domain.Base{ID: "Networks"}, Index: 0})
	c.Put(&domain.StaticFolder{Base: domain.Base{ID: "Zones"}, Index: 1})
	c.Put(&domain.StaticFolder{Base: domain.Base{ID: "VLANs"}, Index: 2})
	c.Put(&domain.StaticFolder{Base: domain.Base{ID: "WiFi SSIDs"}, Index: 3})
	c.Put(&domain.StaticFolder{Base: domain.Base{ID: "Equipment"}, Index: 4})
	c.Put(&domain.StaticFolder{Base: domain.Base{ID: "DNS"}, Index: 5})

	// Add a VLAN.
	c.Put(&domain.VLAN{
		Base:        domain.Base{ID: "100", ParentPath: "VLANs"},
		DisplayName: "Office",
		Description: "Main office VLAN",
	})

	// Add a network with allocation.
	net := &domain.Network{
		Base:           domain.Base{ID: "10.0.0.0/24", ParentPath: "Networks"},
		AllocationMode: domain.AllocationModeHosts,
		DisplayName:    "Office LAN",
		Description:    "Office network",
		VLANID:         100,
	}
	c.Put(net)

	// Add an IP.
	c.Put(&domain.IP{
		Base:        domain.Base{ID: "10.0.0.1", ParentPath: net.GetPath()},
		DisplayName: "Gateway",
		MACAddress:  "00:11:22:33:44:55",
		Description: "Default gateway",
	})

	// Add DNS alias record.
	c.Put(&domain.DNSRecord{
		Base:           domain.Base{ID: "gateway.example.com", ParentPath: "DNS"},
		ReservedIPPath: net.GetPath() + " -> 10.0.0.1",
		Description:    "Gateway alias",
	})
	c.Put(&domain.DNSRecord{
		Base:        domain.Base{ID: "mail.example.com", ParentPath: "DNS"},
		RecordType:  "MX",
		RecordValue: "10 mail.provider.home",
		Description: "Mail route",
	})

	// Add equipment with port.
	eq := &domain.Equipment{
		Base:        domain.Base{ID: "Switch-1", ParentPath: "Equipment"},
		DisplayName: "Switch-1",
		Model:       "SG300",
	}
	c.Put(eq)

	c.Put(&domain.Port{
		Base:     domain.Base{ID: "1", ParentPath: eq.GetPath()},
		Name:     "uplink",
		PortType: "RJ45",
		Speed:    "1G",
	})

	// Add SSID.
	c.Put(&domain.SSID{
		Base:        domain.Base{ID: "OfficeWiFi", ParentPath: "WiFi SSIDs"},
		Description: "Office wireless",
	})

	// Add zone.
	c.Put(&domain.Zone{
		Base:        domain.Base{ID: "DMZ", ParentPath: "Zones"},
		DisplayName: "DMZ",
		Description: "Demilitarized zone",
		VLANIDs:     []int{100},
	})

	md, err := RenderMarkdown(c)
	if err != nil {
		t.Fatalf("RenderMarkdown() error: %v", err)
	}

	// Check that key data appears in the output.
	checks := []string{
		"10.0.0.0/24",
		"Office LAN",
		"10.0.0.1",
		"Gateway",
		"DNS Records",
		"gateway.example.com",
		"`10.0.0.1` (Gateway `00:11:22:33:44:55`)",
		"`10 mail.provider.home`",
		"100",
		"Office",
		"Switch-1",
		"SG300",
		"OfficeWiFi",
		"DMZ",
	}
	for _, check := range checks {
		if !strings.Contains(md, check) {
			t.Errorf("expected markdown to contain %q", check)
		}
	}
}

func TestRenderMarkdownNetworkHierarchy(t *testing.T) {
	c := domain.NewCatalog()
	c.Put(&domain.StaticFolder{Base: domain.Base{ID: "Networks"}, Index: 0})
	c.Put(&domain.StaticFolder{Base: domain.Base{ID: "Zones"}, Index: 1})
	c.Put(&domain.StaticFolder{Base: domain.Base{ID: "VLANs"}, Index: 2})
	c.Put(&domain.StaticFolder{Base: domain.Base{ID: "WiFi SSIDs"}, Index: 3})
	c.Put(&domain.StaticFolder{Base: domain.Base{ID: "Equipment"}, Index: 4})
	c.Put(&domain.StaticFolder{Base: domain.Base{ID: "DNS"}, Index: 5})

	// Parent network allocated as subnets.
	parent := &domain.Network{
		Base:           domain.Base{ID: "10.0.0.0/16", ParentPath: "Networks"},
		AllocationMode: domain.AllocationModeSubnets,
		DisplayName:    "Main",
	}
	c.Put(parent)

	// Child networks.
	c.Put(&domain.Network{
		Base:           domain.Base{ID: "10.0.0.0/24", ParentPath: parent.GetPath()},
		AllocationMode: domain.AllocationModeHosts,
		DisplayName:    "Office",
	})
	c.Put(&domain.Network{
		Base: domain.Base{ID: "10.0.1.0/24", ParentPath: parent.GetPath()},
	})

	md, err := RenderMarkdown(c)
	if err != nil {
		t.Fatalf("RenderMarkdown() error: %v", err)
	}

	if !strings.Contains(md, "10.0.0.0/16") {
		t.Error("expected parent network in markdown")
	}
	if !strings.Contains(md, "10.0.0.0/24") {
		t.Error("expected child network in markdown")
	}
}