~andrewjbeach/juju-ci-tools/make-local-patcher

« back to all changes in this revision

Viewing changes to tests/test_write_industrial_test_metadata.py

  • Committer: Curtis Hovey
  • Date: 2014-08-01 12:44:38 UTC
  • Revision ID: curtis@canonical.com-20140801124438-l48516pldkzh7g5n
Do not show all the files in the tarball because it distracts from the test output.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
import json
2
 
from mock import patch
3
 
import os.path
4
 
from textwrap import dedent
5
 
from unittest import TestCase
6
 
 
7
 
from jujupy import _temp_env
8
 
from utility import temp_dir
9
 
from write_industrial_test_metadata import (
10
 
    main,
11
 
    make_metadata,
12
 
    parse_args,
13
 
)
14
 
 
15
 
 
16
 
class TestWriteIndustrialTestMetadata(TestCase):
17
 
 
18
 
    def test_parse_args_insufficiennt_args(self):
19
 
        with patch('sys.stderr'):
20
 
            with self.assertRaises(SystemExit):
21
 
                parse_args(['foo', 'bar'])
22
 
 
23
 
    def test_parse_args(self):
24
 
        args = parse_args(['foo', 'bar', 'baz'])
25
 
        self.assertItemsEqual(['buildvars', 'env', 'output'],
26
 
                              [a for a in dir(args) if not a.startswith('_')])
27
 
        self.assertEqual(args.buildvars, 'foo')
28
 
        self.assertEqual(args.env, 'bar')
29
 
        self.assertEqual(args.output, 'baz')
30
 
 
31
 
    def test_make_metadata(self):
32
 
        with temp_dir() as tempdir:
33
 
            buildvars_path = os.path.join(tempdir, 'buildvars')
34
 
            with open(buildvars_path, 'w') as buildvars_file:
35
 
                json.dump({'foo': 'bar'}, buildvars_file)
36
 
            with patch('subprocess.check_output', return_value='1.20'):
37
 
                envs = {'environments': {'foobar': {'type': 'local'}}}
38
 
                with _temp_env(envs):
39
 
                    metadata = make_metadata(buildvars_path, 'foobar')
40
 
        self.assertEqual(metadata, {
41
 
            'new_client': {
42
 
                'buildvars': {'foo': 'bar'},
43
 
                'type': 'build',
44
 
                },
45
 
            'old_client': {
46
 
                'type': 'release',
47
 
                'version': '1.20',
48
 
                },
49
 
            'environment': {
50
 
                'name': 'foobar',
51
 
                'substrate': 'LXC (local)',
52
 
                }
53
 
            })
54
 
 
55
 
    def test_main(self):
56
 
        with temp_dir() as tempdir:
57
 
            buildvars_path = os.path.join(tempdir, 'buildvars')
58
 
            with open(buildvars_path, 'w') as buildvars_file:
59
 
                json.dump({'foo': 'bar'}, buildvars_file)
60
 
            output_path = os.path.join(tempdir, 'output')
61
 
            envs = {'environments': {'foo': {'type': 'ec2'}}}
62
 
            with _temp_env(envs):
63
 
                with patch('subprocess.check_output', return_value='1.20'):
64
 
                    main([buildvars_path, 'foo', output_path])
65
 
            with open(output_path) as output_file:
66
 
                output = output_file.read()
67
 
            expected = dedent("""\
68
 
                {
69
 
                  "environment": {
70
 
                    "name": "foo",\x20
71
 
                    "substrate": "AWS"
72
 
                  },\x20
73
 
                  "new_client": {
74
 
                    "buildvars": {
75
 
                      "foo": "bar"
76
 
                    },\x20
77
 
                    "type": "build"
78
 
                  },\x20
79
 
                  "old_client": {
80
 
                    "type": "release",\x20
81
 
                    "version": "1.20"
82
 
                  }
83
 
                }
84
 
                """)
85
 
            self.assertEqual(output, expected)