Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ygnmi validator #93

Merged
merged 20 commits into from
Nov 13, 2023
Merged
47 changes: 44 additions & 3 deletions cmd_gen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
// local run flags
local bool // local run toggle
localResultsDir string // folder into which the command outputs its results
localValidatorId string

Check failure on line 56 in cmd_gen/main.go

View workflow job for this annotation

GitHub Actions / go / Static Analysis

var localValidatorId should be localValidatorID
localModelDirName string // a model directory (e.g. network-instance, aft)

// Miscellaneous flags
Expand Down Expand Up @@ -227,8 +227,8 @@
)
function run-dir() {
declare prefix="$workdir"/"$1"=="$2"==
outdir=$GOPATH/src/"$1"."$2"/
mkdir "$outdir"
outdir=$GOPATH/src/ygot/"$1"."$2"/
mkdir -p "$outdir"
local options=( -output_file="$outdir"/oc.go "${options[@]}" )
shift 2
echo $cmd "${options[@]}" "$@" > ${prefix}cmd
Expand All @@ -246,6 +246,47 @@
}
`),
perModelTemplate: mustTemplate("goyang-ygot", `run-dir "{{ .ModelDirName }}" "{{ .ModelName }}" {{- range $i, $buildFile := .BuildFiles }} {{ $buildFile }} {{- end }} {{- if .Parallel }} & {{- end }}
`),
},
"ygnmi": {
headerTemplate: mustTemplate("ygnmi-header", `#!/bin/bash
workdir={{ .ResultsDir }}
mkdir -p "$workdir"
cmd="ygnmi generator"
options=(
--trim_module_prefix=openconfig
--exclude_modules=ietf-interfaces
--split_package_paths="/network-instances/network-instance/protocols/protocol/isis=netinstisis,/network-instances/network-instance/protocols/protocol/bgp=netinstbgp"
--paths={{ .ModelRoot }}/...,{{ .RepoRoot }}/third_party/ietf/...
--annotations
)
script_options=(
)
function run-dir() {
declare prefix="$workdir"/"$1"=="$2"==
outdir=$GOPATH/src/ygnmi/"$1"."$2"
mkdir -p "$outdir"
local options=( --output_dir="${outdir}"/oc --base_package_path=ygnmi/"$1"."$2"/oc "${options[@]}" )
shift 2
echo $cmd "${options[@]}" "$@" > ${prefix}cmd
status=0
$cmd "${options[@]}" "${script_options[@]}" "$@" &> ${prefix}pass || status=1
if [[ $status -eq "0" ]]; then
cd "$outdir/oc"
go mod init &> /dev/null || status=1
go mod tidy &> /dev/null || status=1
goimports -w *.go &> /dev/null || status=1
go build &> /dev/null || status=1
fi
if [[ $status -eq "1" ]]; then
# Only output if there is an error: otherwise the gist comment is too long.
go build &>> ${prefix}pass || status=1
mv ${prefix}pass ${prefix}fail
fi
}
go install golang.org/x/tools/cmd/goimports@latest &>> ${prefix}pass || status=1
`),
perModelTemplate: mustTemplate("ygnmi", `run-dir "{{ .ModelDirName }}" "{{ .ModelName }}" {{- range $i, $buildFile := .BuildFiles }} {{ $buildFile }} {{- end }} {{- if .Parallel }} & {{- end }}
`),
},
"yanglint": {
Expand Down Expand Up @@ -299,9 +340,9 @@
)

// runInParallel determines whether a particular validator and version should be run in parallel.
func runInParallel(validatorId, version string) bool {

Check failure on line 343 in cmd_gen/main.go

View workflow job for this annotation

GitHub Actions / go / Static Analysis

func parameter validatorId should be validatorID
switch {
case validatorId == "goyang-ygot", validatorId == "pyang" && version == "head":
case validatorId == "pyang" && version == "head":
return false
default:
return true
Expand All @@ -309,7 +350,7 @@
}

// genValidatorCommandForModelDir generates the validator command for a single modelDir.
func genValidatorCommandForModelDir(validatorId, resultsDir, modelDirName string, modelMap commonci.OpenConfigModelMap, parallel bool) (string, error) {

Check failure on line 353 in cmd_gen/main.go

View workflow job for this annotation

GitHub Actions / go / Static Analysis

func parameter validatorId should be validatorID
var builder strings.Builder
cmdTemplate, ok := scriptTemplates[validatorId]
if !ok {
Expand Down
55 changes: 50 additions & 5 deletions cmd_gen/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,8 @@ script_options=(
)
function run-dir() {
declare prefix="$workdir"/"$1"=="$2"==
outdir=$GOPATH/src/"$1"."$2"/
mkdir "$outdir"
outdir=$GOPATH/src/ygot/"$1"."$2"/
mkdir -p "$outdir"
local options=( -output_file="$outdir"/oc.go "${options[@]}" )
shift 2
echo $cmd "${options[@]}" "$@" > ${prefix}cmd
Expand All @@ -214,9 +214,54 @@ function run-dir() {
mv ${prefix}pass ${prefix}fail
fi
}
run-dir "acl" "openconfig-acl" testdata/acl/openconfig-acl.yang testdata/acl/openconfig-acl-evil-twin.yang
run-dir "optical-transport" "openconfig-optical-amplifier" testdata/optical-transport/openconfig-optical-amplifier.yang
run-dir "optical-transport" "openconfig-transport-line-protection" testdata/optical-transport/openconfig-transport-line-protection.yang
run-dir "acl" "openconfig-acl" testdata/acl/openconfig-acl.yang testdata/acl/openconfig-acl-evil-twin.yang &
run-dir "optical-transport" "openconfig-optical-amplifier" testdata/optical-transport/openconfig-optical-amplifier.yang &
run-dir "optical-transport" "openconfig-transport-line-protection" testdata/optical-transport/openconfig-transport-line-protection.yang &
wait
`,
}, {
name: "basic ygnmi",
inModelMap: basicModelMap,
inValidatorName: "ygnmi",
wantCmd: `#!/bin/bash
workdir=/workspace/results/ygnmi
mkdir -p "$workdir"
cmd="ygnmi generator"
options=(
--trim_module_prefix=openconfig
--exclude_modules=ietf-interfaces
--split_package_paths="/network-instances/network-instance/protocols/protocol/isis=netinstisis,/network-instances/network-instance/protocols/protocol/bgp=netinstbgp"
--paths=testdata/...,/workspace/third_party/ietf/...
--annotations
)
script_options=(
)
function run-dir() {
declare prefix="$workdir"/"$1"=="$2"==
outdir=$GOPATH/src/ygnmi/"$1"."$2"
mkdir -p "$outdir"
local options=( --output_dir="${outdir}"/oc --base_package_path=ygnmi/"$1"."$2"/oc "${options[@]}" )
shift 2
echo $cmd "${options[@]}" "$@" > ${prefix}cmd
status=0
$cmd "${options[@]}" "${script_options[@]}" "$@" &> ${prefix}pass || status=1
if [[ $status -eq "0" ]]; then
cd "$outdir/oc"
go mod init &> /dev/null || status=1
go mod tidy &> /dev/null || status=1
goimports -w *.go &> /dev/null || status=1
go build &> /dev/null || status=1
fi
if [[ $status -eq "1" ]]; then
# Only output if there is an error: otherwise the gist comment is too long.
go build &>> ${prefix}pass || status=1
mv ${prefix}pass ${prefix}fail
fi
}
go install golang.org/x/tools/cmd/goimports@latest &>> ${prefix}pass || status=1
run-dir "acl" "openconfig-acl" testdata/acl/openconfig-acl.yang testdata/acl/openconfig-acl-evil-twin.yang &
run-dir "optical-transport" "openconfig-optical-amplifier" testdata/optical-transport/openconfig-optical-amplifier.yang &
run-dir "optical-transport" "openconfig-transport-line-protection" testdata/optical-transport/openconfig-transport-line-protection.yang &
wait
`,
}, {
Expand Down
5 changes: 5 additions & 0 deletions commonci/commonci.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@
// ValidatorResultsDir determines where a particular validator and version's
// results are
// stored.
func ValidatorResultsDir(validatorId, version string) string {

Check failure on line 97 in commonci/commonci.go

View workflow job for this annotation

GitHub Actions / go / Static Analysis

func parameter validatorId should be validatorID
return filepath.Join(ResultsDir, AppendVersionToName(validatorId, version))
}

Expand Down Expand Up @@ -154,6 +154,11 @@
IsPerModel: true,
IsWidelyUsedTool: true,
},
"ygnmi": {
Name: "ygnmi",
IsPerModel: true,
IsWidelyUsedTool: true,
},
"yanglint": {
Name: "yanglint",
IsPerModel: true,
Expand Down Expand Up @@ -280,7 +285,7 @@
}

type ValidatorAndVersion struct {
ValidatorId string

Check failure on line 288 in commonci/commonci.go

View workflow job for this annotation

GitHub Actions / go / Static Analysis

struct field ValidatorId should be ValidatorID
Version string
}

Expand Down
5 changes: 5 additions & 0 deletions commonci/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"errors"
"fmt"
"log"
"math"
"os"
"strings"
"time"
Expand Down Expand Up @@ -101,6 +102,10 @@ func (g *GithubRequestHandler) AddGistComment(gistID, title, output string) (int
defer cancel() // cancel context if the function returns before the timeout

gistComment := fmt.Sprintf("# %s\n%s", title, output)
if bs := []byte(gistComment); len(bs) > math.MaxUint16 {
log.Printf("Truncating gist comment from %d bytes to %d bytes", len(bs), math.MaxUint16)
gistComment = string(bs[:math.MaxUint16])
}

var id int64
if err := Retry(5, "gist comment creation", func() error {
Expand Down
40 changes: 40 additions & 0 deletions validators/ygnmi/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/bin/bash
# Copyright 2022 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
#
# http://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.


ROOT_DIR=/workspace
RESULTSDIR=$ROOT_DIR/results/ygnmi
OUTFILE=$RESULTSDIR/out
FAILFILE=$RESULTSDIR/fail

if ! stat $RESULTSDIR; then
exit 0
fi

# module download logs go to stderr, so only fail if command failed.
if ! go install github.com/openconfig/ygnmi/app/ygnmi@latest &> "${OUTFILE}"; then
echo "failed: go install github.com/openconfig/ygnmi/app/ygnmi@latest" > "${FAILFILE}"
fi

go list -m github.com/openconfig/ygnmi@latest > $RESULTSDIR/latest-version.txt
if bash $RESULTSDIR/script.sh >> $OUTFILE 2>> $FAILFILE; then
# Delete fail file if it's empty and the script passed.
find $FAILFILE -size 0 -delete
fi
$GOPATH/bin/post_results -validator=ygnmi -modelRoot=$_MODEL_ROOT -repo-slug=$_REPO_SLUG -pr-number=$_PR_NUMBER -commit-sha=$COMMIT_SHA -branch=$BRANCH_NAME
BADGEFILE=$RESULTSDIR/upload-badge.sh
if stat $BADGEFILE; then
bash $BADGEFILE
fi
Loading