Skip to content
This repository has been archived by the owner on Apr 14, 2021. It is now read-only.

Initial Version #2

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
57 changes: 57 additions & 0 deletions tests/unit/tests_models/samples/apps.detail.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
{
"cname": [
"hufflepuff-stg.hogwarts.wiz"
],
"deploys": 44,
"description": "https://github.com/hogwarts/hufflepuff-api",
"ip": "hufflepuff-api-prd.tsuru-131.vpc",
"lock": {
"Locked": false,
"Reason": "",
"Owner": "",
"AcquireDate": "0001-01-01T00:00:00Z"
},
"name": "hufflepuff-api-prd",
"owner": "[email protected]",
"plan": {
"cpushare": 2,
"memory": 805306368,
"name": "entry",
"router": "hipache",
"swap": 0
},
"platform": "python",
"pool": "stg",
"repository": "[email protected]:hufflepuff-api-prd.git",
"router": "hipache",
"tags": [],
"teamowner": "albus",
"teams": [
"albus"
],
"units": [
{
"ID": "some-very-large-id-here-for-griffindor",
"Name": "griffindor-stg-7bfecd66649a54a4fc09",
"AppName": "griffindor-stg",
"ProcessName": "web",
"Type": "python",
"Ip": "ip-10-0-0-7.ec2.internal",
"Status": "started",
"Address": {
"Scheme": "http",
"Opaque": "",
"User": null,
"Host": "ip-10-0-0-7.ec2.internal:32772",
"Path": "",
"RawPath": "",
"ForceQuery": false,
"RawQuery": "",
"Fragment": ""
},
"HostAddr": "p-10-0-0-7.ec2.internal",
"HostPort": "32772",
"IP": "p-10-0-0-7.ec2.internal"
}
]
}
96 changes: 96 additions & 0 deletions tests/unit/tests_models/samples/apps.list.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
[
{
"name": "hufflepuff-stg",
"pool": "stg",
"teamowner": "hogwarts",
"plan": {
"name": "entry",
"memory": 805306368,
"swap": 0,
"cpushare": 2
},
"units": [
{
"ID": "some-very-large-id-here-for-hufflepuff",
"Name": "hufflepuff-stg-7bfecd55249a54a4fc09",
"AppName": "hufflepuff-stg",
"ProcessName": "web",
"Type": "python",
"Ip": "ip-10-0-0-42.ec2.internal",
"Status": "started",
"Address": {
"Scheme": "http",
"Opaque": "",
"User": null,
"Host": "ip-10-0-0-42.ec2.internal:32772",
"Path": "",
"RawPath": "",
"ForceQuery": false,
"RawQuery": "",
"Fragment": ""
},
"HostAddr": "p-10-0-0-42.ec2.internal",
"HostPort": "32772",
"IP": "p-10-0-0-42.ec2.internal"
}
],
"cname": [
"hufflepuff-stg.hogwarts.wiz"
],
"ip": "hufflepuff-stg.tsuru.vpc",
"lock": {
"Locked": false,
"Reason": "",
"Owner": "",
"AcquireDate": "0001-01-01T00:00:00Z"
},
"tags": []
},
{
"name": "griffindor-stg",
"pool": "stg",
"teamowner": "hogwarts",
"plan": {
"name": "entry",
"memory": 805306368,
"swap": 0,
"cpushare": 2
},
"units": [
{
"ID": "some-very-large-id-here-for-griffindor",
"Name": "griffindor-stg-7bfecd66649a54a4fc09",
"AppName": "griffindor-stg",
"ProcessName": "web",
"Type": "python",
"Ip": "ip-10-0-0-7.ec2.internal",
"Status": "started",
"Address": {
"Scheme": "http",
"Opaque": "",
"User": null,
"Host": "ip-10-0-0-7.ec2.internal:32772",
"Path": "",
"RawPath": "",
"ForceQuery": false,
"RawQuery": "",
"Fragment": ""
},
"HostAddr": "p-10-0-0-7.ec2.internal",
"HostPort": "32772",
"IP": "p-10-0-0-7.ec2.internal"
}
],
"cname": [
"griffindor-stg.hogwarts.wiz"
],
"ip": "griffindor-stg.tsuru.vpc",
"lock": {
"Locked": false,
"Reason": "",
"Owner": "",
"AcquireDate": "0001-01-01T00:00:00Z"
},
"tags": []
}
]
39 changes: 39 additions & 0 deletions tests/unit/tests_models/tests_app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import unittest

from tsuru import models, exceptions
from tests.unit.tests_models import ModelTestMixin


class TestAppModel(ModelTestMixin, unittest.TestCase):
MODEL_KLASS = models.App

def test_fields(self):
data = self.sample_detail()
app = models.App(data=data)

self.assertEqual('hufflepuff-api-prd', app.name)

def test_invalid_field(self):
with self.assertRaises(exceptions.UnexpectedDataFormat):
models.App(data={})

def test_list(self):
data = self.sample_list()
with self.patch_get(data=data) as get:
list(models.App.list())

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should test the returned value.


self.assertEqual(1, get.call_count)

def test_detail(self):
data = self.sample_detail()
with self.patch_get(data=data) as get:
models.App.get(pk=666)

self.assertEqual(1, get.call_count)

def test_not_found(self):
with self.patch_get_error(status_code=404) as get:
with self.assertRaises(exceptions.DoesNotExist):
models.App.get(pk=666)

self.assertEqual(1, get.call_count)
4 changes: 4 additions & 0 deletions tsuru/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from tsuru.models import (
App,
)
from tsuru.exceptions import (
DoesNotExist,
UnexpectedDataFormat,
Expand All @@ -9,6 +12,7 @@
version = '0.1.0'

__all__ = (
'App',
'DoesNotExist',
'UnexpectedDataFormat',
'UnsupportedModelException',
Expand Down
2 changes: 2 additions & 0 deletions tsuru/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
from tsuru.models.app import App
__all__ = (
'App',
)
93 changes: 93 additions & 0 deletions tsuru/models/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
from tsuru.models.base import BaseModel


class App(BaseModel):
_RESOURCE_NAME = 'apps'
_PK_FIELD = 'name'

@property
def name(self):
return self._get('name')

@property
def pool(self):
return self._get('pool')

@property
def team_owner(self):
return self._get('team_owner')

@property
def owner(self):
return self._get('owner')

@property
def platform(self):
return self._get('platform')

@property
def repository(self):
return self._get('repository')

@property
def router(self):
return self._get('router')

@property
def teams(self):
from tsuru import Team

yield from [Team(data={'name': name}) for name in self._get('team')]

@property
def ip(self):
return self._get('ip')

@property
def cnames(self):
return self._get('cname')

@property
def deploys_amount(self):
return self._get('deploys')

@property
def description(self):
return self._get('description')

@property
def lock(self):
from tsuru import Lock

lock_data = self._get('lock')
return Lock(data=lock_data)

@property
def plan(self):
from tsuru import Plan

plan_data = self._get('plan')
return Plan(data=plan_data)

@property
def envs(self):
from tsuru import Env

yield from self._bound_list(resource_class=Env)

def get_logs(self, lines=10):
from tsuru import Log

yield from self._bound_list(resource_class=Log, params={'lines': lines})

@property
def units(self):
yield from self._get('units')

def get_unit(self, pk):
from tsuru import Unit

return self._bound_detail(
resource_class=Unit,
pk=pk,
)