[autotest] Add _get_label_action()
BUG=chromium:707999
TEST=Run unit tests
Change-Id: I901fdf3ebb206c7e77716658a6656740c04be616
Reviewed-on: https://chromium-review.googlesource.com/446879
Commit-Ready: Allen Li <ayatane@chromium.org>
Tested-by: Allen Li <ayatane@chromium.org>
Reviewed-by: Allen Li <ayatane@chromium.org>
diff --git a/server/cros/provision.py b/server/cros/provision.py
index db73d13..2039abc 100644
--- a/server/cros/provision.py
+++ b/server/cros/provision.py
@@ -2,7 +2,7 @@
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
-
+import collections
import re
import sys
import warnings
@@ -31,6 +31,26 @@
FLAKY_DEVSERVER_ATTEMPTS = 2
+_Action = collections.namedtuple('_Action', 'name, value')
+
+
+def _get_label_action(str_label):
+ """Get action represented by the label.
+
+ This is used for determine actions to perform based on labels, for
+ example for provisioning or repair.
+
+ @param str_label: label string
+ @returns: _Action instance
+ """
+ try:
+ keyval_label = labellib.parse_keyval_label(str_label)
+ except ValueError:
+ return _Action(str_label, None)
+ else:
+ return _Action(keyval_label.key, keyval_label.value)
+
+
### Helpers to convert value to label
def get_version_label_prefix(image):
"""
diff --git a/server/cros/provision_unittest.py b/server/cros/provision_unittest.py
old mode 100644
new mode 100755
index f3abd40..8388c20
--- a/server/cros/provision_unittest.py
+++ b/server/cros/provision_unittest.py
@@ -1,3 +1,4 @@
+#!/usr/bin/python2.7
# Copyright 2017 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
@@ -14,6 +15,23 @@
_ANDROID_VERSION_SAMPLES[0] + '#4'
]
+
+class ActionTestCase(unittest.TestCase):
+ """Tests for Action functions."""
+
+ def test__get_label_action_with_keyval_label(self):
+ got = provision._get_label_action('cros-version:foo')
+ self.assertEqual(got, provision._Action('cros-version', 'foo'))
+
+ def test__get_label_action_with_plain_label(self):
+ got = provision._get_label_action('webcam')
+ self.assertEqual(got, provision._Action('webcam', None))
+
+ def test__get_label_action_with_empty_string(self):
+ got = provision._get_label_action('')
+ self.assertEqual(got, provision._Action('', None))
+
+
class ImageParsingTests(unittest.TestCase):
"""Unit tests for `provision.get_version_label_prefix()`."""