Skip to content

Commit

Permalink
Change by request
Browse files Browse the repository at this point in the history
Signed-off-by: Anatoly Loskutnikov <anatoly.loskutnikov@gmail.com>
  • Loading branch information
Tiberivs committed Mar 19, 2021
1 parent 396c099 commit 0fe2a2e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 28 deletions.
3 changes: 1 addition & 2 deletions extensions/base/suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,13 @@ type Suite struct {

func (s *Suite) SetupSuite() {
// Add other extensions here
s.Suite.WithSuits(
s.Suite = multisuite.New(s.T(),
new(logs.Suite),
&checkout.Suite{
Repository: "networkservicemesh/deployments-k8s",
Version: "2ed0aaac",
Dir: "../", // Note: this should be synced with input parameters in gen.go file
})

s.Suite.SetT(s.T())
s.Suite.SetupSuite()
}
46 changes: 22 additions & 24 deletions extensions/logs/suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,20 @@ import (
"github.com/stretchr/testify/suite"
)

const (
allNamespaces = ""
configPrefix = "LOG"
)

var config Config

type Config struct {
ArtifactsDir string `default:"logs" desc:"Directory for storing container logs" split_words:"true"`
ExcludeK8sNs string `default:"kube-system,local-path-storage" desc:"Comma-separated list of excluded kubernetes namespaces" split_words:"true"`
KubeConfig string `default:"~/.kube/config" desc:"Kubernetes configuration file" envconfig:"KUBECONFIG"`
ArtifactsDir string `default:"logs" desc:"Directory for storing container logs" envconfig:"ARTIFACTS_DIR"`

ExcludeK8sNs []string `default:"kube-system,local-path-storage" desc:"Comma-separated list of excluded kubernetes namespaces" split_words:"true"`
ContextTimeout time.Duration `default:"15s" desc:"Context timeout for kubernetes queries" split_words:"true"`
LogWorkerCount int `default:"4" desc:"Number of log collector workers" split_words:"true"`
WorkerCount int `default:"4" desc:"Number of log collector workers" split_words:"true"`
}

type Suite struct {
Expand All @@ -64,11 +71,11 @@ type logItem struct {
}

func init() {
if err := envconfig.Usage("", &config); err != nil {
if err := envconfig.Usage(configPrefix, &config); err != nil {
panic(err)
}

if err := envconfig.Process("", &config); err != nil {
if err := envconfig.Process(configPrefix, &config); err != nil {
panic(err)
}
}
Expand All @@ -80,7 +87,7 @@ func (s *Suite) SetupSuite() {
require.NoError(s.T(), err)

s.logQueue = make(chan logItem)
for i := 0; i < config.LogWorkerCount; i++ {
for i := 0; i < config.WorkerCount; i++ {
s.waitGroup.Add(1)
go func() {
defer s.waitGroup.Done()
Expand All @@ -103,7 +110,7 @@ func (s *Suite) SetupTest() {
ctx, cancel := context.WithTimeout(context.Background(), config.ContextTimeout)
defer cancel()

pods, err := s.kubeClient.CoreV1().Pods("").List(ctx, metav1.ListOptions{})
pods, err := s.kubeClient.CoreV1().Pods(allNamespaces).List(ctx, metav1.ListOptions{})
require.NoError(s.T(), err)

for podIdx := range pods.Items {
Expand All @@ -127,7 +134,7 @@ func (s *Suite) AfterTest(suiteName, testName string) {
ctx, cancel := context.WithTimeout(context.Background(), config.ContextTimeout)
defer cancel()

pods, err := s.kubeClient.CoreV1().Pods("").List(ctx, metav1.ListOptions{})
pods, err := s.kubeClient.CoreV1().Pods(allNamespaces).List(ctx, metav1.ListOptions{})
require.NoError(s.T(), err)

var waitGroup sync.WaitGroup
Expand Down Expand Up @@ -177,34 +184,25 @@ func (s *Suite) saveLog(src logItem) {
}
}

func envOrDefault(key, defaultValue string) string {
value, ok := os.LookupEnv(key)
if !ok {
value = defaultValue
}

return value
}

// newKubeClient creates new k8s client
func newKubeClient() (kubernetes.Interface, error) {
defaultPath := filepath.Join(os.Getenv("HOME"), ".kube", "config")
path := envOrDefault("KUBECONFIG", defaultPath)
if strings.HasPrefix(config.KubeConfig, "~") {
config.KubeConfig = strings.Replace(config.KubeConfig, "~", os.Getenv("HOME"), 1)
}

kubeconfig, err := clientcmd.BuildConfigFromFlags("", path)
kubeconfig, err := clientcmd.BuildConfigFromFlags("", config.KubeConfig)
if err != nil {
return nil, err
}

kubeconfig.Burst = config.LogWorkerCount * 100
kubeconfig.QPS = float32(config.LogWorkerCount) * 100
kubeconfig.Burst = config.WorkerCount * 100
kubeconfig.QPS = float32(config.WorkerCount) * 100

return kubernetes.NewForConfig(kubeconfig)
}

func isExcludedNamespace(ns string) bool {
excludeList := strings.Split(config.ExcludeK8sNs, `,`)
for _, excluded := range excludeList {
for _, excluded := range config.ExcludeK8sNs {
if ns == excluded {
return true
}
Expand Down
6 changes: 4 additions & 2 deletions extensions/multisuite/suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@ type Suite struct {
suits []suite.TestingSuite
}

func (s *Suite) WithSuits(suits ...suite.TestingSuite) {
s.suits = suits
func New(t *testing.T, suits ...suite.TestingSuite) Suite {
s := Suite{suits: suits}
s.SetT(t)
return s
}

func (s *Suite) SetT(t *testing.T) {
Expand Down

0 comments on commit 0fe2a2e

Please sign in to comment.