netmaker/controllers/dns_test.go

416 lines
13 KiB
Go
Raw Normal View History

2021-05-06 05:24:24 +08:00
package controller
import (
2021-10-17 23:47:25 +08:00
"os"
2021-05-06 05:24:24 +08:00
"testing"
2021-09-10 04:20:21 +08:00
"github.com/gravitl/netmaker/database"
2021-10-17 23:47:25 +08:00
"github.com/gravitl/netmaker/logic"
2021-05-06 05:24:24 +08:00
"github.com/gravitl/netmaker/models"
"github.com/stretchr/testify/assert"
)
2021-10-17 23:47:25 +08:00
func TestGetAllDNS(t *testing.T) {
database.InitializeDatabase()
deleteAllDNS(t)
deleteAllNetworks()
createNet()
t.Run("NoEntries", func(t *testing.T) {
2021-12-08 01:46:55 +08:00
entries, err := logic.GetAllDNS()
2021-10-17 23:47:25 +08:00
assert.Nil(t, err)
assert.Equal(t, []models.DNSEntry(nil), entries)
})
t.Run("OneEntry", func(t *testing.T) {
entry := models.DNSEntry{"10.0.0.3", "newhost", "skynet"}
CreateDNS(entry)
2021-12-08 01:46:55 +08:00
entries, err := logic.GetAllDNS()
2021-10-17 23:47:25 +08:00
assert.Nil(t, err)
assert.Equal(t, 1, len(entries))
})
t.Run("MultipleEntry", func(t *testing.T) {
entry := models.DNSEntry{"10.0.0.7", "anotherhost", "skynet"}
CreateDNS(entry)
2021-12-08 01:46:55 +08:00
entries, err := logic.GetAllDNS()
2021-10-17 23:47:25 +08:00
assert.Nil(t, err)
assert.Equal(t, 2, len(entries))
})
}
2021-05-06 05:24:24 +08:00
func TestGetNodeDNS(t *testing.T) {
2021-09-10 04:20:21 +08:00
database.InitializeDatabase()
2021-10-17 23:47:25 +08:00
deleteAllDNS(t)
2021-09-10 04:20:21 +08:00
deleteAllNetworks()
createNet()
2021-10-17 23:47:25 +08:00
t.Run("NoNodes", func(t *testing.T) {
2021-12-08 01:46:55 +08:00
dns, err := logic.GetNodeDNS("skynet")
2021-10-17 23:47:25 +08:00
assert.EqualError(t, err, "could not find any records")
assert.Equal(t, []models.DNSEntry(nil), dns)
})
t.Run("NodeExists", func(t *testing.T) {
createTestNode()
2021-12-08 01:46:55 +08:00
dns, err := logic.GetNodeDNS("skynet")
2021-10-17 23:47:25 +08:00
assert.Nil(t, err)
assert.Equal(t, "10.0.0.1", dns[0].Address)
})
t.Run("MultipleNodes", func(t *testing.T) {
createnode := &models.Node{PublicKey: "DM5qhLAE20PG9BbfBCger+Ac9D2NDOwCtY1rbYDLf34=", Endpoint: "10.100.100.3", MacAddress: "01:02:03:04:05:07", Password: "password", Network: "skynet"}
2021-12-11 10:09:42 +08:00
err := logic.CreateNode(createnode)
2021-10-17 23:47:25 +08:00
assert.Nil(t, err)
2021-12-08 01:46:55 +08:00
dns, err := logic.GetNodeDNS("skynet")
2021-10-17 23:47:25 +08:00
assert.Nil(t, err)
assert.Equal(t, 2, len(dns))
})
2021-05-06 05:24:24 +08:00
}
func TestGetCustomDNS(t *testing.T) {
2021-09-10 04:20:21 +08:00
database.InitializeDatabase()
2021-10-17 23:47:25 +08:00
deleteAllDNS(t)
2021-09-10 04:20:21 +08:00
deleteAllNetworks()
2021-10-17 23:47:25 +08:00
t.Run("NoNetworks", func(t *testing.T) {
2021-10-27 00:27:29 +08:00
dns, err := logic.GetCustomDNS("skynet")
2021-10-17 23:47:25 +08:00
assert.EqualError(t, err, "could not find any records")
assert.Equal(t, []models.DNSEntry(nil), dns)
})
t.Run("NoNodes", func(t *testing.T) {
createNet()
2021-10-27 00:27:29 +08:00
dns, err := logic.GetCustomDNS("skynet")
2021-10-17 23:47:25 +08:00
assert.EqualError(t, err, "could not find any records")
assert.Equal(t, []models.DNSEntry(nil), dns)
})
t.Run("NodeExists", func(t *testing.T) {
createTestNode()
2021-10-27 00:27:29 +08:00
dns, err := logic.GetCustomDNS("skynet")
2021-10-17 23:47:25 +08:00
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{"10.0.0.3", "newhost", "skynet"}
CreateDNS(entry)
2021-10-27 00:27:29 +08:00
dns, err := logic.GetCustomDNS("skynet")
2021-10-17 23:47:25 +08:00
assert.Nil(t, err)
assert.Equal(t, 1, len(dns))
})
t.Run("MultipleEntries", func(t *testing.T) {
entry := models.DNSEntry{"10.0.0.4", "host4", "skynet"}
CreateDNS(entry)
2021-10-27 00:27:29 +08:00
dns, err := logic.GetCustomDNS("skynet")
2021-10-17 23:47:25 +08:00
assert.Nil(t, err)
assert.Equal(t, 2, len(dns))
})
2021-05-06 05:24:24 +08:00
}
2021-10-17 23:47:25 +08:00
2021-05-06 05:24:24 +08:00
func TestGetDNSEntryNum(t *testing.T) {
2021-09-10 04:20:21 +08:00
database.InitializeDatabase()
2021-10-17 23:47:25 +08:00
deleteAllDNS(t)
2021-09-10 04:20:21 +08:00
deleteAllNetworks()
createNet()
2021-10-17 23:47:25 +08:00
t.Run("NoNodes", func(t *testing.T) {
2021-12-08 01:46:55 +08:00
num, err := logic.GetDNSEntryNum("myhost", "skynet")
2021-10-17 23:47:25 +08:00
assert.Nil(t, err)
assert.Equal(t, 0, num)
})
t.Run("NodeExists", func(t *testing.T) {
entry := models.DNSEntry{"10.0.0.2", "newhost", "skynet"}
_, err := CreateDNS(entry)
assert.Nil(t, err)
2021-12-08 01:46:55 +08:00
num, err := logic.GetDNSEntryNum("newhost", "skynet")
2021-10-17 23:47:25 +08:00
assert.Nil(t, err)
assert.Equal(t, 1, num)
})
2021-05-06 05:24:24 +08:00
}
func TestGetDNS(t *testing.T) {
2021-09-10 04:20:21 +08:00
database.InitializeDatabase()
2021-10-17 23:47:25 +08:00
deleteAllDNS(t)
2021-09-10 04:20:21 +08:00
deleteAllNetworks()
2021-10-17 23:47:25 +08:00
createNet()
t.Run("NoEntries", func(t *testing.T) {
2021-10-27 00:27:29 +08:00
dns, err := logic.GetDNS("skynet")
2021-10-17 23:47:25 +08:00
assert.Nil(t, err)
assert.Nil(t, dns)
})
t.Run("CustomDNSExists", func(t *testing.T) {
entry := models.DNSEntry{"10.0.0.2", "newhost", "skynet"}
_, err := CreateDNS(entry)
assert.Nil(t, err)
2021-10-27 00:27:29 +08:00
dns, err := logic.GetDNS("skynet")
2021-10-17 23:47:25 +08:00
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()
2021-10-27 00:27:29 +08:00
dns, err := logic.GetDNS("skynet")
2021-10-17 23:47:25 +08:00
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{"10.0.0.2", "newhost", "skynet"}
_, err := CreateDNS(entry)
2021-10-27 00:27:29 +08:00
dns, err := logic.GetDNS("skynet")
2021-10-17 23:47:25 +08:00
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))
})
2021-05-06 05:24:24 +08:00
}
2021-10-17 23:47:25 +08:00
2021-05-06 05:24:24 +08:00
func TestCreateDNS(t *testing.T) {
2021-09-10 04:20:21 +08:00
database.InitializeDatabase()
deleteAllDNS(t)
2021-10-17 23:47:25 +08:00
deleteAllNetworks()
2021-05-06 05:24:24 +08:00
createNet()
2021-09-10 04:20:21 +08:00
entry := models.DNSEntry{"10.0.0.2", "newhost", "skynet"}
2021-05-06 05:24:24 +08:00
dns, err := CreateDNS(entry)
assert.Nil(t, err)
2021-10-17 23:47:25 +08:00
assert.Equal(t, "newhost", dns.Name)
}
func TestSetDNS(t *testing.T) {
database.InitializeDatabase()
deleteAllDNS(t)
deleteAllNetworks()
t.Run("NoNetworks", func(t *testing.T) {
2021-10-27 00:27:29 +08:00
err := logic.SetDNS()
2021-10-17 23:47:25 +08:00
assert.Nil(t, err)
info, err := os.Stat("./config/dnsconfig/netmaker.hosts")
assert.Nil(t, err)
assert.False(t, info.IsDir())
assert.Equal(t, int64(0), info.Size())
})
t.Run("NoEntries", func(t *testing.T) {
createNet()
2021-10-27 00:27:29 +08:00
err := logic.SetDNS()
2021-10-17 23:47:25 +08:00
assert.Nil(t, err)
info, err := os.Stat("./config/dnsconfig/netmaker.hosts")
assert.Nil(t, err)
assert.False(t, info.IsDir())
assert.Equal(t, int64(0), info.Size())
})
t.Run("NodeExists", func(t *testing.T) {
createTestNode()
2021-10-27 00:27:29 +08:00
err := logic.SetDNS()
2021-10-17 23:47:25 +08:00
assert.Nil(t, err)
info, err := os.Stat("./config/dnsconfig/netmaker.hosts")
assert.Nil(t, err)
assert.False(t, info.IsDir())
2022-01-07 04:05:38 +08:00
content, err := os.ReadFile("./config/dnsconfig/netmaker.hosts")
2021-10-17 23:47:25 +08:00
assert.Nil(t, err)
assert.Contains(t, string(content), "testnode.skynet")
})
t.Run("EntryExists", func(t *testing.T) {
entry := models.DNSEntry{"10.0.0.3", "newhost", "skynet"}
CreateDNS(entry)
2021-10-27 00:27:29 +08:00
err := logic.SetDNS()
2021-10-17 23:47:25 +08:00
assert.Nil(t, err)
info, err := os.Stat("./config/dnsconfig/netmaker.hosts")
assert.Nil(t, err)
assert.False(t, info.IsDir())
2022-01-07 04:05:38 +08:00
content, err := os.ReadFile("./config/dnsconfig/netmaker.hosts")
2021-10-17 23:47:25 +08:00
assert.Nil(t, err)
assert.Contains(t, string(content), "newhost.skynet")
})
2021-05-06 05:24:24 +08:00
}
2021-10-17 23:47:25 +08:00
2021-05-06 05:24:24 +08:00
func TestGetDNSEntry(t *testing.T) {
2021-09-10 04:20:21 +08:00
database.InitializeDatabase()
2021-10-17 23:47:25 +08:00
deleteAllDNS(t)
2021-09-10 04:20:21 +08:00
deleteAllNetworks()
createNet()
createTestNode()
entry := models.DNSEntry{"10.0.0.2", "newhost", "skynet"}
CreateDNS(entry)
2021-10-17 23:47:25 +08:00
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)
})
2021-05-06 05:24:24 +08:00
}
2021-12-08 01:46:55 +08:00
// func TestUpdateDNS(t *testing.T) {
// var newentry models.DNSEntry
// database.InitializeDatabase()
// deleteAllDNS(t)
// deleteAllNetworks()
// createNet()
// entry := models.DNSEntry{"10.0.0.2", "newhost", "skynet"}
// CreateDNS(entry)
// t.Run("change address", func(t *testing.T) {
// newentry.Address = "10.0.0.75"
// updated, err := UpdateDNS(newentry, entry)
// assert.Nil(t, err)
// assert.Equal(t, newentry.Address, updated.Address)
// })
// t.Run("change name", func(t *testing.T) {
// newentry.Name = "newname"
// updated, err := UpdateDNS(newentry, entry)
// assert.Nil(t, err)
// assert.Equal(t, newentry.Name, updated.Name)
// })
// t.Run("change network", func(t *testing.T) {
// newentry.Network = "wirecat"
// updated, err := UpdateDNS(newentry, entry)
// assert.Nil(t, err)
// assert.NotEqual(t, newentry.Network, updated.Network)
// })
// }
2021-05-06 05:24:24 +08:00
func TestDeleteDNS(t *testing.T) {
2021-09-10 04:20:21 +08:00
database.InitializeDatabase()
2021-10-17 23:47:25 +08:00
deleteAllDNS(t)
deleteAllNetworks()
createNet()
entry := models.DNSEntry{"10.0.0.2", "newhost", "skynet"}
CreateDNS(entry)
2021-05-06 05:24:24 +08:00
t.Run("EntryExists", func(t *testing.T) {
2021-12-08 01:46:55 +08:00
err := logic.DeleteDNS("newhost", "skynet")
2021-05-06 05:24:24 +08:00
assert.Nil(t, err)
})
2021-10-17 23:47:25 +08:00
t.Run("NodeExists", func(t *testing.T) {
2021-12-08 01:46:55 +08:00
err := logic.DeleteDNS("myhost", "skynet")
2021-05-06 05:24:24 +08:00
assert.Nil(t, err)
})
2021-10-17 23:47:25 +08:00
t.Run("NoEntries", func(t *testing.T) {
2021-12-08 01:46:55 +08:00
err := logic.DeleteDNS("myhost", "skynet")
2021-10-17 23:47:25 +08:00
assert.Nil(t, err)
})
2021-05-06 05:24:24 +08:00
}
2021-05-10 01:31:50 +08:00
2021-05-06 05:24:24 +08:00
func TestValidateDNSUpdate(t *testing.T) {
2021-09-10 04:20:21 +08:00
database.InitializeDatabase()
2021-10-17 23:47:25 +08:00
deleteAllDNS(t)
deleteAllNetworks()
createNet()
2021-05-06 05:24:24 +08:00
entry := models.DNSEntry{"10.0.0.2", "myhost", "skynet"}
t.Run("BadNetwork", func(t *testing.T) {
change := models.DNSEntry{"10.0.0.2", "myhost", "badnet"}
2021-12-08 01:46:55 +08:00
err := logic.ValidateDNSUpdate(change, entry)
2021-05-06 05:24:24 +08:00
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{"10.0.0.2", "myhost", ""}
2021-12-08 01:46:55 +08:00
err := logic.ValidateDNSUpdate(change, entry)
2021-05-06 05:24:24 +08:00
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"}
2021-12-08 01:46:55 +08:00
err := logic.ValidateDNSUpdate(change, entry)
2021-05-06 05:24:24 +08:00
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{"10.0.256.1", "myhost", "skynet"}
2021-12-08 01:46:55 +08:00
err := logic.ValidateDNSUpdate(change, entry)
2021-05-06 05:24:24 +08:00
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{"10.0.0.2", "", "skynet"}
2021-12-08 01:46:55 +08:00
err := logic.ValidateDNSUpdate(change, entry)
2021-05-06 05:24:24 +08:00
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 := ""
2021-09-10 04:20:21 +08:00
for i := 1; i < 194; i++ {
2021-05-06 05:24:24 +08:00
name = name + "a"
}
change := models.DNSEntry{"10.0.0.2", name, "skynet"}
2021-12-08 01:46:55 +08:00
err := logic.ValidateDNSUpdate(change, entry)
2021-05-06 05:24:24 +08:00
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{"10.0.0.2", "myhost", "wirecat"}
2021-10-17 23:47:25 +08:00
CreateDNS(entry)
CreateDNS(change)
2021-12-08 01:46:55 +08:00
err := logic.ValidateDNSUpdate(change, entry)
2021-05-06 05:24:24 +08:00
assert.NotNil(t, err)
assert.Contains(t, err.Error(), "Field validation for 'Name' failed on the 'name_unique' tag")
2021-10-17 23:47:25 +08:00
//cleanup
2021-12-08 01:46:55 +08:00
err = logic.DeleteDNS("myhost", "wirecat")
2021-10-17 23:47:25 +08:00
assert.Nil(t, err)
2021-05-06 05:24:24 +08:00
})
}
func TestValidateDNSCreate(t *testing.T) {
2021-09-10 04:20:21 +08:00
database.InitializeDatabase()
2021-12-08 01:46:55 +08:00
_ = logic.DeleteDNS("mynode", "skynet")
2021-05-06 05:24:24 +08:00
t.Run("NoNetwork", func(t *testing.T) {
entry := models.DNSEntry{"10.0.0.2", "myhost", "badnet"}
2021-12-08 01:46:55 +08:00
err := logic.ValidateDNSCreate(entry)
2021-05-06 05:24:24 +08:00
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"}
2021-12-08 01:46:55 +08:00
err := logic.ValidateDNSCreate(entry)
2021-05-06 05:24:24 +08:00
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{"10.0.256.1", "myhost", "skynet"}
2021-12-08 01:46:55 +08:00
err := logic.ValidateDNSCreate(entry)
2021-05-06 05:24:24 +08:00
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{"10.0.0.2", "", "skynet"}
2021-12-08 01:46:55 +08:00
err := logic.ValidateDNSCreate(entry)
2021-05-06 05:24:24 +08:00
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 := ""
2021-09-10 04:20:21 +08:00
for i := 1; i < 194; i++ {
2021-05-06 05:24:24 +08:00
name = name + "a"
}
entry := models.DNSEntry{"10.0.0.2", name, "skynet"}
2021-12-08 01:46:55 +08:00
err := logic.ValidateDNSCreate(entry)
2021-05-06 05:24:24 +08:00
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{"10.0.0.2", "myhost", "skynet"}
_, _ = CreateDNS(entry)
2021-12-08 01:46:55 +08:00
err := logic.ValidateDNSCreate(entry)
2021-05-06 05:24:24 +08:00
assert.NotNil(t, err)
assert.Contains(t, err.Error(), "Field validation for 'Name' failed on the 'name_unique' tag")
})
}
2021-09-10 04:20:21 +08:00
func deleteAllDNS(t *testing.T) {
2021-12-08 01:46:55 +08:00
dns, err := logic.GetAllDNS()
2021-10-17 23:47:25 +08:00
assert.Nil(t, err)
2021-09-10 04:20:21 +08:00
for _, record := range dns {
2021-12-08 01:46:55 +08:00
err := logic.DeleteDNS(record.Name, record.Network)
2021-10-17 23:47:25 +08:00
assert.Nil(t, err)
2021-09-10 04:20:21 +08:00
}
}