~oddbloke/cloud-init/fix-gce-az

« back to all changes in this revision

Viewing changes to tests/unittests/test__init__.py

  • Committer: Scott Moser
  • Date: 2015-07-21 16:02:44 UTC
  • Revision ID: smoser@ubuntu.com-20150721160244-5rgzime1kd61dtce
tests: fix TestHandlerHandlePart tests

these tests were previously passing, but doing so erroneously.
I believe that an update to mock caused them to start failing.

I've updated the tests now.  The simple change is
replacing 'assert_called_with_once' with 'assert_called_once_with'.

The second set of changes is seemingly a correction of the following tests
expectations:
 test_normal_version_2 : was not expecting to get frequency passed into
    handle_part, but should have been.
 test_no_handle_when_modfreq_once: was expecting to have handle_part
    called even though the test implies otherwise.
 test_exception_is_caught: this test just looked broken.
    Now, we're testing that the part handler is called and that no
    exception is raised past handle_part

Show diffs side-by-side

added added

removed removed

Lines of Context:
70
70
                        return_value=self.module_fake) as mockobj:
71
71
            handlers.walker_handle_handler(self.data, self.ctype,
72
72
                                           self.filename, self.payload)
73
 
            mockobj.assert_called_with_once(self.expected_module_name)
74
 
        self.write_file_mock.assert_called_with_once(
 
73
            mockobj.assert_called_once_with(self.expected_module_name)
 
74
        self.write_file_mock.assert_called_once_with(
75
75
            self.expected_file_fullname, self.payload, 0o600)
76
76
        self.assertEqual(self.data['handlercount'], 1)
77
77
 
81
81
                        side_effect=ImportError) as mockobj:
82
82
            handlers.walker_handle_handler(self.data, self.ctype,
83
83
                                           self.filename, self.payload)
84
 
            mockobj.assert_called_with_once(self.expected_module_name)
85
 
        self.write_file_mock.assert_called_with_once(
 
84
            mockobj.assert_called_once_with(self.expected_module_name)
 
85
        self.write_file_mock.assert_called_once_with(
86
86
            self.expected_file_fullname, self.payload, 0o600)
87
87
        self.assertEqual(self.data['handlercount'], 0)
88
88
 
93
93
                        return_value=self.module_fake) as mockobj:
94
94
            handlers.walker_handle_handler(self.data, self.ctype,
95
95
                                           self.filename, self.payload)
96
 
            mockobj.assert_called_with_once(self.expected_module_name)
97
 
        self.write_file_mock.assert_called_with_once(
 
96
            mockobj.assert_called_once_with(self.expected_module_name)
 
97
        self.write_file_mock.assert_called_once_with(
98
98
            self.expected_file_fullname, self.payload, 0o600)
99
99
        self.assertEqual(self.data['handlercount'], 0)
100
100
 
122
122
                          self.frequency, self.headers)
123
123
        # Assert that the handle_part() method of the mock object got
124
124
        # called with the expected arguments.
125
 
        mod_mock.handle_part.assert_called_with_once(
 
125
        mod_mock.handle_part.assert_called_once_with(
126
126
            self.data, self.ctype, self.filename, self.payload)
127
127
 
128
128
    def test_normal_version_2(self):
136
136
                          self.frequency, self.headers)
137
137
        # Assert that the handle_part() method of the mock object got
138
138
        # called with the expected arguments.
139
 
        mod_mock.handle_part.assert_called_with_once(
140
 
            self.data, self.ctype, self.filename, self.payload)
 
139
        mod_mock.handle_part.assert_called_once_with(
 
140
            self.data, self.ctype, self.filename, self.payload,
 
141
            settings.PER_INSTANCE)
141
142
 
142
143
    def test_modfreq_per_always(self):
143
144
        """
150
151
                          self.frequency, self.headers)
151
152
        # Assert that the handle_part() method of the mock object got
152
153
        # called with the expected arguments.
153
 
        mod_mock.handle_part.assert_called_with_once(
 
154
        mod_mock.handle_part.assert_called_once_with(
154
155
            self.data, self.ctype, self.filename, self.payload)
155
156
 
156
157
    def test_no_handle_when_modfreq_once(self):
159
160
        mod_mock = mock.Mock(frequency=settings.PER_ONCE)
160
161
        handlers.run_part(mod_mock, self.data, self.filename, self.payload,
161
162
                          self.frequency, self.headers)
162
 
        # Assert that the handle_part() method of the mock object got
163
 
        # called with the expected arguments.
164
 
        mod_mock.handle_part.assert_called_with_once(
165
 
            self.data, self.ctype, self.filename, self.payload)
 
163
        self.assertEqual(0, mod_mock.handle_part.call_count)
166
164
 
167
165
    def test_exception_is_caught(self):
168
166
        """Exceptions within C{handle_part} are caught and logged."""
169
167
        mod_mock = mock.Mock(frequency=settings.PER_INSTANCE,
170
168
                             handler_version=1)
171
 
        handlers.run_part(mod_mock, self.data, self.filename, self.payload,
172
 
                          self.frequency, self.headers)
173
169
        mod_mock.handle_part.side_effect = Exception
174
 
        handlers.run_part(mod_mock, self.data, self.filename, self.payload,
175
 
                          self.frequency, self.headers)
176
 
        mod_mock.handle_part.assert_called_with_once(
 
170
        try:
 
171
            handlers.run_part(mod_mock, self.data, self.filename,
 
172
                              self.payload, self.frequency, self.headers)
 
173
        except Exception:
 
174
            self.fail("Exception was not caught in handle_part")
 
175
 
 
176
        mod_mock.handle_part.assert_called_once_with(
177
177
            self.data, self.ctype, self.filename, self.payload)
178
178
 
179
179