Skip to content

Commit

Permalink
fix:remove endpoint puttags from watcher
Browse files Browse the repository at this point in the history
  • Loading branch information
geebytes committed May 9, 2024
1 parent 4930174 commit deaba09
Show file tree
Hide file tree
Showing 8 changed files with 388 additions and 382 deletions.
2 changes: 2 additions & 0 deletions internal/biz/gateway/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"io/fs"
"log"
"os"
"path/filepath"
"time"
Expand Down Expand Up @@ -160,6 +161,7 @@ func (e *EndpointUsecase) AddConfig(ctx context.Context, srvConfig *api.Endpoint
ServiceName: srvConfig.ServiceName,
DescriptorSet: srvConfig.DescriptorSet,
}
log.Printf("endpoint add tags :%v", srvConfig.Tags)
err := e.repo.Put(ctx, endpoint)
if err != nil {
return "", errors.New(err, int32(common.Code_INTERNAL_ERROR), codes.Internal, "put_endpoint")
Expand Down
4 changes: 2 additions & 2 deletions internal/biz/gateway/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ func (g *GatewayWatcher) Update(ctx context.Context, key string, value string) e
if err != nil {
return errors.New(fmt.Errorf("register service error: %w", err), int32(common.Code_INTERNAL_ERROR), codes.Internal, "register_service")
}
err = g.repo.PutTags(ctx, endpoint.Key, endpoint.Tags)
return err
// err = g.repo.PutTags(ctx, endpoint.Key, endpoint.Tags)
return nil
}
func (g *GatewayWatcher) del(ctx context.Context, key string, value string) error {
endpoint := &api.Endpoints{}
Expand Down
320 changes: 160 additions & 160 deletions internal/data/app_test.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
package data

import (
"context"
"crypto/rand"
"fmt"
"testing"
"time"

"github.com/agiledragon/gomonkey/v2"
"github.com/begonia-org/begonia"
cfg "github.com/begonia-org/begonia/config"
"github.com/begonia-org/begonia/internal/pkg/config"
"github.com/begonia-org/begonia/internal/pkg/logger"
api "github.com/begonia-org/go-sdk/api/app/v1"
"github.com/cockroachdb/errors"
c "github.com/smartystreets/goconvey/convey"
"github.com/spark-lence/tiga"
"google.golang.org/protobuf/types/known/fieldmaskpb"
"google.golang.org/protobuf/types/known/timestamppb"
"context"
"crypto/rand"
"fmt"
"testing"
"time"

"github.com/agiledragon/gomonkey/v2"
"github.com/begonia-org/begonia"
cfg "github.com/begonia-org/begonia/config"
"github.com/begonia-org/begonia/internal/pkg/config"
"github.com/begonia-org/begonia/internal/pkg/logger"
api "github.com/begonia-org/go-sdk/api/app/v1"
"github.com/cockroachdb/errors"
c "github.com/smartystreets/goconvey/convey"
"github.com/spark-lence/tiga"
"google.golang.org/protobuf/types/known/fieldmaskpb"
"google.golang.org/protobuf/types/known/timestamppb"
)

var appid = ""
Expand All @@ -26,161 +26,161 @@ var secret = ""
var appName = ""

func generateRandomString(n int) (string, error) {
const lettersAndDigits = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
b := make([]byte, n)
if _, err := rand.Read(b); err != nil {
return "", fmt.Errorf("Failed to generate random string: %w", err)
}

for i := 0; i < n; i++ {
// 将随机字节转换为lettersAndDigits中的一个有效字符
b[i] = lettersAndDigits[b[i]%byte(len(lettersAndDigits))]
}

return string(b), nil
const lettersAndDigits = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
b := make([]byte, n)
if _, err := rand.Read(b); err != nil {
return "", fmt.Errorf("Failed to generate random string: %w", err)
}

for i := 0; i < n; i++ {
// 将随机字节转换为lettersAndDigits中的一个有效字符
b[i] = lettersAndDigits[b[i]%byte(len(lettersAndDigits))]
}

return string(b), nil
}
func addTest(t *testing.T) {
c.Convey("test app add success", t, func() {
t.Log("add test")
env := "dev"
if begonia.Env != "" {
env = begonia.Env
}
repo := NewAppRepo(cfg.ReadConfig(env), logger.Log)
snk, _ := tiga.NewSnowflake(1)
access, _ := generateRandomString(32)
accessKey = access
secret, _ = generateRandomString(62)
appid = snk.GenerateIDString()
appName = fmt.Sprintf("app-%s", time.Now().Format("20060102150405"))
err := repo.Add(context.TODO(), &api.Apps{
Appid: appid,
AccessKey: access,
Secret: secret,
Status: api.APPStatus_APP_ENABLED,
IsDeleted: false,
Name: appName,
Description: "test",
CreatedAt: timestamppb.New(time.Now()),
UpdatedAt: timestamppb.New(time.Now()),
})

c.So(err, c.ShouldBeNil)
cfg := config.NewConfig(cfg.ReadConfig(env))
cacheKey := cfg.GetAPPAccessKey(access)

value, err := layered.Get(context.Background(), cacheKey)
c.So(err, c.ShouldBeNil)
c.So(string(value), c.ShouldEqual, secret)

patch := gomonkey.ApplyFuncReturn((*LayeredCache).Get, nil, errors.New("error"))

defer patch.Reset()
val, err := repo.GetSecret(context.Background(), access)
c.So(err, c.ShouldBeNil)
c.So(val, c.ShouldEqual, secret)

})
c.Convey("test app add success", t, func() {
t.Log("add test")
env := "dev"
if begonia.Env != "" {
env = begonia.Env
}
repo := NewAppRepo(cfg.ReadConfig(env), logger.Log)
snk, _ := tiga.NewSnowflake(1)
access, _ := generateRandomString(32)
accessKey = access
secret, _ = generateRandomString(62)
appid = snk.GenerateIDString()
appName = fmt.Sprintf("app-%s", time.Now().Format("20060102150405"))
err := repo.Add(context.TODO(), &api.Apps{
Appid: appid,
AccessKey: access,
Secret: secret,
Status: api.APPStatus_APP_ENABLED,
IsDeleted: false,
Name: appName,
Description: "test",
CreatedAt: timestamppb.New(time.Now()),
UpdatedAt: timestamppb.New(time.Now()),
})

c.So(err, c.ShouldBeNil)
cfg := config.NewConfig(cfg.ReadConfig(env))
cacheKey := cfg.GetAPPAccessKey(access)

value, err := layered.Get(context.Background(), cacheKey)
c.So(err, c.ShouldBeNil)
c.So(string(value), c.ShouldEqual, secret)

patch := gomonkey.ApplyFuncReturn((*LayeredCache).Get, nil, errors.New("error"))

defer patch.Reset()
val, err := repo.GetSecret(context.Background(), access)
c.So(err, c.ShouldBeNil)
c.So(val, c.ShouldEqual, secret)

})
}
func getTest(t *testing.T) {
c.Convey("test app get success", t, func() {
t.Log("get test")
env := "dev"
if begonia.Env != "" {
env = begonia.Env
}
repo := NewAppRepo(cfg.ReadConfig(env), logger.Log)
app, err := repo.Get(context.TODO(), appid)
c.So(err, c.ShouldBeNil)
c.So(app.Appid, c.ShouldEqual, appid)
app, err = repo.Get(context.TODO(), accessKey)
c.So(err, c.ShouldBeNil)
c.So(app.AccessKey, c.ShouldEqual, accessKey)
_, err = repo.Get(context.TODO(), "123")
c.So(err, c.ShouldNotBeNil)

})
c.Convey("test app get success", t, func() {
t.Log("get test")
env := "dev"
if begonia.Env != "" {
env = begonia.Env
}
repo := NewAppRepo(cfg.ReadConfig(env), logger.Log)
app, err := repo.Get(context.TODO(), appid)
c.So(err, c.ShouldBeNil)
c.So(app.Appid, c.ShouldEqual, appid)
app, err = repo.Get(context.TODO(), accessKey)
c.So(err, c.ShouldBeNil)
c.So(app.AccessKey, c.ShouldEqual, accessKey)
_, err = repo.Get(context.TODO(), "123")
c.So(err, c.ShouldNotBeNil)

})
}

func duplicateNameTest(t *testing.T) {
c.Convey("test app add duplicate name", t, func() {
t.Log("add test")
env := "dev"
if begonia.Env != "" {
env = begonia.Env
}
repo := NewAppRepo(cfg.ReadConfig(env), logger.Log)
// snk, _ := tiga.NewSnowflake(1)
access, _ := generateRandomString(32)
accessKey = access
secret, _ = generateRandomString(62)
// appid = snk.GenerateIDString()
err := repo.Add(context.TODO(), &api.Apps{
Appid: appid,
AccessKey: access,
Secret: secret,
Status: api.APPStatus_APP_ENABLED,
IsDeleted: false,
Name: fmt.Sprintf("app-%s", time.Now().Format("20060102150405")),
Description: "test",
CreatedAt: timestamppb.New(time.Now()),
UpdatedAt: timestamppb.New(time.Now()),
})

c.So(err, c.ShouldNotBeNil)
c.So(err.Error(), c.ShouldContainSubstring, "Duplicate entry")
})
c.Convey("test app add duplicate name", t, func() {
t.Log("add test")
env := "dev"
if begonia.Env != "" {
env = begonia.Env
}
repo := NewAppRepo(cfg.ReadConfig(env), logger.Log)
// snk, _ := tiga.NewSnowflake(1)
access, _ := generateRandomString(32)
accessKey = access
secret, _ = generateRandomString(62)
// appid = snk.GenerateIDString()
err := repo.Add(context.TODO(), &api.Apps{
Appid: appid,
AccessKey: access,
Secret: secret,
Status: api.APPStatus_APP_ENABLED,
IsDeleted: false,
Name: fmt.Sprintf("app-%s", time.Now().Format("20060102150405")),
Description: "test",
CreatedAt: timestamppb.New(time.Now()),
UpdatedAt: timestamppb.New(time.Now()),
})

c.So(err, c.ShouldNotBeNil)
c.So(err.Error(), c.ShouldContainSubstring, "Duplicate entry")
})
}
func patchTest(t *testing.T) {
c.Convey("test app patch success", t, func() {
t.Log("patch test")
env := "dev"
if begonia.Env != "" {
env = begonia.Env
}
repo := NewAppRepo(cfg.ReadConfig(env), logger.Log)
err := repo.Patch(context.TODO(), &api.Apps{
Appid: appid,
AccessKey: accessKey,
Secret: secret,
Status: api.APPStatus_APP_DISABLED,
Name: fmt.Sprintf("app-%s", time.Now().Format("20060102150405")),
Description: "test UPDATE",
CreatedAt: timestamppb.New(time.Now()),
UpdatedAt: timestamppb.New(time.Now()),
UpdateMask: &fieldmaskpb.FieldMask{
Paths: []string{"status", "description"},
},
})

c.So(err, c.ShouldBeNil)
t.Log("get test", appid)
updated,err:=repo.Get(context.Background(),appid)
c.So(err,c.ShouldBeNil)
c.So(updated.Status,c.ShouldEqual,api.APPStatus_APP_DISABLED)
c.So(updated.Description,c.ShouldEqual,"test UPDATE")
c.So(updated.Name,c.ShouldEqual,appName)
})
c.Convey("test app patch success", t, func() {
t.Log("patch test")
env := "dev"
if begonia.Env != "" {
env = begonia.Env
}
repo := NewAppRepo(cfg.ReadConfig(env), logger.Log)
err := repo.Patch(context.TODO(), &api.Apps{
Appid: appid,
AccessKey: accessKey,
Secret: secret,
Status: api.APPStatus_APP_DISABLED,
Name: fmt.Sprintf("app-%s", time.Now().Format("20060102150405")),
Description: "test UPDATE",
CreatedAt: timestamppb.New(time.Now()),
UpdatedAt: timestamppb.New(time.Now()),
UpdateMask: &fieldmaskpb.FieldMask{
Paths: []string{"status", "description"},
},
})

c.So(err, c.ShouldBeNil)
t.Log("get test", appid)
updated,err:=repo.Get(context.Background(),appid)
c.So(err,c.ShouldBeNil)
c.So(updated.Status,c.ShouldEqual,api.APPStatus_APP_DISABLED)
c.So(updated.Description,c.ShouldEqual,"test UPDATE")
c.So(updated.Name,c.ShouldEqual,appName)
})

}
func delTest(t *testing.T) {
c.Convey("test app delete success", t, func() {
t.Log("delete test")
env := "dev"
if begonia.Env != "" {
env = begonia.Env
}
repo := NewAppRepo(cfg.ReadConfig(env), logger.Log)
err := repo.Del(context.TODO(), appid)
c.So(err, c.ShouldBeNil)
_, err = repo.Get(context.TODO(), appid)
c.So(err, c.ShouldNotBeNil)
})
c.Convey("test app delete success", t, func() {
t.Log("delete test")
env := "dev"
if begonia.Env != "" {
env = begonia.Env
}
repo := NewAppRepo(cfg.ReadConfig(env), logger.Log)
err := repo.Del(context.TODO(), appid)
c.So(err, c.ShouldBeNil)
_, err = repo.Get(context.TODO(), appid)
c.So(err, c.ShouldNotBeNil)
})
}
func TestApp(t *testing.T) {
t.Run("add app", addTest)
t.Run("get app", getTest)
t.Run("add duplicate name", duplicateNameTest)
t.Run("patch app", patchTest)
t.Run("del app", delTest)
t.Run("add app", addTest)
t.Run("get app", getTest)
t.Run("add duplicate name", duplicateNameTest)
t.Run("patch app", patchTest)
t.Run("del app", delTest)
}
Loading

0 comments on commit deaba09

Please sign in to comment.