Python:使用os函数访问/ sbin

时间:2011-06-30 15:13:26

标签: python mocking

我正在尝试模拟ifconfig,以便测试一些依赖于控制器以太网连接状态的代码。 (我正在使用生菜测试框架。)这是我的代码:

    script = "#!/bin/sh\n" + \
             "echo " + link + "\t" + address
    rename("/sbin/ifconfig", "/sbin/ifconfig_real")
    bash = open(dir+"ifconfig", 'w')
    bash.write(script)
    bash.close()
    chmod("/sbin/ifconfig", 755)

当我运行生菜时,我收到此错误:

Traceback (most recent call last):
      File "/usr/local/lib/python2.7/dist-packages/lettuce/core.py", line 113, in __call__
        ret = self.function(self.step, *args, **kw)
      File        "/home/matthew/Documents/ParkAssist/m3/lettuce_tests/test_commission/features/POSTsteps.py",  line 131, in find_static_or_dhcpip
        ifconfig_setup('eth0', '169.254.13.11')
      File "/home/matthew/Documents/ParkAssist/m3/lettuce_tests/test_commission/features/POSTsteps.py", line 52, in ifconfig_setup
        rename("/sbin/ifconfig", "/sbin/ifconfig_real")
    OSError: [Errno 2] No such file or directory

稍后调用以删除模拟文件的函数会产生相同的错误。当我在Python终端中运行它时,这个代码运行正常,以sudo身份运行。我认为它与Python无法访问/ sbin文件夹有关。但是我把莴苣当作sudo,所以我不确定。

0 个答案:

没有答案