106 lines
4.2 KiB
Go
106 lines
4.2 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/svc/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 config
|
|
import("testing";"os";"strings")
|
|
func TestConfiguration(t *testing.T){
|
|
t.Setenv("GO_OMNI_DB_DATABASE","test");t.Setenv("GO_OMNI_DB_USER","test");t.Setenv("GO_OMNI_DB_PASSWORD","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");t.Setenv("GO_OMNI_SERVER_LOG_FILE","./implicit.log")
|
|
cfg,err=Load("../../etc/config.yaml");if err!=nil||cfg.Server.Log.File!=Default().Server.Log.File{t.Fatalf("implicit environment override: %v",err)}
|
|
raw,err:=os.ReadFile("../../etc/config.yaml");if err!=nil{t.Fatal(err)}
|
|
// Add explicit fields while keeping storage and DB requirements.
|
|
raw=[]byte(strings.Replace(string(raw),"server:\n","server:\n log:\n output: '${OUTPUT:-stdout}'\n file: ''\n rotation:\n enabled: false\n",1))
|
|
path:="explicit.yaml";if err=os.WriteFile(path,raw,0600);err!=nil{t.Fatal(err)};defer os.Remove(path)
|
|
cfg,err=Load(path);if err!=nil||cfg.Server.Log.Output!="stdout"||cfg.Server.Log.File!=""||cfg.Server.Log.Rotation.Enabled{t.Fatalf("explicit file 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/config/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)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
}
|