Files
ProjectOctopus/agent-go/a_executor/FunctionalExecutor_test.go
2024-03-29 11:39:14 +08:00

225 lines
4.9 KiB
Go

package a_executor
import (
"bytes"
"fmt"
"os"
"os/exec"
"reflect"
"strings"
"testing"
)
var closeSELinux = []string{
"sed",
"-i",
"s/SELINUX=enforcing/SELINUX=disabled/g",
"/etc/selinux/config",
}
var callShellScript = []string{
"/bin/bash",
"/root/IdeaProjects/ProjectOctopus/agent-go/tmp/simple.sh",
}
var shutdownFirewalld = []string{
// wrong usage of &&
"systemctl", "stop", "firewalld", "&&", "systemctl", "disable", "firewalld",
}
var wgetCommand = []string{
"wget",
"--timeout=10",
"https://oss-s1.107421.xyz/octopus_ssh_banner",
"-O",
"/home/wdd/IdeaProjects/ProjectOctopus/agent-go/executor/script/123",
}
var echoPathCommand = []string{
"echo",
"$PATH",
}
var pipelineCommandFalse = []string{
"ls",
"/etc",
"|",
"grep",
"passwd",
}
var pipelineCommand = [][]string{
{
"cat",
"/home/wdd/IdeaProjects/ProjectOctopus/agent-go/executor/script/123",
},
{
"grep",
"passwd",
},
}
var pipelineCommandMore = "apt-cache madison docker-ce | awk {print$3} | cut -d: -f2"
var pipelineCommandSecond = "systemctl status -q docker.service | grep found."
var pipelineCommandThird = "ls /etc/ | grep passwd | head -2"
var pipelineCommandFourth = "echo dsadsad | tee /home/wdd/IdeaProjects/ProjectOctopus/agent-go/executor/script/123"
var ifconfigCommand = []string{"ifconfig"}
func TestReadTimeOutput(t *testing.T) {
ReadTimeCommandExecutor(ifconfigCommand)
}
func TestAllCommandExecutor(t *testing.T) {
ok, result := AllCommandExecutor(echoPathCommand)
t.Logf("执行结果为 => %#v", ok)
t.Logf("执行日志为 => %#v", result)
}
func TestPureResultSingleExecute(t *testing.T) {
PureResultSingleExecute(closeSELinux)
}
func TestPipelineCommandExecutorSingle(t *testing.T) {
cmd1 := exec.Command("ls", "/etc/")
cmd2 := exec.Command("grep", "passwd")
var outputBuf1 bytes.Buffer
cmd1.Stdout = &outputBuf1
if err := cmd1.Start(); err != nil {
fmt.Printf("Error: The first command can not be startup %s\n", err)
return
}
if err := cmd1.Wait(); err != nil {
fmt.Printf("Error: Couldn't wait for the first command: %s\n", err)
return
}
cmd2.Stdin = &outputBuf1
var outputBuf2 bytes.Buffer
cmd2.Stdout = &outputBuf2
if err := cmd2.Start(); err != nil {
fmt.Printf("Error: The second command can not be startup: %s\n", err)
return
}
if err := cmd2.Wait(); err != nil {
fmt.Printf("Error: Couldn't wait for the second command: %s\n", err)
return
}
s := outputBuf2.String()
split := strings.Split(s, "\n")
BasicPrettyPrint(true, split)
}
func TestPipelineCommandExecutor(t *testing.T) {
//PipelineCommandExecutor(pipelineCommand)
pipelineStringToCommand := BasicTransPipelineCommand(pipelineCommandSecond)
t.Logf("pipelineCommmand are => %#v", pipelineStringToCommand)
ok, resultLog := PipelineCommandExecutor(pipelineStringToCommand)
t.Logf("command execute ok is => %#v", ok)
t.Logf("command result are 下 \n")
BasicPrettyPrint(ok, resultLog)
}
func TestPipelineCommandExecutor2(t *testing.T) {
// Test case 1: Empty pipeline
pipelineCommand := [][]string{}
ok, log := PipelineCommandExecutor(pipelineCommand)
if !ok {
t.Errorf("Expected success, got failure")
}
if len(log) != 0 {
t.Errorf("Expected no log, got %v", log)
}
// Test case 2: Single command in pipeline
pipelineCommand = [][]string{
{"echo", "Hello, world!"},
}
ok, log = PipelineCommandExecutor(pipelineCommand)
if !ok {
t.Errorf("Expected success, got failure")
}
expectedLog := []string{"Hello, world!"}
if !reflect.DeepEqual(log, expectedLog) {
t.Errorf("Expected log %v, got %v", expectedLog, log)
}
// Test case 3: Multiple commands in pipeline
pipelineCommand = [][]string{
{"echo", "Hello"},
{"echo", "world!"},
}
ok, log = PipelineCommandExecutor(pipelineCommand)
if !ok {
t.Errorf("Expected success, got failure")
}
expectedLog = []string{"Hello", "world!"}
if !reflect.DeepEqual(log, expectedLog) {
t.Errorf("Expected log %v, got %v", expectedLog, log)
}
// Add more test cases as needed...
}
func TestSimple(t *testing.T) {
for i := 0; i < 10; i++ {
var output bytes.Buffer
fmt.Printf("output address is => %p \n", &output)
}
fmt.Println()
var output bytes.Buffer
fmt.Printf("out is => %#v\n", output)
command := exec.Command("ls")
command.StdoutPipe()
}
func TestFileBasedPipe(t *testing.T) {
reader, writer, err := os.Pipe()
if err != nil {
fmt.Printf("Error: Couldn't create the named pipe: %s\n", err)
}
go func() {
output := make([]byte, 100)
n, err := reader.Read(output)
if err != nil {
fmt.Printf("Error: Couldn't read data from the named pipe: %s\n", err)
}
fmt.Printf("Read %d byte(s). [file-based pipe]\n", n)
fmt.Printf("Read content are => %s\n", string(output))
}()
input := make([]byte, 26)
for i := 65; i <= 90; i++ {
input[i-65] = byte(i)
}
n, err := writer.Write(input)
if err != nil {
fmt.Printf("Error: Couldn't write data to the named pipe: %s\n", err)
}
fmt.Printf("Written %d byte(s). [file-based pipe]\n", n)
//time.Sleep(200 * time.Millisecond)
}