Skip to content

Commit

Permalink
Merge pull request #2 from slankdev/slankdev
Browse files Browse the repository at this point in the history
Refactor
  • Loading branch information
slankdev authored Mar 17, 2024
2 parents 88d4e27 + c84dfba commit a8f1d6b
Show file tree
Hide file tree
Showing 31 changed files with 2,958 additions and 1,337 deletions.
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ build:
test:
go test ./...
run: build
./vtyang agent --dbpath ./tmp/config.json --run-path /usr/local/var/run/vtyang
./vtyang agent --run-path /usr/local/var/run/vtyang
godoc:
godoc -http=:6060
kill:
killall vtyang
log:
Expand Down
6 changes: 6 additions & 0 deletions README.md
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
```
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ require (
github.com/k0kubun/colorstring v0.0.0-20150214042306-9440f1994b88 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,8 @@ github.com/pborman/getopt v0.0.0-20190409184431-ee0cd42419d3/go.mod h1:85jBQOZwp
github.com/pelletier/go-toml v1.9.4/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/sftp v1.10.1/go.mod h1:lYOWFsE0bwd1+KfKJaKeuokY15vzFx25BLbzYYoAxZI=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI=
Expand Down
37 changes: 37 additions & 0 deletions pkg/util/cobra.go
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
}
8 changes: 8 additions & 0 deletions pkg/util/file.go
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
}
79 changes: 0 additions & 79 deletions pkg/vtyang/agent.go

This file was deleted.

198 changes: 198 additions & 0 deletions pkg/vtyang/agent_test.go
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"
}
]
}
]
}
}
`
Loading

0 comments on commit a8f1d6b

Please sign in to comment.