Adds more docker py test

Addes more docker py test
Optimize test to import images from cache
Rename test class and dir for python unittest framework

Signed-off-by: Sujil02 <sushah@redhat.com>
This commit is contained in:
Sujil02
2020-06-11 09:18:52 -04:00
parent 3f026eb6a6
commit 4a3f3b5c02
9 changed files with 282 additions and 53 deletions

View File

@ -739,4 +739,23 @@ var _ = Describe("Podman containers ", func() {
//Expect(code).To(BeNumerically("==", http.StatusInternalServerError)) //Expect(code).To(BeNumerically("==", http.StatusInternalServerError))
}) })
It("List containers with filters", func() {
var name = "top"
var name2 = "top2"
cid, err := bt.RunTopContainer(&name, bindings.PFalse, nil)
Expect(err).To(BeNil())
_, err = bt.RunTopContainer(&name2, bindings.PFalse, nil)
Expect(err).To(BeNil())
s := specgen.NewSpecGenerator(alpine.name, false)
s.Terminal = true
s.Command = []string{"date", "-R"}
_, err = containers.CreateWithSpec(bt.conn, s)
Expect(err).To(BeNil())
// Validate list container with id filter
filters := make(map[string][]string)
filters["id"] = []string{cid}
c, err := containers.List(bt.conn, filters, bindings.PTrue, nil, nil, nil, nil)
Expect(err).To(BeNil())
Expect(len(c)).To(Equal(1))
})
}) })

View File

@ -1,46 +0,0 @@
import unittest
import docker
import requests
import os
from docker import Client
from . import constant
from . import common
client = common.get_client()
class TestContainers(unittest.TestCase):
podman = None
def setUp(self):
super().setUp()
common.run_top_container()
def tearDown(self):
common.remove_all_containers()
common.remove_all_images()
return super().tearDown()
@classmethod
def setUpClass(cls):
super().setUpClass()
common.enable_sock(cls)
@classmethod
def tearDownClass(cls):
common.terminate_connection(cls)
return super().tearDownClass()
def test_inspect_container(self):
# Inspect bogus container
with self.assertRaises(requests.HTTPError):
client.inspect_container("dummy")
# Inspect valid container
container = client.inspect_container(constant.TOP)
self.assertIn(constant.TOP , container["Name"])
if __name__ == '__main__':
# Setup temporary space
unittest.main()

View File

@ -23,13 +23,30 @@ def podman():
binary = "bin/podman" binary = "bin/podman"
return binary return binary
def restore_image_from_cache(): def restore_image_from_cache(TestClass):
client.load_image(constant.ImageCacheDir+alpineDict["tarballName"]) alpineImage = os.path.join(constant.ImageCacheDir , alpineDict["tarballName"])
if not os.path.exists(alpineImage):
os.makedirs(constant.ImageCacheDir)
client.pull(constant.ALPINE)
response = client.get_image(constant.ALPINE)
image_tar = open(alpineImage,mode="wb")
image_tar.write(response.data)
image_tar.close()
else :
TestClass.podman = subprocess.run(
[
podman(), "load", "-i", alpineImage
],
shell=False,
stdin=subprocess.DEVNULL,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)
def run_top_container(): def run_top_container():
client.pull(constant.ALPINE) c = client.create_container(image=constant.ALPINE,command='/bin/sleep 5',name=constant.TOP)
c = client.create_container(constant.ALPINE,name=constant.TOP)
client.start(container=c.get("Id")) client.start(container=c.get("Id"))
return c.get("Id")
def enable_sock(TestClass): def enable_sock(TestClass):
TestClass.podman = subprocess.Popen( TestClass.podman = subprocess.Popen(

View File

@ -0,0 +1,194 @@
import unittest
import docker
import requests
import os
from docker import Client
from . import constant
from . import common
import time
client = common.get_client()
class TestContainers(unittest.TestCase):
podman = None
topContainerId = ""
def setUp(self):
super().setUp()
common.restore_image_from_cache(self)
TestContainers.topContainerId = common.run_top_container()
def tearDown(self):
common.remove_all_containers()
common.remove_all_images()
return super().tearDown()
@classmethod
def setUpClass(cls):
super().setUpClass()
common.enable_sock(cls)
@classmethod
def tearDownClass(cls):
common.terminate_connection(cls)
return super().tearDownClass()
def test_inspect_container(self):
# Inspect bogus container
with self.assertRaises(requests.HTTPError) as error:
client.inspect_container("dummy")
self.assertEqual(error.exception.response.status_code, 404)
# Inspect valid container by name
container = client.inspect_container(constant.TOP)
self.assertIn(TestContainers.topContainerId , container["Id"])
# Inspect valid container by Id
container = client.inspect_container(TestContainers.topContainerId)
self.assertIn(constant.TOP , container["Name"])
def test_create_container(self):
# Run a container with detach mode
container = client.create_container(image="alpine", detach=True)
self.assertEqual(len(container),2)
def test_start_container(self):
# Start bogus container
with self.assertRaises(requests.HTTPError) as error:
client.start("dummy")
self.assertEqual(error.exception.response.status_code, 404)
# Podman docs says it should give a 304 but returns with no response
# # Start a already started container should return 304
# response = client.start(container=TestContainers.topContainerId)
# self.assertEqual(error.exception.response.status_code, 304)
# Create a new container and validate the count
client.create_container(image=constant.ALPINE,name="container2")
containers = client.containers(quiet=True,all=True)
self.assertEqual(len(containers),2)
def test_stop_container(self):
# Stop bogus container
with self.assertRaises(requests.HTTPError) as error:
client.stop("dummy")
self.assertEqual(error.exception.response.status_code, 404)
# Validate the container state
container = client.inspect_container(constant.TOP)
self.assertEqual(container["State"]["Status"], "running")
# Stop a running container and validate the state
client.stop(TestContainers.topContainerId)
container = client.inspect_container(constant.TOP)
self.assertIn(container["State"]["Status"],"stopped exited",)
def test_restart_container(self):
# Restart bogus container
with self.assertRaises(requests.HTTPError) as error:
client.restart("dummy")
self.assertEqual(error.exception.response.status_code, 404)
# Validate the container state
client.stop(TestContainers.topContainerId)
container = client.inspect_container(constant.TOP)
self.assertEqual(container["State"]["Status"], "stopped")
# restart a running container and validate the state
client.restart(TestContainers.topContainerId)
container = client.inspect_container(constant.TOP)
self.assertEqual(container["State"]["Status"], "running")
def test_remove_container(self):
# Remove bogus container
with self.assertRaises(requests.HTTPError) as error:
client.remove_container("dummy")
self.assertEqual(error.exception.response.status_code, 404)
# Remove container by ID with force
client.remove_container(TestContainers.topContainerId, force=True)
containers = client.containers()
self.assertEqual(len(containers),0)
def test_remove_container_without_force(self):
# Validate current container count
containers = client.containers()
self.assertTrue(len(containers),1)
# Remove running container should throw error
with self.assertRaises(requests.HTTPError) as error:
client.remove_container(TestContainers.topContainerId)
self.assertEqual(error.exception.response.status_code, 500)
# Remove container by ID with force
client.stop(TestContainers.topContainerId)
client.remove_container(TestContainers.topContainerId)
containers = client.containers()
self.assertEqual(len(containers),0)
def test_pause_container(self):
# Pause bogus container
with self.assertRaises(requests.HTTPError) as error:
client.pause("dummy")
self.assertEqual(error.exception.response.status_code, 404)
# Validate the container state
container = client.inspect_container(constant.TOP)
self.assertEqual(container["State"]["Status"], "running")
# Pause a running container and validate the state
client.pause(container)
container = client.inspect_container(constant.TOP)
self.assertEqual(container["State"]["Status"], "paused")
def test_pause_stoped_container(self):
# Stop the container
client.stop(TestContainers.topContainerId)
# Pause exited container should trow error
with self.assertRaises(requests.HTTPError) as error:
client.pause(TestContainers.topContainerId)
self.assertEqual(error.exception.response.status_code, 500)
def test_unpause_container(self):
# Unpause bogus container
with self.assertRaises(requests.HTTPError) as error:
client.unpause("dummy")
self.assertEqual(error.exception.response.status_code, 404)
# Validate the container state
client.pause(TestContainers.topContainerId)
container = client.inspect_container(constant.TOP)
self.assertEqual(container["State"]["Status"], "paused")
# Pause a running container and validate the state
client.unpause(TestContainers.topContainerId)
container = client.inspect_container(constant.TOP)
self.assertEqual(container["State"]["Status"], "running")
def test_list_container(self):
# Add container and validate the count
client.create_container(image="alpine", detach=True)
containers = client.containers(all=True)
self.assertEqual(len(containers),2)
# Not working for now......checking
# # List container with filter by id
# filters = {'id':TestContainers.topContainerId}
# filteredContainers = client.containers(all=True,filters = filters)
# self.assertEqual(len(filteredContainers) , 1)
# # List container with filter by name
# filters = {'name':constant.TOP}
# filteredContainers = client.containers(all=True,filters = filters)
# self.assertEqual(len(filteredContainers) , 1)
@unittest.skip("Not Supported yet")
def test_rename_container(self):
# rename bogus container
with self.assertRaises(requests.HTTPError) as error:
client.rename(container="dummy", name="newname")
self.assertEqual(error.exception.response.status_code, 404)

View File

@ -14,7 +14,7 @@ class TestImages(unittest.TestCase):
podman = None podman = None
def setUp(self): def setUp(self):
super().setUp() super().setUp()
client.pull(constant.ALPINE) common.restore_image_from_cache(self)
def tearDown(self): def tearDown(self):
common.remove_all_images() common.remove_all_images()
@ -73,9 +73,8 @@ class TestImages(unittest.TestCase):
self.assertEqual(len(allImages), 1) self.assertEqual(len(allImages), 1)
# Add more images # Add more images
client.pull(constant.BB) client.pull(constant.BB)
client.pull(constant.NGINX)
allImages = client.images() allImages = client.images()
self.assertEqual(len(allImages) , 3) self.assertEqual(len(allImages) , 2)
# List images with filter # List images with filter

View File

@ -0,0 +1,46 @@
import unittest
import docker
from docker import Client
from . import constant
from . import common
client = common.get_client()
class TestInfo_Version(unittest.TestCase):
podman = None
topContainerId = ""
def setUp(self):
super().setUp()
common.restore_image_from_cache(self)
TestInfo_Version.topContainerId = common.run_top_container()
def tearDown(self):
common.remove_all_containers()
common.remove_all_images()
return super().tearDown()
@classmethod
def setUpClass(cls):
super().setUpClass()
common.enable_sock(cls)
@classmethod
def tearDownClass(cls):
common.terminate_connection(cls)
return super().tearDownClass()
def test_Info(self):
self.assertIsNotNone(client.info())
def test_info_container_details(self):
info = client.info()
self.assertEqual(info["Containers"],1)
client.create_container(image=constant.ALPINE)
info = client.info()
self.assertEqual(info["Containers"],2)
def test_version(self):
self.assertIsNotNone(client.version())