-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from slankdev/slankdev
Refactor
- Loading branch information
Showing
31 changed files
with
2,958 additions
and
1,337 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,8 @@ | ||
# vtyang | ||
Yang based VTY | ||
|
||
``` | ||
mkdir -p /tmp/vtyang/run | ||
echo '{"users": {"user": [{"name": "hiroki"}]}}' > /tmp/config.json | ||
./vtyang agent --dbpath /tmp/config.json --run-path /tmp/vtyang/run | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package util | ||
|
||
import ( | ||
"os" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
func NewCommandCompletion(rootCmd *cobra.Command) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "completion [sub operation]", | ||
Short: "Display completion snippet", | ||
Args: cobra.MinimumNArgs(1), | ||
} | ||
|
||
cmd.AddCommand(&cobra.Command{ | ||
Use: "bash", | ||
Short: "Display bash-completion snippet", | ||
Args: cobra.MinimumNArgs(0), | ||
Run: func(cmd *cobra.Command, args []string) { | ||
rootCmd.GenBashCompletion(os.Stdout) | ||
}, | ||
SilenceUsage: true, | ||
}) | ||
|
||
cmd.AddCommand(&cobra.Command{ | ||
Use: "zsh", | ||
Short: "Display zsh-completion snippet", | ||
Args: cobra.MinimumNArgs(0), | ||
Run: func(cmd *cobra.Command, args []string) { | ||
rootCmd.GenZshCompletion(os.Stdout) | ||
}, | ||
SilenceUsage: true, | ||
}) | ||
|
||
return cmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package util | ||
|
||
import "os" | ||
|
||
func FileExists(filename string) bool { | ||
_, err := os.Stat(filename) | ||
return err == nil | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,198 @@ | ||
package vtyang | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"github.com/slankdev/vtyang/pkg/util" | ||
) | ||
|
||
const ( | ||
YANG_PATH = "./testdata" | ||
RUNTIME_PATH = "/tmp/run/vtyang" | ||
) | ||
|
||
type TestCaseForTestAgent struct { | ||
Inputs []string | ||
Output string | ||
} | ||
|
||
func TestAgentNoDatabase(t *testing.T) { | ||
testcases := []TestCaseForTestAgent{ | ||
{ | ||
Inputs: []string{ | ||
"show running-config", | ||
}, | ||
Output: "{}\n", | ||
}, | ||
{ | ||
Inputs: []string{ | ||
"configure", | ||
"set users user hiroki", | ||
"commit", | ||
"do show running-config", | ||
}, | ||
Output: TestAgentNoDatabaseOutput2, | ||
}, | ||
} | ||
|
||
// Preparation | ||
GlobalOptRunFilePath = RUNTIME_PATH | ||
if util.FileExists(getDatabasePath()) { | ||
if err := os.Remove(getDatabasePath()); err != nil { | ||
t.Error(err) | ||
} | ||
} | ||
|
||
// Initializing Agent | ||
if err := InitAgent(RUNTIME_PATH, YANG_PATH); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
// EXECUTE TEST CASES | ||
for idx, tc := range testcases { | ||
buf := setStdoutWithBuffer() | ||
for _, input := range tc.Inputs { | ||
t.Logf("Testcase[%d] executing %s", idx, input) | ||
getCommandNodeCurrent().executeCommand(input) | ||
} | ||
result := buf.String() | ||
if tc.Output != result { | ||
t.Errorf("Unexpected output") | ||
for _, input := range tc.Inputs { | ||
t.Errorf("input %+v", input) | ||
} | ||
t.Errorf("expect(len=%d) %+v", len(tc.Output), tc.Output) | ||
t.Errorf("result(len=%d) %+v", len(result), result) | ||
t.Fatal("quiting test with FAILED result") | ||
} | ||
t.Logf("Testcase[%d] output check is succeeded", idx) | ||
} | ||
} | ||
|
||
func TestAgentLoadDatabase(t *testing.T) { | ||
testcases := []TestCaseForTestAgent{ | ||
{ | ||
Inputs: []string{ | ||
"show running-config", | ||
}, | ||
Output: TestAgentLoadDatabaseOutput1, | ||
}, | ||
{ | ||
Inputs: []string{ | ||
"configure", | ||
"set users user shirokura projects mfplane", | ||
"set users user shirokura age 28", | ||
"commit", | ||
"do show running-config", | ||
}, | ||
Output: TestAgentLoadDatabaseOutput2, | ||
}, | ||
// (3) Delete database node | ||
// inputs: | ||
// - configure | ||
// - set segment-routing ... | ||
// output: xxx | ||
// (4) Update database node | ||
// (5) CLI Completion | ||
} | ||
|
||
// Preparation | ||
if err := os.WriteFile(getDatabasePath(), []byte(dbContent), 0644); err != nil { | ||
t.Error(err) | ||
} | ||
|
||
// Initializing Agent | ||
if err := InitAgent(RUNTIME_PATH, YANG_PATH); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
// EXECUTE TEST CASES | ||
for idx, tc := range testcases { | ||
buf := setStdoutWithBuffer() | ||
for _, input := range tc.Inputs { | ||
t.Logf("Testcase[%d] executing %s", idx, input) | ||
getCommandNodeCurrent().executeCommand(input) | ||
} | ||
result := buf.String() | ||
if tc.Output != result { | ||
t.Errorf("Unexpected output") | ||
for _, input := range tc.Inputs { | ||
t.Errorf("input %+v", input) | ||
} | ||
t.Errorf("expect(len=%d) %+v", len(tc.Output), tc.Output) | ||
t.Errorf("result(len=%d) %+v", len(result), result) | ||
t.Fatal("quiting test with FAILED result") | ||
} | ||
t.Logf("Testcase[%d] output check is succeeded", idx) | ||
} | ||
} | ||
|
||
const dbContent = ` | ||
{ | ||
"users": { | ||
"user": [ | ||
{ | ||
"age": 22, | ||
"name": "hiroki" | ||
}, | ||
{ | ||
"age": 30, | ||
"name": "slank" | ||
} | ||
] | ||
} | ||
} | ||
` | ||
|
||
const TestAgentNoDatabaseOutput2 = `{ | ||
"users": { | ||
"user": [ | ||
{ | ||
"name": "hiroki" | ||
} | ||
] | ||
} | ||
} | ||
` | ||
|
||
const TestAgentLoadDatabaseOutput1 = `{ | ||
"users": { | ||
"user": [ | ||
{ | ||
"age": 22, | ||
"name": "hiroki" | ||
}, | ||
{ | ||
"age": 30, | ||
"name": "slank" | ||
} | ||
] | ||
} | ||
} | ||
` | ||
|
||
const TestAgentLoadDatabaseOutput2 = `{ | ||
"users": { | ||
"user": [ | ||
{ | ||
"age": 22, | ||
"name": "hiroki" | ||
}, | ||
{ | ||
"age": 30, | ||
"name": "slank" | ||
}, | ||
{ | ||
"age": 28, | ||
"name": "shirokura", | ||
"projects": [ | ||
{ | ||
"name": "mfplane" | ||
} | ||
] | ||
} | ||
] | ||
} | ||
} | ||
` |
Oops, something went wrong.