示例#1
0
 def test_getkey(self):
     '''!
     Verify that the section 'ztp' exist in a valid json configuration file
     '''
     ztpjson = ZTPJson()
     ztpcfg = ConfigSection()
     assert (ztpjson.jsonDict['ztp'] == ztpcfg.jsonDict['ztp'])
     assert (ztpjson['ztp'] == None)
     with pytest.raises(TypeError):
         ztpjson.plugin(None)
示例#2
0
    def test_ztp_use_section_name_for_plugin_name_seqnum(self, tmpdir):
        '''!
        Verify that when no plugin data is provided, the script name is derived from the section name.
        Verify that the sequence number in front of the section name is removed.
        '''
        content = """{
    "ztp": {
        "0001-test-provisioning-script": {
            "halt-on-failure": false,
            "ignore-result": false,
            "reboot-on-failure": false,
            "reboot-on-success": false,
            "status": "BOOT",
            "timestamp": "2019-04-18 19:49:49"
        },
        "halt-on-failure": false,
        "ignore-result": false,
        "reboot-on-failure": false,
        "reboot-on-success": false,
        "status": "BOOT",
        "timestamp": "2019-04-18 19:49:49"
    }
}"""
        self.__write_file("/tmp/test_firmware_" + socket.gethostname() + ".sh",
                          content)
        d = tmpdir.mkdir("valid")
        fh = d.join("test.json")
        fh.write("""
        {
          "ztp" : {
            "dynamic-url" : {
              "source" : {
               "prefix" : "file:///tmp/test_firmware_",
               "identifier" : "hostname",
               "suffix" : ".sh"
              }
            }
          }
        }
        """)
        d2 = tmpdir.mkdir("dest")
        fh2 = d2.join("dest_file")
        ztpjson = ZTPJson(str(fh), json_dst_file=str(fh2))

        content = """
        #!/bin/sh

        echo Hello
        exit 2
"""
        self.__write_file("/tmp/test_firmware.json", content)
        os.system('touch ' + getCfg('plugins-dir') + '/' +
                  'test-provisioning-script')
        plugin_name = ztpjson.plugin('0001-test-provisioning-script')
        os.system('rm -f ' + getCfg('plugins-dir') + '/' +
                  'test-provisioning-script')
        assert (plugin_name == getCfg('plugins-dir') +
                '/test-provisioning-script')
示例#3
0
    def test_ztp_graphservice_do_not_exist(self, tmpdir):
        '''!
        Test when the plugin is a graphservice and does not exist.
        '''
        content = """{
    "ztp": {
        "0001-test-provisioning-script": {
            "halt-on-failure": false,
            "ignore-result": false,
            "plugin": "graphservice",
            "reboot-on-failure": false,
            "reboot-on-success": false,
            "status": "BOOT",
            "timestamp": "2019-04-18 19:49:49"
        },
        "config-fallback": false,
        "halt-on-failure": false,
        "ignore-result": false,
        "reboot-on-failure": false,
        "reboot-on-success": false,
        "restart-ztp-no-config": true,
        "restart-ztp-on-failure": false,
        "status": "BOOT",
        "timestamp": "2019-04-18 19:49:49",
        "ztp-json-version": "1.0"
    }
}"""
        self.__write_file(
            "/tmp/test_firmware_" + socket.gethostname() + ".json", content)
        d = tmpdir.mkdir("valid")
        fh = d.join("test.json")
        fh.write("""
        {
          "ztp" : {
            "dynamic-url" : {
              "source" : {
               "prefix" : "file:///tmp/test_firmware_",
               "identifier" : "hostname",
               "suffix" : ".json"
              }
            }
          }
        }
        """)
        d2 = tmpdir.mkdir("dest")
        fh2 = d2.join("dest_file")
        ztpjson = ZTPJson(str(fh), json_dst_file=str(fh2))
        assert (self.__read_file(str(fh2)) == content)

        content = """
        #!/bin/sh

        echo Hello
        exit 2
"""
        plugin_name = ztpjson.plugin('0001-test-provisioning-script')
        assert (plugin_name == getCfg('plugins-dir') + '/graphservice')
示例#4
0
    def test_ztp_plugin_data_invalid(self, tmpdir):
        '''!
        Verify that when the plugin data is invalid, the plugin return None
        '''
        content = """{
    "ztp": {
        "0001-test-provisioning-script": {
            "halt-on-failure": false,
            "ignore-result": false,
            "plugin": [12],
            "reboot-on-failure": false,
            "reboot-on-success": false,
            "status": "BOOT",
            "timestamp": "2019-04-18 19:49:49"
        },
        "halt-on-failure": false,
        "ignore-result": false,
        "reboot-on-failure": false,
        "reboot-on-success": false,
        "status": "BOOT",
        "timestamp": "2019-04-18 19:49:49"
    }
}"""
        self.__write_file("/tmp/test_firmware_" + socket.gethostname() + ".sh",
                          content)
        d = tmpdir.mkdir("valid")
        fh = d.join("test.json")
        fh.write("""
        {
          "ztp" : {
            "dynamic-url" : {
              "source" : {
               "prefix" : "file:////tmp/test_firmware_",
               "identifier" : "hostname",
               "suffix" : ".sh"
              }
            }
          }
        }
        """)
        d2 = tmpdir.mkdir("dest")
        fh2 = d2.join("dest_file")
        ztpjson = ZTPJson(str(fh), json_dst_file=str(fh2))

        content = """
        #!/bin/sh

        echo Hello
        exit 2
"""
        plugin_name = ztpjson.plugin('0001-test-provisioning-script')
        assert (plugin_name == None)
示例#5
0
    def test_ztp_invalid_plugin_type(self, tmpdir):
        '''!
        Test when the plugin is neither a URL, Dynamic URL or Name type.
        Verify that the plugin name return is None.
        '''
        content = """{
    "ztp": {
        "04-connectivity-tests-2": {
          "plugin": {
            "nothing": "firmware"
          }
        },
        "reboot-on-failure": false,
        "reboot-on-success": false,
        "status": "BOOT",
        "timestamp": "2019-04-18 19:49:49"
    }
}"""
        self.__write_file(
            "/tmp/test_firmware_" + socket.gethostname() + ".json", content)
        d = tmpdir.mkdir("valid")
        fh = d.join("test.json")
        fh.write("""
        {
          "ztp" : {
            "dynamic-url" : {
              "source" : {
               "prefix" : "file:///tmp/test_firmware_",
               "identifier" : "hostname",
               "suffix" : ".json"
              }
            }
          }
        }
        """)
        d2 = tmpdir.mkdir("dest")
        fh2 = d2.join("dest_file")
        ztpjson = ZTPJson(str(fh), json_dst_file=str(fh2))

        content = """
        #!/bin/sh

        echo Hello
        exit -1
"""
        plugin_name = ztpjson.plugin('04-connectivity-tests-2')
        assert (plugin_name == None)
示例#6
0
    def test_ztp_firmware_plugin(self, tmpdir):
        '''!
        Test case with a plugin name such as "firmware".
        Use "firmware" as plugin name, and verify that the script name return is
        "/usr/lib/ztp/plugins/firmware"
        '''
        content = """{
    "ztp": {
        "04-connectivity-tests-2": {
          "plugin": {
            "name": "firmware"
          }
        },
        "reboot-on-failure": false,
        "reboot-on-success": false,
        "status": "BOOT",
        "timestamp": "2019-04-18 19:49:49"
    }
}"""
        self.__write_file(
            "/tmp/test_firmware_" + socket.gethostname() + ".json", content)
        d = tmpdir.mkdir("valid")
        fh = d.join("test.json")
        fh.write("""
        {
          "ztp" : {
            "dynamic-url" : {
              "source" : {
               "prefix" : "file:///tmp/test_firmware_",
               "identifier" : "hostname",
               "suffix" : ".json"
              }
            }
          }
        }
        """)
        d2 = tmpdir.mkdir("dest")
        fh2 = d2.join("dest_file")
        ztpjson = ZTPJson(str(fh), json_dst_file=str(fh2))

        content = """
        #!/bin/sh

        echo Hello
        exit -1
"""
        plugin_name = ztpjson.plugin('04-connectivity-tests-2')
示例#7
0
    def test_ztp_use_section_name_for_plugin_name(self, tmpdir):
        '''!
        Verify that when no plugin data is provided, the script name is derived from the section name.
        Test case when there is no sequence number in front of the section name.
        '''
        content = """{
    "ztp": {
        "test-provisioning-script": {
            "halt-on-failure": false,
            "ignore-result": false,
            "reboot-on-failure": false,
            "reboot-on-success": false,
            "status": "BOOT",
            "timestamp": "2019-04-18 19:49:49"
        },
        "halt-on-failure": false,
        "ignore-result": false,
        "reboot-on-failure": false,
        "reboot-on-success": false,
        "status": "BOOT",
        "timestamp": "2019-04-18 19:49:49"
    }
}"""
        self.__write_file("/tmp/ztp_" + socket.gethostname() + ".json",
                          content)
        d = tmpdir.mkdir("valid")
        fh = d.join("test.json")
        fh.write("""
        {
          "ztp" : {
            "dynamic-url" : {
              "source" : {
               "prefix" : "file:///tmp/ztp_",
               "identifier" : "hostname",
               "suffix" : ".json"
              }
            }
          }
        }
        """)
        d2 = tmpdir.mkdir("dest")
        fh2 = d2.join("dest_file")
        ztpjson = ZTPJson(str(fh), json_dst_file=str(fh2))

        plugin_name = ztpjson.plugin('test-provisioning-script')
        assert (plugin_name == None)
示例#8
0
    def test_ztp_dynamic_url_download_2(self, tmpdir):
        '''!
        Verify that it can download a script provided by a dynamic URL
        '''
        content = """{
    "ztp": {
        "0001-test-provisioning-script": {
            "halt-on-failure": false,
            "ignore-result": false,
            "plugin": {
                "dynamic-url": {
                      "source" : {
                       "prefix" : "file:///tmp/test_firmware_post_install_",
                       "identifier" : "hostname",
                       "suffix" : ".sh"
                      }
                }
            },
            "reboot-on-failure": false,
            "reboot-on-success": false,
            "status": "BOOT",
            "timestamp": "2019-04-18 19:49:49"
        },
        "halt-on-failure": false,
        "ignore-result": false,
        "reboot-on-failure": false,
        "reboot-on-success": false,
        "status": "BOOT",
        "timestamp": "2019-04-18 19:49:49"
    }
}"""
        self.__write_file(
            "/tmp/test_firmware_" + socket.gethostname() + ".json", content)
        d = tmpdir.mkdir("valid")
        fh = d.join("test.json")
        fh.write("""
        {
          "ztp" : {
            "dynamic-url" : {
               "source" : {
                 "prefix" : "file:///tmp/test_firmware_",
                 "identifier" : "hostname",
                 "suffix" : ".json"
              }
            }
          }
        }
        """)
        d2 = tmpdir.mkdir("dest")
        fh2 = d2.join("dest_file")
        ztpjson = ZTPJson(str(fh), json_dst_file=str(fh2))

        content = """
        #!/bin/sh

        echo Hello
        exit 2
"""
        self.__write_file(
            "/tmp/test_firmware_post_install_" + socket.gethostname() + ".sh",
            content)
        plugin_name = ztpjson.plugin('0001-test-provisioning-script')
        assert (plugin_name ==
                '/var/lib/ztp/sections/0001-test-provisioning-script/plugin')
示例#9
0
    def test_ztp_url_reusing_plugin_2(self, tmpdir):
        '''!
        Test using an URL, and not a dynamic URL.
        Test reusing the same plugin when the plugin return SUSPEND.
        '''
        content = """{
    "ztp": {
        "0001-test-provisioning-script": {
            "halt-on-failure": false,
            "ignore-result": false,
            "plugin": {
                "url": {
                    "source": "file:///tmp/test_firmware.sh",
                    "destination": "/tmp/firmware_check.sh"
                }
            },
            "reboot-on-failure": false,
            "reboot-on-success": false,
            "status": "BOOT",
            "timestamp": "2019-04-18 19:49:49"
        },
        "halt-on-failure": false,
        "ignore-result": false,
        "reboot-on-failure": false,
        "reboot-on-success": false,
        "status": "BOOT",
        "timestamp": "2019-04-18 19:49:49"
    }
}"""
        self.__write_file(
            "/tmp/test_firmware_" + socket.gethostname() + ".json", content)
        d = tmpdir.mkdir("valid")
        fh = d.join("test.json")
        fh.write("""
        {
          "ztp" : {
            "dynamic-url" : {
              "source" : {
               "prefix" : "file:///tmp/test_firmware_",
               "identifier" : "hostname",
               "suffix" : ".json"
              }
            }
          }
        }
        """)
        self.__write_file("/tmp/test_firmware_" + socket.gethostname() + ".sh",
                          content)
        d2 = tmpdir.mkdir("dest")
        fh2 = d2.join("dest_file")
        ztpjson = ZTPJson(str(fh), json_dst_file=str(fh2))

        content = """
        #!/bin/sh

        echo Hello
        exit -1
"""
        self.__write_file("/tmp/test_firmware.sh", content)
        plugin1_name = ztpjson.plugin('0001-test-provisioning-script')
        assert (self.__read_file(plugin1_name) == content)
        plugin2_name = ztpjson.plugin('0001-test-provisioning-script')
        assert (self.__read_file(plugin2_name) == content)
示例#10
0
    def test_ztp_non_existent_plugin_section_name(self, tmpdir):
        '''!
        Test when using a non existent section plugin section name.
        Verify that the plugin name returned is None.
        '''
        content = """{
    "ztp": {
        "0001-test-provisioning-script": {
            "halt-on-failure": false,
            "ignore-result": false,
            "plugin": {
                "url": {
                    "source": "http://localhost:2000/ztp/scripts/post_install.sh"
                }
            },
            "reboot-on-failure": false,
            "reboot-on-success": false,
            "status": "BOOT",
            "timestamp": "2019-04-18 19:49:49"
        },
        "config-fallback": false,
        "halt-on-failure": false,
        "ignore-result": false,
        "reboot-on-failure": false,
        "reboot-on-success": false,
        "restart-ztp-no-config": true,
        "restart-ztp-on-failure": false,
        "status": "BOOT",
        "timestamp": "2019-04-18 19:49:49",
        "ztp-json-version": "1.0"
    }
}"""
        self.__write_file(
            "/tmp/test_firmware_" + socket.gethostname() + ".json", content)
        d = tmpdir.mkdir("valid")
        fh = d.join("test.json")
        fh.write("""
        {
          "ztp" : {
            "dynamic-url" : {
              "source" : {
               "prefix" : "file:///tmp/test_firmware_",
               "identifier" : "hostname",
               "suffix" : ".json"
              }
            }
          }
        }
        """)
        d2 = tmpdir.mkdir("dest")
        fh2 = d2.join("dest_file")
        ztpjson = ZTPJson(str(fh), json_dst_file=str(fh2))
        assert (self.__read_file(str(fh2)) == content)

        content = """
        #!/bin/sh

        echo Hello
        exit 2
"""
        plugin_name = ztpjson.plugin('XYZ')
        assert (plugin_name == None)
示例#11
0
    def test_ztp_dynamic_url_invalid_arg_type(self, tmpdir):
        '''!
        Test a valid input json file, with a plugin described as a dynamic URL, but with an URL
        instead of a Dynamic URL. Load the plugin.
        Verify that this condition happens:
        "DynamicURL provided with invalid argument types"
        '''
        content = """{
    "ztp": {
        "0001-test-provisioning-script": {
            "halt-on-failure": false,
            "ignore-result": false,
            "plugin": {
                "dynamic-url": {
                    "source": "/tmp/test_firmware_%s.json"
                }
            },
            "reboot-on-failure": false,
            "reboot-on-success": false,
            "status": "BOOT",
            "timestamp": "2019-04-18 19:49:49"
        },
        "config-fallback": false,
        "halt-on-failure": false,
        "ignore-result": false,
        "reboot-on-failure": false,
        "reboot-on-success": false,
        "restart-ztp-no-config": true,
        "restart-ztp-on-failure": false,
        "status": "BOOT",
        "timestamp": "2019-04-18 19:49:49",
        "ztp-json-version": "1.0"
    }
}""" % socket.gethostname()
        self.__write_file(
            "/tmp/test_firmware_" + socket.gethostname() + ".json", content)
        d = tmpdir.mkdir("valid")
        fh = d.join("test.json")
        fh.write("""
        {
          "ztp" : {
            "dynamic-url" : {
              "source" : {
               "prefix" : "file:///tmp/test_firmware_",
               "identifier" : "hostname",
               "suffix" : ".json"
              }
            }
          }
        }
        """)
        d2 = tmpdir.mkdir("dest")
        fh2 = d2.join("dest_file")
        ztpjson = ZTPJson(str(fh), json_dst_file=str(fh2))
        assert (self.__read_file(str(fh2)) == content)

        content = """
        #!/bin/sh

        echo Hello
        exit 2
"""
        self.__write_file("/tmp/test_firmware.txt", content)
        plugin_name = ztpjson.plugin('0001-test-provisioning-script')
        assert (plugin_name == None)