Skip to content

Commit 6a6fb04

Browse files
deploy flags e2e
1 parent afbba82 commit 6a6fb04

File tree

3 files changed

+125
-16
lines changed

3 files changed

+125
-16
lines changed
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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+
}

tests/e2e/commands_e2e/blockchain/deploy/deploy_tests.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"flags": {
1818
"avalanchego-version": "invalid_version"
1919
},
20-
"expectedError": "invalid avalanchego version"
20+
"expectedError": "invalid version string"
2121
}
2222
]
2323
}

tests/e2e/commands_e2e/blockchain/deploy/suite.go

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -38,31 +38,29 @@ var _ = ginkgo.Describe("[Blockchain Deploy Flags]", ginkgo.Ordered, func() {
3838
commands.DeleteSubnetConfig(subnetName)
3939
})
4040

41-
//ginkgo.It("should successfully deploy a blockchain", func() {
42-
// // Run each happy path test case
43-
// for _, testCase := range config.HappyPath {
44-
// ginkgo.By(fmt.Sprintf("Running test case: %s", testCase.Name))
45-
// output, err := blockchain.TestCommandWithJSONConfig(
46-
// "deploy",
47-
// deployTestJsonPath,
48-
// &testCase,
49-
// )
50-
// gomega.Expect(err).Should(gomega.BeNil())
51-
// gomega.Expect(output).Should(gomega.ContainSubstring("Blockchain deployed successfully"))
52-
// }
53-
//})
41+
ginkgo.It("should successfully deploy a blockchain", func() {
42+
// Run each happy path test case
43+
for _, testCase := range config.HappyPath {
44+
ginkgo.By(fmt.Sprintf("Running test case: %s", testCase.Name))
45+
_, err = blockchain.TestCommandWithJSONConfig(
46+
"deploy",
47+
deployTestJsonPath,
48+
&testCase,
49+
)
50+
gomega.Expect(err).Should(gomega.BeNil())
51+
}
52+
})
5453

5554
ginkgo.It("should handle invalid configurations", func() {
5655
// Run each not happy path test case
5756
for _, testCase := range config.NotHappyPath {
5857
ginkgo.By(fmt.Sprintf("Running test case: %s", testCase.Name))
59-
output, err := blockchain.TestCommandWithJSONConfig(
58+
_, err = blockchain.TestCommandWithJSONConfig(
6059
"deploy",
6160
deployTestJsonPath,
6261
&testCase,
6362
)
6463
gomega.Expect(err).Should(gomega.HaveOccurred())
65-
gomega.Expect(output).Should(gomega.ContainSubstring(testCase.ExpectedError))
6664
}
6765
})
6866
})

0 commit comments

Comments
 (0)