SLIDER-337 Check if the OS is known before accessing it.

There are a few providers for some common OSes. Using an
OS that isn't listed in this provider dict failed with a
KeyError before it could fall back to the default case.


Project: http://git-wip-us.apache.org/repos/asf/incubator-slider/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-slider/commit/5d2f6264
Tree: http://git-wip-us.apache.org/repos/asf/incubator-slider/tree/5d2f6264
Diff: http://git-wip-us.apache.org/repos/asf/incubator-slider/diff/5d2f6264

Branch: refs/heads/feature/SLIDER-151_REST_API
Commit: 5d2f62642ac6aeeb0b64965b36ab52bae5fc0a19
Parents: 0b40acd
Author: Josh Elser <els...@apache.org>
Authored: Wed Aug 20 13:51:24 2014 -0400
Committer: Josh Elser <els...@apache.org>
Committed: Wed Aug 20 19:31:27 2014 -0400

----------------------------------------------------------------------
 .../core/providers/__init__.py                  | 19 +++++++-----
 .../python/resource_management/TestPackage.py   | 32 ++++++++++++++++++++
 2 files changed, 43 insertions(+), 8 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-slider/blob/5d2f6264/slider-agent/src/main/python/resource_management/core/providers/__init__.py
----------------------------------------------------------------------
diff --git 
a/slider-agent/src/main/python/resource_management/core/providers/__init__.py 
b/slider-agent/src/main/python/resource_management/core/providers/__init__.py
index 630183b..3b453f2 100644
--- 
a/slider-agent/src/main/python/resource_management/core/providers/__init__.py
+++ 
b/slider-agent/src/main/python/resource_management/core/providers/__init__.py
@@ -72,14 +72,7 @@ PROVIDERS = dict(
 
 def find_provider(env, resource, class_path=None):
   if not class_path:
-    providers = [PROVIDERS, LIBRARY_PROVIDERS]
-    for provider in providers:
-      if resource in provider[env.system.os_family]:
-        class_path = provider[env.system.os_family][resource]
-        break
-      if resource in provider["default"]:
-        class_path = provider["default"][resource]
-        break
+    class_path = get_class_path(env.system.os_family, resource)
 
   try:
     mod_path, class_name = class_path.rsplit('.', 1)
@@ -87,3 +80,13 @@ def find_provider(env, resource, class_path=None):
     raise Fail("Unable to find provider for %s as %s" % (resource, class_path))
   mod = __import__(mod_path, {}, {}, [class_name])
   return getattr(mod, class_name)
+
+def get_class_path(os, resource):
+  providers = [PROVIDERS, LIBRARY_PROVIDERS]
+  for provider in providers:
+    if os in provider:
+      if resource in provider[os]:
+        return provider[os][resource]
+    if resource in provider["default"]:
+      return provider["default"][resource]
+  return None

http://git-wip-us.apache.org/repos/asf/incubator-slider/blob/5d2f6264/slider-agent/src/test/python/resource_management/TestPackage.py
----------------------------------------------------------------------
diff --git a/slider-agent/src/test/python/resource_management/TestPackage.py 
b/slider-agent/src/test/python/resource_management/TestPackage.py
new file mode 100644
index 0000000..fe03ccf
--- /dev/null
+++ b/slider-agent/src/test/python/resource_management/TestPackage.py
@@ -0,0 +1,32 @@
+'''
+Licensed to the Apache Software Foundation (ASF) under one
+or more contributor license agreements.  See the NOTICE file
+distributed with this work for additional information
+regarding copyright ownership.  The ASF licenses this file
+to you 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 unittest import TestCase
+from mock.mock import patch, MagicMock
+
+from resource_management.core import Environment, Fail
+from resource_management.core.system import System
+from resource_management.core.resources import Package
+
+from resource_management.core.providers import get_class_path, PROVIDERS
+
+class TestPackage(TestCase):
+  
+  def test_default_provider(self):
+    self.assertEquals(PROVIDERS['default']['Tarball'], 
get_class_path('something unknown', 'Tarball'))
+

Reply via email to