Skip to content

Commit

Permalink
Project import generated by Copybara.
Browse files Browse the repository at this point in the history
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
Show file tree
Hide file tree
Showing 35 changed files with 50,737 additions and 47,150 deletions.
38 changes: 38 additions & 0 deletions binding/abstract.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,12 +214,50 @@ var _ CLIClient = &AbstractCLIClient{}
type AbstractCLIClient struct{}

// SendCommand returns an unimplemented error.
// Deprecated: Use RunCommand() instead.
// TODO(team): Remove when all clients using RunCommand.
func (*AbstractCLIClient) SendCommand(ctx context.Context, cmd string) (string, error) {
return "", errors.New("SendCommand unimplemented")
}

// SendCommandUsingRun implements SendCommand using the client's RunCommand.
func SendCommandUsingRun(ctx context.Context, cmd string, c CLIClient) (string, error) {
res, err := c.RunCommand(ctx, cmd)
if err != nil {
return "", err
}
if res.Error() != "" {
return "", errors.New(res.Error())
}
return res.Output(), nil
}

// RunCommand returns an unimplemented error.
func (*AbstractCLIClient) RunCommand(ctx context.Context, cmd string) (CommandResult, error) {
return nil, errors.New("RunCommand unimplemented")
}

func (*AbstractCLIClient) mustEmbedAbstractCLIClient() {}

var _ CommandResult = &AbstractCommandResult{}

// AbstractCommandResult is implementation support for the CommandResult interface.
type AbstractCommandResult struct{}

// Output logs a fatal unimplemented error.
func (*AbstractCommandResult) Output() string {
log.Fatal("Output unimplemented")
return ""
}

// Error logs a fatal unimplemented error.
func (*AbstractCommandResult) Error() string {
log.Fatal("Error unimplemented")
return ""
}

func (*AbstractCommandResult) mustEmbedAbstractCommandResult() {}

var _ ConsoleClient = &AbstractConsoleClient{}

// AbstractConsoleClient is implementation support for the ConsoleClient interface.
Expand Down
14 changes: 14 additions & 0 deletions binding/binding.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,10 +222,24 @@ type GNSIClients interface {
// CLIClient provides the interface for sending CLI commands to the DUT.
// All implementations of this interface must embed AbstractCLIClient.
type CLIClient interface {
// TODO(team): Remove when all clients using RunCommand.
SendCommand(context.Context, string) (string, error)
RunCommand(context.Context, string) (CommandResult, error)
mustEmbedAbstractCLIClient()
}

// CommandResult provides the interface for the result of a CLI command.
// All implementations of this interface must embed AbstractCommandResult.
type CommandResult interface {
// Output returns the output of the command.
// The return value may be non-empty even when the command fails.
Output() string
// Error returns an error message that occurred when running the command.
// The return value is the empty string if and only if the command succeeds.
Error() string
mustEmbedAbstractCommandResult()
}

// ConsoleClient provides the interface for console access to the DUT.
// All implementations of this interface must embed AbstractConsoleClient.
type ConsoleClient interface {
Expand Down
75 changes: 75 additions & 0 deletions cli/cli.go
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)
}
104 changes: 104 additions & 0 deletions cli/cli_test.go
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
}
6 changes: 6 additions & 0 deletions dut.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package ondatra

import (
"github.com/openconfig/ondatra/binding"
"github.com/openconfig/ondatra/cli"
"github.com/openconfig/ondatra/config"
"github.com/openconfig/ondatra/console"
"github.com/openconfig/ondatra/raw"
Expand Down Expand Up @@ -43,6 +44,11 @@ func (c *Config) New() *config.VendorConfig {
return config.NewVendorConfig(c.dut)
}

// CLI returns a handle to the DUT CLI API.
func (d *DUTDevice) CLI() *cli.CLI {
return cli.New(d.res.(binding.DUT))
}

// Console returns a handle to the DUT Console API.
func (d *DUTDevice) Console() *console.Console {
return console.New(d.res.(binding.DUT))
Expand Down
1 change: 1 addition & 0 deletions gnmi/generate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ YANG_FILES=(
public/release/models/bfd/openconfig-bfd.yang
public/release/models/bgp/openconfig-bgp-policy.yang
public/release/models/bgp/openconfig-bgp-types.yang
public/release/models/extensions/openconfig-metadata.yang
public/release/models/interfaces/openconfig-if-aggregate.yang
public/release/models/interfaces/openconfig-if-ethernet.yang
public/release/models/interfaces/openconfig-if-ethernet-ext.yang
Expand Down
1 change: 1 addition & 0 deletions gnmi/oc/acl/acl-0.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ using the following YANG input files:
- public/release/models/bfd/openconfig-bfd.yang
- public/release/models/bgp/openconfig-bgp-policy.yang
- public/release/models/bgp/openconfig-bgp-types.yang
- public/release/models/extensions/openconfig-metadata.yang
- public/release/models/interfaces/openconfig-if-aggregate.yang
- public/release/models/interfaces/openconfig-if-ethernet.yang
- public/release/models/interfaces/openconfig-if-ethernet-ext.yang
Expand Down
1 change: 1 addition & 0 deletions gnmi/oc/ateflow/ateflow-0.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ using the following YANG input files:
- public/release/models/bfd/openconfig-bfd.yang
- public/release/models/bgp/openconfig-bgp-policy.yang
- public/release/models/bgp/openconfig-bgp-types.yang
- public/release/models/extensions/openconfig-metadata.yang
- public/release/models/interfaces/openconfig-if-aggregate.yang
- public/release/models/interfaces/openconfig-if-ethernet.yang
- public/release/models/interfaces/openconfig-if-ethernet-ext.yang
Expand Down
1 change: 1 addition & 0 deletions gnmi/oc/definedsets/definedsets-0.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ using the following YANG input files:
- public/release/models/bfd/openconfig-bfd.yang
- public/release/models/bgp/openconfig-bgp-policy.yang
- public/release/models/bgp/openconfig-bgp-types.yang
- public/release/models/extensions/openconfig-metadata.yang
- public/release/models/interfaces/openconfig-if-aggregate.yang
- public/release/models/interfaces/openconfig-if-ethernet.yang
- public/release/models/interfaces/openconfig-if-ethernet-ext.yang
Expand Down
1 change: 1 addition & 0 deletions gnmi/oc/enum.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ using the following YANG input files:
- public/release/models/bfd/openconfig-bfd.yang
- public/release/models/bgp/openconfig-bgp-policy.yang
- public/release/models/bgp/openconfig-bgp-types.yang
- public/release/models/extensions/openconfig-metadata.yang
- public/release/models/interfaces/openconfig-if-aggregate.yang
- public/release/models/interfaces/openconfig-if-ethernet.yang
- public/release/models/interfaces/openconfig-if-ethernet-ext.yang
Expand Down
25 changes: 25 additions & 0 deletions gnmi/oc/enum_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ using the following YANG input files:
- public/release/models/bfd/openconfig-bfd.yang
- public/release/models/bgp/openconfig-bgp-policy.yang
- public/release/models/bgp/openconfig-bgp-types.yang
- public/release/models/extensions/openconfig-metadata.yang
- public/release/models/interfaces/openconfig-if-aggregate.yang
- public/release/models/interfaces/openconfig-if-ethernet.yang
- public/release/models/interfaces/openconfig-if-ethernet-ext.yang
Expand Down Expand Up @@ -3412,6 +3413,18 @@ func initΛEnumTypes() {
"/network-instances/network-instance/policy-forwarding/policies/policy/state/type": {
reflect.TypeOf((E_Policy_Type)(0)),
},
"/network-instances/network-instance/protocols/protocol/bgp/global/afi-safis/afi-safi/apply-policy/config/default-export-policy": {
reflect.TypeOf((E_RoutingPolicy_DefaultPolicyType)(0)),
},
"/network-instances/network-instance/protocols/protocol/bgp/global/afi-safis/afi-safi/apply-policy/config/default-import-policy": {
reflect.TypeOf((E_RoutingPolicy_DefaultPolicyType)(0)),
},
"/network-instances/network-instance/protocols/protocol/bgp/global/afi-safis/afi-safi/apply-policy/state/default-export-policy": {
reflect.TypeOf((E_RoutingPolicy_DefaultPolicyType)(0)),
},
"/network-instances/network-instance/protocols/protocol/bgp/global/afi-safis/afi-safi/apply-policy/state/default-import-policy": {
reflect.TypeOf((E_RoutingPolicy_DefaultPolicyType)(0)),
},
"/network-instances/network-instance/protocols/protocol/bgp/global/afi-safis/afi-safi/config/afi-safi-name": {
reflect.TypeOf((E_BgpTypes_AFI_SAFI_TYPE)(0)),
},
Expand All @@ -3424,6 +3437,18 @@ func initΛEnumTypes() {
"/network-instances/network-instance/protocols/protocol/bgp/global/afi-safis/afi-safi/state/send-community-type": {
reflect.TypeOf((E_Bgp_CommunityType)(0)),
},
"/network-instances/network-instance/protocols/protocol/bgp/global/apply-policy/config/default-export-policy": {
reflect.TypeOf((E_RoutingPolicy_DefaultPolicyType)(0)),
},
"/network-instances/network-instance/protocols/protocol/bgp/global/apply-policy/config/default-import-policy": {
reflect.TypeOf((E_RoutingPolicy_DefaultPolicyType)(0)),
},
"/network-instances/network-instance/protocols/protocol/bgp/global/apply-policy/state/default-export-policy": {
reflect.TypeOf((E_RoutingPolicy_DefaultPolicyType)(0)),
},
"/network-instances/network-instance/protocols/protocol/bgp/global/apply-policy/state/default-import-policy": {
reflect.TypeOf((E_RoutingPolicy_DefaultPolicyType)(0)),
},
"/network-instances/network-instance/protocols/protocol/bgp/neighbors/neighbor/afi-safis/afi-safi/apply-policy/config/default-export-policy": {
reflect.TypeOf((E_RoutingPolicy_DefaultPolicyType)(0)),
},
Expand Down
1 change: 1 addition & 0 deletions gnmi/oc/gnmicollectormetadata/gnmicollectormetadata-0.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ using the following YANG input files:
- public/release/models/bfd/openconfig-bfd.yang
- public/release/models/bgp/openconfig-bgp-policy.yang
- public/release/models/bgp/openconfig-bgp-types.yang
- public/release/models/extensions/openconfig-metadata.yang
- public/release/models/interfaces/openconfig-if-aggregate.yang
- public/release/models/interfaces/openconfig-if-ethernet.yang
- public/release/models/interfaces/openconfig-if-ethernet-ext.yang
Expand Down
Loading

0 comments on commit 9e989f2

Please sign in to comment.