Skip to content

Instantly share code, notes, and snippets.

@LiamJolly
LiamJolly / main.py
Created November 18, 2021 10:15
03-volumes
from docker.models.volumes import Volume
from docker_manager import DockerManager
if __name__ == "__main__":
VOLUME_NAME = "TestVolume"
docker_manager = DockerManager()
print("Creating volume...")
docker_manager.create_volume(VOLUME_NAME)
print("List all volumes...")
@LiamJolly
LiamJolly / docker_manager.py
Created November 18, 2021 10:08
03-volumes
def create_volume(self, name, driver="local", driver_opts=None, labels=None):
"""
Create a volume
:param name: The name of the volume
:param driver: The type of driver to use
:param driver_opts: Additional options
:param labels: Labels for the volume
:return: The created Volume
"""
if driver_opts is None:
package com.overliambitious.vaadin.frontend;
import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.html.Label;
import com.vaadin.flow.component.notification.Notification;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.router.Route;
import javax.annotation.PostConstruct;
import sys
from typing import List
import docker
from docker.errors import NotFound
from docker.models.containers import Container
class DockerManager:
def __init__(self):
import docker
def run_container():
client = docker.client.from_env()
client.containers.run("busybox:latest", "echo Hello World!", name="tutorial1")
if __name__ == "__main__":
run_container()
def function_one():
print "One"
def function_two():
print "Two"
def function_three():
print "Three"
@LiamJolly
LiamJolly / MyException.java
Created May 6, 2017 05:53
An example Java exception to demonstrate what is important when it comes to testing.
package com.liamdjolly;
public class MyException extends Exception {
public MyException() {
}
public MyException(String message) {
super(message);
}
@LiamJolly
LiamJolly / exception_example_test.py
Last active April 2, 2017 08:09
The unit test for our simple exception_example, uses side_effect to trigger the exception.
import exception_example
import unittest
import mock
class ExceptionExampleTest(unittest.TestCase):
def test_does_user_exist(self):
self.assertTrue(exception_example.does_user_exist("user1"))
def test_does_user_exist_no_user(self):
@LiamJolly
LiamJolly / exception_example.py
Last active April 2, 2017 08:08
A really simple example to help demonstrate mocks that throw exceptions.
def find_user(username):
if username == "user1":
return dict(name="user1", id_no="12345")
else:
raise Exception
def does_user_exist(username):
try:
find_user(username)
@LiamJolly
LiamJolly / file_utils_test.py
Last active March 15, 2017 09:34
Unit tests for file_utils.py, demontrating mock_open.
import unittest
import mock
import file_utils
class FileUtilsTest(unittest.TestCase):
def test_read_file(self):
with mock.patch("__builtin__.open", mock.mock_open(read_data="MOCKED"), create=True) as mock_file:
result = file_utils.read_file("path")