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 snabb config test functionality #1339

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/lib/yang/yang.lua
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,10 @@ function load_configuration(filename, opts)
end
local function err(msg, ...) error(err_msg(msg, ...)) end
local function log(msg, ...)
io.stderr:write(err_msg(msg, ...)..'\n')
io.stderr:flush()
if opts.verbose then
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need to change the default above at line 34 to verbose={default=true}, to keep the current behavior for existing callers.

io.stderr:write(err_msg(msg, ...)..'\n')
io.stderr:flush()
end
end
local function assert(exp, msg, ...)
if exp then return exp else err(msg, ...) end
Expand Down
1 change: 1 addition & 0 deletions src/program/config/README
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Usage:
snabb config get-state
snabb config listen
snabb config load
snabb config test
snabb config remove
snabb config set

Expand Down
49 changes: 35 additions & 14 deletions src/program/config/common.lua
Original file line number Diff line number Diff line change
Expand Up @@ -73,37 +73,58 @@ function parse_command_line(args, opts)
function handlers.s(arg) ret.schema_name = arg end
function handlers.r(arg) ret.revision_date = arg end
function handlers.c(arg) ret.socket = arg end
function handlers.v(arg) ret.verbose = true end
function handlers.f(arg)
assert(arg == "yang" or arg == "xpath", "Not valid output format")
ret.format = arg
end
handlers['print-default'] = function ()
ret.print_default = true
end
args = lib.dogetopt(args, handlers, "hs:r:c:f:",
{help="h", ['schema-name']="s", schema="s",
args = lib.dogetopt(args, handlers, "hvs:r:c:f:",
{help="h", verbose="v", ['schema-name']="s", schema="s",
['revision-date']="r", revision="r", socket="c",
['print-default']=0, format="f"})

if #args == 0 then err() end
ret.instance_id = table.remove(args, 1)
local descr = call_leader(ret.instance_id, 'describe', {})
if not ret.schema_name then
if opts.require_schema then err("missing --schema arg") end
ret.schema_name = descr.default_schema

if opts.command == 'test' then
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dunno, I think since there's these two ways to deal with the schema -- either as provided by the user or as received from the remote -- I think it makes sense to move all argument handling to test.lua, to avoid making this common.lua too specific on the commands being used. WDYT?

if #args > 1 then
ret.instance_id = table.remove(args, 1)
end
else
ret.instance_id = table.remove(args, 1)
end
require('lib.yang.schema').set_default_capabilities(descr.capability)

-- If instance ID was given, then load description from instance
if ret.instance_id then
local descr = call_leader(ret.instance_id, 'describe', {})
-- If schema name was not explicitly passed, load from instance
if not ret.schema_name then
if opts.require_schema then err("missing --schema arg") end
ret.schema_name = descr.default_schema
end
require('lib.yang.schema').set_default_capabilities(descr.capability)
end

if not pcall(yang.load_schema_by_name, ret.schema_name) then
local response = call_leader(
ret.instance_id, 'get-schema',
{schema=ret.schema_name, revision=ret.revision_date})
assert(not response.error, response.error)
yang.add_schema(response.source, ret.schema_name)
local schema_file = S.lstat(ret.schema_name)
if schema_file and schema_file.isreg then
ret.schema_name = yang.add_schema_file(ret.schema_name)
else
if not ret.instance_id then err("no schema loaded and instance id not given") end
local response = call_leader(
ret.instance_id, 'get-schema',
{schema=ret.schema_name, revision=ret.revision_date})
assert(not response.error, response.error)
yang.add_schema(response.source, ret.schema_name)
end
end
if opts.with_config_file then
if #args == 0 then err("missing config file argument") end
local file = table.remove(args, 1)
local opts = {schema_name=ret.schema_name,
revision_date=ret.revision_date}
revision_date=ret.revision_date, verbose=ret.verbose}
ret.config_file = file
ret.config = yang.load_configuration(file, opts)
end
Expand Down
18 changes: 18 additions & 0 deletions src/program/config/test/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
Usage:
snabb config test [OPTIONS] [ID] FILE

Available options:
-s SCHEMA-NAME-OR-FILE-PATH
--schema SCHEMA-NAME-OR-FILE-PATH
--schema-name SCHEMA-NAME

-r REVISION
--revision REVISION
--revision-date REVISION

-v
--verbose

-h
--help

18 changes: 18 additions & 0 deletions src/program/config/test/README.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
Usage:
snabb config test [OPTIONS] [ID] FILE

Available options:
-s SCHEMA-NAME-OR-FILE-PATH
--schema SCHEMA-NAME-OR-FILE-PATH
--schema-name SCHEMA-NAME

-r REVISION
--revision REVISION
--revision-date REVISION

-v
--verbose

-h
--help

12 changes: 12 additions & 0 deletions src/program/config/test/test.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
-- Use of this source code is governed by the Apache 2.0 license; see COPYING.
module(..., package.seeall)

local yang = require("lib.yang.yang")
local common = require("program.config.common")

function run(args)
local opts = { command='test', with_config_file=true, is_config = false}
local ret, args = common.parse_command_line(args, opts)

yang.print_config_for_schema_by_name(ret.schema_name, ret.config, io.stdout)
end