45 lines
1.6 KiB
Go
45 lines
1.6 KiB
Go
package planner
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func validIntent() Intent {
|
|
return Intent{ProtocolVersion: 1, HostID: "host-one", InstanceID: "git-one", AppID: "gitea", PackageDigest: "sha256:" + strings.Repeat("a", 64), ImageDigest: "sha256:" + strings.Repeat("b", 64), Domain: "git.example.com", ObservedStateDigest: "sha256:" + strings.Repeat("c", 64)}
|
|
}
|
|
|
|
func TestIntentValidation(t *testing.T) {
|
|
if err := validIntent().Validate(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
cases := []struct {
|
|
name string
|
|
change func(*Intent)
|
|
}{
|
|
{"protocol", func(i *Intent) { i.ProtocolVersion = 2 }},
|
|
{"host empty", func(i *Intent) { i.HostID = "" }},
|
|
{"path escape", func(i *Intent) { i.InstanceID = "../git" }},
|
|
{"shell", func(i *Intent) { i.AppID = "git;id" }},
|
|
{"long id", func(i *Intent) { i.InstanceID = strings.Repeat("a", 49) }},
|
|
{"floating tag", func(i *Intent) { i.ImageDigest = "gitea:latest" }},
|
|
{"package hash", func(i *Intent) { i.PackageDigest = "sha256:xyz" }},
|
|
{"state missing", func(i *Intent) { i.ObservedStateDigest = "" }},
|
|
{"url", func(i *Intent) { i.Domain = "https://git.example.com" }},
|
|
{"wildcard", func(i *Intent) { i.Domain = "*.example.com" }},
|
|
{"label", func(i *Intent) { i.Domain = "-git.example.com" }},
|
|
{"empty label", func(i *Intent) { i.Domain = "git..com" }},
|
|
{"uppercase", func(i *Intent) { i.Domain = "Git.example.com" }},
|
|
{"ip", func(i *Intent) { i.Domain = "127.0.0.1" }},
|
|
}
|
|
for _, tc := range cases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
i := validIntent()
|
|
tc.change(&i)
|
|
if i.Validate() == nil {
|
|
t.Fatal("accepted invalid intent")
|
|
}
|
|
})
|
|
}
|
|
}
|