Skip to content

Commit

Permalink
Test ungraceful node(s) reboot(test part)
Browse files Browse the repository at this point in the history
  • Loading branch information
yprokule committed Feb 5, 2024
1 parent f50dcb8 commit 420fa7c
Show file tree
Hide file tree
Showing 7 changed files with 417 additions and 0 deletions.
121 changes: 121 additions & 0 deletions tests/reboot/internal/rebootconfig/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package rebootconfig

import (
"encoding/json"
"fmt"
"log"
"os"
"path/filepath"
"runtime"

"github.com/kelseyhightower/envconfig"
"github.com/openshift-kni/eco-gosystem/tests/internal/config"
"gopkg.in/yaml.v2"
)

const (
// PathToDefaultRebootParamsFile path to config file with default ran du parameters.
PathToDefaultRebootParamsFile = "./default.yaml"
)

// RebootConfig type keeps ran du configuration.
type RebootConfig struct {
*config.GeneralConfig
ControlPlaneLabelStr string `yaml:"control_plane_nodes_label" envconfig:"ECO_REBOOT_CONTROL_PLANE_NODES_LABEL"`
MasterNodesLabelStr string `yaml:"master_nodes_label" envconfig:"ECO_REBOOT_MASTER_NODES_LABEL"`
WorkerNodesLabelStr string `yaml:"worker_nodes_label" envconfig:"ECO_REBOOT_WORKER_NODES_LABEL"`
NodesCredentialsMap NodesBMCMap `yaml:"nodes_bmc_map" envconfig:"ECO_SYSTEM_NODES_CREDENTIALS_MAP"`
}

// BMCDetails structure to hold BMC details.
type BMCDetails struct {
Username string `json:"username"`
Password string `json:"password"`
BMCAddress string `json:"bmc"`
}

// NodesBMCMap holds info about BMC connection for a specific node.
type NodesBMCMap map[string]BMCDetails

// Decode - method for envconfig package to parse JSON encoded environment variables.
func (nad *NodesBMCMap) Decode(value string) error {
nodesAuthMap := new(map[string]BMCDetails)

err := json.Unmarshal([]byte(value), nodesAuthMap)

if err != nil {
log.Printf("Error to parse data %v", err)

return fmt.Errorf("invalid map json: %w", err)
}

*nad = *nodesAuthMap

return nil
}

// NewRebootConfig returns instance of RebootConfig config type.
func NewRebootConfig() *RebootConfig {
log.Print("Creating new RebootConfig struct")

var rebootConf RebootConfig
rebootConf.GeneralConfig = config.NewConfig()

var confFile string

if fileFromEnv, exists := os.LookupEnv("ECO_SYSTEM_REBOOT_CONFIG_FILE_PATH"); !exists {
_, filename, _, _ := runtime.Caller(0)
baseDir := filepath.Dir(filename)
confFile = filepath.Join(baseDir, PathToDefaultRebootParamsFile)
} else {
confFile = fileFromEnv
}

log.Printf("Open config file %s", confFile)

err := readFile(&rebootConf, confFile)
if err != nil {
log.Printf("Error to read config file %s", confFile)

return nil
}

err = readEnv(&rebootConf)

if err != nil {
log.Print("Error to read environment variables")

return nil
}

return &rebootConf
}

func readFile(rebootConfig *RebootConfig, cfgFile string) error {
openedCfgFile, err := os.Open(cfgFile)
if err != nil {
return err
}

defer func() {
_ = openedCfgFile.Close()
}()

decoder := yaml.NewDecoder(openedCfgFile)
err = decoder.Decode(&rebootConfig)

if err != nil {
return err
}

return nil
}

func readEnv(rebootConfig *RebootConfig) error {
err := envconfig.Process("", rebootConfig)
if err != nil {
return err
}

return nil
}
10 changes: 10 additions & 0 deletions tests/reboot/internal/rebootconfig/default.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
# default configurations.
verbose_level: 0
dump_failed_tests: false
reports_dump_dir: "/tmp/reports"
polarion_report: true
dry_run: false
control_plane_nodes_label: "node-role.kubernetes.io/control-plane=''"
master_nodes_label: "node-role.kubernetes.io/master=''"
worker_nodes_label: "node-role.kubernetes.io/worker=''"
21 changes: 21 additions & 0 deletions tests/reboot/internal/rebootinittools/rebootinittools.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package rebootinittools

import (
"github.com/openshift-kni/eco-goinfra/pkg/clients"
"github.com/openshift-kni/eco-gosystem/tests/internal/inittools"
"github.com/openshift-kni/eco-gosystem/tests/reboot/internal/rebootconfig"
)

var (
// APIClient provides API access to cluster.
APIClient *clients.Settings
// RebootTestConfig provides access to tests configuration parameters.
RebootTestConfig *rebootconfig.RebootConfig
)

// init loads all variables automatically when this package is imported. Once package is imported a user has full
// access to all vars within init function. It is recommended to import this package using dot import.
func init() {
RebootTestConfig = rebootconfig.NewRebootConfig()
APIClient = inittools.APIClient
}
12 changes: 12 additions & 0 deletions tests/reboot/internal/rebootparams/const.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package rebootparams

const (
// Label is used for 'reboot' test cases selection.
Label = "reboot"

// LabelValidateReboot is used to select tests that reboot cluster.
LabelValidateReboot = "validate_reboots"

// RebootLogLevel configures logging level for reboot related tests.
RebootLogLevel = 90
)
22 changes: 22 additions & 0 deletions tests/reboot/internal/rebootparams/rebootvars.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package rebootparams

import (
systemtestsparams "github.com/openshift-kni/eco-gosystem/tests/internal/params"
"github.com/openshift-kni/k8sreporter"
v1 "k8s.io/api/core/v1"
)

var (
// Labels represents the range of labels that can be used for test cases selection.
Labels = []string{systemtestsparams.Label, Label}

// ReporterNamespacesToDump tells to the reporter from where to collect logs.
ReporterNamespacesToDump = map[string]string{
"openshift-machine-api": "openshift-machine-api",
}

// ReporterCRDsToDump tells to the reporter what CRs to dump.
ReporterCRDsToDump = []k8sreporter.CRData{
{Cr: &v1.PodList{}},
}
)
37 changes: 37 additions & 0 deletions tests/reboot/reboots_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package spk_system_test

import (
"runtime"
"testing"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/openshift-kni/eco-goinfra/pkg/clients"
"github.com/openshift-kni/eco-goinfra/pkg/polarion"
"github.com/openshift-kni/eco-goinfra/pkg/reporter"
. "github.com/openshift-kni/eco-gosystem/tests/internal/inittools"

"github.com/openshift-kni/eco-gosystem/tests/reboot/internal/rebootparams"
_ "github.com/openshift-kni/eco-gosystem/tests/reboot/tests"
)

var _, currentFile, _, _ = runtime.Caller(0)

func TestClusterReboot(t *testing.T) {
_, reporterConfig := GinkgoConfiguration()
reporterConfig.JUnitReport = GeneralConfig.GetJunitReportPath(currentFile)

RegisterFailHandler(Fail)
RunSpecs(t, "SystemTests Reboot Suite", Label(rebootparams.Labels...), reporterConfig)
}

var _ = JustAfterEach(func() {
reporter.ReportIfFailed(
CurrentSpecReport(), GeneralConfig.GetDumpFailedTestReportLocation(currentFile), GeneralConfig.ReportsDirAbsPath,
rebootparams.ReporterNamespacesToDump, rebootparams.ReporterCRDsToDump, clients.SetScheme)
})

var _ = ReportAfterSuite("", func(report Report) {
polarion.CreateReport(
report, GeneralConfig.GetPolarionReportPath(), GeneralConfig.PolarionTCPrefix)
})
Loading

0 comments on commit 420fa7c

Please sign in to comment.