Skip to content

Commit

Permalink
and check rm-label
Browse files Browse the repository at this point in the history
  • Loading branch information
gdraheim committed Apr 28, 2018
1 parent 83473b9 commit d9d972d
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ descriptive (likeSQL).
./docker-copyedit.py FROM image1 INTO image2 -vv \
set shell cmd "/entrypoint.sh foo"
./docker-copyedit.py FROM image1 INTO image2 -vv \
set label author "real me"
set label author "real me" and rm label oldie

./docker-copyedit.py FROM image1 INTO image2 -vv \
REMOVE PORT 4444
Expand Down
79 changes: 78 additions & 1 deletion docker-copyedit-tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1402,7 +1402,7 @@ def test_850_change_arch(self):
#
self.assertEqual(dat1[0]["Architecture"], u"amd64")
self.assertEqual(dat2[0]["Architecture"], u"i386")
def test_890_change_license_label(self):
def test_900_change_license_label(self):
img = IMG
testname = self.testname()
testdir = self.testdir()
Expand Down Expand Up @@ -1441,6 +1441,83 @@ def test_890_change_license_label(self):
#
self.assertEqual(dat1[0]["Config"]["Labels"]["license"], u"free")
self.assertEqual(dat2[0]["Config"]["Labels"]["license"], u"LGPLv2")
def test_910_change_info_label(self):
img = IMG
testname = self.testname()
testdir = self.testdir()
text_file(os_path(testdir, "Dockerfile"),"""
FROM centos:centos7
RUN { echo "#! /bin/sh"; echo "exec sleep 4"; } > /entrypoint.sh
RUN chmod 0700 /entrypoint.sh
LABEL info free
CMD ["/entrypoint.sh"]
""")
cmd = "docker build {testdir} -t {img}:{testname}"
run = sh(cmd.format(**locals()))
logg.info("%s\n%s", run.stdout, run.stderr)
#
cmd = "docker inspect {img}:{testname}"
run = sh(cmd.format(**locals()))
data = json.loads(run.stdout)
logg.info("LABELS:\n%s", data[0]["Config"]["Labels"])
logg.info("{testname} Info = %s", data[0]["Config"]["Labels"]["info"])
dat1 = data
#
cmd = "./docker-copyedit.py FROM {img}:{testname} INTO {img}:{testname}x SET LABEL info new -vv"
run = sh(cmd.format(**locals()))
logg.info("%s\n%s\n%s", cmd, run.stdout, run.stderr)
#
cmd = "docker inspect {img}:{testname}x"
run = sh(cmd.format(**locals()))
data = json.loads(run.stdout)
logg.debug("CONFIG:\n%s", data[0]["Config"])
dat2 = data
#
cmd = "docker rmi {img}:{testname} {img}:{testname}x"
run = sh(cmd.format(**locals()))
logg.info("[%s] %s", run.returncode, cmd.format(**locals()))
#
self.assertEqual(dat1[0]["Config"]["Labels"]["info"], u"free")
self.assertEqual(dat2[0]["Config"]["Labels"]["info"], u"new")
def test_920_remove_other_label(self):
img = IMG
testname = self.testname()
testdir = self.testdir()
text_file(os_path(testdir, "Dockerfile"),"""
FROM centos:centos7
RUN { echo "#! /bin/sh"; echo "exec sleep 4"; } > /entrypoint.sh
RUN chmod 0700 /entrypoint.sh
LABEL info free
LABEL other text
CMD ["/entrypoint.sh"]
""")
cmd = "docker build {testdir} -t {img}:{testname}"
run = sh(cmd.format(**locals()))
logg.info("%s\n%s", run.stdout, run.stderr)
#
cmd = "docker inspect {img}:{testname}"
run = sh(cmd.format(**locals()))
data = json.loads(run.stdout)
logg.info("LABELS:\n%s", data[0]["Config"]["Labels"])
logg.info("{testname} Info = %s", data[0]["Config"]["Labels"]["info"])
dat1 = data
#
cmd = "./docker-copyedit.py FROM {img}:{testname} INTO {img}:{testname}x REMOVE LABEL other -vv"
run = sh(cmd.format(**locals()))
logg.info("%s\n%s\n%s", cmd, run.stdout, run.stderr)
#
cmd = "docker inspect {img}:{testname}x"
run = sh(cmd.format(**locals()))
data = json.loads(run.stdout)
logg.debug("CONFIG:\n%s", data[0]["Config"])
dat2 = data
#
cmd = "docker rmi {img}:{testname} {img}:{testname}x"
run = sh(cmd.format(**locals()))
logg.info("[%s] %s", run.returncode, cmd.format(**locals()))
#
self.assertEqual(dat1[0]["Config"]["Labels"]["other"], u"text")
self.assertEqual(dat2[0]["Config"]["Labels"].get("other", "<nonexistant>"), u"<nonexistant>")

if __name__ == "__main__":
## logging.basicConfig(level = logging.INFO)
Expand Down
22 changes: 19 additions & 3 deletions docker-copyedit.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,14 @@ def edit_datadir(datadir, out, edits):
logg.warning("done edit %s %s", action, arg)
except KeyError, e:
logg.warning("there was no '%s' in %s", key, config_filename)
if action in ["remove-label", "rm-label"]:
key = "Labels"
try:
if key in config[CONFIG]:
del config[CONFIG][key][target]
logg.warning("done actual %s %s '%s'", action, target, arg)
except KeyError, e:
logg.warning("there was no label %s in %s", target, config_filename)
if action in ["set-label"]:
key = "Labels"
try:
Expand Down Expand Up @@ -355,18 +363,26 @@ def parsing(args):
for n in xrange(len(args)):
arg = args[n]
if target is not None:
commands.append((action, target, arg))
action, target = None, None
continue
commands.append((action, target, arg))
action, target = None, None
continue
if action is None:
if arg in ["and", "+", ",", "/"]:
continue
action = arg.lower()
continue
if action in ["rm-label", "remove-label"]:
target = arg
commands.append((action, target, None))
action, target = None, None
continue
#
if action in ["set"] and arg.lower() in ["shell", "label"]:
action = "%s-%s" % (action, arg.lower())
continue
if action in ["rm", "remove"] and arg.lower() in ["label"]:
action = "%s-%s" % (action, arg.lower())
continue
if action in ["from"]:
inp = arg
action = None
Expand Down

0 comments on commit d9d972d

Please sign in to comment.