Add more unit tests

This commit is contained in:
Jamie Curnow
2024-11-07 13:00:07 +10:00
parent 208037946f
commit 3774a40498
14 changed files with 810 additions and 24 deletions

View File

@ -9,6 +9,12 @@ import (
"github.com/rotisserie/eris"
)
var (
certificateGetByID = certificate.GetByID
upstreamGetByID = upstream.GetByID
nginxtemplateGetByID = nginxtemplate.GetByID
)
// ValidateHost will check if associated objects exist and other checks
// will return a nil error if things are OK
func ValidateHost(h host.Model) error {
@ -16,14 +22,14 @@ func ValidateHost(h host.Model) error {
// Check certificate exists and is valid
// This will not determine if the certificate is Ready to use,
// as this validation only cares that the row exists.
if _, cErr := certificate.GetByID(h.CertificateID.Uint); cErr != nil {
if _, cErr := certificateGetByID(h.CertificateID.Uint); cErr != nil {
return eris.Wrapf(cErr, "Certificate #%d does not exist", h.CertificateID.Uint)
}
}
if h.UpstreamID.Uint > 0 {
// Check upstream exists
if _, uErr := upstream.GetByID(h.UpstreamID.Uint); uErr != nil {
if _, uErr := upstreamGetByID(h.UpstreamID.Uint); uErr != nil {
return eris.Wrapf(uErr, "Upstream #%d does not exist", h.UpstreamID.Uint)
}
}
@ -37,7 +43,7 @@ func ValidateHost(h host.Model) error {
}
// Check the nginx template exists and has the same type.
nginxTemplate, tErr := nginxtemplate.GetByID(h.NginxTemplateID)
nginxTemplate, tErr := nginxtemplateGetByID(h.NginxTemplateID)
if tErr != nil {
return eris.Wrapf(tErr, "Host Template #%d does not exist", h.NginxTemplateID)
}