Skip to content
This repository has been archived by the owner on Aug 28, 2024. It is now read-only.

Commit

Permalink
feat: github action init
Browse files Browse the repository at this point in the history
  • Loading branch information
ozline committed Aug 18, 2023
1 parent 4f94ed6 commit 386c2d0
Show file tree
Hide file tree
Showing 11 changed files with 201 additions and 34 deletions.
30 changes: 30 additions & 0 deletions .github/workflows/workflow.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: User interface test

jobs:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3

- name: Set up golang
uses: actions/setup-go@v4
with:
go-version: '1.20.x'

- name: Set up environment
uses: isbang/compose-action@v1.5.1
with:
compose-file: "./docker-compose.ci.yml"

- name: Install dependencies
run: go mod tidy

- name: Build target
run: make user

- name: Run local server
run: cd ./output/user && nohup sh bootstrap.sh > nohup.out 2> nohup.err < /dev/null &

- name: Test interfaces
run: cd ../../test && go test -v
33 changes: 33 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
DIR = $(shell pwd)
CMD = $(DIR)/CMD
CONFIG_PATH = $(DIR)/config
IDL_PATH = $(DIR)/idl
OUTPUT_PATH = $(DIR)/output

SERVICES := api user follow interaction video
service = $(word 1, $@)

# mock gen
MOCKS := user_mock
mock = $(word 1, $@)

.PHONY: env-up
env-up:
docker-compose up -d

.PHONY: env-down
env-down:
docker-compose down

.PHONY: $(SERVICES)
$(SERVICES):
mkdir -p output
cd $(CMD)/$(service) && sh build.sh
cd $(CMD)/$(service)/output && cp -r . $(OUTPUT_PATH)/$(service)
sh $(OUTPUT_PATH)/$(service)/bootstrap.sh


.PHONY: $(MOCKS)
$(MOCKS):
@mkdir -p mocks
mockgen -source=./idl/$(mock).go -destination=./mocks/$(mock).go -package=mocks
15 changes: 14 additions & 1 deletion cmd/user/dal/db/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"time"

"github.com/ozline/tiktok/pkg/errno"
"gorm.io/gorm"
)

Expand All @@ -29,6 +30,18 @@ TODO: follow_count, follower_count, is_follow, work_count, favorite_count, total
*/

func CreateUser(ctx context.Context, user *User) (*User, error) {
userResp := new(User)

err := DB.WithContext(ctx).Where("username = ?", user.Username).First(&userResp).Error

if err == nil {
return nil, errno.UserExistedError
}

if !errors.Is(err, gorm.ErrRecordNotFound) {
return nil, err
}

if err := DB.WithContext(ctx).Create(user).Error; err != nil {
// add some logs
return nil, err
Expand All @@ -46,7 +59,7 @@ func GetUserByUsername(ctx context.Context, username string) (*User, error) {
// add some logs

if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, errors.New("User not found")
return nil, err
}
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/user/script/bootstrap.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#! /usr/bin/env bash
CURDIR=$(cd $(dirname $0); pwd)
CONFIG_PATH=$(dirname $(dirname $(dirname $CURDIR)))/config
CONFIG_PATH=$(dirname $(dirname $CURDIR))/config

if [ "X$1" != "X" ]; then
RUNTIME_ROOT=$1
Expand Down
51 changes: 51 additions & 0 deletions docker-compose.ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
version: '3.7'

networks:
tiktok:
driver: bridge

services:

mysql:
container_name: mysql
image: mysql:latest
restart: always
environment:
- MYSQL_ROOT_PASSWORD=root
- MYSQL_DATABASE=tiktok
- MYSQL_USER=tiktok
- MYSQL_PASSWORD=tiktok
- TZ=Asia/Shanghai
volumes:
- ./config/sql:/docker-entrypoint-initdb.d/
ports:
- "3306:3306"
networks:
- tiktok

redis:
container_name: redis
image: "redis:latest"
restart: always
ports:
- 6379:6379
volumes:
- /usr/local/redis/conf/redis.conf:/var/lib/redis/conf/redis.conf
- /usr/local/redis/data:/data
environment:
- REDIS_PASSWORD=tiktok
- ALLOW_EMPTY_PASSWORD=no
networks:
- tiktok

rabbitmq:
container_name: rabiitmq
image: "rabbitmq:latest"
ports:
- 5672:5672
- 15672:15672
environment:
- RABBITMQ_DEFAULT_USER=tiktok
- RABBITMQ_DEFAULT_PASS=tiktok
networks:
- tiktok
2 changes: 1 addition & 1 deletion pkg/errno/code.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ const (
AuthorizationFailedErrCode = 10003 // 鉴权失败
UnexpectedTypeErrorCode = 10004 // 未知类型
NotImplementErrorCode = 10005 // 未实装
SensitiveWordsErrorCode = 10006 //敏感词错误
SensitiveWordsErrorCode = 10006 // 敏感词错误
)
17 changes: 11 additions & 6 deletions cmd/user/test/info_test.go → test/user/info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,20 @@ import (
"github.com/cloudwego/kitex/client/callopt"
"github.com/ozline/tiktok/kitex_gen/user"
"github.com/ozline/tiktok/pkg/errno"
"github.com/ozline/tiktok/pkg/utils"
)

func TestGetUserInfo(t *testing.T) {
TestLogin(t)
t.Logf("Token: %v", token)
func testGetUserInfo(t *testing.T) {

token, err := utils.CreateToken(10001)

if err != nil {
t.Error(err)
t.Fail()
}

req := &user.InfoRequest{
UserId: 10001, // 按需修改账号
UserId: id, // 按需修改账号
Token: token,
}

Expand All @@ -29,6 +36,4 @@ func TestGetUserInfo(t *testing.T) {
t.Error(errno.NewErrNo(resp.Base.Code, resp.Base.Msg))
t.Fail()
}

t.Logf("Resp:\n%v\n\n", resp)
}
5 changes: 2 additions & 3 deletions cmd/user/test/login_test.go → test/user/login_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/ozline/tiktok/pkg/errno"
)

func TestLogin(t *testing.T) {
func testLogin(t *testing.T) {
req := &user.LoginRequest{
Username: "ozline",
Password: "123456",
Expand All @@ -28,7 +28,6 @@ func TestLogin(t *testing.T) {
t.Fail()
}

t.Logf("Resp:\n%v\n\n", resp)

token = resp.Token
id = resp.User.Id
}
45 changes: 45 additions & 0 deletions test/user/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package main

import (
"testing"

"github.com/ozline/tiktok/pkg/constants"

"github.com/cloudwego/kitex/client"
"github.com/ozline/tiktok/kitex_gen/user/userservice"
)

var (
conn userservice.Client
username string
password string
token string
id int64
)

func TestMain(m *testing.M) {
// 连接服务器
c, err := userservice.NewClient("user",
client.WithMuxConnection(constants.MuxConnection),
client.WithHostPorts("0.0.0.0:10002"))

if err != nil {
panic(err)
}

username = "ozline"
password = "123456"

conn = c
m.Run()
}

func TestMainOrder(t *testing.T) {
t.Run("register", testRegister)

t.Run("login", testLogin)

t.Run("info", testGetUserInfo)

t.Run("RPC Test", testRPC)
}
17 changes: 7 additions & 10 deletions cmd/user/test/register_test.go → test/user/register_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,21 @@ import (
"github.com/ozline/tiktok/pkg/errno"
)

func TestRegister(t *testing.T) {
req := &user.RegisterRequest{
Username: "ozline",
Password: "123456",
}
func testRegister(t *testing.T) {

resp, err := conn.Register(context.Background(), req, callopt.WithRPCTimeout(3*time.Second))
resp, err := conn.Register(context.Background(), &user.RegisterRequest{
Username: username,
Password: password,
}, callopt.WithRPCTimeout(3*time.Second))

if err != nil {
t.Error(err)
t.FailNow()

t.Fail()
}

if resp.Base.Code != errno.SuccessCode {
t.Error(errno.NewErrNo(resp.Base.Code, resp.Base.Msg))
t.Fail()
}

t.Logf("Resp:\n%v\n\n", resp)
// t.Logf("Resp:\n%v\n\n", resp)
}
18 changes: 6 additions & 12 deletions cmd/user/test/main_test.go → test/user/rpc_test.go
Original file line number Diff line number Diff line change
@@ -1,26 +1,20 @@
package main

import (
"github.com/ozline/tiktok/pkg/constants"
"testing"

"github.com/cloudwego/kitex/client"
"github.com/ozline/tiktok/kitex_gen/user/userservice"
"github.com/ozline/tiktok/pkg/constants"
)

var conn userservice.Client
var token string

func TestMain(m *testing.M) {
// 连接服务器
c, err := userservice.NewClient("user",
func testRPC(t *testing.T) {
_, err := userservice.NewClient("user",
client.WithMuxConnection(constants.MuxConnection),
client.WithHostPorts("0.0.0.0:8888"))
client.WithHostPorts("0.0.0.0:10002"))

if err != nil {
panic(err)
t.Error(err)
t.Fail()
}

conn = c
m.Run()
}

0 comments on commit 386c2d0

Please sign in to comment.