-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Project import generated by Copybara.
FolderOrigin-RevId: /usr/local/google/home/gdennis/copybara/temp/folder-destination16942705691238465215/.
- Loading branch information
GGN Engprod Team
authored and
greg-dennis
committed
Nov 9, 2023
1 parent
eddb185
commit 9e989f2
Showing
35 changed files
with
50,737 additions
and
47,150 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
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,75 @@ | ||
// Copyright 2023 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
// Package cli provides an API to send CLI commands to a DUT. | ||
package cli | ||
|
||
import ( | ||
"testing" | ||
|
||
"golang.org/x/net/context" | ||
|
||
"github.com/openconfig/ondatra/binding" | ||
"github.com/openconfig/ondatra/internal/events" | ||
) | ||
|
||
// New constructs a new instance of the CLI API. | ||
// Tests must not call this directly | ||
func New(dut binding.DUT) *CLI { | ||
return &CLI{dut: dut} | ||
} | ||
|
||
// CLI is the device CLI API. | ||
type CLI struct { | ||
dut binding.DUT | ||
} | ||
|
||
// Run runs the specified CLI command on the DUT and returns its output. | ||
// Run fails fatally if either (a) the command runs and reports an error, or | ||
// (b) an error occurs that prevents the command from being run at all. | ||
// To capture the error from case (a) instead, use [CLI.RunResult]. | ||
func (c *CLI) Run(t testing.TB, cmd string) string { | ||
t.Helper() | ||
t = events.ActionStarted(t, "Running CLI command on %s", c.dut) | ||
res, err := c.run(context.Background(), cmd) | ||
if err != nil { | ||
t.Fatalf("Run(t, %q) on %s: %v", cmd, c.dut, err) | ||
} | ||
if res.Error() != "" { | ||
t.Fatalf("Run(t, %q) on %s: %v", cmd, c.dut, res.Error()) | ||
} | ||
return res.Output() | ||
} | ||
|
||
// RunResult runs the specified CLI command on the DUT and returns its result. | ||
// RunResult fails fatally if an error occurs that prevents the command from | ||
// being run. If the command runs and reports an error, that error is available | ||
// in the result. To fail fatally in the latter case instead, use [CLI.Run]. | ||
func (c *CLI) RunResult(t testing.TB, cmd string) binding.CommandResult { | ||
t.Helper() | ||
t = events.ActionStarted(t, "Running CLI command on %s", c.dut) | ||
res, err := c.run(context.Background(), cmd) | ||
if err != nil { | ||
t.Fatalf("RunResult(t, %q) on %s: %v", cmd, c.dut, err) | ||
} | ||
return res | ||
} | ||
|
||
func (c *CLI) run(ctx context.Context, cmd string) (binding.CommandResult, error) { | ||
cli, err := c.dut.DialCLI(ctx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return cli.RunCommand(ctx, 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,104 @@ | ||
// Copyright 2023 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package cli | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
|
||
"golang.org/x/net/context" | ||
|
||
"github.com/openconfig/ondatra/binding" | ||
"github.com/openconfig/ondatra/fakebind" | ||
"github.com/openconfig/testt" | ||
) | ||
|
||
var ( | ||
fakeClient = &fakeCLIClient{} | ||
dut = &fakebind.DUT{ | ||
AbstractDUT: &binding.AbstractDUT{&binding.Dims{Name: "fakeDUT"}}, | ||
DialCLIFn: func(context.Context) (binding.CLIClient, error) { | ||
return fakeClient, nil | ||
}, | ||
} | ||
) | ||
|
||
func TestRun(t *testing.T) { | ||
cli := New(dut) | ||
|
||
t.Run("pass", func(t *testing.T) { | ||
want := "fake output" | ||
fakeClient.result = &fakeCommandResult{output: want} | ||
got := cli.Run(t, "cmd") | ||
if got != want { | ||
t.Errorf("Run() got %q, want %q", got, want) | ||
} | ||
}) | ||
|
||
t.Run("fail", func(t *testing.T) { | ||
wantErr := "fake error" | ||
fakeClient.result = &fakeCommandResult{output: "output", error: wantErr} | ||
gotErr := testt.ExpectFatal(t, func(t testing.TB) { | ||
cli.Run(t, "cmd") | ||
}) | ||
if !strings.Contains(gotErr, wantErr) { | ||
t.Errorf("Run() got error %q, want %q", gotErr, wantErr) | ||
} | ||
}) | ||
} | ||
|
||
func TestRunResult(t *testing.T) { | ||
cli := New(dut) | ||
|
||
t.Run("pass", func(t *testing.T) { | ||
want := &fakeCommandResult{output: "fake output"} | ||
fakeClient.result = want | ||
got := cli.RunResult(t, "cmd") | ||
if got != want { | ||
t.Errorf("RunResult() got %q, want %q", got, want) | ||
} | ||
}) | ||
|
||
t.Run("error", func(t *testing.T) { | ||
want := &fakeCommandResult{output: "fake output", error: "fake error"} | ||
fakeClient.result = want | ||
got := cli.RunResult(t, "cmd") | ||
if got != want { | ||
t.Errorf("RunResult() got %q, want %q", got, want) | ||
} | ||
}) | ||
} | ||
|
||
type fakeCLIClient struct { | ||
binding.CLIClient | ||
result binding.CommandResult | ||
} | ||
|
||
func (c *fakeCLIClient) RunCommand(ctx context.Context, cmd string) (binding.CommandResult, error) { | ||
return c.result, nil | ||
} | ||
|
||
type fakeCommandResult struct { | ||
*binding.AbstractCommandResult | ||
output, error string | ||
} | ||
|
||
func (r *fakeCommandResult) Output() string { | ||
return r.output | ||
} | ||
|
||
func (r *fakeCommandResult) Error() string { | ||
return r.error | ||
} |
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
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
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
Oops, something went wrong.