|
| 1 | +package blockchain |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "os" |
| 8 | + "os/exec" |
| 9 | + |
| 10 | + "github.com/ava-labs/avalanche-cli/tests/e2e/utils" |
| 11 | +) |
| 12 | + |
| 13 | +// TestCase represents a single test case configuration |
| 14 | +type TestCase struct { |
| 15 | + Name string `json:"name"` |
| 16 | + Flags map[string]string `json:"flags"` |
| 17 | + ExpectedError string `json:"expectedError,omitempty"` |
| 18 | +} |
| 19 | + |
| 20 | +// TestJSONConfig represents the json configuration that contains cli command flag inputs |
| 21 | +type TestJSONConfig struct { |
| 22 | + GlobalFlags map[string]interface{} `json:"globalFlags"` |
| 23 | + HappyPath []TestCase `json:"happyPath"` |
| 24 | + NotHappyPath []TestCase `json:"notHappyPath"` |
| 25 | +} |
| 26 | + |
| 27 | +var avalancheBinaryPath = "./bin/avalanche" |
| 28 | + |
| 29 | +// SetAvalancheBinaryPath sets the path to the avalanche binary |
| 30 | +func SetAvalancheBinaryPath(path string) { |
| 31 | + avalancheBinaryPath = path |
| 32 | +} |
| 33 | + |
| 34 | +// TestCommandWithJSONConfig tests a CLI command with flag inputs from a JSON file |
| 35 | +func TestCommandWithJSONConfig(command string, configPath string, testCase *TestCase) (string, error) { |
| 36 | + blockchainCmd := "blockchain" |
| 37 | + |
| 38 | + // Read and parse the JSON config file |
| 39 | + configData, err := os.ReadFile(configPath) |
| 40 | + if err != nil { |
| 41 | + return "", fmt.Errorf("failed to read config file: %w", err) |
| 42 | + } |
| 43 | + |
| 44 | + var config TestJSONConfig |
| 45 | + if err := json.Unmarshal(configData, &config); err != nil { |
| 46 | + return "", fmt.Errorf("failed to parse config file: %w", err) |
| 47 | + } |
| 48 | + |
| 49 | + // Build command arguments |
| 50 | + cmdArgs := []string{blockchainCmd, command} |
| 51 | + |
| 52 | + // Add blockchain name from global flags |
| 53 | + if blockchainName, ok := config.GlobalFlags["blockchainName"].(string); ok { |
| 54 | + cmdArgs = append(cmdArgs, blockchainName) |
| 55 | + } |
| 56 | + |
| 57 | + // Create a map to store all flags, starting with global flags |
| 58 | + allFlags := make(map[string]interface{}) |
| 59 | + for flag, value := range config.GlobalFlags { |
| 60 | + if flag != "blockchainName" { |
| 61 | + allFlags[flag] = value |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + // Override with test case specific flags if provided |
| 66 | + if testCase != nil { |
| 67 | + for flag, value := range testCase.Flags { |
| 68 | + allFlags[flag] = value |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + // Add all flags to command arguments |
| 73 | + for flag, value := range allFlags { |
| 74 | + cmdArgs = append(cmdArgs, "--"+flag+"="+fmt.Sprintf("%v", value)) |
| 75 | + } |
| 76 | + |
| 77 | + // Execute the command |
| 78 | + cmd := exec.Command(avalancheBinaryPath, cmdArgs...) |
| 79 | + fmt.Println(cmd) |
| 80 | + output, err := cmd.CombinedOutput() |
| 81 | + if err != nil { |
| 82 | + var ( |
| 83 | + exitErr *exec.ExitError |
| 84 | + stderr string |
| 85 | + ) |
| 86 | + if errors.As(err, &exitErr) { |
| 87 | + stderr = string(exitErr.Stderr) |
| 88 | + } |
| 89 | + fmt.Println(string(output)) |
| 90 | + utils.PrintStdErr(err) |
| 91 | + fmt.Println(stderr) |
| 92 | + return "", err |
| 93 | + } |
| 94 | + |
| 95 | + return string(output), nil |
| 96 | +} |
| 97 | + |
| 98 | +// ReadTestConfig reads and parses the test configuration from a JSON file |
| 99 | +func ReadTestConfig(configPath string) (*TestJSONConfig, error) { |
| 100 | + configData, err := os.ReadFile(configPath) |
| 101 | + if err != nil { |
| 102 | + return nil, fmt.Errorf("failed to read config file: %w", err) |
| 103 | + } |
| 104 | + |
| 105 | + var config TestJSONConfig |
| 106 | + if err := json.Unmarshal(configData, &config); err != nil { |
| 107 | + return nil, fmt.Errorf("failed to parse config file: %w", err) |
| 108 | + } |
| 109 | + |
| 110 | + return &config, nil |
| 111 | +} |
0 commit comments