def init(mode=None, **kwargs): """Wrapper to create the API object you need to acces the CCU API. By default it detects whether or not this code is being executed on the CCU or on another system. And initializes either a LocalAPI() object when run directly on a CCU or, in all other cases, a RemoteAPI() object. This object is then being returned. You can provide the mode argument to disable auto detection and either set it to "local" to enforce a LocalAPI() object to be created or "remote" to enforce a RemoteAPI() object. In case a RemoteAPI() object is being created, you need to provide at least the additional keyword arguments address="http://[HOST]" which needs to contain the base URL to your CCU together with credentials=("[USER]", "PASSWORD") which must be valid credentials to authenticate with the CCU. """ if mode is None: mode = utils.is_ccu() and "local" or "remote" if mode == "local": if not utils.is_ccu(): raise PMException("local mode can only be used on the CCU.") return LocalAPI() elif mode == "remote": try: return RemoteAPI(**kwargs) except TypeError as e: raise PMException( "You need to provide at least the address and credentials " "to access your CCU (%s)." % e) else: raise PMException( "Invalid mode given. Valid ones are \"local\" and \"remote\".")
def init(mode=None, **kwargs): """Wrapper to create the API object you need to acces the CCU API. By default it detects whether or not this code is being executed on the CCU or on another system. And initializes either a LocalAPI() object when run directly on a CCU or, in all other cases, a RemoteAPI() object. This object is then being returned. You can provide the mode argument to disable auto detection and either set it to "local" to enforce a LocalAPI() object to be created or "remote" to enforce a RemoteAPI() object. In case a RemoteAPI() object is being created, you need to provide at least the additional keyword arguments address="http://[HOST]" which needs to contain the base URL to your CCU together with credentials=("[USER]", "PASSWORD") which must be valid credentials to authenticate with the CCU. """ if mode is None: mode = utils.is_ccu() and "local" or "remote" if mode == "local": if not utils.is_ccu(): raise PMException("local mode can only be used on the CCU.") return LocalAPI() elif mode == "remote": try: return RemoteAPI(**kwargs) except TypeError as e: raise PMException("You need to provide at least the address and credentials " "to access your CCU (%s)." % e) else: raise PMException("Invalid mode given. Valid ones are \"local\" and \"remote\".")
def test_is_ccu(monkeypatch): def no_ccu_os_release(x): return StringIO( "PRETTY_NAME=\"Debian GNU/Linux 8 (jessie)\"\n" "NAME=\"Debian GNU/Linux\"\n" "VERSION_ID=\"8\"\n" "VERSION=\"8 (jessie)\"\n" "ID=debian\n" "HOME_URL=\"http://www.debian.org/\"\n" "SUPPORT_URL=\"http://www.debian.org/support/\"\n" "BUG_REPORT_URL=\"https://bugs.debian.org/\"\n" ) def ccu_os_release(x): return StringIO( "NAME=Buildroot\n" "VERSION=2015.08.1\n" "ID=buildroot\n" "VERSION_ID=2015.08.1\n" "PRETTY_NAME=\"Buildroot 2015.08.1\"" ) def no_os_release(x): raise IOError("bla") monkeypatch.setattr(builtins, "open", no_os_release) monkeypatch.setattr(platform, "uname", lambda: ( 'Linux', 'dev', '3.16.0-4-amd64', '#1 SMP Debian 3.16.7-ckt9-3~deb8u1 (2015-04-24)', 'x86_64')) assert utils.is_ccu() == False monkeypatch.setattr(builtins, "open", no_ccu_os_release) monkeypatch.setattr(platform, "uname", lambda: ( 'Linux', 'dev', '3.16.0-4-amd64', '#1 SMP Debian 3.16.7-ckt9-3~deb8u1 (2015-04-24)', 'x86_64')) assert utils.is_ccu() == False monkeypatch.setattr(platform, "uname", lambda: ( 'Darwin', 'hubert.local', '11.4.2', 'Darwin Kernel Version 11.4.2: Thu Aug 23 16:25:48 PDT 2012; ' +'root:xnu-1699.32.7~1/RELEASE_X86_64', 'x86_64', 'i386')) assert utils.is_ccu() == False monkeypatch.setattr(platform, "uname", lambda: ( 'Windows', 'dhellmann', '2008ServerR2', '6.1.7600', 'AMD64', 'Intel64 Family 6 Model 15 Stepping 11, GenuineIntel')) assert utils.is_ccu() == False monkeypatch.setattr(builtins, "open", ccu_os_release) monkeypatch.setattr(platform, "uname", lambda: ( 'Linux', 'dev', '3.16.0-4-amd64', '#1 SMP Debian 3.16.7-ckt9-3~deb8u1 (2015-04-24)', 'x86_64')) assert utils.is_ccu() == True monkeypatch.setattr(platform, "uname", lambda: ( 'Linux', 'ccu', '3.4.11.ccu2', '#1 PREEMPT Fri Oct 16 10:43:35 CEST 2015', 'armv5tejl')) assert utils.is_ccu() == True monkeypatch.setattr(platform, "uname", lambda: ( 'Linux', 'ccu2', '3.4.11.ccu2', '#1 PREEMPT Wed Dec 16 09:23:30 CET 2015', 'armv5tejl')) assert utils.is_ccu() == True
def test_is_ccu(monkeypatch): def no_ccu_os_release(x): return StringIO("PRETTY_NAME=\"Debian GNU/Linux 8 (jessie)\"\n" "NAME=\"Debian GNU/Linux\"\n" "VERSION_ID=\"8\"\n" "VERSION=\"8 (jessie)\"\n" "ID=debian\n" "HOME_URL=\"http://www.debian.org/\"\n" "SUPPORT_URL=\"http://www.debian.org/support/\"\n" "BUG_REPORT_URL=\"https://bugs.debian.org/\"\n") def ccu_os_release(x): return StringIO("NAME=Buildroot\n" "VERSION=2015.08.1\n" "ID=buildroot\n" "VERSION_ID=2015.08.1\n" "PRETTY_NAME=\"Buildroot 2015.08.1\"") def no_os_release(x): raise IOError("bla") monkeypatch.setattr(builtins, "open", no_os_release) monkeypatch.setattr( platform, "uname", lambda: ('Linux', 'dev', '3.16.0-4-amd64', '#1 SMP Debian 3.16.7-ckt9-3~deb8u1 (2015-04-24)', 'x86_64')) assert utils.is_ccu() == False monkeypatch.setattr(builtins, "open", no_ccu_os_release) monkeypatch.setattr( platform, "uname", lambda: ('Linux', 'dev', '3.16.0-4-amd64', '#1 SMP Debian 3.16.7-ckt9-3~deb8u1 (2015-04-24)', 'x86_64')) assert utils.is_ccu() == False monkeypatch.setattr( platform, "uname", lambda: ('Darwin', 'hubert.local', '11.4.2', 'Darwin Kernel Version 11.4.2: Thu Aug 23 16:25:48 PDT 2012; ' + 'root:xnu-1699.32.7~1/RELEASE_X86_64', 'x86_64', 'i386')) assert utils.is_ccu() == False monkeypatch.setattr( platform, "uname", lambda: ('Windows', 'dhellmann', '2008ServerR2', '6.1.7600', 'AMD64', 'Intel64 Family 6 Model 15 Stepping 11, GenuineIntel')) assert utils.is_ccu() == False monkeypatch.setattr(builtins, "open", ccu_os_release) monkeypatch.setattr( platform, "uname", lambda: ('Linux', 'dev', '3.16.0-4-amd64', '#1 SMP Debian 3.16.7-ckt9-3~deb8u1 (2015-04-24)', 'x86_64')) assert utils.is_ccu() == True monkeypatch.setattr( platform, "uname", lambda: ('Linux', 'ccu', '3.4.11.ccu2', '#1 PREEMPT Fri Oct 16 10:43:35 CEST 2015', 'armv5tejl')) assert utils.is_ccu() == True monkeypatch.setattr( platform, "uname", lambda: ('Linux', 'ccu2', '3.4.11.ccu2', '#1 PREEMPT Wed Dec 16 09:23:30 CET 2015', 'armv5tejl')) assert utils.is_ccu() == True