Skip to content

Commit

Permalink
Merge pull request #96 from johanix/music-unit-testtest
Browse files Browse the repository at this point in the history
Music unit testtest
  • Loading branch information
johanix authored Nov 22, 2024
2 parents 54d28b4 + 54f6395 commit 4980cfd
Show file tree
Hide file tree
Showing 11 changed files with 872 additions and 11 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ server/tdns-server
agent/tdns-agent
cli/tdns-cli
dog/dog
sidecar/music-sidecar
sidecar-cli/sidecar-cli

*/version.go

Expand Down
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

all:
$(MAKE) -C ./server/
$(MAKE) -C ./cli/
Expand All @@ -22,3 +23,5 @@ install:
$(MAKE) -C ./dog/ install
$(MAKE) -C ./sidecar/ install
$(MAKE) -C ./sidecar-cli/ install

include utils/Makefile.common
10 changes: 7 additions & 3 deletions music/fsm/go.mod
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
module fsm

go 1.22.0

toolchain go1.23.2
go 1.23.2

replace (
github.com/johanix/tdns/music => ../
github.com/johanix/tdns/music/mocks => ../mocks
github.com/johanix/tdns/tdns => ../../tdns
)

require (
github.com/johanix/tdns/music v0.0.0-00010101000000-000000000000
github.com/johanix/tdns/music/mocks v0.0.0-00010101000000-000000000000
github.com/miekg/dns v1.1.62
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
github.com/go-playground/locales v0.14.1 // indirect
Expand All @@ -32,11 +33,14 @@ require (
github.com/orcaman/concurrent-map/v2 v2.0.1 // indirect
github.com/pelletier/go-toml v1.9.5 // indirect
github.com/pelletier/go-toml/v2 v2.0.5 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/afero v1.9.2 // indirect
github.com/spf13/cast v1.5.0 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/spf13/viper v1.14.0 // indirect
github.com/stretchr/objx v0.5.2 // indirect
github.com/stretchr/testify v1.9.0 // indirect
github.com/subosito/gotenv v1.4.1 // indirect
github.com/twotwotwo/sorts v0.0.0-20160814051341-bf5c1f2b8553 // indirect
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
Expand Down
13 changes: 8 additions & 5 deletions music/fsm/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/rogpeppe/go-internal v1.6.1 h1:/FiVV8dS/e+YqF2JvO3yXRFbBLTIuSDkuC7aBOAvL+k=
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
github.com/rogpeppe/go-internal v1.8.0 h1:FCbCCtXNOY3UtUuHUYaghJg4y7Fd14rXifAYUAtL9R8=
github.com/rogpeppe/go-internal v1.8.0/go.mod h1:WmiCO8CzOY8rg0OYDC4/i/2WRWAB6poM+XZ2dLUbcbE=
github.com/spf13/afero v1.9.2 h1:j49Hj62F0n+DaZ1dDCvhABaPNSGNkt32oRFxI33IEMw=
github.com/spf13/afero v1.9.2/go.mod h1:iUV7ddyEEZPO5gA3zD4fJt6iStLlL+Lg4m2cihcDf8Y=
github.com/spf13/cast v1.5.0 h1:rj3WzYc11XZaIZMPKmwP96zkFEnnAmV8s6XbB2aY32w=
Expand All @@ -188,14 +188,16 @@ github.com/spf13/viper v1.14.0 h1:Rg7d3Lo706X9tHsJMUjdiwMpHB7W8WnSVOssIY+JElU=
github.com/spf13/viper v1.14.0/go.mod h1:WT//axPky3FdvXHzGw33dNdXXXfFQqmEalje+egj8As=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/subosito/gotenv v1.4.1 h1:jyEFiXpy21Wm81FBN71l9VoMMV8H8jG+qIK3GCpY6Qs=
github.com/subosito/gotenv v1.4.1/go.mod h1:ayKnFf/c6rvx/2iiLrJUk1e6plDbT3edrFNGqEflhK0=
github.com/twotwotwo/sorts v0.0.0-20160814051341-bf5c1f2b8553 h1:DRC1ubdb3ZmyyIeCSTxjZIQAnpLPfKVgYrLETQuOPjo=
Expand Down Expand Up @@ -503,8 +505,9 @@ google.golang.org/protobuf v1.23.1-0.20200526195155-81db48ad09cc/go.mod h1:EGpAD
google.golang.org/protobuf v1.24.0/go.mod h1:r/3tXBNzIEhYS9I1OUVjXDlt8tc493IdKGjtUeSXeh4=
google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlbajtzgsN7c=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA=
gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
Expand Down
200 changes: 200 additions & 0 deletions music/fsm/join_sync_dnskeys_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
// join_sync_dnskeys_test.go (written by CodeRabbit)

package fsm

import (
"testing"

"github.com/johanix/tdns/music"
"github.com/johanix/tdns/music/mocks"
"github.com/miekg/dns"
)

func TestJoinSyncDnskeys(t *testing.T) {
// Set up a mock zone and signers
zone := &music.Zone{
Name: "example.com",
ZoneType: "normal",
SGroup: &music.SignerGroup{
Name: "default",
SignerMap: map[string]*music.Signer{
"signer1": {
Name: "signer1",
Method: "mock",
},
"signer2": {
Name: "signer2",
Method: "mock",
},
},
},
}

// Mock DNSKEY records for each signer
dnskey1 := &dns.DNSKEY{
Hdr: dns.RR_Header{
Name: "example.com.",
Rrtype: dns.TypeDNSKEY,
Class: dns.ClassINET,
Ttl: 3600,
},
Flags: 256, // ZSK
Protocol: 3,
Algorithm: dns.RSASHA256,
PublicKey: "publickey1",
}

dnskey2 := &dns.DNSKEY{
Hdr: dns.RR_Header{
Name: "example.com.",
Rrtype: dns.TypeDNSKEY,
Class: dns.ClassINET,
Ttl: 3600,
},
Flags: 256, // ZSK
Protocol: 3,
Algorithm: dns.RSASHA256,
PublicKey: "publickey2",
}

// Set up mock updater
music.Updaters["mock"] = &mocks.MockUpdater{
FetchRRset: func(signer *music.Signer, zoneName, owner string, rrtype uint16) (error, []dns.RR) {
if signer.Name == "signer1" {
return nil, []dns.RR{dnskey1}
}
if signer.Name == "signer2" {
return nil, []dns.RR{dnskey2}
}
return nil, nil
},
Update: func(signer *music.Signer, zoneName, owner string, inserts, removes *[][]dns.RR) error {
// Simulate successful update
return nil
},
}

// Execute the function under test
success := JoinSyncDnskeys(zone)
if !success {
t.Errorf("JoinSyncDnskeys failed")
}

// Verify that keys have been set for signers
if len(zone.SignerDnskeys) == 0 {
t.Errorf("SignerDnskeys map is empty")
}
}

func TestVerifyDnskeysSynched(t *testing.T) {
// Set up mock zone and signers
zone := &music.Zone{
Name: "example.com",
ZoneType: "normal",
SGroup: &music.SignerGroup{
Name: "default",
SignerMap: map[string]*music.Signer{
"signer1": {
Name: "signer1",
Method: "mock",
},
"signer2": {
Name: "signer2",
Method: "mock",
},
},
},
}

dnskey := &dns.DNSKEY{
Hdr: dns.RR_Header{
Name: "example.com.",
Rrtype: dns.TypeDNSKEY,
Class: dns.ClassINET,
Ttl: 3600,
},
Flags: 256,
Protocol: 3,
Algorithm: dns.RSASHA256,
PublicKey: "publickey",
}

// Both signers have the same DNSKEY
music.Updaters["mock"] = &mocks.MockUpdater{
FetchRRsetFunc: func(signer *music.Signer, zoneName, owner string, rrtype uint16) (error, []dns.RR) {
return nil, []dns.RR{dnskey}
},
}

// Execute the function under test
success := VerifyDnskeysSynched(zone)
if !success {
t.Errorf("VerifyDnskeysSynched failed: DNSKEYs are not synchronized")
}
}

func TestVerifyDnskeysNotSynched(t *testing.T) {
// Set up mock zone and signers with different DNSKEYs
zone := &music.Zone{
Name: "example.com",
ZoneType: "normal",
SGroup: &music.SignerGroup{
Name: "default",
SignerMap: map[string]*music.Signer{
"signer1": {
Name: "signer1",
Method: "mock",
},
"signer2": {
Name: "signer2",
Method: "mock",
},
},
},
}

dnskey1 := &dns.DNSKEY{
Hdr: dns.RR_Header{
Name: "example.com.",
Rrtype: dns.TypeDNSKEY,
Class: dns.ClassINET,
Ttl: 3600,
},
Flags: 256,
Protocol: 3,
Algorithm: dns.RSASHA256,
PublicKey: "publickey1",
}

dnskey2 := &dns.DNSKEY{
Hdr: dns.RR_Header{
Name: "example.com.",
Rrtype: dns.TypeDNSKEY,
Class: dns.ClassINET,
Ttl: 3600,
},
Flags: 256,
Protocol: 3,
Algorithm: dns.RSASHA256,
PublicKey: "publickey2",
}

// Signers have different DNSKEYs
music.Updaters["mock"] = &mocks.MockUpdater{
FetchRRsetFunc: func(signer *music.Signer, zoneName, owner string, rrtype uint16) (error, []dns.RR) {
if signer.Name == "signer1" {
return nil, []dns.RR{dnskey1}
}
if signer.Name == "signer2" {
return nil, []dns.RR{dnskey2}
}
return nil, nil
},
}

// Execute the function under test
success := VerifyDnskeysSynched(zone)
if success {
t.Errorf("VerifyDnskeysSynched should have failed: DNSKEYs are not synchronized")
}
}
58 changes: 58 additions & 0 deletions music/mocks/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
module mocks

go 1.23.2

replace (
github.com/johanix/tdns/music => ../
github.com/johanix/tdns/music/fsm => ../fsm
github.com/johanix/tdns/tdns => ../../tdns
)

require (
github.com/johanix/tdns/music v0.0.0-00010101000000-000000000000
github.com/miekg/dns v1.1.62
github.com/stretchr/testify v1.9.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.22.1 // indirect
github.com/gookit/color v1.5.4 // indirect
github.com/gookit/goutil v0.6.15 // indirect
github.com/gorilla/mux v1.8.1 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/johanix/tdns/tdns v0.0.0-00010101000000-000000000000 // indirect
github.com/leodido/go-urn v1.4.0 // indirect
github.com/magiconair/properties v1.8.6 // indirect
github.com/mattn/go-sqlite3 v1.14.16 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/orcaman/concurrent-map/v2 v2.0.1 // indirect
github.com/pelletier/go-toml v1.9.5 // indirect
github.com/pelletier/go-toml/v2 v2.0.5 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/afero v1.9.2 // indirect
github.com/spf13/cast v1.5.0 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/spf13/viper v1.14.0 // indirect
github.com/stretchr/objx v0.5.2 // indirect
github.com/subosito/gotenv v1.4.1 // indirect
github.com/twotwotwo/sorts v0.0.0-20160814051341-bf5c1f2b8553 // indirect
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
golang.org/x/crypto v0.27.0 // indirect
golang.org/x/exp v0.0.0-20220909182711-5c715a9e8561 // indirect
golang.org/x/mod v0.21.0 // indirect
golang.org/x/net v0.29.0 // indirect
golang.org/x/sync v0.8.0 // indirect
golang.org/x/sys v0.25.0 // indirect
golang.org/x/text v0.18.0 // indirect
golang.org/x/tools v0.25.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit 4980cfd

Please sign in to comment.