99 lines
3.4 KiB
Go
99 lines
3.4 KiB
Go
package apigen
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestInfrastructureProfileOwnership(t *testing.T) {
|
|
dir := filepath.Join(t.TempDir(), "project")
|
|
o := Options{Dir: dir, Module: "example.com/profile", FrameworkDir: frameworkRoot(t), Database: "mysql", Storage: "local", LogOutput: "file"}
|
|
if _, err := New(o); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
o = Options{Dir: dir, API: filepath.Join(dir, "api/service.api"), Check: true}
|
|
if _, err := Generate(o); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
before := snapshot(t, dir)
|
|
for _, changed := range []Options{
|
|
{Database: "none"}, {Storage: "s3"}, {LogOutput: "stdout"}, {LogFormat: "text"},
|
|
} {
|
|
changed.Dir, changed.API = dir, o.API
|
|
if _, err := Generate(changed); err == nil {
|
|
t.Fatal("profile switch accepted")
|
|
}
|
|
if !sameFiles(before, snapshot(t, dir)) {
|
|
t.Fatal("profile rejection changed project")
|
|
}
|
|
}
|
|
// Restoring a missing seed must use the saved profile, not current defaults.
|
|
if err := os.Remove(filepath.Join(dir, "internal/servicecontext/storage.go")); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := Generate(o); err == nil {
|
|
t.Fatal("check accepted missing infrastructure")
|
|
}
|
|
o.Check = false
|
|
if _, err := Generate(o); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !sameFiles(before, snapshot(t, dir)) {
|
|
t.Fatal("profile reconstruction changed project")
|
|
}
|
|
}
|
|
|
|
func TestAPIInfrastructureConsumers(t *testing.T) {
|
|
if os.Getenv("GO_OMNI_SCAFFOLD_INTEGRATION") == "" {
|
|
t.Skip("consumer module integration")
|
|
}
|
|
for _, db := range []string{"none", "mysql"} {
|
|
for _, storage := range []string{"none", "local", "s3", "r2", "oss"} {
|
|
t.Run(db+"/"+storage, func(t *testing.T) {
|
|
dir := filepath.Join(t.TempDir(), "project")
|
|
if _, err := New(Options{Dir: dir, Module: "example.com/profile", FrameworkDir: frameworkRoot(t), Database: db, Storage: storage, LogOutput: "file"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := Generate(Options{Dir: dir, API: filepath.Join(dir, "api/service.api"), Check: true}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
test := `package settings
|
|
import("testing";"os")
|
|
func TestConfiguration(t *testing.T){
|
|
t.Setenv("GO_OMNI_DB_DSN","test:test@tcp(127.0.0.1:1)/test")
|
|
t.Setenv("GO_OMNI_STORAGE_SIGNING_KEY","01234567890123456789012345678901")
|
|
t.Setenv("GO_OMNI_STORAGE_ACCESS_KEY","test");t.Setenv("GO_OMNI_STORAGE_SECRET_KEY","test")
|
|
t.Setenv("GO_OMNI_STORAGE_BUCKET","test");t.Setenv("GO_OMNI_STORAGE_ENDPOINT","ENDPOINT")
|
|
t.Setenv("GO_OMNI_STORAGE_ACCOUNT_ID","01234567890123456789012345678901")
|
|
cfg,err:=Load("../../etc/config.yaml");if err!=nil{t.Fatal(err)}
|
|
if cfg.Server.Log.File==""{t.Fatal("file logger absent")}
|
|
t.Setenv("GO_OMNI_LOG_OUTPUT","stdout");cfg,err=Load("");if err!=nil||cfg.Server.Log.File!=""{t.Fatalf("override: %v",err)}
|
|
_=os.Getenv("unused")
|
|
}`
|
|
endpoint := ""
|
|
if storage == "s3" {
|
|
endpoint = "127.0.0.1:9000"
|
|
}
|
|
if storage == "oss" {
|
|
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
|
|
}
|
|
writeFile(t, filepath.Join(dir, "internal/settings/profile_test.go"), strings.ReplaceAll(test, `"ENDPOINT"`, `"`+endpoint+`"`))
|
|
ctx, cancel := context.WithTimeout(context.Background(), 90*time.Second)
|
|
defer cancel()
|
|
cmd := exec.CommandContext(ctx, "go", "test", "-mod=mod", "-race", "-count=1", "./...")
|
|
cmd.Dir = dir
|
|
cmd.Env = append(os.Environ(), "GOWORK=off", "GOFLAGS=")
|
|
out, err := cmd.CombinedOutput()
|
|
if err != nil {
|
|
t.Fatalf("%v\n%s", err, out)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
}
|