Skip to content

Commit 7c90544

Browse files
committed
added comments/test for new code and added go.mod
1 parent 0b05972 commit 7c90544

3 files changed

Lines changed: 73 additions & 3 deletions

File tree

cmd/internal/runtest.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ type (
5757
runOutputFunc func(ctx context.Context, pingID perfops.TestID) (*perfops.RunOutput, error)
5858
)
5959

60-
// RunTest runs an MTR or ping testm retrives its output and presents it to the user.
60+
// RunTest runs an MTR or ping test retrieves its output and presents it to the user.
6161
func RunTest(ctx context.Context, target, location string, nodeIDs []int, limit int, debug, outputJSON bool, runTest runFunc, runOutput runOutputFunc) error {
6262
runReq := &perfops.RunRequest{
6363
Target: target,
@@ -112,6 +112,7 @@ func RunTest(ctx context.Context, target, location string, nodeIDs []int, limit
112112
return nil
113113
}
114114

115+
// formatFileName Returns a file name based on provided string and number
115116
func formatFileName(name string, index int) string {
116117
split := strings.Split(name, ".")
117118

@@ -122,7 +123,7 @@ func formatFileName(name string, index int) string {
122123

123124
}
124125

125-
// Outputs perfops response to a file
126+
// OutputToFile Outputs perfops response to a file or multiple files if more than one check is being ran.
126127
func OutputToFile(f *Formatter, output *perfops.RunOutput, fileOut string) {
127128
if f.printID {
128129
f.Printf("Test ID: %v\n", output.ID)
@@ -172,7 +173,11 @@ func OutputToFile(f *Formatter, output *perfops.RunOutput, fileOut string) {
172173

173174
fmt.Fprintf(w, "Node%d, AS%d, %s, %s\n%s\n", n.ID, n.AsNumber, n.City, n.Country.Name, o)
174175

175-
w.Flush()
176+
err := w.Flush()
177+
178+
if err != nil {
179+
return
180+
}
176181

177182
} else if r.Message != "NO DATA" {
178183
fileName := ""

cmd/internal/runtest_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ import (
1818
"context"
1919
"encoding/json"
2020
"errors"
21+
"fmt"
2122
"io"
23+
"os"
2224
"testing"
2325

2426
"github.com/ProspectOne/perfops-cli/perfops"
@@ -60,6 +62,8 @@ func TestRunTest(t *testing.T) {
6062
nil,
6163
},
6264
}
65+
66+
fmt.Println("tests", testCases)
6367
ctx := context.Background()
6468
for name, tc := range testCases {
6569
t.Run(name, func(t *testing.T) {
@@ -116,6 +120,52 @@ func TestPrintOutput(t *testing.T) {
116120
}
117121
}
118122

123+
func TestOutputToFile(t *testing.T) {
124+
testCases := map[string]struct {
125+
output func() *perfops.RunOutput
126+
exp []byte
127+
}{
128+
"all": {
129+
func() *perfops.RunOutput {
130+
var o *perfops.RunOutput
131+
json.Unmarshal([]byte(`{"id":"706fc55e3377104da01f05569e35a30b","items":[{"id":"bba072473bd034d432df03730e116686","result":{"output":"121","finished": true,"node":{"id":27,"as_number":12345,"latitude":22.28512548314,"longitude":114.17507171631,"country":{"id":195,"name":"Hong Kong","continent":{"id":2,"name":"Asia","iso":"AS"},"iso":"HK","iso_numeric":"344"},"city":"Hong Kong","sub_region":"Eastern Asia"},"time":1508061924.088372}}],"requested":"sendergram.com","finished":true,"elapsedTime":0.66500000000000004}`), &o)
132+
return o
133+
},
134+
[]byte("\x1b[200DNode27, AS12345, Hong Kong, Hong Kong\n121\n"),
135+
},
136+
"timeout": {
137+
func() *perfops.RunOutput {
138+
var o *perfops.RunOutput
139+
json.Unmarshal([]byte(`{"id":"706fc55e3377104da01f05569e35a30b","items":[{"id":"bba072473bd034d432df03730e116686","result":{"output":"-2","finished": true,"node":{"id":27,"as_number":23456,"latitude":22.28512548314,"longitude":114.17507171631,"country":{"id":195,"name":"Hong Kong","continent":{"id":2,"name":"Asia","iso":"AS"},"iso":"HK","iso_numeric":"344"},"city":"Hong Kong","sub_region":"Eastern Asia"},"time":1508061924.088372}}],"requested":"sendergram.com","finished":true,"elapsedTime":0.66500000000000004}`), &o)
140+
return o
141+
},
142+
[]byte("\x1b[200DNode27, AS23456, Hong Kong, Hong Kong\nThe command timed-out. It either took too long to execute or we could not connect to your target at all.\n"),
143+
},
144+
"array output": {
145+
func() *perfops.RunOutput {
146+
var o *perfops.RunOutput
147+
json.Unmarshal([]byte(`{"id":"6e0c06f7445bb8c63949f84fcdbdae55","items":[{"id":"2bff5d6b3a4df8a268afca6c60977032","result":{"dnsServer":"","node":{"as_number":197328,"id":103,"latitude":41.030549854339,"longitude":28.987083435058,"country":{"id":93,"name":"Turkey","continent":{"id":2,"name":"Asia","iso":"AS"},"iso":"TR","iso_numeric":"792","is_eu":false},"city":"Istanbul","sub_region":"Western Asia"},"finished":true,"output":["header"," 1 row", " 10 row"],"time":1539517079.937741}}],"requested":"ns2.no-ip.com","finished":true,"elapsedTime":2.21,"creditsWithdrawn":1}`), &o)
148+
return o
149+
},
150+
[]byte("\x1b[200DNode103, AS197328, Istanbul, Turkey\nheader\n 1 row\n 10 row\n"),
151+
},
152+
}
153+
var b bytes.Buffer
154+
f := newTestFormatter(&b, false)
155+
for name, tc := range testCases {
156+
t.Run(name, func(t *testing.T) {
157+
OutputToFile(f, tc.output(), "test.txt")
158+
if _, err := os.Stat("test.txt"); err != nil {
159+
t.Error("File was not created")
160+
}
161+
err := os.Remove("test.txt")
162+
if err != nil {
163+
return
164+
}
165+
})
166+
}
167+
}
168+
119169
type testTerminalWriter struct {
120170
io.Writer
121171
}

go.mod

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
module github.com/ProspectOne/perfops-cli
2+
3+
go 1.19
4+
5+
require (
6+
github.com/gosuri/uilive v0.0.3
7+
github.com/spf13/cobra v0.0.5
8+
github.com/spf13/pflag v1.0.5
9+
)
10+
11+
require (
12+
github.com/inconshreveable/mousetrap v1.0.0 // indirect
13+
github.com/mattn/go-isatty v0.0.9 // indirect
14+
golang.org/x/sys v0.0.0-20191002091554-b397fe3ad8ed // indirect
15+
)

0 commit comments

Comments
 (0)