-
-
Notifications
You must be signed in to change notification settings - Fork 354
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
60 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# -*- coding: utf8 -*- | ||
# Copyright © 2015 Philippe Pepiot | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from __future__ import unicode_literals | ||
from __future__ import absolute_import | ||
|
||
from testinfra.backend import base | ||
|
||
try: | ||
import salt.client | ||
except ImportError: | ||
HAS_SALT = False | ||
else: | ||
HAS_SALT = True | ||
|
||
|
||
class SaltBackend(base.BaseBackend): | ||
|
||
def __init__(self, host, *args, **kwargs): | ||
self.host = host | ||
self._client = None | ||
super(SaltBackend, self).__init__(*args, **kwargs) | ||
|
||
@property | ||
def client(self): | ||
if self._client is None: | ||
if not HAS_SALT: | ||
raise RuntimeError( | ||
"You must install salt package to use the salt backend") | ||
self._client = salt.client.LocalClient() | ||
return self._client | ||
|
||
def run(self, command, *args): | ||
command = self.quote(command, *args) | ||
out = self.client.cmd( | ||
self.host, 'cmd.run_all', [command], | ||
)[self.host] | ||
return base.CommandResult( | ||
out['retcode'], out['stdout'], out['stderr'], command) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1fcf076
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome. Do you have an example on how to use Salt and a use case in which it makes sense?
1fcf076
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi, the salt integration is a work in progress (no docs yet).
The salt backend allow testinfra to use salt communication channel to run commands on remote hosts (minions). This have to be run from the salt master, for example:
Next I'm planning a Salt fixture (and maybe a SaltGrains too) that expose all the salt modules in testinfra, the syntax of the test file will be something like:
Under the hood testinfra will call the salt api (or run salt-call if not running under the salt backend).
The idea of salt integration in testinfra is that it is the most convenient way to write tests for salt based deployments and salt has a full and better os support than testinfra will never have :)
1fcf076
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I understand. Looks very promising 😄.
1fcf076
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Have you given any thought to using/supporting Ansible for this too?
1fcf076
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes,. But the integration may be more difficult because most of ansible modules DO things and does not expose the current state. But at least the connection module (and configuration), the "setup" module and the check mode may be convenient to write testinfra based tests.
Do you known if puppet or chef have an API (at least CLI based), to get the actual state of servers ? Any integration with infrastructure tools is good to have :)
1fcf076
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Puppet does. The Resource Abstraction Layer is accessible through
puppet resource <type> <name>
so you can do things likepuppet resource package apt
which will return this:Unfortunately it doesn't support a
--yaml
or--json
switch like Facter does so we'd have to parse the output a bit. It's easy enough to do, just a bit more work thanjson.loads()
.1fcf076
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, I've just installed puppet and run some
puppet resource
commands, looks very usefull to have as a "testinfra fixture" (and facter too).1fcf076
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Facter is very easy, all it takes is
json.loads(subprocess.check_output(['facter', '--json']))