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 Bibtex4Word 引用中文文献
  • ¥20 用opencv c/c++ 转换成灰度图,然后做一下直方图均衡,输出mp4文件
  • ¥20 matlab中的双层数值积分
  • ¥50 服务器打印水晶报表问题
  • ¥30 gradle环境下javafx项目如何使用druid连接池
  • ¥15 服务器打印水晶报表问题
  • ¥18 深度学习tensorflow1,ssdv1,coco数据集训练一个模型
  • ¥100 关于注册表摄像头和麦克风的问题
  • ¥30 代码本地运行正常,但是TOMCAT部署时闪退
  • ¥15 关于#python#的问题