netmaker/controllers/dns_test.go
Matthew R Kasun 74778b6757
Net 503 - use NewHosts to init hostfile (#2473)
* use NewHosts to init hostfile

* Revert "Bump github.com/txn2/txeh from 1.4.0 to 1.5.3 (#2464)" (#2475)

This reverts commit aafaa91af0.

* NET-433: Only Set Routes for NAT enabled egress GW (#2469)

* remove related fields and code

* remover metrics collection from server code

* fw update struct

* add ext client flag to metrics data

* simply nat types

* rm proxy update from cli

* remove ingress routes from firewall update

* check if egress ranges are present

* rm unused func

* ignore network parameter passed to delete ingress endpoint (#2471)

* fix(NET-388): fix to update node dns toggle (#2472)

* use local port for metrics pinger (#2479)

* v0.20.5 release notes (#2482)

* v0.20.5 release notes

* add a known issue

* update develop to v0.20.6 (#2485)

* enforce unique names for ext client names (#2476)

* enforce unique names for ext client names

* only check for unique id on creation

* check for unique id if changed

* prune(NET-483): remove defunct host.internetgateway field (#2487)

* don't reference host on err (#2493)

* deprecrate netclient install scripts (#2490)

* Net 500: validate network parameter passed to node endpoints (#2480)

* enforce unique names for ext client names

* only check for unique id on creation

* check for unique id if changed

* validate network parameter passed to node endpoints

---------

Co-authored-by: Abhishek K <32607604+abhishek9686@users.noreply.github.com>

* NET-513 (#2492)

nm-certs.sh now requests certificate for EE and CE edition domains accordingly.

* [NET-404] Run in limited mode when ee checks fail (#2474)

* Add limited http handlers functionality to rest handler

* Export ee.errValidation (ee.ErrValidation)

* Export a fatal error handled by the hook manager

* Export a new status variable for unlicensed server

* Mark server as unlicensed when ee checks fail

* Handle license validation failures with a (re)boot in a limited state

* Revert "Export a fatal error handled by the hook manager"

This reverts commit 069c21974a8d36e889c73ad78023448d787d62a5.

* Revert "Export ee.errValidation (ee.ErrValidation)"

This reverts commit 59dbab8c79773ca5d879f28cbaf53f3dd4297b9b.

* Revert "Add limited http handlers functionality to rest handler"

This reverts commit e2f1f28facaca54713db76a588839cd2733cf673.

* Revert "Handle license validation failures with a (re)boot in a limited state"

This reverts commit 58cfbbaf522a1345aac1fa67964ebff0a6d60cd8.

* Revert "Mark server as unlicensed when ee checks fail"

This reverts commit 77c6dbdd3c9cfa6e7d6becedef6251e8617ae367.

* Handle license validation failures with a middleware

* Forbid responses if unlicensed ee and not in status api

* Remove unused func

* feat(NET-449): add sync feature to request a host pull from server (#2491)

* fix(NET-486): change client name length validation (#2498)

set limit to 5<=x<=32

* [NET-477] Pick AMB URL dynamically (#2489)

* Introduce config for environment

* Introduce func to get environment

* Choose accounts api host from environment

* Test the ee package on workflows

* Use build tag ee for license_test.go

* [Feature]: nm-quick script tackling arm TODO support (#2488)

* domain flag for auto installs

* use static servers with custom domain (#2421)

* send delete peer update always

* fix add/remove host api calls

* keep mq updates in a single go func

* move branch test logic to devops (#2443)

* handle IOT OS

* save server name to env (#2460)

* ensure branch test servers available after test runs (#2467)

* save server name to env

* free server always; add PR to discord messages

* use correct method to delete droplets (#2468)

* quick fix for the launcher

* removed exit when triggering not supported exit and removed the TODO comments related to this issue

---------

Co-authored-by: Matthew R Kasun <mkasun@nusak.ca>
Co-authored-by: Alex Feiszli <31018251+afeiszli@users.noreply.github.com>
Co-authored-by: Christopher Blaha <crispspiceguitar@gmail.com>
Co-authored-by: Abhishek Kondur <abhi281342@gmail.com>
Co-authored-by: Abhishek K <32607604+abhishek9686@users.noreply.github.com>

* Bump github.com/go-playground/validator/v10 from 10.14.1 to 10.15.0 (#2503)

Bumps [github.com/go-playground/validator/v10](https://github.com/go-playground/validator) from 10.14.1 to 10.15.0.
- [Release notes](https://github.com/go-playground/validator/releases)
- [Commits](https://github.com/go-playground/validator/compare/v10.14.1...v10.15.0)

---
updated-dependencies:
- dependency-name: github.com/go-playground/validator/v10
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Bump golang.org/x/oauth2 from 0.10.0 to 0.11.0 (#2502)

Bumps [golang.org/x/oauth2](https://github.com/golang/oauth2) from 0.10.0 to 0.11.0.
- [Commits](https://github.com/golang/oauth2/compare/v0.10.0...v0.11.0)

---
updated-dependencies:
- dependency-name: golang.org/x/oauth2
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Bump alpine from 3.18.2 to 3.18.3 (#2500)

Bumps alpine from 3.18.2 to 3.18.3.

---
updated-dependencies:
- dependency-name: alpine
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Net 509 515 (#2496)

* NET-509

* External client config files with IPv6 endpoints now have the [] separating the address and port.

* NET-515

* Increased network name max length to 32

* NET-509-515

* Updated unit test for network name max length check.

* Updated extclient endpoint ip string manipulation to use sprintf

* Added proper error message for network name length more than max allowed.

* NET-515 small typo fix for error strings should not be capitalized

* [NET-494 / ACC-322] New free tier limits (#2495)

* Rename var

* Rename consts and use iota

* Use switch instead of repeated else if

* Rename limits related vars

* Introduce new free tier limits

* Measure new limits and report on license validation

* Separate usage and limits, have new ones

* Don't check for hosts and clients limits, but for machines instead

* Error on egress creation @ free tier w/ internet gateways

* Remove clients and hosts limit from code

* Rename var

* Rename consts and use iota

* Use switch instead of repeated else if

* Rename limits related vars

* Introduce new free tier limits

* Measure new limits and report on license validation

* Separate usage and limits, have new ones

* Don't check for hosts and clients limits, but for machines instead

* Error on egress creation @ free tier w/ internet gateways

* Remove clients and hosts limit from code

* NET-507 (#2506)

* NET-507

* Fixed server restart changing the node expiration date to the defaults.

* Removed expired nodes removal from zombie cleanup routine.

* Added a new expired nodes deletion routine which removes expired nodes every hour.

* NET-507 suggested changes

* Possible fix for zombie nodes upon node deletion from the UI.

* Suggested changes implemented for expired nodes deletion go routine.

* NET-507 typo fix

* typo fix for DeleteNode purge parameter

* NET-517 (#2510)

* Fixed emqx docker compose file syntax and added volumes for persistence.

* Added helper comments for docker-compose.yml and netmaker.env files to enable emqx broker support.

* migration (#2509)

* create gateways during migration

* set version for testing

* restruct migration

* debug logging

* enforce unique names for ext client names (#2476)

* enforce unique names for ext client names

* only check for unique id on creation

* check for unique id if changed

* prune(NET-483): remove defunct host.internetgateway field (#2487)

* don't reference host on err (#2493)

* deprecrate netclient install scripts (#2490)

* Net 500: validate network parameter passed to node endpoints (#2480)

* enforce unique names for ext client names

* only check for unique id on creation

* check for unique id if changed

* validate network parameter passed to node endpoints

---------

Co-authored-by: Abhishek K <32607604+abhishek9686@users.noreply.github.com>

* NET-513 (#2492)

nm-certs.sh now requests certificate for EE and CE edition domains accordingly.

* [NET-404] Run in limited mode when ee checks fail (#2474)

* Add limited http handlers functionality to rest handler

* Export ee.errValidation (ee.ErrValidation)

* Export a fatal error handled by the hook manager

* Export a new status variable for unlicensed server

* Mark server as unlicensed when ee checks fail

* Handle license validation failures with a (re)boot in a limited state

* Revert "Export a fatal error handled by the hook manager"

This reverts commit 069c21974a8d36e889c73ad78023448d787d62a5.

* Revert "Export ee.errValidation (ee.ErrValidation)"

This reverts commit 59dbab8c79773ca5d879f28cbaf53f3dd4297b9b.

* Revert "Add limited http handlers functionality to rest handler"

This reverts commit e2f1f28facaca54713db76a588839cd2733cf673.

* Revert "Handle license validation failures with a (re)boot in a limited state"

This reverts commit 58cfbbaf522a1345aac1fa67964ebff0a6d60cd8.

* Revert "Mark server as unlicensed when ee checks fail"

This reverts commit 77c6dbdd3c9cfa6e7d6becedef6251e8617ae367.

* Handle license validation failures with a middleware

* Forbid responses if unlicensed ee and not in status api

* Remove unused func

* feat(NET-449): add sync feature to request a host pull from server (#2491)

* fix(NET-486): change client name length validation (#2498)

set limit to 5<=x<=32

* [NET-477] Pick AMB URL dynamically (#2489)

* Introduce config for environment

* Introduce func to get environment

* Choose accounts api host from environment

* Test the ee package on workflows

* Use build tag ee for license_test.go

* [Feature]: nm-quick script tackling arm TODO support (#2488)

* domain flag for auto installs

* use static servers with custom domain (#2421)

* send delete peer update always

* fix add/remove host api calls

* keep mq updates in a single go func

* move branch test logic to devops (#2443)

* handle IOT OS

* save server name to env (#2460)

* ensure branch test servers available after test runs (#2467)

* save server name to env

* free server always; add PR to discord messages

* use correct method to delete droplets (#2468)

* quick fix for the launcher

* removed exit when triggering not supported exit and removed the TODO comments related to this issue

---------

Co-authored-by: Matthew R Kasun <mkasun@nusak.ca>
Co-authored-by: Alex Feiszli <31018251+afeiszli@users.noreply.github.com>
Co-authored-by: Christopher Blaha <crispspiceguitar@gmail.com>
Co-authored-by: Abhishek Kondur <abhi281342@gmail.com>
Co-authored-by: Abhishek K <32607604+abhishek9686@users.noreply.github.com>

* rebase conflict

* include pass and os in mirgration data

* node network ranges

* remove debugging logs

* add gateways

* use sent node

* upgrade shell script

* associate node to host during migration

* add node to host.Nodes and publish peer update

* save host outside loop

* fix script name

* simplify upgrade script

* don't migrate relays

* simplify upgrade script even more

* guard against blank address or address6

* typos

---------

Co-authored-by: Aceix <aceixsmartX@gmail.com>
Co-authored-by: Abhishek K <32607604+abhishek9686@users.noreply.github.com>
Co-authored-by: Farukh Khan <farukhkhan21@gmail.com>
Co-authored-by: Gabriel de Souza Seibel <gabrielseibel1@gmail.com>
Co-authored-by: bornav <51048565+bornav@users.noreply.github.com>
Co-authored-by: Alex Feiszli <31018251+afeiszli@users.noreply.github.com>
Co-authored-by: Christopher Blaha <crispspiceguitar@gmail.com>
Co-authored-by: Abhishek Kondur <abhi281342@gmail.com>

* dns tests

* go mod tidy

* set dns dir when testing

* troubleshoot test failure

* test fail troubleshooting

* use txeh.RenderHostFile to compare

---------

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: Abhishek K <32607604+abhishek9686@users.noreply.github.com>
Co-authored-by: Aceix <aceixsmartX@gmail.com>
Co-authored-by: Christopher Blaha <crispspiceguitar@gmail.com>
Co-authored-by: Farukh Khan <farukhkhan21@gmail.com>
Co-authored-by: Gabriel de Souza Seibel <gabrielseibel1@gmail.com>
Co-authored-by: bornav <51048565+bornav@users.noreply.github.com>
Co-authored-by: Alex Feiszli <31018251+afeiszli@users.noreply.github.com>
Co-authored-by: Abhishek Kondur <abhi281342@gmail.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2023-08-25 10:55:47 +05:30

447 lines
14 KiB
Go

package controller
import (
"net"
"os"
"testing"
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
"github.com/txn2/txeh"
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
"github.com/gravitl/netmaker/functions"
"github.com/gravitl/netmaker/logic"
"github.com/gravitl/netmaker/models"
)
var dnsHost models.Host
func TestGetAllDNS(t *testing.T) {
deleteAllDNS(t)
deleteAllNetworks()
createNet()
createHost()
t.Run("NoEntries", func(t *testing.T) {
entries, err := logic.GetAllDNS()
assert.Nil(t, err)
assert.Equal(t, []models.DNSEntry(nil), entries)
})
t.Run("OneEntry", func(t *testing.T) {
entry := models.DNSEntry{
Address: "10.0.0.3", Name: "newhost", Network: "skynet",
}
_, err := logic.CreateDNS(entry)
assert.Nil(t, err)
entries, err := logic.GetAllDNS()
assert.Nil(t, err)
assert.Equal(t, 1, len(entries))
})
t.Run("MultipleEntry", func(t *testing.T) {
entry := models.DNSEntry{Address: "10.0.0.7", Name: "anotherhost", Network: "skynet"}
_, err := logic.CreateDNS(entry)
assert.Nil(t, err)
entries, err := logic.GetAllDNS()
assert.Nil(t, err)
assert.Equal(t, 2, len(entries))
})
}
func TestGetNodeDNS(t *testing.T) {
deleteAllDNS(t)
deleteAllNetworks()
createNet()
createHost()
err := functions.SetDNSDir()
assert.Nil(t, err)
t.Run("NoNodes", func(t *testing.T) {
dns, _ := logic.GetNodeDNS("skynet")
assert.Equal(t, []models.DNSEntry(nil), dns)
})
t.Run("NodeExists", func(t *testing.T) {
createHost()
_, ipnet, _ := net.ParseCIDR("10.0.0.1/32")
tmpCNode := models.CommonNode{
ID: uuid.New(),
Network: "skynet",
Address: *ipnet,
DNSOn: true,
}
createnode := models.Node{
CommonNode: tmpCNode,
}
err := logic.AssociateNodeToHost(&createnode, &dnsHost)
assert.Nil(t, err)
dns, err := logic.GetNodeDNS("skynet")
assert.Nil(t, err)
assert.Equal(t, "10.0.0.1", dns[0].Address)
})
t.Run("MultipleNodes", func(t *testing.T) {
_, ipnet, _ := net.ParseCIDR("10.100.100.3/32")
tmpCNode := models.CommonNode{
ID: uuid.New(),
Network: "skynet",
Address: *ipnet,
DNSOn: true,
}
createnode := models.Node{
CommonNode: tmpCNode,
}
err := logic.AssociateNodeToHost(&createnode, &dnsHost)
assert.Nil(t, err)
dns, err := logic.GetNodeDNS("skynet")
assert.Nil(t, err)
assert.Equal(t, 2, len(dns))
})
}
func TestGetCustomDNS(t *testing.T) {
deleteAllDNS(t)
deleteAllNetworks()
t.Run("NoNetworks", func(t *testing.T) {
dns, err := logic.GetCustomDNS("skynet")
assert.EqualError(t, err, "could not find any records")
assert.Equal(t, []models.DNSEntry(nil), dns)
})
t.Run("NoNodes", func(t *testing.T) {
createNet()
dns, err := logic.GetCustomDNS("skynet")
assert.EqualError(t, err, "could not find any records")
assert.Equal(t, []models.DNSEntry(nil), dns)
})
t.Run("NodeExists", func(t *testing.T) {
createTestNode()
dns, err := logic.GetCustomDNS("skynet")
assert.EqualError(t, err, "could not find any records")
assert.Equal(t, 0, len(dns))
})
t.Run("EntryExist", func(t *testing.T) {
entry := models.DNSEntry{Address: "10.0.0.3", Name: "custom1", Network: "skynet"}
_, err := logic.CreateDNS(entry)
assert.Nil(t, err)
dns, err := logic.GetCustomDNS("skynet")
assert.Nil(t, err)
assert.Equal(t, 1, len(dns))
})
t.Run("MultipleEntries", func(t *testing.T) {
entry := models.DNSEntry{Address: "10.0.0.4", Name: "host4", Network: "skynet"}
_, err := logic.CreateDNS(entry)
assert.Nil(t, err)
dns, err := logic.GetCustomDNS("skynet")
assert.Nil(t, err)
assert.Equal(t, 2, len(dns))
})
}
func TestGetDNSEntryNum(t *testing.T) {
deleteAllDNS(t)
deleteAllNetworks()
createNet()
t.Run("NoNodes", func(t *testing.T) {
num, err := logic.GetDNSEntryNum("myhost", "skynet")
assert.Nil(t, err)
assert.Equal(t, 0, num)
})
t.Run("NodeExists", func(t *testing.T) {
entry := models.DNSEntry{Address: "10.0.0.2", Name: "newhost", Network: "skynet"}
_, err := logic.CreateDNS(entry)
assert.Nil(t, err)
num, err := logic.GetDNSEntryNum("newhost", "skynet")
assert.Nil(t, err)
assert.Equal(t, 1, num)
})
}
func TestGetDNS(t *testing.T) {
deleteAllDNS(t)
deleteAllNetworks()
createNet()
t.Run("NoEntries", func(t *testing.T) {
dns, err := logic.GetDNS("skynet")
assert.Nil(t, err)
assert.Nil(t, dns)
})
t.Run("CustomDNSExists", func(t *testing.T) {
entry := models.DNSEntry{Address: "10.0.0.2", Name: "newhost", Network: "skynet"}
_, err := logic.CreateDNS(entry)
assert.Nil(t, err)
dns, err := logic.GetDNS("skynet")
t.Log(dns)
assert.Nil(t, err)
assert.NotNil(t, dns)
assert.Equal(t, "skynet", dns[0].Network)
assert.Equal(t, 1, len(dns))
})
t.Run("NodeExists", func(t *testing.T) {
deleteAllDNS(t)
createTestNode()
dns, err := logic.GetDNS("skynet")
assert.Nil(t, err)
assert.NotNil(t, dns)
assert.Equal(t, "skynet", dns[0].Network)
assert.Equal(t, 1, len(dns))
})
t.Run("NodeAndCustomDNS", func(t *testing.T) {
entry := models.DNSEntry{Address: "10.0.0.2", Name: "newhost", Network: "skynet"}
_, err := logic.CreateDNS(entry)
assert.Nil(t, err)
dns, err := logic.GetDNS("skynet")
t.Log(dns)
assert.Nil(t, err)
assert.NotNil(t, dns)
assert.Equal(t, "skynet", dns[0].Network)
assert.Equal(t, "skynet", dns[1].Network)
assert.Equal(t, 2, len(dns))
})
}
func TestCreateDNS(t *testing.T) {
deleteAllDNS(t)
deleteAllNetworks()
createNet()
entry := models.DNSEntry{Address: "10.0.0.2", Name: "newhost", Network: "skynet"}
dns, err := logic.CreateDNS(entry)
assert.Nil(t, err)
assert.Equal(t, "newhost", dns.Name)
}
func TestSetDNS(t *testing.T) {
deleteAllDNS(t)
deleteAllNetworks()
etc, err := txeh.NewHosts(&txeh.HostsConfig{})
assert.Nil(t, err)
err = functions.SetDNSDir()
assert.Nil(t, err)
t.Run("NoNetworks", func(t *testing.T) {
err := logic.SetDNS()
assert.Nil(t, err)
info, err := txeh.NewHosts(&txeh.HostsConfig{
ReadFilePath: "./config/dnsconfig/netmaker.hosts",
})
assert.Nil(t, err)
assert.Equal(t, etc.RenderHostsFile(), info.RenderHostsFile())
})
t.Run("NoEntries", func(t *testing.T) {
createNet()
err := logic.SetDNS()
assert.Nil(t, err)
info, err := txeh.NewHosts(&txeh.HostsConfig{
ReadFilePath: "./config/dnsconfig/netmaker.hosts",
})
assert.Nil(t, err)
assert.Equal(t, etc.RenderHostsFile(), info.RenderHostsFile())
})
t.Run("NodeExists", func(t *testing.T) {
createTestNode()
err := logic.SetDNS()
assert.Nil(t, err)
info, err := os.Stat("./config/dnsconfig/netmaker.hosts")
assert.Nil(t, err)
assert.False(t, info.IsDir())
content, err := os.ReadFile("./config/dnsconfig/netmaker.hosts")
assert.Nil(t, err)
assert.Contains(t, string(content), "linuxhost.skynet")
})
t.Run("EntryExists", func(t *testing.T) {
entry := models.DNSEntry{Address: "10.0.0.3", Name: "newhost", Network: "skynet"}
_, err := logic.CreateDNS(entry)
assert.Nil(t, err)
err = logic.SetDNS()
assert.Nil(t, err)
info, err := os.Stat("./config/dnsconfig/netmaker.hosts")
assert.Nil(t, err)
assert.False(t, info.IsDir())
content, err := os.ReadFile("./config/dnsconfig/netmaker.hosts")
assert.Nil(t, err)
assert.Contains(t, string(content), "newhost.skynet")
})
}
func TestGetDNSEntry(t *testing.T) {
deleteAllDNS(t)
deleteAllNetworks()
createNet()
createTestNode()
entry := models.DNSEntry{Address: "10.0.0.2", Name: "newhost", Network: "skynet"}
_, _ = logic.CreateDNS(entry)
t.Run("wrong net", func(t *testing.T) {
entry, err := GetDNSEntry("newhost", "w286 Toronto Street South, Uxbridge, ONirecat")
assert.EqualError(t, err, "no result found")
assert.Equal(t, models.DNSEntry{}, entry)
})
t.Run("wrong host", func(t *testing.T) {
entry, err := GetDNSEntry("badhost", "skynet")
assert.EqualError(t, err, "no result found")
assert.Equal(t, models.DNSEntry{}, entry)
})
t.Run("good host", func(t *testing.T) {
entry, err := GetDNSEntry("newhost", "skynet")
assert.Nil(t, err)
assert.Equal(t, "newhost", entry.Name)
})
t.Run("node", func(t *testing.T) {
entry, err := GetDNSEntry("testnode", "skynet")
assert.EqualError(t, err, "no result found")
assert.Equal(t, models.DNSEntry{}, entry)
})
}
func TestDeleteDNS(t *testing.T) {
deleteAllDNS(t)
deleteAllNetworks()
createNet()
entry := models.DNSEntry{Address: "10.0.0.2", Name: "newhost", Network: "skynet"}
_, _ = logic.CreateDNS(entry)
t.Run("EntryExists", func(t *testing.T) {
err := logic.DeleteDNS("newhost", "skynet")
assert.Nil(t, err)
})
t.Run("NodeExists", func(t *testing.T) {
err := logic.DeleteDNS("myhost", "skynet")
assert.Nil(t, err)
})
t.Run("NoEntries", func(t *testing.T) {
err := logic.DeleteDNS("myhost", "skynet")
assert.Nil(t, err)
})
}
func TestValidateDNSUpdate(t *testing.T) {
deleteAllDNS(t)
deleteAllNetworks()
createNet()
entry := models.DNSEntry{Address: "10.0.0.2", Name: "myhost", Network: "skynet"}
t.Run("BadNetwork", func(t *testing.T) {
change := models.DNSEntry{Address: "10.0.0.2", Name: "myhost", Network: "badnet"}
err := logic.ValidateDNSUpdate(change, entry)
assert.NotNil(t, err)
assert.Contains(t, err.Error(), "Field validation for 'Network' failed on the 'network_exists' tag")
})
t.Run("EmptyNetwork", func(t *testing.T) {
// this can't actually happen as change.Network is populated if is blank
change := models.DNSEntry{Address: "10.0.0.2", Name: "myhost"}
err := logic.ValidateDNSUpdate(change, entry)
assert.NotNil(t, err)
assert.Contains(t, err.Error(), "Field validation for 'Network' failed on the 'network_exists' tag")
})
// t.Run("EmptyAddress", func(t *testing.T) {
// //this can't actually happen as change.Address is populated if is blank
// change := models.DNSEntry{"", "", "myhost", "skynet"}
// err := logic.ValidateDNSUpdate(change, entry)
// assert.NotNil(t, err)
// assert.Contains(t, err.Error(), "Field validation for 'Address' failed on the 'required' tag")
// })
t.Run("BadAddress", func(t *testing.T) {
change := models.DNSEntry{Address: "10.0.256.1", Name: "myhost", Network: "skynet"}
err := logic.ValidateDNSUpdate(change, entry)
assert.NotNil(t, err)
assert.Contains(t, err.Error(), "Field validation for 'Address' failed on the 'ip' tag")
})
t.Run("EmptyName", func(t *testing.T) {
// this can't actually happen as change.Name is populated if is blank
change := models.DNSEntry{Address: "10.0.0.2", Network: "skynet"}
err := logic.ValidateDNSUpdate(change, entry)
assert.NotNil(t, err)
assert.Contains(t, err.Error(), "Field validation for 'Name' failed on the 'required' tag")
})
t.Run("NameTooLong", func(t *testing.T) {
name := ""
for i := 1; i < 194; i++ {
name = name + "a"
}
change := models.DNSEntry{Address: "10.0.0.2", Name: name, Network: "skynet"}
err := logic.ValidateDNSUpdate(change, entry)
assert.NotNil(t, err)
assert.Contains(t, err.Error(), "Field validation for 'Name' failed on the 'max' tag")
})
t.Run("NameUnique", func(t *testing.T) {
change := models.DNSEntry{Address: "10.0.0.2", Name: "myhost", Network: "wirecat"}
_, _ = logic.CreateDNS(entry)
_, _ = logic.CreateDNS(change)
err := logic.ValidateDNSUpdate(change, entry)
assert.NotNil(t, err)
assert.Contains(t, err.Error(), "Field validation for 'Name' failed on the 'name_unique' tag")
// cleanup
err = logic.DeleteDNS("myhost", "wirecat")
assert.Nil(t, err)
})
}
func TestValidateDNSCreate(t *testing.T) {
_ = logic.DeleteDNS("mynode", "skynet")
t.Run("NoNetwork", func(t *testing.T) {
entry := models.DNSEntry{Address: "10.0.0.2", Name: "myhost", Network: "badnet"}
err := logic.ValidateDNSCreate(entry)
assert.NotNil(t, err)
assert.Contains(t, err.Error(), "Field validation for 'Network' failed on the 'network_exists' tag")
})
// t.Run("EmptyAddress", func(t *testing.T) {
// entry := models.DNSEntry{"", "", "myhost", "skynet"}
// err := logic.ValidateDNSCreate(entry)
// assert.NotNil(t, err)
// assert.Contains(t, err.Error(), "Field validation for 'Address' failed on the 'required' tag")
// })
t.Run("BadAddress", func(t *testing.T) {
entry := models.DNSEntry{Address: "10.0.256.1", Name: "myhost", Network: "skynet"}
err := logic.ValidateDNSCreate(entry)
assert.NotNil(t, err)
assert.Contains(t, err.Error(), "Field validation for 'Address' failed on the 'ip' tag")
})
t.Run("EmptyName", func(t *testing.T) {
entry := models.DNSEntry{Address: "10.0.0.2", Network: "skynet"}
err := logic.ValidateDNSCreate(entry)
assert.NotNil(t, err)
assert.Contains(t, err.Error(), "Field validation for 'Name' failed on the 'required' tag")
})
t.Run("NameTooLong", func(t *testing.T) {
name := ""
for i := 1; i < 194; i++ {
name = name + "a"
}
entry := models.DNSEntry{Address: "10.0.0.2", Name: name, Network: "skynet"}
err := logic.ValidateDNSCreate(entry)
assert.NotNil(t, err)
assert.Contains(t, err.Error(), "Field validation for 'Name' failed on the 'max' tag")
})
t.Run("NameUnique", func(t *testing.T) {
entry := models.DNSEntry{Address: "10.0.0.2", Name: "myhost", Network: "skynet"}
_, _ = logic.CreateDNS(entry)
err := logic.ValidateDNSCreate(entry)
assert.NotNil(t, err)
assert.Contains(t, err.Error(), "Field validation for 'Name' failed on the 'name_unique' tag")
})
t.Run("WhiteSpace", func(t *testing.T) {
entry := models.DNSEntry{Address: "10.10.10.5", Name: "white space", Network: "skynet"}
err := logic.ValidateDNSCreate(entry)
assert.NotNil(t, err)
assert.Contains(t, err.Error(), "Field validation for 'Name' failed on the 'whitespace' tag")
})
t.Run("AllSpaces", func(t *testing.T) {
entry := models.DNSEntry{Address: "10.10.10.5", Name: " ", Network: "skynet"}
err := logic.ValidateDNSCreate(entry)
assert.NotNil(t, err)
assert.Contains(t, err.Error(), "Field validation for 'Name' failed on the 'whitespace' tag")
})
}
func createHost() {
k, _ := wgtypes.ParseKey("DM5qhLAE20PG9BbfBCger+Ac9D2NDOwCtY1rbYDLf34=")
dnsHost = models.Host{
ID: uuid.New(),
PublicKey: k.PublicKey(),
HostPass: "password",
OS: "linux",
Name: "dnshost",
}
_ = logic.CreateHost(&dnsHost)
}
func deleteAllDNS(t *testing.T) {
dns, err := logic.GetAllDNS()
assert.Nil(t, err)
for _, record := range dns {
err := logic.DeleteDNS(record.Name, record.Network)
assert.Nil(t, err)
}
}