Skip to content

Commit

Permalink
readd reset now with the right api contract (#451)
Browse files Browse the repository at this point in the history
this will make the device restore to the kne starting
point (startup-config) not try to zeroize the config
  • Loading branch information
marcushines authored Oct 27, 2023
1 parent 7c8567b commit a7ea214
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 1 deletion.
27 changes: 27 additions & 0 deletions topo/node/arista/arista.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"path/filepath"
"regexp"
"strings"
"time"

cpb "github.com/openconfig/kne/proto/ceos"
tpb "github.com/openconfig/kne/proto/topo"
Expand Down Expand Up @@ -301,6 +302,32 @@ func (n *Node) ConfigPush(ctx context.Context, r io.Reader) error {
return resp.Failed
}

func (n *Node) ResetCfg(ctx context.Context) error {
log.Infof("%s resetting config", n.Name())

err := n.SpawnCLIConn()
if err != nil {
return err
}

defer n.cliConn.Close()

// this takes a long time sometimes, so we crank timeouts up
resp, err := n.cliConn.SendCommand(
"configure replace startup-config",
scrapliopts.WithTimeoutOps(300*time.Second),
)
if err != nil {
return err
}

if resp.Failed == nil {
log.Infof("%s - finshed resetting config", n.Name())
}

return resp.Failed
}

func defaults(pb *tpb.Node) *tpb.Node {
if pb == nil {
pb = &tpb.Node{
Expand Down
86 changes: 86 additions & 0 deletions topo/node/arista/arista_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"context"
"fmt"
"testing"
"time"

ceos "github.com/aristanetworks/arista-ceoslab-operator/v2/api/v1alpha1"
ceosclient "github.com/aristanetworks/arista-ceoslab-operator/v2/api/v1alpha1/dynamic"
Expand All @@ -26,6 +27,9 @@ import (
ceospb "github.com/openconfig/kne/proto/ceos"
topopb "github.com/openconfig/kne/proto/topo"
"github.com/openconfig/kne/topo/node"
scrapliopts "github.com/scrapli/scrapligo/driver/options"
scraplitransport "github.com/scrapli/scrapligo/transport"
scrapliutil "github.com/scrapli/scrapligo/util"
"google.golang.org/protobuf/encoding/prototext"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/anypb"
Expand Down Expand Up @@ -434,3 +438,85 @@ func TestCRD(t *testing.T) {
})
}
}

func TestResetCfg(t *testing.T) {
ki := fake.NewSimpleClientset(&corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "pod1",
},
})

reaction := func(action ktest.Action) (handled bool, ret watch.Interface, err error) {
f := &fakeWatch{
e: []watch.Event{{
Object: &corev1.Pod{
Status: corev1.PodStatus{
Phase: corev1.PodRunning,
},
},
}},
}
return true, f, nil
}
ki.PrependWatchReactor("*", reaction)

ni := &node.Impl{
KubeClient: ki,
Namespace: "test",
Proto: &topopb.Node{
Name: "pod1",
Vendor: topopb.Vendor_ARISTA,
Config: &topopb.Config{},
},
}

tests := []struct {
desc string
wantErr bool
ni *node.Impl
testFile string
}{
{
// successfully configure certificate
desc: "success",
wantErr: false,
ni: ni,
testFile: "testdata/reset_config_success",
},
{
// device returns "% Invalid input" -- we expect to fail
desc: "failure",
wantErr: true,
ni: ni,
testFile: "testdata/reset_config_failure",
},
}

for _, tt := range tests {
t.Run(tt.desc, func(t *testing.T) {
nImpl, err := New(tt.ni)

if err != nil {
t.Fatalf("failed creating kne arista node")
}

n, _ := nImpl.(*Node)

n.testOpts = []scrapliutil.Option{
scrapliopts.WithTransportType(scraplitransport.FileTransport),
scrapliopts.WithFileTransportFile(tt.testFile),
scrapliopts.WithTimeoutOps(2 * time.Second),
scrapliopts.WithTransportReadSize(1),
scrapliopts.WithReadDelay(0),
scrapliopts.WithDefaultLogger(),
}

ctx := context.Background()

err = n.ResetCfg(ctx)
if err != nil && !tt.wantErr {
t.Fatalf("resetting config failed, error: %+v\n", err)
}
})
}
}
2 changes: 1 addition & 1 deletion topo/node/arista/testdata/reset_config_success
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ spine1#
spine1#terminal length 0
Pagination disabled.
spine1#
spine1#configure replace clean-config
spine1#configure replace startup-config
Aug 21 18:37:16 localhost Launcher: %LAUNCHER-6-PROCESS_STOP: Configuring process 'CapiApp' to stop in role 'ActiveSupervisor'
Aug 21 18:37:16 localhost Stp: %SPANTREE-6-STABLE_CHANGE: Stp state is now not stable
Aug 21 18:37:16 localhost Stp: %SPANTREE-6-INTERFACE_ADD: Interface Ethernet11 has been added to instance MST0
Expand Down

0 comments on commit a7ea214

Please sign in to comment.