Code Monkey home page Code Monkey logo

automated-testing-with-azurite's Introduction

page_type languages products name description urlFragment
sample
python
yaml
azure
azure-devops
azure-blob-storage
Azure Blob Storage testing Pipeline with Azurite
Using Azurite to run blob storage tests in Azure DevOps Pipeline with a sample application
automated-testing-with-azure

Using Azurite to run blob storage tests in Azure DevOps Pipeline

Overview

This repo demonstrates the approach for writing automated tests with a short feedback loop (i.e. unit tests) against security considerations (private endpoints) for Azure Blob Storage functionality.

Once private endpoints are enabled for the Azure Storage accounts, tests will fail, when executed locally or as part of a pipeline, because this connection will be blocked.

This repo contains a sample for Azure Blob Storage that will upload images from a url and upload files from a local folder. To run, cp sample_env.txt .env file to load environment variables for running code on local or pipeline and using conftest.py to load environment variables for tests.

Getting Started

Folder structure

Here's the folder structure for the sample:

  • build
    • azure-pipelines.yml - Azure Pipelines yaml file
  • data - Local files examples to be uploaded to Blob Storage
  • src
    • azure_blob_storage.py - Azure Blob Storage Sample Code
  • tests
    • conftest.py - Configuration file for running tests
    • test_azure_blob_storage.py - Azure Blob Storage Test Code
  • .env - Environment variable files for Blob Storage
  • docker-compose.yml - Docker Compose yaml file to run Azurite Docker Image
  • requirements.txt - Required pip packages to run python code on your local and pipeline
  • requirements_dev.txt - Required pip packages to run test code on your local and pipeline

Prerequisites

Install and Launch Azurite in local environment

There are several different ways to install and run Azurite on your local system as listed here. In this document, we will cover Install and run Azurite using NPM and Install and run the Azurite Docker image.

a. Using NPM

In order to run Azurite V3 you need Node.js >= 8.0 installed on your system. Azurite works cross-platform on Windows, Linux, and OS X.

After the Node.js installation, you can install Azurite simply with npm which is the Node.js package management tool included with every Node.js installation.

# Install Azurite
npm install -g azurite

# Create azurite directory
mkdir c:/azurite

# Launch Azurite for Windows
azurite --silent --location c:\azurite --debug c:\azurite\debug.log

#Launch Azurite for MacOs or Linux:
azurite -s -l /usr/local/lib/node_modules/azurite -d /usr/local/lib/node_modules/azurite/debug.log

b. Using docker image

Docker Compose will run the docker image using the docker-compose.yml file.

docker-compose up

Either option (a) Using NPM or (b) Using docker image, the output should be:

Azurite Blob service is starting at http://127.0.0.1:10000
Azurite Blob service is successfully listening at http://127.0.0.1:10000
Azurite Queue service is starting at http://127.0.0.1:10001
Azurite Queue service is successfully listening at http://127.0.0.1:10001

Run tests on local

Python 3.8 is used for this, but it should also work fine on other 3.6+ versions.

  1. To test and see how these endpoints are running, you can attach your local blob storage to the Azure Storage Explorer. In Azure Storage Explorer:
  • right click on Storage Accounts and select Connect to Azure Storage connect storage

  • then select Attach to a local emulator
    connect blob

  1. Provide a Display name and port number, then your connection will be ready and you can use Storage Explorer to manage your local blob storage.
    attach to local

Do not forget to start your emulator, Storage Explorer will not start it for you.

  1. Create a virtual python environment from working directory:

    python3 -m venv env
    source env/bin/activate [on Linux] or env/scripts/activate [on Windows]

  2. Install the dependencies: python3 -m pip install -r requirements_dev.txt

  3. Run tests:

    python3 -m pytest ./tests

After the tests run, you can see the files in your local blob storage.

https local blob

Run tests on Azure DevOps Pipelines

After successfully running tests on local, run the azure-pipelines build yaml file using Azure DevOps Pipelines.

Note: You will need

  1. Login to Azure DevOps

  2. Under the Organization and Project, select the sample folder git Repo and Configure a Pipeline using the build/azure-pipelines.yml

  3. Run the Pipeline

After a successful set up and running the pipeline in Azure DevOps Pipelines, the result should look like:

azure pipelines

License:

See LICENSE.

Code of Conduct

This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact [email protected] with any additional questions or comments.

Contributing

See CONTRIBUTING

automated-testing-with-azurite's People

Contributors

launakay avatar olasowemimo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

automated-testing-with-azurite's Issues

The files loaded by create_blob_from_url() are empty

With a working docker Azurite container, the files uploaded by azure_blob_storage.py are empty.

image

Facts

  • When I connect Azure Storage Explorer to the local instance, I can upload files without problem.
  • Uncommenting the create_blob_from_path(...) call in the main, uploaded files are ok
    #create_blob_from_path(storage_connection_string,container_name)
  • Saving the files downloaded by create_blob_from_url in the data folder, they are uploaded fine by create_blob_from_path. (I add this line after : with open(os.path.join("."+os.sep+"data"+os.sep+"saved-"+get_filename_from_url(u)), 'wb') as f: f.write(r.content))

Context

  • Windows 10 / Docker Desktop / Wsl2 (Debian)
  • repo cloned from master branch (commit #fb53233 )
  • Python 3.8.13
  • from pip (requests is not freezed in requirements.txt:
    • requests==2.28.2
    • requests-oauthlib==1.3.1

I don't know how to investigate more on this ?

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.