From b0ed0bc0c04e1ec80f427041db02c1b860ecd030 Mon Sep 17 00:00:00 2001
From: Lukasz Nowak <luke@nexedi.com>
Date: Thu, 14 Feb 2019 13:57:49 +0100
Subject: [PATCH] caddy-frontend/test: Assert file existence before rewrite

---
 software/caddy-frontend/test/test.py | 42 +++++++++++++++-------------
 1 file changed, 22 insertions(+), 20 deletions(-)

diff --git a/software/caddy-frontend/test/test.py b/software/caddy-frontend/test/test.py
index ce2031d78..f2639e074 100644
--- a/software/caddy-frontend/test/test.py
+++ b/software/caddy-frontend/test/test.py
@@ -2411,9 +2411,9 @@ http://apachecustomhttpsaccepted.example.com:%%(http_port)s {
     self.assertEqual(httplib.NOT_FOUND, result_http.status_code)
 
     # rewrite SR/bin/is-icmp-packet-lost
-    open(
-      os.path.join(self.software_path, 'bin', 'is-icmp-packet-lost'), 'w'
-    ).write('echo "$@"')
+    fname = os.path.join(self.software_path, 'bin', 'is-icmp-packet-lost')
+    self.assertTrue(os.path.isfile(fname))
+    open(fname, 'w').write('echo "$@"')
     # call the monitor for this partition
     monitor_file = glob.glob(
       os.path.join(
@@ -2453,9 +2453,9 @@ http://apachecustomhttpsaccepted.example.com:%%(http_port)s {
     self.assertEqual(httplib.NOT_FOUND, result_http.status_code)
 
     # rewrite SR/bin/is-icmp-packet-lost
-    open(
-      os.path.join(self.software_path, 'bin', 'is-icmp-packet-lost'), 'w'
-    ).write('echo "$@"')
+    fname = os.path.join(self.software_path, 'bin', 'is-icmp-packet-lost')
+    self.assertTrue(os.path.isfile(fname))
+    open(fname, 'w').write('echo "$@"')
     # call the monitor for this partition
     monitor_file = glob.glob(
       os.path.join(
@@ -2495,10 +2495,10 @@ http://apachecustomhttpsaccepted.example.com:%%(http_port)s {
     self.assertEqual(httplib.NOT_FOUND, result_http.status_code)
 
     # rewrite SR/bin/is-icmp-packet-lost
-    open(
-      os.path.join(
-        self.software_path, 'bin', 'check-re6st-optimal-status'), 'w'
-    ).write('echo "$@"')
+    fname = os.path.join(
+      self.software_path, 'bin', 'check-re6st-optimal-status')
+    self.assertTrue(os.path.isfile(fname))
+    open(fname, 'w').write('echo "$@"')
     # call the monitor for this partition
     monitor_file = glob.glob(
       os.path.join(
@@ -3836,10 +3836,10 @@ https://www.google.com {}""",
     self.assertEqual(httplib.NOT_FOUND, result.status_code)
 
     # rewrite SR/bin/is-icmp-packet-lost
-    open(
-      os.path.join(
-        self.software_path, 'bin', 'check-re6st-optimal-status'), 'w'
-    ).write('echo "$@"')
+    fname = os.path.join(
+      self.software_path, 'bin', 'check-re6st-optimal-status')
+    self.assertTrue(os.path.isfile(fname))
+    open(fname, 'w').write('echo "$@"')
     # call the monitor for this partition
     monitor_file = glob.glob(
       os.path.join(
@@ -4030,9 +4030,10 @@ https://www.google.com {}""",
     self.assertEqual(httplib.NOT_FOUND, result_http.status_code)
 
     # rewrite SR/bin/is-icmp-packet-lost
-    open(
-      os.path.join(self.software_path, 'bin', 'is-icmp-packet-lost'), 'w'
-    ).write('echo "$@"')
+    fname = os.path.join(
+      self.software_path, 'bin', 'is-icmp-packet-lost')
+    self.assertTrue(os.path.isfile(fname))
+    open(fname, 'w').write('echo "$@"')
     # call the monitor for this partition
     monitor_file = glob.glob(
       os.path.join(
@@ -4072,9 +4073,10 @@ https://www.google.com {}""",
     self.assertEqual(httplib.NOT_FOUND, result_http.status_code)
 
     # rewrite SR/bin/is-icmp-packet-lost
-    open(
-      os.path.join(self.software_path, 'bin', 'is-icmp-packet-lost'), 'w'
-    ).write('echo "$@"')
+    fname = os.path.join(
+      self.software_path, 'bin', 'is-icmp-packet-lost')
+    self.assertTrue(os.path.isfile(fname))
+    open(fname, 'w').write('echo "$@"')
     # call the monitor for this partition
     monitor_file = glob.glob(
       os.path.join(
-- 
2.30.9