Skip to content

Commit

Permalink
Xray Migration Package function/variable name change (#298)
Browse files Browse the repository at this point in the history
(cherry picked from commit 33bd92cf1c1b425a914f4593a3c2b5b243b914f8)
  • Loading branch information
Paramadon authored and sky333999 committed Jul 5, 2023
1 parent c77df3d commit 6325ddf
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 45 deletions.
38 changes: 18 additions & 20 deletions tool/xraydaemonmigration/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,15 @@ package xraydaemonmigration

import (
"encoding/json"
"fmt"
"github.com/shirou/gopsutil/process"
"path/filepath"
"strings"

"github.com/shirou/gopsutil/process"

"gopkg.in/yaml.v3"
)

type Process interface {
Name() (string, error)
Cwd() (string, error)
CmdlineSlice() ([]string, error)
}
Expand Down Expand Up @@ -57,14 +56,9 @@ type JsonConfig struct {
} `json:"traces"`
}

func mapJsonStructToYamlStruct(yamlData []byte, process Process) (YamlConfig, error) {
func daemonFlagSet(yamlConfig YamlConfig, process Process) (YamlConfig, error) {
var configFilePath string
flag := NewFlag("X-Ray Daemon")
var yamlConfig YamlConfig
err := yaml.Unmarshal(yamlData, &yamlConfig)
if err != nil {
return yamlConfig, err
}

flag.StringVarF(&yamlConfig.ResourceARN, "resource-arn", "a", yamlConfig.ResourceARN, "Amazon Resource Name (ARN) of the AWS resource running the daemon.")
flag.BoolVarF(&yamlConfig.LocalMode, "local-mode", "o", yamlConfig.LocalMode, "Don't check for EC2 instance metadata.")
Expand All @@ -75,9 +69,12 @@ func mapJsonStructToYamlStruct(yamlData []byte, process Process) (YamlConfig, er
flag.StringVarF(&yamlConfig.RoleARN, "role-arn", "r", yamlConfig.RoleARN, "Assume the specified IAM role to upload segments to a different account.")
flag.StringVarF(&configFilePath, "config", "c", "", "Load a configuration file from the specified path.")
flag.StringVarF(&yamlConfig.ProxyAddress, "proxy-address", "p", yamlConfig.ProxyAddress, "Proxy address through which to upload segments.")
temp, err := process.CmdlineSlice()
if len(temp) != 0 {
flag.fs.Parse(temp[1:])
cmdline, err := process.CmdlineSlice()
if err != nil {
return yamlConfig, err
}
if len(cmdline) != 0 {
flag.fs.Parse(cmdline[1:])
}
return yamlConfig, nil
}
Expand All @@ -104,9 +101,10 @@ var GetProcesses = func() ([]Process, error) {
func ConvertYamlToJson(yamlData []byte, process Process) ([]byte, error) {

var jsonConfig JsonConfig
yamlConfig, err := mapJsonStructToYamlStruct(yamlData, process)
var yamlConfig YamlConfig
err := yaml.Unmarshal(yamlData, &yamlConfig)
yamlConfig, err = daemonFlagSet(yamlConfig, process)
if err != nil {
fmt.Println("There was a problem mapping json struct to yaml struct")
return nil, err
}
jsonConfig.Traces.TracesCollected.Xray.BindAddress = yamlConfig.Socket.UDPAddress
Expand Down Expand Up @@ -148,15 +146,15 @@ func FindAllPotentialConfigFiles() ([]string, error) {
return nil, err
}

temp := path
configFile := path
//If the cwd in path, then that is the full path, otherwise add to config file path
if filepath.IsAbs(path) {
temp = path
configFile = path
} else {
temp = filepath.Join(cwd, path)
configFile = filepath.Join(cwd, path)
}

allPotentialConfigFiles = append(allPotentialConfigFiles, temp)
allPotentialConfigFiles = append(allPotentialConfigFiles, configFile)

}
if len(allPotentialConfigFiles) == 0 {
Expand All @@ -182,8 +180,8 @@ func FindAllDaemons() ([]Process, error) {
// get the config file path from arguments
func GetPathFromArgs(argList []string) string {
for i := 0; i < len(argList); i++ {
temp := strings.Trim(strings.ToLower(argList[i]), "-")
if temp == "c" || temp == "config" {
arg := strings.Trim(strings.ToLower(argList[i]), "-")
if arg == "c" || arg == "config" {
if i+1 < len(argList) {
return argList[i+1]
} else {
Expand Down
28 changes: 3 additions & 25 deletions tool/xraydaemonmigration/migrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
package xraydaemonmigration

import (
"github.com/shirou/gopsutil/v3/process"
"os"
"path/filepath"
"testing"
Expand All @@ -13,7 +12,6 @@ import (
)

type proc struct {
*process.Process
pid int32
name string
cmdline []string
Expand Down Expand Up @@ -58,26 +56,6 @@ var mockProcesses = func() ([]Process, error) {
processes := []Process{correctDaemonProcess, duplicateDaemonProcess, randomProcess, randomNoNameProcess}
return processes, nil
}
var mockDaemonProcesses = func() ([]Process, error) {
wd, _ := os.Getwd()

var correctDaemonProcess = &proc{
pid: 123,
name: "xray",
cmdline: []string{"xray", "-c", filepath.Join("testdata", "cfg.yaml"), "-b", "127.0.0.1:2000", "-t", "127.0.0.1:2000", "-a", "resourceTesting", "-n", "us-east-1", "-m", "23", "-r", "roleTest", "-p", "127.0.0.1:2000"},
cwd: filepath.Join(wd),
}

var duplicateDaemonProcess = &proc{
pid: 456,
name: "xray",
cmdline: []string{"xray", "-c", filepath.Join("testdata", "cfg.yaml")},
cwd: filepath.Join(wd),
}

processes := []Process{correctDaemonProcess, duplicateDaemonProcess}
return processes, nil
}

var mockProcessesNone = func() ([]Process, error) {
return nil, nil
Expand All @@ -86,10 +64,10 @@ var mockProcessesNone = func() ([]Process, error) {
var _ Process = (*proc)(nil)

func TestAllDaemonFunction(t *testing.T) {
GetProcesses = mockDaemonProcesses
GetProcesses = mockProcesses
result, err := FindAllDaemons()
require.NoError(t, err)
assert.Equal(t, 2, len(result))
assert.Equal(t, 4, len(result))
GetProcesses = mockProcessesNone
result, err = FindAllDaemons()
require.NoError(t, err)
Expand Down Expand Up @@ -151,7 +129,7 @@ func TestCovertYamlToJson(t *testing.T) {
cwd: filepath.Join(wd),
}
jsonFile, err := ConvertYamlToJson(yamlFile, duplicateDaemonProcess)
assert.NotNil(t, err)
assert.Nil(t, err)

actualFilePath := filepath.Join("testdata", "actualConfig.json")
yamlFile, err = os.ReadFile(configFilePath)
Expand Down

0 comments on commit 6325ddf

Please sign in to comment.