Skip to content

Commit

Permalink
tests: Do not execute code in ginkgo containers body
Browse files Browse the repository at this point in the history
Ginkgo containers body are evaluated at definition, i.e. at a pre-main
stage. Running code at this stage is not recommended as some
initializations are occurring only in the test `main` (which is
auto-generated by the test framework).

An example of such a scenario is the command line flag parsing.
Libraries, tested code and the test infra is expected to declare the
flags and the parsing is expected to be execute only once (at main
usually).

This change moves all code that executes in the containers body to the
Before* setup bodies, while leaving on the containers body the variable
declarations.

Signed-off-by: Edward Haas <edwardh@redhat.com>
  • Loading branch information
EdDev committed Jul 16, 2020
1 parent 1122035 commit d921fc6
Show file tree
Hide file tree
Showing 42 changed files with 737 additions and 531 deletions.
11 changes: 6 additions & 5 deletions tests/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,14 @@ import (
var _ = Describe("[rfe_id:899][crit:medium][vendor:cnv-qe@redhat.com][level:component]Config", func() {

tests.FlagParse()
virtClient, err := kubecli.GetKubevirtClient()
tests.PanicOnError(err)
var virtClient kubecli.KubevirtClient

BeforeEach(func() {
var err error

virtClient, err = kubecli.GetKubevirtClient()
tests.PanicOnError(err)

tests.BeforeTestCleanup()
})

Expand Down Expand Up @@ -237,9 +241,6 @@ var _ = Describe("[rfe_id:899][crit:medium][vendor:cnv-qe@redhat.com][level:comp

Context("With a ServiceAccount defined", func() {

virtClient, err := kubecli.GetKubevirtClient()
tests.PanicOnError(err)

serviceAccountPath := config.ServiceAccountSourceDir

It("[test_id:998]Should be the namespace and token the same for a pod and vmi", func() {
Expand Down
8 changes: 6 additions & 2 deletions tests/console_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,15 @@ import (
var _ = Describe("[rfe_id:127][posneg:negative][crit:medium][vendor:cnv-qe@redhat.com][level:component]Console", func() {

tests.FlagParse()
var virtClient kubecli.KubevirtClient

virtClient, err := kubecli.GetKubevirtClient()
tests.PanicOnError(err)

BeforeEach(func() {
var err error

virtClient, err = kubecli.GetKubevirtClient()
tests.PanicOnError(err)

tests.BeforeTestCleanup()
})

Expand Down
7 changes: 5 additions & 2 deletions tests/container_disk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,13 @@ var _ = Describe("[rfe_id:588][crit:medium][vendor:cnv-qe@redhat.com][level:comp

tests.FlagParse()

virtClient, err := kubecli.GetKubevirtClient()
tests.PanicOnError(err)
var virtClient kubecli.KubevirtClient
var err error

BeforeEach(func() {
virtClient, err = kubecli.GetKubevirtClient()
tests.PanicOnError(err)

tests.BeforeTestCleanup()
})

Expand Down
8 changes: 6 additions & 2 deletions tests/datavolume_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,14 @@ const DummyFilePath = "/usr/share/nginx/html/dummy.file"
var _ = Describe("DataVolume Integration", func() {
tests.FlagParse()

virtClient, err := kubecli.GetKubevirtClient()
tests.PanicOnError(err)

var virtClient kubecli.KubevirtClient
var err error

BeforeEach(func() {
virtClient, err = kubecli.GetKubevirtClient()
tests.PanicOnError(err)

tests.BeforeTestCleanup()
if !tests.HasCDI() {
Skip("Skip DataVolume tests when CDI is not present")
Expand Down
10 changes: 8 additions & 2 deletions tests/expose_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,16 @@ var _ = Describe("[rfe_id:253][crit:medium][vendor:cnv-qe@redhat.com][level:comp

tests.FlagParse()

virtClient, err := kubecli.GetKubevirtClient()
tests.PanicOnError(err)
var virtClient kubecli.KubevirtClient
var err error

const testPort = 1500

BeforeEach(func() {
virtClient, err = kubecli.GetKubevirtClient()
tests.PanicOnError(err)
})

Context("Expose service on a VM", func() {
var tcpVM *v1.VirtualMachineInstance
tests.BeforeAll(func() {
Expand Down
10 changes: 8 additions & 2 deletions tests/imageupload_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,14 @@ var _ = Describe("ImageUpload", func() {
dvName := "alpine-dv"
pvcSize := "100Mi"

virtClient, err := kubecli.GetKubevirtClient()
tests.PanicOnError(err)
var virtClient kubecli.KubevirtClient

BeforeEach(func() {
var err error

virtClient, err = kubecli.GetKubevirtClient()
tests.PanicOnError(err)
})

BeforeEach(func() {
By("Getting CDI HTTP import server pod")
Expand Down
22 changes: 15 additions & 7 deletions tests/infra_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,23 @@ import (
var _ = Describe("Infrastructure", func() {
tests.FlagParse()

virtClient, err := kubecli.GetKubevirtClient()
tests.PanicOnError(err)
var virtClient kubecli.KubevirtClient
var aggregatorClient *aggregatorclient.Clientset
var err error

config, err := kubecli.GetConfig()
if err != nil {
panic(err)
}
BeforeEach(func() {
virtClient, err = kubecli.GetKubevirtClient()
tests.PanicOnError(err)

aggregatorClient := aggregatorclient.NewForConfigOrDie(config)
if aggregatorClient == nil {
config, err := kubecli.GetConfig()
if err != nil {
panic(err)
}

aggregatorClient = aggregatorclient.NewForConfigOrDie(config)
}
})

Describe("[rfe_id:4102][crit:medium][vendor:cnv-qe@redhat.com][level:component]certificates", func() {

Expand Down
9 changes: 6 additions & 3 deletions tests/kubectl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,14 @@ var _ = Describe("[rfe_id:3423][vendor:cnv-qe@redhat.com][level:component]oc/kub

var k8sClient, result string
var vm *v1.VirtualMachine

virtCli, err := kubecli.GetKubevirtClient()
tests.PanicOnError(err)
var virtCli kubecli.KubevirtClient
var err error

BeforeEach(func() {

virtCli, err = kubecli.GetKubevirtClient()
tests.PanicOnError(err)

k8sClient = tests.GetK8sCmdClient()
tests.SkipIfNoCmd(k8sClient)
tests.BeforeTestCleanup()
Expand Down
8 changes: 5 additions & 3 deletions tests/migration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,16 @@ const (

var _ = Describe("[rfe_id:393][crit:high][vendor:cnv-qe@redhat.com][level:system] VM Live Migration", func() {
tests.FlagParse()

virtClient, err := kubecli.GetKubevirtClient()
tests.PanicOnError(err)
var virtClient kubecli.KubevirtClient

var originalKubeVirtConfig *k8sv1.ConfigMap
var err error

tests.BeforeAll(func() {

virtClient, err = kubecli.GetKubevirtClient()
tests.PanicOnError(err)

originalKubeVirtConfig, err = virtClient.CoreV1().ConfigMaps(tests.KubeVirtInstallNamespace).Get(virtconfig.ConfigMapName, metav1.GetOptions{})
if err != nil && !errors.IsNotFound(err) {
Expect(err).ToNot(HaveOccurred())
Expand Down
8 changes: 6 additions & 2 deletions tests/networkpolicy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,18 @@ var _ = Describe("[rfe_id:150][crit:high][vendor:cnv-qe@redhat.com][level:compon

tests.FlagParse()

virtClient, err := kubecli.GetKubevirtClient()
tests.PanicOnError(err)
var virtClient kubecli.KubevirtClient

var vmia *v1.VirtualMachineInstance
var vmib *v1.VirtualMachineInstance
var vmic *v1.VirtualMachineInstance

tests.BeforeAll(func() {
var err error

virtClient, err = kubecli.GetKubevirtClient()
tests.PanicOnError(err)

tests.SkipIfUseFlannel(virtClient)
tests.SkipIfNotUseNetworkPolicy(virtClient)
tests.BeforeTestCleanup()
Expand Down
Loading

0 comments on commit d921fc6

Please sign in to comment.