Support LABEL in docker_build rule

Allows users to associate custom metadata with docker images.

Discussion:
https://groups.google.com/d/msg/bazel-dev/FTQVg2U3CvQ/X-8RJ01_AgAJ

--
Change-Id: Ia9f6ceb1dd99aa91cf0c41f3d7afc447ab5792e5
Reviewed-on: https://bazel-review.googlesource.com/#/c/2350/
MOS_MIGRATED_REVID=110489115
diff --git a/tools/build_defs/docker/rewrite_json_test.py b/tools/build_defs/docker/rewrite_json_test.py
index 3d89d1d..d751208 100644
--- a/tools/build_defs/docker/rewrite_json_test.py
+++ b/tools/build_defs/docker/rewrite_json_test.py
@@ -556,17 +556,17 @@
     }
     name = 'deadbeef'
     parent = 'blah'
-    env = [
-        'baz=blah',
-        'foo=bar',
-    ]
+    env = {'baz': 'blah', 'foo': 'bar',}
     expected = {
         'id': name,
         'parent': parent,
         'config': {
             'User': 'mattmoor',
             'WorkingDir': '/usr/home/mattmoor',
-            'Env': env,
+            'Env': [
+                'baz=blah',
+                'foo=bar',
+            ],
         },
         'docker_version': _DOCKER_VERSION,
         'architecture': _PROCESSOR_ARCHITECTURE,
@@ -591,10 +591,7 @@
     }
     name = 'deadbeef'
     parent = 'blah'
-    env = [
-        'baz=replacement',
-        'foo=$foo:asdf',
-    ]
+    env = {'baz': 'replacement', 'foo': '$foo:asdf',}
     expected = {
         'id': name,
         'parent': parent,
@@ -616,6 +613,75 @@
         name=name, env=env, parent=parent))
     self.assertEquals(expected, actual)
 
+  def testLabel(self):
+    in_data = {
+        'config': {
+            'User': 'mattmoor',
+            'WorkingDir': '/usr/home/mattmoor'
+        }
+    }
+    name = 'deadbeef'
+    parent = 'blah'
+    labels = {'baz': 'blah', 'foo': 'bar',}
+    expected = {
+        'id': name,
+        'parent': parent,
+        'config': {
+            'User': 'mattmoor',
+            'WorkingDir': '/usr/home/mattmoor',
+            'Label': [
+                'baz=blah',
+                'foo=bar',
+            ],
+        },
+        'docker_version': _DOCKER_VERSION,
+        'architecture': _PROCESSOR_ARCHITECTURE,
+        'os': _OPERATING_SYSTEM,
+    }
+
+    actual = RewriteMetadata(in_data,
+                             MetadataOptions(name=name,
+                                             labels=labels,
+                                             parent=parent))
+    self.assertEquals(expected, actual)
+
+  def testAugmentLabel(self):
+    in_data = {
+        'config': {
+            'User': 'mattmoor',
+            'WorkingDir': '/usr/home/mattmoor',
+            'Label': [
+                'baz=blah',
+                'blah=still around',
+            ],
+        }
+    }
+    name = 'deadbeef'
+    parent = 'blah'
+    labels = {'baz': 'replacement', 'foo': 'bar',}
+    expected = {
+        'id': name,
+        'parent': parent,
+        'config': {
+            'User': 'mattmoor',
+            'WorkingDir': '/usr/home/mattmoor',
+            'Label': [
+                'baz=replacement',
+                'blah=still around',
+                'foo=bar',
+            ],
+        },
+        'docker_version': _DOCKER_VERSION,
+        'architecture': _PROCESSOR_ARCHITECTURE,
+        'os': _OPERATING_SYSTEM,
+    }
+
+    actual = RewriteMetadata(in_data,
+                             MetadataOptions(name=name,
+                                             labels=labels,
+                                             parent=parent))
+    self.assertEquals(expected, actual)
+
   def testAugmentVolumeWithNullInput(self):
     in_data = {
         'config': {