dongqing220586 2019-01-31 14:17
浏览 80
已采纳

具有外部软件包依赖关系的Golang测试未通过Gitlab Ci传递,而是通过本地传递

I have test:

package misc

var (
    Trace   *log.Logger
    Info    *log.Logger
    Warning *log.Logger
    Error   *log.Logger
)

func TestFTPGetFilesFromServer(t *testing.T) {
    misc.InitLogger(4) 
}

with InitLogger is located in another package

func InitSimpleLogger(level int) {
    if level == 1 {
        Trace = log.New(ioutil.Discard, "[TRACE]\t", log.Ldate|log.Ltime|log.Lshortfile)
        Info = log.New(ioutil.Discard, "[INFO]\t", log.Ldate|log.Ltime|log.Lshortfile)
        Warning = log.New(ioutil.Discard, "[WARNING]\t", log.Ldate|log.Ltime|log.Lshortfile)
        Error = log.New(os.Stdout, "[ERROR]\t", log.Ldate|log.Ltime|log.Lshortfile)
    } else if level == 2 {
        Trace = log.New(ioutil.Discard, "[TRACE]\t", log.Ldate|log.Ltime|log.Lshortfile)
        Info = log.New(ioutil.Discard, "[INFO]\t", log.Ldate|log.Ltime|log.Lshortfile)
        Warning = log.New(os.Stdout, "[WARNING]\t", log.Ldate|log.Ltime|log.Lshortfile)
        Error = log.New(os.Stdout, "[ERROR]\t", log.Ldate|log.Ltime|log.Lshortfile)
    } else if level == 3 {
        Trace = log.New(os.Stdout, "[TRACE]\t", log.Ldate|log.Ltime|log.Lshortfile)
        Info = log.New(ioutil.Discard, "[INFO]\t", log.Ldate|log.Ltime|log.Lshortfile)
        Warning = log.New(os.Stdout, "[WARNING]\t", log.Ldate|log.Ltime|log.Lshortfile)
        Error = log.New(os.Stdout, "[ERROR]\t", log.Ldate|log.Ltime|log.Lshortfile)
    } else {
        Trace = log.New(os.Stdout, "[TRACE]\t", log.Ldate|log.Ltime|log.Lshortfile)
        Info = log.New(os.Stdout, "[INFO]\t", log.Ldate|log.Ltime|log.Lshortfile)
        Warning = log.New(os.Stdout, "[WARNING]\t", log.Ldate|log.Ltime|log.Lshortfile)
        Error = log.New(os.Stdout, "[ERROR]\t", log.Ldate|log.Ltime|log.Lshortfile)
    }
}

EDIT:

I have:

ftph := ftp_handler.NewFTPHandler()
ftp, err = ftph.connect()

with NewFTPHandler()

func NewFTPHandler() *FTPHandler {
    return &FTPHandler{
        Addr:     os.Getenv("FTP_ADDR"),
        username: os.Getenv("FTP_USER"),
        password: os.Getenv("FTP_PASSWD"),
        key:      os.Getenv("FTP_SGEKEY"),
        iv:       os.Getenv("FTP_SGEIV"),
        Port:     os.Getenv("FTP_PORT"),
    }
}

and call to connect():

func (fh *FTPHandler) connect() (*goftp.FTP, error) {
    var err error
    var ftp *goftp.FTP
    if ftp, err = goftp.Connect(fh.Addr + ":" + fh.Port); err != nil {
        misc.Error.Println("Cannot reach the FTP server", fh.Addr, "with port", fh.Port, "\t:", err)
        return nil, err
    }
    if err = ftp.Login(fh.username, fh.password); err != nil {
        misc.Error.Println("Bad credential for the FTP server", fh.Addr, "with port", fh.Port, "\t:", err)
        return nil, err
    }
    return ftp, nil
}

When I run my test in gitlab-ci, the line:

misc.InitLogger(4) 

fails at runtime

--- FAIL: TestFTPConnect (0.00s)
panic: runtime error: invalid memory address or nil pointer dereference [recovered]
    panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x5ef467]

goroutine 5 [running]:
testing.tRunner.func1(0xc0000ea100)
    /usr/local/go/src/testing/testing.go:792 +0x387
panic(0x6af2a0, 0x9205a0)
    /usr/local/go/src/runtime/panic.go:513 +0x1b9
log.(*Logger).Output(0x0, 0x2, 0xc00004e0e0, 0x63, 0x0, 0x0)
    /usr/local/go/src/log/log.go:153 +0x47
log.(*Logger).Println(0x0, 0xc0000f9ef8, 0x6, 0x6)
    /usr/local/go/src/log/log.go:188 +0x6a
gitlab.com/ColinBois/pamela/ftp_handler.(*FTPHandler).connect(0xc000050480, 0xc00002c798, 0x4766a6, 0x5c52ff3a)
    /root/go/src/gitlab.com/ColinBois/pamela/ftp_handler/ftpHandler.go:45 +0x1f3
gitlab.com/ColinBois/pamela/ftp_handler.TestFTPConnect(0xc0000ea100)
    /root/go/src/gitlab.com/ColinBois/pamela/ftp_handler/ftpHandler_test.go:20 +0x27
testing.tRunner(0xc0000ea100, 0x717140)
    /usr/local/go/src/testing/testing.go:827 +0xbf
created by testing.(*T).Run

Here is my gitlab-ci.yml

test:
  image: docker:18
  services:
    - docker:dind
  stage: test
  before_script:
    - touch test.env
    - apk update
    - apk upgrade
    - apk add --no-cache py-pip
    - pip install docker-compose
    - docker network create mygoapp_network
    - mkdir -p volume/log

  script:
    - docker-compose -f docker-local.yaml up --build -d
    - docker exec mygoapp go test ./... -v
  • 写回答

1条回答 默认 最新

  • dongtan5558 2019-01-31 15:30
    关注

    The tests do not ensure that misc.InitSimpleLogger(4) is executed before the tests that rely on the side effects of this function call. Move the call to an init() function to ensure that it's executed before the tests. Change

    func TestFTPGetFilesFromServer(t *testing.T) {
        misc.InitLogger(4) 
    }
    

    to

    func init() {
        misc.InitLogger(4) 
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 12864只亮屏 不显示汉字
  • ¥20 三极管1000倍放大电路
  • ¥15 vscode报错如何解决
  • ¥15 前端vue CryptoJS Aes CBC加密后端java解密
  • ¥15 python随机森林对两个excel表格读取,shap报错
  • ¥15 基于STM32心率血氧监测(OLED显示)相关代码运行成功后烧录成功OLED显示屏不显示的原因是什么
  • ¥100 X轴为分离变量(因子变量),如何控制X轴每个分类变量的长度。
  • ¥30 求给定范围的全体素数p的(p-2)/p的连乘积值
  • ¥15 VFP如何使用阿里TTS实现文字转语音?
  • ¥100 需要跳转番茄畅听app的adb命令