def test_json_config_nvalid(self, tmpdir):
     '''!
     Test case when the new json configuration file to apply is syntaxically incorrect.
     Verify that the plugin return with a non zero exit code.
     '''
     content = """
     #!/bin/sh
     exit 1
     """
     self.__write_file("file:///tmp/test_firmware_"+sysEeprom.get_product_name(), content)
     d = tmpdir.mkdir("valid")
     fh = d.join("input.json")
     fh.write("""
     {
         "configdb-json": {
             "clear-config": false,
             "halt-on-failure": false,
             "ignore-result": false,
             "reboot-on-failure": false,
             "reboot-on-success": false,
             "status": "BOOT",
             "timestamp": "2019-05-01 19:49:25",
             "url": {
                 "destination": "/etc/sonic/config_db.json",
                 "source": "file:///tmp/test_firmware_%s"
             }
         }
     }
     """ %(sysEeprom.get_product_name()))
     configdb_json = ConfigDBJson(str(fh))
     with pytest.raises(SystemExit) as pytest_wrapped_e:
         configdb_json.main()
     assert pytest_wrapped_e.type == SystemExit
     assert pytest_wrapped_e.value.code == 1
 def test_input_nfound(self, tmpdir):
     '''!
     Test case when input file does not exist
     '''
     configdb_json = ConfigDBJson('/abc/xyz/foo')
     with pytest.raises(SystemExit) as pytest_wrapped_e:
         configdb_json.main()
     assert pytest_wrapped_e.type == SystemExit
     assert pytest_wrapped_e.value.code == 1
Exemple #3
0
    def test_json_config_valid_load(self, tmpdir):
        '''!
        Test case when the new json configuration file to apply is syntaxically valid.
        Verify that the new configuration is correctly applied using 'config load'.
        Verify that the plugin does not return with a non zero exit code.
        '''

        d = tmpdir.mkdir("valid")
        fh_before = d.join("config-before.json")
        cmd = 'config save -y ' + str(fh_before)
        rc = runCommand(cmd, capture_stdout=False)
        assert (rc == 0)

        data = self.__read_file(str(fh_before))
        fh_after = d.join("config-after.json")
        self.__write_file(str(fh_after), data)

        cmd = "/bin/sed -i -e 's/\"hostname\": \".*\"/\"hostname\": \"something\"/' " + str(
            fh_after)
        rc = runCommand(cmd)

        fh = d.join("input.json")
        fh.write("""
        {
            "configdb-json": {
                "clear-config": false,
                "halt-on-failure": false,
                "ignore-result": false,
                "reboot-on-failure": false,
                "reboot-on-success": false,
                "status": "BOOT",
                "timestamp": "2019-05-01 19:49:25",
                "url": {
                    "destination": "/etc/sonic/config_db.json",
                    "source": "file://%s"
                }
            }
        }
        """ % (str(fh_after)))
        configdb_json = ConfigDBJson(str(fh))
        configdb_json.main()

        fh_after = d.join("config-after.json")
        cmd = 'config save -y ' + str(fh_after)
        rc = runCommand(cmd, capture_stdout=False)
        assert (rc == 0)

        # Collect the differences between the two configurations
        cmd = "/usr/bin/diff --changed-group-format='%>' --unchanged-group-format='' " + str(
            fh_before) + ' ' + str(fh_after)
        (rc2, cmd_stdout, cmd_stderr) = runCommand(cmd)

        # Restore initial configuration
        cmd = 'config load -y ' + str(fh_before)
        rc = runCommand(cmd, capture_stdout=False)
        assert (rc == 0)
 def test_constructor_wrong_type(self):
     '''!
     Test case when we call the constructor with the wrong type for the argument
     '''
     with pytest.raises(TypeError) as pytest_wrapped_e:
         configdb_json = ConfigDBJson(123)
     with pytest.raises(TypeError) as pytest_wrapped_e:
         configdb_json = ConfigDBJson([])
     with pytest.raises(TypeError) as pytest_wrapped_e:
         configdb_json = ConfigDBJson(('abc', 'foo'))
     with pytest.raises(TypeError) as pytest_wrapped_e:
         configdb_json = ConfigDBJson(None)
 def test_invalid_input(self, tmpdir):
     '''!
     Test case when input file is not json syntaxically valid
     '''
     d = tmpdir.mkdir("valid")
     fh = d.join("input.json")
     fh.write("""
     {
         "abc" "foo"
     }
     """)
     configdb_json = ConfigDBJson(str(fh))
     with pytest.raises(SystemExit) as pytest_wrapped_e:
         configdb_json.main()
     assert pytest_wrapped_e.type == SystemExit
     assert pytest_wrapped_e.value.code == 1
 def test_input_no_section(self, tmpdir):
     '''!
     Test case when there is no valid configdb-json related section
     '''
     d = tmpdir.mkdir("valid")
     fh = d.join("input.json")
     fh.write("""
     {
         "snmp": {
             "halt-on-failure": false,
             "ignore-result": false,
             "reboot-on-failure": false,
             "reboot-on-success": false,
             "restart-agent": false,
             "status": "BOOT",
             "timestamp": "2019-04-29 16:12:08"
         }
     }
     """)
     configdb_json = ConfigDBJson(str(fh))
     with pytest.raises(SystemExit) as pytest_wrapped_e:
         configdb_json.main()
     assert pytest_wrapped_e.type == SystemExit
     assert pytest_wrapped_e.value.code == 1
    def test_json_config_valid_reload(self, tmpdir):
        '''!
        Test case when the new json configuration file to apply is syntaxically valid.
        Verify that the new configuration is correctly applied using 'config reload'.
        Verify that the plugin does not return with a non zero exit code.
        '''

        d = tmpdir.mkdir("valid")
        fh_before = d.join("config-before.json")
        cmd = 'config save -y ' + str(fh_before)
        rc = runCommand(cmd, capture_stdout=False)
        assert (rc == 0)
        cmd = 'sonic-cfggen -d -H -v DEVICE_METADATA.localhost.platform'
        (rc, cmd_stdout, cmd_stderr) = runCommand(cmd)
        assert (rc == 0)
        platform = cmd_stdout[0]
        cmd = 'sonic-cfggen -d -H -v DEVICE_METADATA.localhost.hwsku'
        (rc, cmd_stdout, cmd_stderr) = runCommand(cmd)
        assert (rc == 0)
        hwsku = cmd_stdout[0]
        sku_dir = '/usr/share/sonic/device/{}/{}'.format(platform, hwsku)
        os.system('rm -rf {}'.format(sku_dir + '_dup'))
        os.system('cp -R {} {}'.format(sku_dir, sku_dir + '_dup'))

        data = self.__read_file(str(fh_before))
        fh_after = d.join("config-after.json")
        self.__write_file(str(fh_after), data)

        cfgDict = {}
        with open(str(fh_after)) as fp:
            cfgDict = json.load(fp)
            cfgDict['DEVICE_METADATA']['localhost']['hostname'] = "something"
            cfgDict['DEVICE_METADATA']['localhost']['platform'] = "invalid1"
            cfgDict['DEVICE_METADATA']['localhost']['mac'] = "invalid2"
            cfgDict['DEVICE_METADATA']['localhost']['hwsku'] = hwsku + "_dup"
        with open(str(fh_after), "w") as fp:
            json.dump(cfgDict, fp, indent=4)

        fh = d.join("input.json")
        fh.write("""
        {
            "configdb-json": {
                "clear-config": true,
                "halt-on-failure": false,
                "ignore-result": false,
                "reboot-on-failure": false,
                "reboot-on-success": false,
                "status": "BOOT",
                "timestamp": "2019-05-01 19:49:25",
                "url": {
                    "destination": "/etc/sonic/config_db.json",
                    "source": "file://%s"
                }
            }
        }
        """ % (str(fh_after)))
        configdb_json = ConfigDBJson(str(fh))
        configdb_json.main()

        fh_after = d.join("config-after.json")
        cmd = 'config save -y ' + str(fh_after)
        rc = runCommand(cmd, capture_stdout=False)
        assert (rc == 0)

        with open(str(fh_after)) as json_file:
            json_dict = json.load(json_file)
            assert (json_dict.get('DEVICE_METADATA').get('localhost').get(
                'platform') != 'invalid1')
            assert (
                json_dict.get('DEVICE_METADATA').get('localhost').get('mac') !=
                'invalid2')
            assert (json_dict.get('DEVICE_METADATA').get('localhost').get(
                'hwsku') == hwsku + "_dup")

        # Restore initial configuration
        cmd = 'config reload -y -f ' + str(fh_before)
        rc = runCommand(cmd, capture_stdout=False)
        assert (rc == 0)
        rc = runCommand('config save -y', capture_stdout=False)
    def test_json_config_valid_load(self, tmpdir):
        '''!
        Test case when the new json configuration file to apply is syntaxically valid.
        Verify that the new configuration is correctly applied using 'config load'.
        Verify that the plugin does not return with a non zero exit code.
        '''

        d = tmpdir.mkdir("valid")
        fh_before = d.join("config-before.json")
        cmd = 'config save -y ' + str(fh_before)
        rc = runCommand(cmd, capture_stdout=False)
        assert (rc == 0)

        data = self.__read_file(str(fh_before))
        fh_after = d.join("config-after.json")
        self.__write_file(str(fh_after), data)

        cfgDict = {}
        with open(str(fh_after)) as fp:
            cfgDict = json.load(fp)
            cfgDict['DEVICE_METADATA']['localhost']['hostname'] = "something"
            cfgDict['DEVICE_METADATA']['localhost']['platform'] = "invalid1"
            cfgDict['DEVICE_METADATA']['localhost']['mac'] = "invalid2"
            cfgDict['DEVICE_METADATA']['localhost']['hwsku'] = "invalid3"
        with open(str(fh_after), "w") as fp:
            json.dump(cfgDict, fp, indent=4)

        fh = d.join("input.json")
        fh.write("""
        {
            "configdb-json": {
                "clear-config": false,
                "halt-on-failure": false,
                "ignore-result": false,
                "reboot-on-failure": false,
                "reboot-on-success": false,
                "status": "BOOT",
                "timestamp": "2019-05-01 19:49:25",
                "url": {
                    "destination": "/etc/sonic/config_db.json",
                    "source": "file://%s"
                }
            }
        }
        """ % (str(fh_after)))
        configdb_json = ConfigDBJson(str(fh))
        configdb_json.main()

        fh_after = d.join("config-after.json")
        cmd = 'config save -y ' + str(fh_after)
        rc = runCommand(cmd, capture_stdout=False)
        assert (rc == 0)

        with open(str(fh_after)) as json_file:
            json_dict = json.load(json_file)
            assert (json_dict.get('DEVICE_METADATA').get('localhost').get(
                'platform') != 'invalid1')
            assert (
                json_dict.get('DEVICE_METADATA').get('localhost').get('mac') !=
                'invalid2')
            assert (
                json_dict.get('DEVICE_METADATA').get('localhost').get('hwsku')
                != 'invalid3')

        # Collect the differences between the two configurations
        cmd = "/usr/bin/diff --changed-group-format='%>' --unchanged-group-format='' " + str(
            fh_before) + ' ' + str(fh_after)
        (rc2, cmd_stdout, cmd_stderr) = runCommand(cmd)

        # Restore initial configuration
        cmd = 'config load -y ' + str(fh_before)
        rc = runCommand(cmd, capture_stdout=False)
        assert (rc == 0)