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

Commit

Permalink
tests: don't call to flag.Parse() from init()
Browse files Browse the repository at this point in the history
Since golang 1.13 packages that call flag.Parse during package initialization
may cause tests to fail[1].
Add a new function named `KataInit` to initialize the tests and parse
the command line, this function needs to be called as soon as possible,
preferably from `TestMain`.

[1] - https://golang.org/doc/go1.13#testing

fixes #2195

Signed-off-by: Julio Montes <julio.montes@intel.com>
  • Loading branch information
Julio Montes committed Mar 20, 2020
1 parent 65b6de7 commit 44ab7de
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 18 deletions.
18 changes: 0 additions & 18 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,12 @@ package tests

import (
"bytes"
"flag"
"fmt"
"os/exec"
"syscall"
"time"
)

// Runtime is the path of a Kata Containers Runtime
var Runtime string

// Timeout specifies the time limit in seconds for each test
var Timeout int

// Hypervisor is the hypervisor currently being used with Kata
var Hypervisor string

// Command contains the information of the command to run
type Command struct {
// cmd exec.Cmd
Expand All @@ -31,14 +21,6 @@ type Command struct {
Timeout time.Duration
}

func init() {
flag.StringVar(&Runtime, "runtime", "", "Path of the desired Kata Runtime")
flag.IntVar(&Timeout, "timeout", 5, "Time limit in seconds for each test")
flag.StringVar(&Hypervisor, "hypervisor", "", "The hypervisor currently being used with Kata")

flag.Parse()
}

// NewCommand returns a new instance of Command
func NewCommand(path string, args ...string) *Command {
c := new(Command)
Expand Down
24 changes: 24 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package tests

import (
"flag"
"io/ioutil"
"log"
"os"
Expand All @@ -14,6 +15,15 @@ import (
"github.com/BurntSushi/toml"
)

// Runtime is the path of a Kata Containers Runtime
var Runtime string

// Timeout specifies the time limit in seconds for each test
var Timeout int

// Hypervisor is the hypervisor currently being used with Kata
var Hypervisor string

// KataConfiguration is the runtime configuration
type KataConfiguration struct {
Hypervisor map[string]hypervisor
Expand Down Expand Up @@ -95,7 +105,21 @@ var KataConfig KataConfiguration
var KataHypervisor string

func init() {
flag.StringVar(&Runtime, "runtime", "", "Path of the desired Kata Runtime")
flag.IntVar(&Timeout, "timeout", 5, "Time limit in seconds for each test")
flag.StringVar(&Hypervisor, "hypervisor", "", "The hypervisor currently being used with Kata")
}

// KataInit initializes the kata test suite.
// This function should be called as soon as possible
// preferably from `TestMain`
func KataInit() {
var err error

// Since golang 1.13 packages that call flag.Parse during package initialization
// may cause tests to fail. https://golang.org/doc/go1.13#testing
flag.Parse()

kataConfigPath := DefaultKataConfigPath

args := []string{"--kata-show-default-config-paths"}
Expand Down

0 comments on commit 44ab7de

Please # to comment.