diff --git a/controllers/common_test.go b/controllers/common_test.go index 6e026c1a..f00f8e86 100644 --- a/controllers/common_test.go +++ b/controllers/common_test.go @@ -124,7 +124,7 @@ func TestSetNetworkNodesLastModified(t *testing.T) { } func createTestNode() models.Node { - createnode := models.Node{PublicKey: "DM5qhLAE20PG9BbfBCger+Ac9D2NDOwCtY1rbYDLf34=", Endpoint: "10.0.0.1", MacAddress: "01:02:03:04:05:06", Password: "password", Network: "skynet"} + createnode := models.Node{PublicKey: "DM5qhLAE20PG9BbfBCger+Ac9D2NDOwCtY1rbYDLf34=", Name: "testnode", Endpoint: "10.0.0.1", MacAddress: "01:02:03:04:05:06", Password: "password", Network: "skynet"} node, _ := logic.CreateNode(createnode, "skynet") return node } diff --git a/controllers/dnsHttpController_test.go b/controllers/dnsHttpController_test.go index db6a7443..632fc0d9 100644 --- a/controllers/dnsHttpController_test.go +++ b/controllers/dnsHttpController_test.go @@ -1,100 +1,309 @@ package controller import ( + "io/ioutil" + "os" "testing" "github.com/gravitl/netmaker/database" "github.com/gravitl/netmaker/dnslogic" + "github.com/gravitl/netmaker/logic" "github.com/gravitl/netmaker/models" "github.com/stretchr/testify/assert" ) +func TestGetAllDNS(t *testing.T) { + database.InitializeDatabase() + deleteAllDNS(t) + deleteAllNetworks() + createNet() + t.Run("NoEntries", func(t *testing.T) { + entries, err := GetAllDNS() + 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) + entries, err := GetAllDNS() + 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) + entries, err := GetAllDNS() + assert.Nil(t, err) + assert.Equal(t, 2, len(entries)) + }) +} + func TestGetNodeDNS(t *testing.T) { database.InitializeDatabase() + deleteAllDNS(t) deleteAllNetworks() createNet() - createTestNode() - dns, err := GetNodeDNS("skynet") - assert.Nil(t, err) - t.Log(dns) + t.Run("NoNodes", func(t *testing.T) { + dns, err := GetNodeDNS("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 := GetNodeDNS("skynet") + 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"} + _, err := logic.CreateNode(createnode, "skynet") + assert.Nil(t, err) + dns, err := GetNodeDNS("skynet") + assert.Nil(t, err) + assert.Equal(t, 2, len(dns)) + }) } func TestGetCustomDNS(t *testing.T) { - t.Skip() database.InitializeDatabase() + deleteAllDNS(t) deleteAllNetworks() - createNet() - createTestNode() - dns, err := dnslogic.GetCustomDNS("skynet") - assert.Nil(t, err) - t.Log(dns) + t.Run("NoNetworks", func(t *testing.T) { + dns, err := dnslogic.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 := dnslogic.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 := dnslogic.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{"10.0.0.3", "newhost", "skynet"} + CreateDNS(entry) + dns, err := dnslogic.GetCustomDNS("skynet") + 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) + dns, err := dnslogic.GetCustomDNS("skynet") + assert.Nil(t, err) + assert.Equal(t, 2, len(dns)) + }) } + func TestGetDNSEntryNum(t *testing.T) { database.InitializeDatabase() + deleteAllDNS(t) deleteAllNetworks() createNet() - createTestNode() - num, err := GetDNSEntryNum("myhost", "skynet") - assert.Nil(t, err) - t.Log(num) + t.Run("NoNodes", func(t *testing.T) { + num, err := GetDNSEntryNum("myhost", "skynet") + 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) + num, err := GetDNSEntryNum("newhost", "skynet") + assert.Nil(t, err) + assert.Equal(t, 1, num) + }) } func TestGetDNS(t *testing.T) { database.InitializeDatabase() + deleteAllDNS(t) deleteAllNetworks() - dns, err := dnslogic.GetDNS("skynet") - assert.Nil(t, err) - t.Log(dns) + createNet() + t.Run("NoEntries", func(t *testing.T) { + dns, err := dnslogic.GetDNS("skynet") + 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) + dns, err := dnslogic.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 := dnslogic.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{"10.0.0.2", "newhost", "skynet"} + _, err := CreateDNS(entry) + dns, err := dnslogic.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) { database.InitializeDatabase() - deleteAllNetworks() deleteAllDNS(t) + deleteAllNetworks() createNet() - //dns, err := GetDNS("skynet") - //assert.Nil(t, err) - //for _, entry := range dns { - // _, _ = DeleteDNS(entry.Name, "skynet") - //} entry := models.DNSEntry{"10.0.0.2", "newhost", "skynet"} - err := ValidateDNSCreate(entry) - assert.Nil(t, err) - if err != nil { - t.Log(err) - } dns, err := CreateDNS(entry) assert.Nil(t, err) - t.Log(dns) + assert.Equal(t, "newhost", dns.Name) } + +func TestSetDNS(t *testing.T) { + database.InitializeDatabase() + deleteAllDNS(t) + deleteAllNetworks() + t.Run("NoNetworks", func(t *testing.T) { + err := dnslogic.SetDNS() + 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() + err := dnslogic.SetDNS() + 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() + err := dnslogic.SetDNS() + assert.Nil(t, err) + info, err := os.Stat("./config/dnsconfig/netmaker.hosts") + assert.Nil(t, err) + assert.False(t, info.IsDir()) + content, err := ioutil.ReadFile("./config/dnsconfig/netmaker.hosts") + 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) + err := dnslogic.SetDNS() + assert.Nil(t, err) + info, err := os.Stat("./config/dnsconfig/netmaker.hosts") + assert.Nil(t, err) + assert.False(t, info.IsDir()) + content, err := ioutil.ReadFile("./config/dnsconfig/netmaker.hosts") + assert.Nil(t, err) + assert.Contains(t, string(content), "newhost.skynet") + }) + +} + func TestGetDNSEntry(t *testing.T) { database.InitializeDatabase() + deleteAllDNS(t) deleteAllNetworks() createNet() createTestNode() entry := models.DNSEntry{"10.0.0.2", "newhost", "skynet"} CreateDNS(entry) - entry, err := GetDNSEntry("newhost", "skynet") - assert.Nil(t, err) - t.Log(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 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) + }) } func TestDeleteDNS(t *testing.T) { database.InitializeDatabase() + deleteAllDNS(t) + deleteAllNetworks() + createNet() + entry := models.DNSEntry{"10.0.0.2", "newhost", "skynet"} + CreateDNS(entry) t.Run("EntryExists", func(t *testing.T) { - err := DeleteDNS("myhost", "skynet") + err := DeleteDNS("newhost", "skynet") assert.Nil(t, err) }) - t.Run("NoEntry", func(t *testing.T) { + t.Run("NodeExists", func(t *testing.T) { err := DeleteDNS("myhost", "skynet") assert.Nil(t, err) }) + t.Run("NoEntries", func(t *testing.T) { + err := DeleteDNS("myhost", "skynet") + assert.Nil(t, err) + }) } func TestValidateDNSUpdate(t *testing.T) { database.InitializeDatabase() + deleteAllDNS(t) + deleteAllNetworks() + createNet() entry := models.DNSEntry{"10.0.0.2", "myhost", "skynet"} - _ = DeleteDNS("mynode", "skynet") t.Run("BadNetwork", func(t *testing.T) { change := models.DNSEntry{"10.0.0.2", "myhost", "badnet"} err := ValidateDNSUpdate(change, entry) @@ -140,11 +349,14 @@ func TestValidateDNSUpdate(t *testing.T) { }) t.Run("NameUnique", func(t *testing.T) { change := models.DNSEntry{"10.0.0.2", "myhost", "wirecat"} - _, _ = CreateDNS(entry) - _, _ = CreateDNS(change) + CreateDNS(entry) + CreateDNS(change) err := ValidateDNSUpdate(change, entry) assert.NotNil(t, err) assert.Contains(t, err.Error(), "Field validation for 'Name' failed on the 'name_unique' tag") + //cleanup + err = DeleteDNS("myhost", "wirecat") + assert.Nil(t, err) }) } @@ -196,11 +408,18 @@ func TestValidateDNSCreate(t *testing.T) { func deleteAllDNS(t *testing.T) { dns, err := GetAllDNS() - t.Log(err) - t.Log(dns) + assert.Nil(t, err) for _, record := range dns { - t.Log(dns) err := DeleteDNS(record.Name, record.Network) - t.Log(err) + assert.Nil(t, err) } } + +func TestDeleteAllDNS(t *testing.T) { + database.InitializeDatabase() + deleteAllDNS(t) + dns, err := GetAllDNS() + assert.Nil(t, err) + assert.Equal(t, 0, len(dns)) + t.Log(dns) +} diff --git a/go.mod b/go.mod index fb7785c9..00453777 100644 --- a/go.mod +++ b/go.mod @@ -8,7 +8,7 @@ require ( github.com/golang/protobuf v1.5.2 // indirect github.com/gorilla/handlers v1.5.1 github.com/gorilla/mux v1.8.0 - github.com/lib/pq v1.10.3 // indirect + github.com/lib/pq v1.10.3 github.com/mattn/go-sqlite3 v1.14.8 github.com/rqlite/gorqlite v0.0.0-20210514125552-08ff1e76b22f github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e