Juniper / jsnapy

Python version of Junos Snapshot Administrator
Apache License 2.0
115 stars 58 forks source link

Fixed issue #391 #392

Closed chidanandpujar closed 2 years ago

chidanandpujar commented 2 years ago

What does this PR do?

Delta values are not calculated correctly when using negative values e.g. -10% or positive values e.g +10%

What issues does this PR fix or reference?

Added checks in Delta function for positive % range and negative % range between pre and post snapshot values This fix address the following conditions pre-snapshot value 100 and post-snapshot value 111 , if delta is +10% - returns error post snapshot value is not within the range 100 -110 pre-snapshot value100 and post-snapshot value 89 , if delta is -10% , returns error if post snapshot values are not within the range of 100 - 90

Tests written?

No

chidanandpujar commented 2 years ago

Hi,

I have updated the fix and verified , please find the results .

Case 1: When pre-snapshot value is 100 and post-snanpshot is +10% i.e 111

:~/jsnapy_issue_392/jsnapy/samples# cat /root/jsnapy_issue_392/venv/etc/jsnapy/snapshots/10.220.12.56_22_pre_show_route_summary.xml
<route-summary-information>
    <as-number>44745</as-number>
    <router-id>10.255.255.250</router-id>
    <route-table>
        <table-name>inet.0</table-name>
        <destination-count>141</destination-count>
        <total-route-count>141</total-route-count>
        <active-route-count>100</active-route-count>
        <holddown-route-count>1</holddown-route-count>
        <hidden-route-count>1</hidden-route-count>
        <protocols>
            <protocol-name>Direct</protocol-name>
            <protocol-route-count>9</protocol-route-count>
            <active-route-count>8</active-route-count>
        </protocols>
        <protocols>
            <protocol-name>Local</protocol-name>
            <protocol-route-count>7</protocol-route-count>
            <active-route-count>7</active-route-count>
        </protocols>
        <protocols>
            <protocol-name>OSPF</protocol-name>
            <protocol-route-count>124</protocol-route-count>
            <active-route-count>123</active-route-count>
        </protocols>
        <protocols>
            <protocol-name>Static</protocol-name>
            <protocol-route-count>1</protocol-route-count>
            <active-route-count>1</active-route-count>
        </protocols>
    </route-table>
</route-summary-information>

~/jsnapy_issue_392/jsnapy/samples# cat /root/jsnapy_issue_392/venv/etc/jsnapy/snapshots/10.220.12.56_22_post_show_route_summary.xml
<route-summary-information>
    <as-number>44745</as-number>
    <router-id>10.255.255.250</router-id>
    <route-table>
        <table-name>inet.0</table-name>
        <destination-count>140</destination-count>
        <total-route-count>140</total-route-count>
        <active-route-count>111</active-route-count>
        <holddown-route-count>0</holddown-route-count>
        <hidden-route-count>1</hidden-route-count>
        <protocols>
            <protocol-name>Direct</protocol-name>
            <protocol-route-count>9</protocol-route-count>
            <active-route-count>8</active-route-count>
        </protocols>
        <protocols>
            <protocol-name>Local</protocol-name>
            <protocol-route-count>7</protocol-route-count>
            <active-route-count>7</active-route-count>
        </protocols>
        <protocols>
            <protocol-name>OSPF</protocol-name>
            <protocol-route-count>123</protocol-route-count>
            <active-route-count>123</active-route-count>
        </protocols>
        <protocols>
            <protocol-name>Static</protocol-name>
            <protocol-route-count>1</protocol-route-count>
            <active-route-count>1</active-route-count>
        </protocols>
    </route-table>
</route-summary-information>

:~/jsnapy_issue_392/jsnapy/samples# cat test.yaml 
tests_include:
  - check_route_summary

check_route_summary:
- command: show route summary
- iterate:
    xpath: //route-summary-information/route-table
    id: ./table-name
    ignore-null: true
    tests:
      - delta: active-route-count, +10%
        info: "Active routes on: <{{post['table-name']}}> has not changed by more than 10%. Pre: <{{pre['active-route-count']}}>, Post <{{post['active-route-count']}}> "
        err: "WARNING!! <{{post['table-name']}}> has changed by more than 10% delta. Pre <{{pre['active-route-count']}}>, Post <{{post['active-route-count']}}> "
      - delta: active-route-count, -10%
        info: "Active routes on: <{{post['table-name']}}> has not changed by more than 10%. Pre: <{{pre['active-route-count']}}>, Post <{{post['active-route-count']}}> "
        err: "WARNING!! <{{post['table-name']}}> has changed by more than 10% delta. Pre <{{pre['active-route-count']}}>, Post <{{post['active-route-count']}}> "

~/jsnapy_issue_392/jsnapy/samples# jsnapy --check pre post -f config.yaml
**************************** Device: 10.220.12.56 ****************************
Tests Included: check_route_summary 
************************ Command: show route summary ************************
WARNING!! <inet.0> has changed by more than 10% delta. Pre <100.0>, Post <111.0> 
FAIL | All "active-route-count" is not with in delta difference of +10% [ 0 value matched / 1 value failed ]
PASS | All "active-route-count" is with in delta difference of -10% [ 1 value matched ]
------------------------------- Final Result!! -------------------------------
check_route_summary : Failed
Total No of tests passed: 1
Total No of tests failed: 1 
Overall Tests failed!!! 

Case 2:  pre-snapshot value is 100 and post-snapshot vlaue is -10% i.e 89 

~/jsnapy_issue_392/jsnapy/samples# cat ~/jsnapy_issue_392/venv/etc/jsnapy/snapshots/10.220.12.56_22_post_show_route_summary.xml
<route-summary-information>
    <as-number>44745</as-number>
    <router-id>10.255.255.250</router-id>
    <route-table>
        <table-name>inet.0</table-name>
        <destination-count>140</destination-count>
        <total-route-count>140</total-route-count>
        <active-route-count>89</active-route-count>
        <holddown-route-count>0</holddown-route-count>
        <hidden-route-count>1</hidden-route-count>
        <protocols>
            <protocol-name>Direct</protocol-name>
            <protocol-route-count>9</protocol-route-count>
            <active-route-count>8</active-route-count>
        </protocols>
        <protocols>
            <protocol-name>Local</protocol-name>
            <protocol-route-count>7</protocol-route-count>
            <active-route-count>7</active-route-count>
        </protocols>
        <protocols>
            <protocol-name>OSPF</protocol-name>
            <protocol-route-count>123</protocol-route-count>
            <active-route-count>123</active-route-count>
        </protocols>
        <protocols>
            <protocol-name>Static</protocol-name>
            <protocol-route-count>1</protocol-route-count>
            <active-route-count>1</active-route-count>
        </protocols>
    </route-table>
</route-summary-information>

~/jsnapy_issue_392/jsnapy/samples# jsnapy --check pre post -f config.yaml
**************************** Device: 10.220.12.56 ****************************
Tests Included: check_route_summary 
************************ Command: show route summary ************************
PASS | All "active-route-count" is with in delta difference of +10% [ 1 value matched ]
WARNING!! <inet.0> has changed by more than 10% delta. Pre <100.0>, Post <89.0> 
FAIL | All "active-route-count" is not with in delta difference of -10% [ 0 value matched / 1 value failed ]
------------------------------- Final Result!! -------------------------------
check_route_summary : Failed
Total No of tests passed: 1
Total No of tests failed: 1 
Overall Tests failed!!! 

Case 3: pre-snapshot is 100 and post-snapshot is either + or - 10% ,  ie  110 or 90 .

~/jsnapy_issue_392/jsnapy/samples# cat test.yaml 
tests_include:
  - check_route_summary

check_route_summary:
- command: show route summary
- iterate:
    xpath: //route-summary-information/route-table
    id: ./table-name
    ignore-null: true
    tests:
      - delta: active-route-count, 10%
        info: "Active routes on: <{{post['table-name']}}> has not changed by more than 10%. Pre: <{{pre['active-route-count']}}>, Post <{{post['active-route-count']}}> "
        err: "WARNING!! <{{post['table-name']}}> has changed by more than 10% delta. Pre <{{pre['active-route-count']}}>, Post <{{post['active-route-count']}}> "

~/jsnapy_issue_392/jsnapy/samples# vi ~/jsnapy_issue_392/venv/etc/jsnapy/snapshots/10.220.12.56_22_post_show_route_summary.xml
<route-summary-information>
        <as-number>44745</as-number>
        <router-id>10.255.255.250</router-id>
        <route-table>
                <table-name>inet.0</table-name>
                <destination-count>140</destination-count>
                <total-route-count>140</total-route-count>
                <active-route-count>90</active-route-count>
                <holddown-route-count>0</holddown-route-count>
                <hidden-route-count>1</hidden-route-count>
                <protocols>
                        <protocol-name>Direct</protocol-name>
                        <protocol-route-count>9</protocol-route-count>
                        <active-route-count>8</active-route-count>
                </protocols>
                <protocols>
                        <protocol-name>Local</protocol-name>
                        <protocol-route-count>7</protocol-route-count>
                        <active-route-count>7</active-route-count>
                </protocols>
                <protocols>
                        <protocol-name>OSPF</protocol-name>
                        <protocol-route-count>123</protocol-route-count>
                        <active-route-count>123</active-route-count>
                </protocols>
                <protocols>
                        <protocol-name>Static</protocol-name>
                        <protocol-route-count>1</protocol-route-count>
                        <active-route-count>1</active-route-count>
                </protocols>
        </route-table>
</route-summary-information>

~/jsnapy_issue_392/jsnapy/samples# jsnapy --check pre post -f config.yaml
**************************** Device: 10.220.12.56 ****************************
Tests Included: check_route_summary 
************************ Command: show route summary ************************
PASS | All "active-route-count" is with in delta difference of 10% [ 1 value matched ]
------------------------------- Final Result!! -------------------------------
check_route_summary : Passed
Total No of tests passed: 1
Total No of tests failed: 0 
Overall Tests passed!!! 

~/jsnapy_issue_392/jsnapy/samples# vi ~/jsnapy_issue_392/venv/etc/jsnapy/snapshots/10.220.12.56_22_post_show_route_summary.xml
<route-summary-information>
        <as-number>44745</as-number>
        <router-id>10.255.255.250</router-id>
        <route-table>
                <table-name>inet.0</table-name>
                <destination-count>140</destination-count>
                <total-route-count>140</total-route-count>
                <active-route-count>110</active-route-count>
                <holddown-route-count>0</holddown-route-count>
                <hidden-route-count>1</hidden-route-count>
                <protocols>
                        <protocol-name>Direct</protocol-name>
                        <protocol-route-count>9</protocol-route-count>
                        <active-route-count>8</active-route-count>
                </protocols>
                <protocols>
                        <protocol-name>Local</protocol-name>
                        <protocol-route-count>7</protocol-route-count>
                        <active-route-count>7</active-route-count>
                </protocols>
                <protocols>
                        <protocol-name>OSPF</protocol-name>
                        <protocol-route-count>123</protocol-route-count>
                        <active-route-count>123</active-route-count>
                </protocols>
                <protocols>
                        <protocol-name>Static</protocol-name>
                        <protocol-route-count>1</protocol-route-count>
                        <active-route-count>1</active-route-count>
                </protocols>
        </route-table>
</route-summary-information>

~/jsnapy_issue_392/jsnapy/samples# jsnapy --check pre post -f config.yaml
**************************** Device: 10.220.12.56 ****************************
Tests Included: check_route_summary 
************************ Command: show route summary ************************
PASS | All "active-route-count" is with in delta difference of 10% [ 1 value matched ]
------------------------------- Final Result!! -------------------------------
check_route_summary : Passed
Total No of tests passed: 1
Total No of tests failed: 0 
Overall Tests passed!!! 

Case 4: when both pre-snapshot  is 100 and post-snapshot value are same i.e 100

 ~/jsnapy_issue_392/venv/etc/jsnapy/snapshots/10.220.12.56_22_post_show_route_summary.xml
<route-summary-information>
        <as-number>44745</as-number>
        <router-id>10.255.255.250</router-id>
        <route-table>
                <table-name>inet.0</table-name>
                <destination-count>140</destination-count>
                <total-route-count>140</total-route-count>
                <active-route-count>100</active-route-count>
                <holddown-route-count>0</holddown-route-count>
                <hidden-route-count>1</hidden-route-count>
                <protocols>
                        <protocol-name>Direct</protocol-name>
                        <protocol-route-count>9</protocol-route-count>
                        <active-route-count>8</active-route-count>
                </protocols>
                <protocols>
                        <protocol-name>Local</protocol-name>
                        <protocol-route-count>7</protocol-route-count>
                        <active-route-count>7</active-route-count>
                </protocols>
                <protocols>
                        <protocol-name>OSPF</protocol-name>
                        <protocol-route-count>123</protocol-route-count>
                        <active-route-count>123</active-route-count>
                </protocols>
                <protocols>
                        <protocol-name>Static</protocol-name>
                        <protocol-route-count>1</protocol-route-count>
                        <active-route-count>1</active-route-count>
                </protocols>
        </route-table>
</route-summary-information>

~/jsnapy_issue_392/jsnapy/samples# jsnapy --check pre post -f config.yaml
**************************** Device: 10.220.12.56 ****************************
Tests Included: check_route_summary 
************************ Command: show route summary ************************
PASS | All "active-route-count" is with in delta difference of 10% [ 1 value matched ]
------------------------------- Final Result!! -------------------------------
check_route_summary : Passed
Total No of tests passed: 1
Total No of tests failed: 0 
Overall Tests passed!!! 

Thanks & Regards Chidanand