Skip to content

Commit

Permalink
更多端到端测试
Browse files Browse the repository at this point in the history
  • Loading branch information
movsb committed Jun 21, 2024
1 parent 7f7099d commit 3bd4ace
Show file tree
Hide file tree
Showing 19 changed files with 210 additions and 115 deletions.
1 change: 1 addition & 0 deletions cmd/config/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ func DefaultDataConfig() DataConfig {

// FileDataConfig ...
type FileDataConfig struct {
// 如果路径为空,使用内存文件系统。
Path string `yaml:"path"`
}

Expand Down
45 changes: 36 additions & 9 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"errors"
"fmt"
"log"
"net"
"net/http"
"net/url"
"os"
Expand All @@ -34,6 +35,7 @@ import (
"github.com/movsb/taoblog/setup/migration"
"github.com/movsb/taoblog/theme"
"github.com/movsb/taoblog/theme/modules/canonical"
theme_fs "github.com/movsb/taoblog/theme/modules/fs"
"github.com/movsb/taorm"
"github.com/spf13/cobra"
)
Expand All @@ -45,19 +47,25 @@ func AddCommands(rootCmd *cobra.Command) {
Short: `Run the server`,
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
serve(context.Background())
cfg := config.LoadFile(`taoblog.yml`)
s := &Server{}
s.Serve(context.Background(), false, cfg, nil)
},
}

rootCmd.AddCommand(serveCommand)
}

func serve(ctx context.Context) {
type Server struct {
HTTPAddr string // 服务器实际端口
GRPCAddr string // 服务器实际端口
Auther *auth.Auth
}

func (s *Server) Serve(ctx context.Context, testing bool, cfg *config.Config, ready chan<- struct{}) {
ctx, cancel := context.WithCancel(ctx)
defer cancel()

cfg := config.LoadFile(`taoblog.yml`)

db := InitDatabase(cfg.Database.Path)
defer db.Close()

Expand Down Expand Up @@ -101,13 +109,20 @@ func serve(ctx context.Context) {
}
}

storage := storage.NewLocal(cfg.Data.File.Path)
var store theme_fs.FS
if path := cfg.Data.File.Path; path == "" {
store = storage.NewMemory()
} else {
store = storage.NewLocal(cfg.Data.File.Path)
}

theAuth := auth.New(cfg.Auth, service.DevMode())
theService := service.NewService(ctx, cancel, cfg, db, theAuth,
service.WithPostDataFileSystem(storage),
s.Auther = theAuth
theService := service.NewService(ctx, cancel, testing, cfg, db, theAuth,
service.WithPostDataFileSystem(store),
service.WithInstantNotifier(instantNotifier),
)
s.GRPCAddr = theService.Addr().String()
r.MustRegister(theService.Exporter())
theAuth.SetService(theService)

Expand Down Expand Up @@ -153,7 +168,7 @@ func serve(ctx context.Context) {
)
}

theme := theme.New(service.DevMode(), cfg, theService, theService, theService, theAuth, storage)
theme := theme.New(service.DevMode(), cfg, theService, theService, theService, theAuth, store)
canon := canonical.New(theme, r)
mux.Handle(`/`, canon)

Expand All @@ -176,8 +191,14 @@ func serve(ctx context.Context) {
),
}

l, err := net.Listen("tcp", server.Addr)
if err != nil {
panic(err)
}
defer l.Close()
s.HTTPAddr = l.Addr().String()
go func() {
if err := server.ListenAndServe(); err != nil {
if err := server.Serve(l); err != nil {
if err != http.ErrServerClosed {
panic(err)
}
Expand All @@ -193,6 +214,10 @@ func serve(ctx context.Context) {
signal.Notify(quit, syscall.SIGINT)
signal.Notify(quit, syscall.SIGTERM)

if ready != nil {
ready <- struct{}{}
}

select {
case <-quit:
case <-ctx.Done():
Expand All @@ -206,6 +231,7 @@ func serve(ctx context.Context) {

// TODO 文章 1 必须存在。可以是非公开状态。
// TODO 放在服务里面 tasks.go
// TODO 放在 daemon 里面(同 webhooks)
func liveCheck(s *service.Service, cc notify.InstantNotifier) {
t := time.NewTicker(time.Minute * 15)
defer t.Stop()
Expand All @@ -229,6 +255,7 @@ func liveCheck(s *service.Service, cc notify.InstantNotifier) {
}
}

// 如果路径为空,使用内存数据库。
func InitDatabase(path string) *sql.DB {
var db *sql.DB
var err error
Expand Down
2 changes: 1 addition & 1 deletion gateway/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func NewGateway(service *service.Service, auther *auth.Auth, mux *http.ServeMux,
service: service,
auther: auther,

client: clients.NewFromGrpcAddr(service.GrpcAddress()),
client: clients.NewFromGrpcAddr(service.Addr().String()),
instantNotifier: instantNotifier,
}

Expand Down
9 changes: 6 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ require (
github.com/spf13/cobra v1.0.0
github.com/xeonx/timeago v1.0.0-rc4
github.com/yuin/goldmark v1.7.1
google.golang.org/genproto v0.0.0-20230410155749-daa745c078e1
google.golang.org/grpc v1.56.3
google.golang.org/genproto v0.0.0-20231106174013-bbf56f31fb17 // indirect
google.golang.org/grpc v1.59.0
google.golang.org/grpc/cmd/protoc-gen-go-grpc v1.2.0
google.golang.org/protobuf v1.33.0
gopkg.in/yaml.v2 v2.4.0
Expand All @@ -30,8 +30,10 @@ require (
github.com/go-webauthn/webauthn v0.10.2
github.com/movsb/pkg v0.0.0-20240609000755-7489894593e4
github.com/phuslu/lru v1.0.15
github.com/spf13/afero v1.11.0
github.com/yuin/goldmark-highlighting/v2 v2.0.0-20230729083705-37449abec8cc
go.abhg.dev/goldmark/hashtag v0.3.1
google.golang.org/genproto/googleapis/api v0.0.0-20231106174013-bbf56f31fb17
nhooyr.io/websocket v1.8.11
)

Expand All @@ -49,6 +51,7 @@ require (
github.com/rivo/uniseg v0.2.0 // indirect
github.com/x448/float16 v0.8.4 // indirect
golang.org/x/crypto v0.22.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20231120223509-83a465c0220f // indirect
)

require (
Expand All @@ -68,7 +71,7 @@ require (
github.com/dgryski/go-metro v0.0.0-20180109044635-280f6062b5bc // indirect
github.com/ghodss/yaml v1.0.0 // indirect
github.com/go-sql-driver/mysql v1.8.1 // indirect
github.com/golang/glog v1.1.0 // indirect
github.com/golang/glog v1.1.2 // indirect
github.com/golang/snappy v0.0.1 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
Expand Down
20 changes: 12 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ github.com/golang-jwt/jwt/v5 v5.2.1 h1:OuVbFODueb089Lh128TAcimifWaLhJwVflnrgM17w
github.com/golang-jwt/jwt/v5 v5.2.1/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk=
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
github.com/golang/glog v1.1.0 h1:/d3pCKDPWNnvIWe0vVUpNP32qc8U3PDVxySP/y360qE=
github.com/golang/glog v1.1.0/go.mod h1:pfYeQZ3JWZoXTV5sFc986z3HTpwQs9At6P4ImfuP3NQ=
github.com/golang/glog v1.1.2 h1:DVjP2PbBOzHyzA+dn3WhHIq4NdVu3Q+pvivFICf/7fo=
github.com/golang/glog v1.1.2/go.mod h1:zR+okUeTbrL6EL3xHUDxZuEtGv04p5shwip1+mL/rLQ=
github.com/golang/groupcache v0.0.0-20190129154638-5b532d6fd5ef/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
Expand Down Expand Up @@ -210,8 +210,6 @@ github.com/movsb/goldmark-wiki-table v0.0.0-20231120060153-11f5865ff575 h1:pMe8e
github.com/movsb/goldmark-wiki-table v0.0.0-20231120060153-11f5865ff575/go.mod h1:qryy4AEogyw8d+zR2jGs4QnN4OFIsAVmoe6dglyjjJM=
github.com/movsb/google-idtoken-verifier v0.0.0-20190329202541-1a6aa2c7e316 h1:eeMZo/pDQ2Vdmq7jFlb3JMYDLHtQWELZIw+oeEevh4A=
github.com/movsb/google-idtoken-verifier v0.0.0-20190329202541-1a6aa2c7e316/go.mod h1:yZbOV1cKd0igi4wBBcVrs4G2ooTWhjxUT9pYV5RDN44=
github.com/movsb/pkg v0.0.0-20230529040743-ec54d414c1e6 h1:vDusal4I65KBQ/ArtEXfj/iS6OIbStdpcc47tWeRyOY=
github.com/movsb/pkg v0.0.0-20230529040743-ec54d414c1e6/go.mod h1:hyHk9U4bOyLwcfLpjUjRI/Kz+lUeHRHL57u2UUDDLcM=
github.com/movsb/pkg v0.0.0-20240609000755-7489894593e4 h1:kuMEW9vTjkCF0lUu36IjIY/iXV+/1KBtLVSzof6vhQY=
github.com/movsb/pkg v0.0.0-20240609000755-7489894593e4/go.mod h1:hyHk9U4bOyLwcfLpjUjRI/Kz+lUeHRHL57u2UUDDLcM=
github.com/movsb/taorm v0.0.0-20240509064341-03180739c22c h1:2jPnw/yLpx6P5J9mDkWzwzEph1Lk8MklSZvbZmoGDvM=
Expand Down Expand Up @@ -269,6 +267,8 @@ github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrf
github.com/soheilhy/cmux v0.1.4/go.mod h1:IM3LyeVVIOuxMH7sFAkER9+bJ4dT7Ms6E4xg4kGIyLM=
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ=
github.com/spf13/afero v1.11.0 h1:WJQKhtpdm3v2IzqG8VMqrr6Rf3UYpEF239Jy9wNepM8=
github.com/spf13/afero v1.11.0/go.mod h1:GH9Y3pIexgf1MTIWtNGyogA5MwRIDXGUr+hbWNoBjkY=
github.com/spf13/cast v1.3.0/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
github.com/spf13/cobra v0.0.5/go.mod h1:3K3wKZymM7VvHMDS9+Akkh4K60UwM26emMESw8tLCHU=
github.com/spf13/cobra v1.0.0 h1:6m/oheQuQ13N9ks4hubMG6BnvwOeaJrqSPLahSnczz8=
Expand Down Expand Up @@ -428,17 +428,21 @@ google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoA
google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55/go.mod h1:DMBHOl98Agz4BDEuKkezgsaosCRResVns1a3J2ZsMNc=
google.golang.org/genproto v0.0.0-20200423170343-7949de9c1215/go.mod h1:55QSHmfGQM9UVYDPBsyGGes0y52j32PQ3BqQfXhyH3c=
google.golang.org/genproto v0.0.0-20200513103714-09dca8ec2884/go.mod h1:55QSHmfGQM9UVYDPBsyGGes0y52j32PQ3BqQfXhyH3c=
google.golang.org/genproto v0.0.0-20230410155749-daa745c078e1 h1:KpwkzHKEF7B9Zxg18WzOa7djJ+Ha5DzthMyZYQfEn2A=
google.golang.org/genproto v0.0.0-20230410155749-daa745c078e1/go.mod h1:nKE/iIaLqn2bQwXBg8f1g2Ylh6r5MN5CmZvuzZCgsCU=
google.golang.org/genproto v0.0.0-20231106174013-bbf56f31fb17 h1:wpZ8pe2x1Q3f2KyT5f8oP/fa9rHAKgFPr/HZdNuS+PQ=
google.golang.org/genproto v0.0.0-20231106174013-bbf56f31fb17/go.mod h1:J7XzRzVy1+IPwWHZUzoD0IccYZIrXILAQpc+Qy9CMhY=
google.golang.org/genproto/googleapis/api v0.0.0-20231106174013-bbf56f31fb17 h1:JpwMPBpFN3uKhdaekDpiNlImDdkUAyiJ6ez/uxGaUSo=
google.golang.org/genproto/googleapis/api v0.0.0-20231106174013-bbf56f31fb17/go.mod h1:0xJLfVdJqpAPl8tDg1ujOCGzx6LFLttXT5NhllGOXY4=
google.golang.org/genproto/googleapis/rpc v0.0.0-20231120223509-83a465c0220f h1:ultW7fxlIvee4HYrtnaRPon9HpEgFk5zYpmfMgtKB5I=
google.golang.org/genproto/googleapis/rpc v0.0.0-20231120223509-83a465c0220f/go.mod h1:L9KNLi232K1/xB6f7AlSX692koaRnKaWSR0stBki0Yc=
google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c=
google.golang.org/grpc v1.21.0/go.mod h1:oYelfM1adQP15Ek0mdvEgi9Df8B9CZIaU1084ijfRaM=
google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg=
google.golang.org/grpc v1.25.1/go.mod h1:c3i+UQWmh7LiEpx4sFZnkU36qjEYZ0imhYfXVyQciAY=
google.golang.org/grpc v1.27.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk=
google.golang.org/grpc v1.29.1/go.mod h1:itym6AZVZYACWQqET3MqgPpjcuV5QH3BxFS3IjizoKk=
google.golang.org/grpc v1.33.1/go.mod h1:fr5YgcSWrqhRRxogOsw7RzIpsmvOZ6IcH4kBYTpR3n0=
google.golang.org/grpc v1.56.3 h1:8I4C0Yq1EjstUzUJzpcRVbuYA2mODtEmpWiQoN/b2nc=
google.golang.org/grpc v1.56.3/go.mod h1:I9bI3vqKfayGqPUAwGdOSu7kt6oIJLixfffKrpXqQ9s=
google.golang.org/grpc v1.59.0 h1:Z5Iec2pjwb+LEOqzpB2MR12/eKFhDPhuqW91O+4bwUk=
google.golang.org/grpc v1.59.0/go.mod h1:aUPDwccQo6OTjy7Hct4AfBPD1GptF4fyUjIkQ9YtF98=
google.golang.org/grpc/cmd/protoc-gen-go-grpc v1.2.0 h1:TLkBREm4nIsEcexnCjgQd5GQWaHcqMzwQV0TX9pq8S0=
google.golang.org/grpc/cmd/protoc-gen-go-grpc v1.2.0/go.mod h1:DNq5QpG7LJqD2AamLZ7zvKE0DEpVl2BSEVjFycAAjRY=
google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8=
Expand Down
2 changes: 2 additions & 0 deletions modules/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,9 +287,11 @@ func (a *Auth) RemoveCookie(w http.ResponseWriter) {
}

// 仅用于测试的帐号。
// 可同时用于 HTTP 和 GRPC 请求。
func TestingAdminUserContext(a *Auth, userAgent string) context.Context {
md := metadata.Pairs()
md.Append(GatewayCookie, shasum(userAgent+a.Login()))
md.Append(GatewayUserAgent, userAgent)
md.Append(`Authorization`, fmt.Sprintf(`token %d:%s`, AdminID, a.cfg.Key))
return metadata.NewOutgoingContext(context.TODO(), md)
}
13 changes: 7 additions & 6 deletions modules/utils/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

"github.com/fsnotify/fsnotify"
"github.com/movsb/taoblog/protocols/go/proto"
"github.com/spf13/afero"
)

// walk returns the file list for a directory.
Expand Down Expand Up @@ -81,8 +82,8 @@ func ListBackupFiles(fsys fs.FS, dir string) ([]*proto.BackupFileSpec, error) {
// TODO 没移除失败的文件。
// NOTE:安全写:先写临时文件,再移动过去。临时文件写在目标目录,不存在跨设备移动文件的问题。
// NOTE:如果 r 超过 size,会报错。
func WriteFile(path string, mode fs.FileMode, modified time.Time, size int64, r io.Reader) error {
tmp, err := os.CreateTemp(filepath.Dir(path), `taoblog-*`)
func WriteFile(fs afero.Fs, path string, mode fs.FileMode, modified time.Time, size int64, r io.Reader) error {
tmp, err := afero.TempFile(fs, filepath.Dir(path), `taoblog-*`)
if err != nil {
return err
}
Expand All @@ -91,19 +92,19 @@ func WriteFile(path string, mode fs.FileMode, modified time.Time, size int64, r
return fmt.Errorf(`write error: %d %v`, n, err)
}

if err := tmp.Chmod(mode); err != nil {
if err := tmp.Close(); err != nil {
return err
}

if err := tmp.Close(); err != nil {
if err := fs.Chmod(tmp.Name(), mode); err != nil {
return err
}

if err := os.Chtimes(tmp.Name(), modified, modified); err != nil {
if err := fs.Chtimes(tmp.Name(), modified, modified); err != nil {
return err
}

if err := os.Rename(tmp.Name(), path); err != nil {
if err := fs.Rename(tmp.Name(), path); err != nil {
return err
}

Expand Down
16 changes: 3 additions & 13 deletions service/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ type Service struct {
cancel context.CancelFunc

testing bool
addr net.Addr // 服务器的监听地址
addr net.Addr // 服务器的实际监听地址

home *url.URL

Expand Down Expand Up @@ -124,13 +124,8 @@ func (s *Service) Addr() net.Addr {
return s.addr
}

func NewServiceForTesting(cfg *config.Config, db *sql.DB, auther *auth.Auth) *Service {
ctx, cancel := context.WithCancel(context.Background())
return newService(ctx, cancel, cfg, db, auther, true)
}

func NewService(ctx context.Context, cancel context.CancelFunc, cfg *config.Config, db *sql.DB, auther *auth.Auth, options ...With) *Service {
return newService(ctx, cancel, cfg, db, auther, false, options...)
func NewService(ctx context.Context, cancel context.CancelFunc, testing bool, cfg *config.Config, db *sql.DB, auther *auth.Auth, options ...With) *Service {
return newService(ctx, cancel, cfg, db, auther, testing, options...)
}

func newService(ctx context.Context, cancel context.CancelFunc, cfg *config.Config, db *sql.DB, auther *auth.Auth, testing bool, options ...With) *Service {
Expand Down Expand Up @@ -244,11 +239,6 @@ func (s *Service) MustBeAdmin(ctx context.Context) *auth.AuthContext {
return ac
}

// GrpcAddress ...
func (s *Service) GrpcAddress() string {
return s.cfg.Server.GRPCListen
}

func grpcLoggerUnary(ctx context.Context, req any, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp any, err error) {
grpcLogger(ctx, info.FullMethod)
return handler(ctx, req)
Expand Down
1 change: 1 addition & 0 deletions service/modules/cache/tmpfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type TmpFiles struct {
lock sync.Mutex
}

// TODO 支持内存文件系统以方便测试。
func NewTmpFiles(dir string, keep time.Duration) *TmpFiles {
if keep < time.Minute {
panic(`cache time too small`)
Expand Down
Loading

0 comments on commit 3bd4ace

Please sign in to comment.