Code Monkey home page Code Monkey logo

mazure's Introduction

Mazure - Moto for Azure

Build Status

A library that allows you to mock Azure services.

Demo

Installation

$ pip install --editable .

Usage

Mazure provides two modes of operation. It can be used as a

  • Context Manager
  • Function Decorator

Imagine a test setup as follows. To this

import uuid
import secrets
import unittest

from azure.identity import ClientSecretCredential
from azure.mgmt.storage import StorageManagementClient
from azure.mgmt.storage.models import StorageAccountCreateParameters


class TestStorageAccounts(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        cls.subscription = str(uuid.uuid4())
        cls.creds = ClientSecretCredential(
            tenant_id=str(uuid.uuid4()),
            client_id=str(uuid.uuid4()),
            client_secret=secrets.token_urlsafe())
        cls.client = StorageManagementClient(cls.creds, cls.subscription)

    def test_create_and_list(self):
        """ TODO
        Creates a storage account & lists all storage accounts in a subscription
        """

Function Decorator

Simply wrap your test function with a @mazure decorator.

...
...
from mazure import mazure   # Import the mazure decorator


class TestStorageAccounts(unittest.TestCase):
    ...
    ...

    @mazure
    def test_create_and_list(self):
        accounts = self.client.storage_accounts.list()
        self.assertEqual(len([account for account in accounts]), 0)

        kws = {
            "sku": {"name": "Premium_LRS"},
            "kind": "BlockBlobStorage",
            "location": "eastus",
        }
        self.client.storage_accounts.begin_create(
            'testrg', 'testaccount', StorageAccountCreateParameters(**kws))
        accounts = self.client.storage_accounts.list()
        self.assertGreater(len([account for account in accounts]), 0)

Context Manager

Make use of the Mazure class to mock out calls to the Azure API's

...
...

from mazure import Mazure   # Import the Mazure class


class TestStorageAccounts(unittest.TestCase):
    ...
    ...

    def test_create_and_list(self):
        with Mazure():
            accounts = self.client.storage_accounts.list()
            self.assertEqual(len([account for account in accounts]), 0)

            kws = {
                "sku": {"name": "Premium_LRS"},
                "kind": "BlockBlobStorage",
                "location": "eastus",
            }
            self.client.storage_accounts.begin_create(
                'testrg', 'testaccount', StorageAccountCreateParameters(**kws))
            accounts = self.client.storage_accounts.list()
            self.assertGreater(len([account for account in accounts]), 0)

Usage options

With both the decorator and the context manager, Mazure allows you some fine tuning options.

  • Selective mocking

    You can request only certain services to be mocked in your code. For instance,

    @mazure('storage_accounts', 'blob_services')
    def method(self):
        storage = StorageManagementClient(self.creds, self.subscription)
        compute = ComputeManagementClient(self.creds, self.subscription)
        accounts = [acc for acc in storage.storage_accounts.list()]
        machines = [vm for vm in compute.virtual_machines.list_all()]
        return accounts, machines

    In the above code block, only storage account API calls are mocked out. API calls to any other Azure services will raise an exception.

    When using a context manager, provide a list of services to be mocked

    def method(self):
        with Mazure(['storage_accounts', 'blob_services']):
            ...
            ...
  • Passthrough option

    To avoid the above scenario, you can use the allow=True flag in your decorator, which allows all unmocked API calls to pass through to query the live Azure API's.

    @mazure('storage_accounts', 'blob_services', allow=True)
    def method(self):
        ...
        ...

    When using a context manager, the same functionality is available as follows.

    def method(self):
        with Mazure(['storage_accounts', 'blob_services'], allow=True):
            ...
            ...

Supported Azure Services

Mazure is still very much a work in progress and aims to eventually implement the basic functionality of some of the most commonly used Azure services. At present, Mazure is able to mock the basic functionality of the below mentioned services.

  • Resource groups
  • Storage accounts
  • Virtual machines

Resources

mazure's People

Contributors

tinvaan avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

avishayil

mazure's Issues

Resource group behaviour tests

Replicate some user journeys for resource group access using the Azure ResourceManagementClient under the Mazure mocks.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.