Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Abingcbc committed Oct 29, 2024
1 parent cd39d49 commit 35e7fb2
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 132 deletions.
21 changes: 14 additions & 7 deletions test/engine/setup/controller/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"context"
"fmt"
"os"
"path/filepath"
"time"

"github.com/avast/retry-go/v4"
Expand All @@ -25,9 +26,10 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/serializer/yaml"
"k8s.io/client-go/discovery"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/restmapper"

"github.com/alibaba/ilogtail/test/config"
)
Expand All @@ -50,7 +52,8 @@ type DaemonSetController struct {
}

type DynamicController struct {
dynamicClient dynamic.Interface
discoveryClient discovery.DiscoveryInterface
dynamicClient dynamic.Interface
}

func NewDeploymentController(k8sClient *kubernetes.Clientset) *DeploymentController {
Expand Down Expand Up @@ -215,8 +218,8 @@ func (c *DaemonSetController) GetDaemonSetPods(dsName, dsNamespace string) (*cor
return pods, nil
}

func NewDynamicController(dynamicClient dynamic.Interface) *DynamicController {
return &DynamicController{dynamicClient: dynamicClient}
func NewDynamicController(dynamicClient dynamic.Interface, discoveryClient discovery.DiscoveryInterface) *DynamicController {
return &DynamicController{dynamicClient: dynamicClient, discoveryClient: discoveryClient}
}

func (c *DynamicController) Apply(filePath string) error {
Expand Down Expand Up @@ -267,7 +270,8 @@ func (c *DynamicController) Delete(filePath string) error {

func (c *DynamicController) parseObjFromYaml(filePath string) (*meta.RESTMapping, *unstructured.Unstructured, error) {
// Read the YAML file
yamlFile, err := os.ReadFile(filePath)
basePath := "test_cases"
yamlFile, err := os.ReadFile(filepath.Join(basePath, filePath))
if err != nil {
return nil, nil, err
}
Expand All @@ -280,8 +284,11 @@ func (c *DynamicController) parseObjFromYaml(filePath string) (*meta.RESTMapping
return nil, nil, err
}

// Retrieve the REST mapping for the GVK
restMapper := meta.NewDefaultRESTMapper(scheme.Scheme.PreferredVersionAllGroups())
apiGroupResources, err := restmapper.GetAPIGroupResources(c.discoveryClient)
if err != nil {
return nil, nil, err
}
restMapper := restmapper.NewDiscoveryRESTMapper(apiGroupResources)
mapping, err := restMapper.RESTMapping(gvk.GroupKind(), gvk.Version)
if err != nil {
return nil, nil, err
Expand Down
15 changes: 11 additions & 4 deletions test/engine/setup/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"crypto/rand"
"fmt"
"math/big"
"sort"
"strings"

corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -80,12 +81,15 @@ func (k *K8sEnv) ExecOnLogtail(command string) (string, error) {
}
}
results := make([]string, 0)
sort.Slice(pods.Items, func(i, j int) bool {
return pods.Items[i].Name < pods.Items[j].Name
})
for _, pod := range pods.Items {
if result, err := k.execInPod(k.config, pod.Namespace, pod.Name, pod.Spec.Containers[0].Name, []string{"bash", "-c", command}); err != nil {
result, err := k.execInPod(k.config, pod.Namespace, pod.Name, pod.Spec.Containers[0].Name, []string{"bash", "-c", command})
if err != nil {
return "", err
} else {
results = append(results, result)
}
results = append(results, result)
}
return strings.Join(results, "\n"), nil
}
Expand Down Expand Up @@ -152,7 +156,10 @@ func (k *K8sEnv) init() {
k.daemonsetController = controller.NewDaemonSetController(k.k8sClient)

dynamicClient, err := dynamic.NewForConfig(c)
k.dynamicController = controller.NewDynamicController(dynamicClient)
if err != nil {
panic(err)
}
k.dynamicController = controller.NewDynamicController(dynamicClient, k.k8sClient.Discovery())
}

func (k *K8sEnv) execInPod(config *rest.Config, namespace, podName, containerName string, command []string) (string, error) {
Expand Down
6 changes: 2 additions & 4 deletions test/engine/steps.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,8 @@ func ScenarioInitializer(ctx *godog.ScenarioContext) {
ctx.When(`^generate \{(\d+)\} apsara logs to file \{(.*)\}, with interval \{(\d+)\}ms$`, trigger.Apsara)
ctx.When(`^generate \{(\d+)\} delimiter logs to file \{(.*)\}, with interval \{(\d+)\}ms, with delimiter \{(.*)\} and quote \{(.*)\}$`, trigger.DelimiterSingle)
ctx.When(`^generate \{(\d+)\} multiline delimiter logs to file \{(.*)\}, with interval \{(\d+)\}ms, with delimiter \{(.*)\} and quote \{(.*)\}$`, trigger.DelimiterMultiline)
ctx.When(`^generate \{(\d+)\} json logs to file \{(.*)\}, with interval \{(\d+)\}ms$`, trigger.JsonSingle)
ctx.When(`^generate \{(\d+)\} multiline json logs to file \{(.*)\}, with interval \{(\d+)\}ms$`, trigger.JsonMultiline)
ctx.When(`^generate \{(\d+)\} logs to stdout, with interval \{(\d+)\}ms$`, trigger.Stdout)
ctx.When(`^generate \{(\d+)\} logs to stderr, with interval \{(\d+)\}ms$`, trigger.Stderr)
ctx.When(`^generate \{(\d+)\} json logs to file \{(.*)\}, with interval \{(\d+)\}ms$`, trigger.JSONSingle)
ctx.When(`^generate \{(\d+)\} multiline json logs to file \{(.*)\}, with interval \{(\d+)\}ms$`, trigger.JSONMultiline)
ctx.When(`^execute \{(\d+)\} commands to generate file security events on files \{(.*)\}$`, trigger.TrigerFileSecurityEvents)
// ------------------------------------------

Expand Down
9 changes: 6 additions & 3 deletions test/engine/trigger/generator/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@
package generator

import (
"crypto/rand"
"fmt"
"math/rand"
"math/big"
"os"
"strconv"
"text/template"
Expand Down Expand Up @@ -66,12 +67,14 @@ func string2Template(strings []string) []*template.Template {
}

func getRandomLogLevel() string {
return Levels[rand.Intn(len(Levels))]
randInt, _ := rand.Int(rand.Reader, big.NewInt(int64(len(Levels))))
return Levels[randInt.Int64()]
}

func getRandomMark() string {
marks := []string{"-", "F"}
return marks[rand.Intn(2)]
randInt, _ := rand.Int(rand.Reader, big.NewInt(int64(len(marks))))
return marks[randInt.Int64()]
}

func getEnvOrDefault(env, fallback string) string {
Expand Down
10 changes: 5 additions & 5 deletions test/engine/trigger/generator/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ import (
"time"
)

// TestGenerateJsonSingle will be executed in the environment being collected.
func TestGenerateJsonSingle(t *testing.T) {
// TestGenerateJSONSingle will be executed in the environment being collected.
func TestGenerateJSONSingle(t *testing.T) {
config, err := getGenerateFileLogConfigFromEnv()
if err != nil {
t.Fatalf("get generate file log config from env failed: %v", err)
Expand Down Expand Up @@ -56,7 +56,7 @@ func TestGenerateJsonSingle(t *testing.T) {
} else {
currentTime = strconv.FormatInt(time.Now().UnixNano()/1000, 10)
}
err = testLogConentTmpl[logIndex].Execute(file, map[string]interface{}{
testLogConentTmpl[logIndex].Execute(file, map[string]interface{}{
"Mark": getRandomMark(),
"FileNo": fileNo,
"LogNo": logNo + i,
Expand All @@ -71,7 +71,7 @@ func TestGenerateJsonSingle(t *testing.T) {
}
}

func TestGenerateJsonMultiline(t *testing.T) {
func TestGenerateJSONMultiline(t *testing.T) {
config, err := getGenerateFileLogConfigFromEnv()
if err != nil {
t.Fatalf("get generate file log config from env failed: %v", err)
Expand Down Expand Up @@ -103,7 +103,7 @@ func TestGenerateJsonMultiline(t *testing.T) {
fileNo := rand.Intn(10000)
for i := 0; i < config.TotalLog; i++ {
currentTime := time.Now().Format("2006-01-02T15:04:05.999999999")
err = testLogConentTmpl[logIndex].Execute(file, map[string]interface{}{
testLogConentTmpl[logIndex].Execute(file, map[string]interface{}{
"Mark": getRandomMark(),
"FileNo": fileNo,
"LogNo": logNo + i,
Expand Down
61 changes: 0 additions & 61 deletions test/engine/trigger/generator/stdout_test.go

This file was deleted.

52 changes: 4 additions & 48 deletions test/engine/trigger/trigger.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ func RegexMultiline(ctx context.Context, totalLog int, path string, interval int
return generate(ctx, totalLog, path, interval, "TestGenerateRegexLogMultiline")
}

func JsonSingle(ctx context.Context, totalLog int, path string, interval int) (context.Context, error) {
return generate(ctx, totalLog, path, interval, "TestGenerateJsonSingle")
func JSONSingle(ctx context.Context, totalLog int, path string, interval int) (context.Context, error) {
return generate(ctx, totalLog, path, interval, "TestGenerateJSONSingle")
}

func JsonMultiline(ctx context.Context, totalLog int, path string, interval int) (context.Context, error) {
return generate(ctx, totalLog, path, interval, "TestGenerateJsonMultiline")
func JSONMultiline(ctx context.Context, totalLog int, path string, interval int) (context.Context, error) {
return generate(ctx, totalLog, path, interval, "TestGenerateJSONMultiline")
}

func Apsara(ctx context.Context, totalLog int, path string, interval int) (context.Context, error) {
Expand All @@ -57,50 +57,6 @@ func DelimiterMultiline(ctx context.Context, totalLog int, path string, interval
return generate(ctx, totalLog, path, interval, "TestGenerateDelimiterMultiline", "Delimiter", delimiter, "Quote", quote)
}

func Stdout(ctx context.Context, totalLog int, interval int) (context.Context, error) {
time.Sleep(3 * time.Second)
command := getRunTriggerCommand("TestGenerateStdout")
var triggerCommand strings.Builder
template := template.Must(template.New("trigger").Parse(triggerTemplate))
if err := template.Execute(&triggerCommand, map[string]interface{}{
"WorkDir": "",
"TotalLog": totalLog,
"Interval": interval,
"TestMode": "stdout",
"Filename": "",
"Custom": "",
"Command": command,
}); err != nil {
return ctx, err
}
if _, err := setup.Env.ExecOnSource(ctx, triggerCommand.String()); err != nil {
return ctx, err
}
return ctx, nil
}

func Stderr(ctx context.Context, totalLog int, interval int) (context.Context, error) {
time.Sleep(3 * time.Second)
command := getRunTriggerCommand("TestGenerateStdout")
var triggerCommand strings.Builder
template := template.Must(template.New("trigger").Parse(triggerTemplate))
if err := template.Execute(&triggerCommand, map[string]interface{}{
"WorkDir": "",
"TotalLog": totalLog,
"Interval": interval,
"TestMode": "stderr",
"Filename": "",
"Custom": "",
"Command": command,
}); err != nil {
return ctx, err
}
if _, err := setup.Env.ExecOnSource(ctx, triggerCommand.String()); err != nil {
return ctx, err
}
return ctx, nil
}

func generate(ctx context.Context, totalLog int, path string, interval int, commandName string, customKV ...string) (context.Context, error) {
time.Sleep(3 * time.Second)
command := getRunTriggerCommand(commandName)
Expand Down

0 comments on commit 35e7fb2

Please sign in to comment.