Skip to content

Commit

Permalink
Merge branch 'main' into traceroute_packetin_with_vrf_selection_test
Browse files Browse the repository at this point in the history
  • Loading branch information
rahulsy26 authored Jul 28, 2024
2 parents 186102b + ae4d456 commit 982d970
Show file tree
Hide file tree
Showing 8 changed files with 236 additions and 156 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

"github.com/open-traffic-generator/snappi/gosnappi"
"github.com/openconfig/featureprofiles/internal/attrs"
"github.com/openconfig/featureprofiles/internal/cfgplugins"
"github.com/openconfig/featureprofiles/internal/deviations"
"github.com/openconfig/featureprofiles/internal/fptest"
"github.com/openconfig/featureprofiles/internal/helpers"
Expand Down Expand Up @@ -181,6 +182,9 @@ func TestBGPLinkBandwidth(t *testing.T) {
}
baseSetupConfigAndVerification(t, td)
configureExtCommunityRoutingPolicy(t, dut)
if deviations.BgpExplicitExtendedCommunityEnable(dut) {
enableExtCommunityCLIConfig(t, dut)
}
testCases := []testCase{
{
name: "Policy set not_match_100_set_linkbw_1M",
Expand Down Expand Up @@ -224,6 +228,17 @@ func TestBGPLinkBandwidth(t *testing.T) {
}
}

func enableExtCommunityCLIConfig(t *testing.T, dut *ondatra.DUTDevice) {
var extCommunityEnableCLIConfig string
switch dut.Vendor() {
case ondatra.CISCO:
extCommunityEnableCLIConfig = fmt.Sprintf("router bgp %v instance BGP neighbor-group %v \n ebgp-recv-extcommunity-dmz \n ebgp-send-extcommunity-dmz\n", dutAS, cfgplugins.BGPPeerGroup1)
default:
t.Fatalf("Unsupported vendor %s for deviation 'BgpExplicitExtendedCommunityEnable'", dut.Vendor())
}
helpers.GnmiCLIConfig(t, dut, extCommunityEnableCLIConfig)
}

func applyPolicyDut(t *testing.T, dut *ondatra.DUTDevice, policyName string) {
// Apply ipv4 policy to bgp neighbour.
root := &oc.Root{}
Expand All @@ -241,6 +256,17 @@ func applyPolicyDut(t *testing.T, dut *ondatra.DUTDevice, policyName string) {
policy = root.GetOrCreateNetworkInstance(dni).GetOrCreateProtocol(oc.PolicyTypes_INSTALL_PROTOCOL_TYPE_BGP, bgpName).GetOrCreateBgp().GetOrCreateNeighbor(atePort1.IPv6).GetOrCreateAfiSafi(oc.BgpTypes_AFI_SAFI_TYPE_IPV6_UNICAST).GetOrCreateApplyPolicy()
policy.SetImportPolicy([]string{policyName})
gnmi.Replace(t, dut, path.Config(), policy)

ni := root.GetOrCreateNetworkInstance(deviations.DefaultNetworkInstance(dut))
niProto := ni.GetOrCreateProtocol(oc.PolicyTypes_INSTALL_PROTOCOL_TYPE_BGP, "BGP")
bgp := niProto.GetOrCreateBgp()
bgp.GetOrCreatePeerGroup(cfgplugins.BGPPeerGroup1).GetOrCreateAfiSafi(oc.BgpTypes_AFI_SAFI_TYPE_IPV4_UNICAST).Enabled = ygot.Bool(true)
bgp.GetOrCreatePeerGroup(cfgplugins.BGPPeerGroup1).GetOrCreateAfiSafi(oc.BgpTypes_AFI_SAFI_TYPE_IPV6_UNICAST).Enabled = ygot.Bool(true)
bgpNbrV4 := bgp.GetOrCreateNeighbor(atePort1.IPv4)
bgpNbrV4.PeerGroup = ygot.String(cfgplugins.BGPPeerGroup1)
bgpNbrV6 := bgp.GetOrCreateNeighbor(atePort1.IPv6)
bgpNbrV6.PeerGroup = ygot.String(cfgplugins.BGPPeerGroup1)
gnmi.Update(t, dut, gnmi.OC().NetworkInstance(deviations.DefaultNetworkInstance(dut)).Protocol(oc.PolicyTypes_INSTALL_PROTOCOL_TYPE_BGP, "BGP").Config(), niProto)
}

func validatPolicyDut(t *testing.T, dut *ondatra.DUTDevice, policyName string) {
Expand Down Expand Up @@ -292,6 +318,10 @@ func validateRouteCommunityV4Prefix(t *testing.T, td testData, community, v4Pref
}
}
default:
if len(bgpPrefix.ExtendedCommunity) == 0 {
t.Errorf("ERROR extended community is empty, expected %v", community)
return
}
for _, ec := range bgpPrefix.ExtendedCommunity {
lbSubType := ec.Structured.NonTransitive_2OctetAsType.LinkBandwidthSubtype
listCommunity := strings.Split(community, ":")
Expand Down Expand Up @@ -359,6 +389,10 @@ func validateRouteCommunityV6Prefix(t *testing.T, td testData, community, v6Pref
}
}
default:
if len(bgpPrefix.ExtendedCommunity) == 0 {
t.Errorf("ERROR extended community is empty, expected %v", community)
return
}
for _, ec := range bgpPrefix.ExtendedCommunity {
lbSubType := ec.Structured.NonTransitive_2OctetAsType.LinkBandwidthSubtype
listCommunity := strings.Split(community, ":")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ platform_exceptions: {
skip_bgp_send_community_type: true
bgp_extended_community_index_unsupported: true
bgp_conditions_match_community_set_unsupported: true
bgp_explicit_extended_community_enable: true
}
}
tags: TAGS_AGGREGATION
Expand Down
9 changes: 5 additions & 4 deletions feature/gnoi/factory_reset/tests/factory_reset_test/README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
# gNOI-6.1: Factory Reset

## Summary
Performs Factory Reset with and without disk-encryption
Performs Factory Reset

## Procedure
* Create dummy files in the harddisk of the router using bash dd
* Checks for disk-encryption status and performs reset on both the scenarios
* Create a sample file in the harddisk of the router using gNOI PUT RPC
* Secure ZTP server should be up and running in the background for the router to boot up with the base config once factory reset command is sent on the box.
* Send out Factory reset via GNOI Raw API
* Wait for the box to boot up via Secure ZTP
* The base config is updated on the box via Secure ZTP
* Connect to the router and check if the files in the harddisk are removed as a part of verifying Factory reset.
* Send a gNOI file STAT RPC to check if the file in the harddisk are removed as a part of verifying Factory reset.

## OpenConfig Path and RPC Coverage

Expand All @@ -21,4 +20,6 @@ paths used for test setup are not listed here.
rpcs:
gnoi:
factory_reset.FactoryReset.Start:
file.File.Put:
file.File.Stat:
```
234 changes: 127 additions & 107 deletions feature/gnoi/factory_reset/tests/factory_reset_test/factory_reset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,80 +2,39 @@ package factoryreset

import (
"context"
"fmt"
"path"
"strings"
"crypto/md5"
"crypto/rand"
"io"
"path/filepath"
"regexp"
"testing"
"time"

"github.com/openconfig/featureprofiles/internal/fptest"
frpb "github.com/openconfig/gnoi/factory_reset"
fpb "github.com/openconfig/gnoi/file"
"github.com/openconfig/gnoi/types"
"github.com/openconfig/gnoigo"
"github.com/openconfig/ondatra"
"github.com/openconfig/ondatra/gnmi"
"github.com/openconfig/testt"
)

var (
filesCreated = []string{}
fileCreateDevRand = "bash dd if=/dev/urandom of=%s bs=1M count=2"
checkFileExists = "bash [ -f \"%s\" ] && echo \"YES_exists\""
fileExists = "YES_exists"
fileCreate = "bash fallocate -l %dM %s"
remoteFilePath = map[ondatra.Vendor]string{
ondatra.CISCO: "/misc/disk1/",
ondatra.NOKIA: "/tmp/",
ondatra.JUNIPER: "/var/tmp/",
}
afterReset = false
fileName = "devrandom.log"
)

const maxRebootTime = 40 // 40 mins wait time for the factory reset and sztp to kick in
func TestMain(m *testing.M) {
fptest.RunTests(m)
}

type encryptionCommands struct {
EncryptionStatus string
EncryptionActivate string
EncryptionDeactivate string
DevicePaths []string
}

var enCiscoCommands encryptionCommands

// creating files before factory reset
func createFiles(t *testing.T, dut *ondatra.DUTDevice, devicePaths []string) {
for _, folderPath := range devicePaths {
fPath := path.Join(folderPath, "devrandom.log")
dut.CLI().Run(t, fmt.Sprintf(fileCreateDevRand, fPath))
t.Log("Check if the file is created")
time.Sleep(30 * time.Second)
filesCreated = append(filesCreated, fPath)
fPath = path.Join(folderPath, ".devrandom.log")
dut.CLI().Run(t, fmt.Sprintf(fileCreateDevRand, fPath))

filesCreated = append(filesCreated, fPath)
fPath = path.Join(folderPath, "largeFile.log")
dut.CLI().Run(t, fmt.Sprintf(fileCreate, 100, fPath))

filesCreated = append(filesCreated, fPath)
}
for _, f := range filesCreated {
resp := dut.CLI().Run(t, fmt.Sprintf(checkFileExists, f))
t.Logf("%v", resp)
if !strings.Contains(resp, fileExists) {
t.Fatalf("Unable to Create a file object %s in device %s", f, dut.Name())
}
}

}

// checkFiles check if the files created are deleted from the device after factory reset
func checkFiles(t *testing.T, dut *ondatra.DUTDevice) {
for _, f := range filesCreated {
resp := dut.CLI().Run(t, fmt.Sprintf(checkFileExists, f))
t.Logf(resp)
if strings.Contains(resp, fileExists) == true {
t.Fatalf("File %s not cleared by system Reset, in device %s", f, dut.Name())
}

}
}

func deviceBootStatus(t *testing.T, dut *ondatra.DUTDevice) {
startReboot := time.Now()
t.Logf("Wait for DUT to boot up by polling the telemetry output.")
Expand All @@ -100,70 +59,131 @@ func deviceBootStatus(t *testing.T, dut *ondatra.DUTDevice) {
t.Logf("Device boot time: %.2f minutes", time.Since(startReboot).Minutes())
}

// performs factory reset
func factoryReset(t *testing.T, dut *ondatra.DUTDevice, devicePaths []string) {
createFiles(t, dut, devicePaths)
func gNOIPutFile(t *testing.T, dut *ondatra.DUTDevice, gnoiClient gnoigo.Clients, fName string) {
dutVendor := dut.Vendor()
fullPath := filepath.Join(remoteFilePath[dutVendor], fName)
stream, err := gnoiClient.File().Put(context.Background())
t.Logf("Attempting to send gNOI File Put here: %v", fullPath)
if err != nil {
t.Fatalf("Failed to create stream channel: %v", err)
}
defer stream.CloseSend()
h := md5.New()
fPutOpen := &fpb.PutRequest_Open{
Open: &fpb.PutRequest_Details{
RemoteFile: fullPath,
Permissions: 744,
},
}
err = stream.Send(&fpb.PutRequest{
Request: fPutOpen,
})
if err != nil {
t.Fatalf("Stream failed to send PutRequest: %v", err)
}

b := make([]byte, 64*1024)
n, err := rand.Read(b)
if err != nil && err != io.EOF {
t.Fatalf("Error reading bytes: %v", err)
}
h.Write(b[:n])
req := &fpb.PutRequest{
Request: &fpb.PutRequest_Contents{
Contents: b[:n],
},
}
err = stream.Send(req)
if err != nil {
t.Fatalf("Stream failed to send Req: %v", err)
}

hashReq := &fpb.PutRequest{
Request: &fpb.PutRequest_Hash{
Hash: &types.HashType{
Method: types.HashType_MD5,
Hash: h.Sum(nil),
},
},
}
err = stream.Send(hashReq)
if err != nil {
t.Fatalf("Stream failed to send hash: %v", err)
}

_, err = stream.CloseAndRecv()
if err != nil {
t.Fatalf("Problem closing the stream: %v", err)
}
}

func gNOIStatFile(t *testing.T, dut *ondatra.DUTDevice, fName string) {
dutVendor := dut.Vendor()
fullPath := filepath.Join(remoteFilePath[dutVendor], fName)
gnoiClient, err := dut.RawAPIs().BindingDUT().DialGNOI(context.Background())
if err != nil {
t.Fatalf("Error dialing gNOI: %v", err)
}
if _, ok := remoteFilePath[dutVendor]; !ok {
t.Fatalf("Please add support for vendor %v in var remoteFilePath ", dutVendor)
}

in := &fpb.StatRequest{
Path: remoteFilePath[dutVendor],
}
statResp, err := gnoiClient.File().Stat(context.Background(), in)
if err != nil {
t.Fatalf("Error fetching stat path %v for the created file on DUT. %v", remoteFilePath[dutVendor], err)
}

if len(statResp.GetStats()) == 0 {
t.Log("gNOI STAT did not find any files")
}

r := regexp.MustCompile(fName)
var isCreatedFile bool

for _, fileStats := range statResp.GetStats() {
isCreatedFile = r.MatchString(fileStats.GetPath()) && (fileStats.GetSize() == uint64(64*1024))
if isCreatedFile {
break
}
}
if isCreatedFile {
if !afterReset {
t.Logf("gNOI PUT successfully created file: %s", fullPath)
} else {
t.Errorf("gNOI PUT file was found after Factory Reset: %s", fullPath)
}
}
if !isCreatedFile {
if !afterReset {
t.Error("gNOI PUT file was never Created")
} else {
t.Logf("Did not find %s in the list of files", fullPath)
}
}
}

func factoryReset(t *testing.T, dut *ondatra.DUTDevice) {
gnoiClient, err := dut.RawAPIs().BindingDUT().DialGNOI(context.Background())
if err != nil {
t.Fatalf("Error dialing gNOI: %v", err)
}
gNOIPutFile(t, dut, gnoiClient, fileName)
gNOIStatFile(t, dut, fileName)
facRe, err := gnoiClient.FactoryReset().Start(context.Background(), &frpb.StartRequest{FactoryOs: false, ZeroFill: false})
if err != nil {
t.Fatalf("Failed to initiate Factory Reset on the device, Error : %v ", err)
}
t.Logf("Factory reset Response %v ", facRe)
time.Sleep(2 * time.Minute)
deviceBootStatus(t, dut)
dutNew := ondatra.DUT(t, "dut")
checkFiles(t, dutNew)
t.Log("Factory reset successfull")
afterReset = true
gNOIStatFile(t, dut, fileName)
}

func TestFactoryReset(t *testing.T) {
dut := ondatra.DUT(t, "dut")

switch dut.Vendor() {
case ondatra.CISCO:
enCiscoCommands = encryptionCommands{EncryptionStatus: "show disk-encryption status", EncryptionActivate: "disk-encryption activate", EncryptionDeactivate: "disk-encryption deactivate", DevicePaths: []string{"/misc/disk1"}}
t.Logf("Cisco commands for disk encryption %v ", enCiscoCommands)
default:
t.Fatalf("Disk Encryption commands is missing for %v ", dut.Vendor().String())
}

showDiskEncryptionStatus := dut.CLI().Run(t, enCiscoCommands.EncryptionStatus)
t.Logf("Disk encryption status %v", showDiskEncryptionStatus)

if strings.Contains(showDiskEncryptionStatus, "Not Encrypted") {
t.Log("Performing Factory reset without Encryption\n")
factoryReset(t, dut, enCiscoCommands.DevicePaths)
t.Log("Stablise after factory reset\n")
time.Sleep(5 * time.Minute)
t.Log("Activate Encryption\n")
encrypt := dut.CLI().Run(t, enCiscoCommands.EncryptionActivate)
t.Logf("Sleep for 5 mins after disk-encryption activate")
time.Sleep(5 * time.Minute)
t.Logf("Device encryption acrivare: %v", encrypt)
deviceBootStatus(t, dut)
encrypt = dut.CLI().Run(t, enCiscoCommands.EncryptionStatus)
t.Logf("Show device encryption status: %v", encrypt)
t.Log("Wait for the system to stabilize\n")
time.Sleep(5 * time.Minute)
factoryReset(t, dut, enCiscoCommands.DevicePaths)
} else {
t.Log("Performing Factory reset with Encryption\n")
factoryReset(t, dut, enCiscoCommands.DevicePaths)
t.Log("Stablise after factory reset\n")
time.Sleep(5 * time.Minute)
t.Log("Deactivate Encryption\n")
encrypt := dut.CLI().Run(t, enCiscoCommands.EncryptionDeactivate)
t.Logf("Device encrytion deactivate: %v", encrypt)
t.Logf("Sleep for 5 mins after disk-encryption deactivate")
time.Sleep(5 * time.Minute)
deviceBootStatus(t, dut)
encrypt = dut.CLI().Run(t, enCiscoCommands.EncryptionStatus)
t.Logf("Show device encrytion status: %v", encrypt)
t.Logf("Wait for the system to stabilize\n")
time.Sleep(5 * time.Minute)
factoryReset(t, dut, enCiscoCommands.DevicePaths)
}
factoryReset(t, dut)
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
uuid: "15e3ade1-e3c4-4da3-8553-3a9ef5fba344"
plan_id: "gNOI-6.1"
description: "Factory Reset"
testbed: TESTBED_DUT_ATE_2LINKS
testbed: TESTBED_DUT
Loading

0 comments on commit 982d970

Please sign in to comment.