Revision ff0d18e6 test/ganeti.opcodes_unittest.py

b/test/ganeti.opcodes_unittest.py
45 45
      self.assert_(cls.OP_ID.startswith("OP_"))
46 46
      self.assert_(len(cls.OP_ID) > 3)
47 47
      self.assertEqual(cls.OP_ID, cls.OP_ID.upper())
48
      self.assertEqual(cls.OP_ID, opcodes._NameToId(cls.__name__))
48 49

  
49 50
      self.assertRaises(TypeError, cls, unsupported_parameter="some value")
50 51

  
......
88 89
      self.assertEqual("OP_%s" % summary, op.OP_ID)
89 90

  
90 91
  def testSummary(self):
91
    class _TestOp(opcodes.OpCode):
92
      OP_ID = "OP_TEST"
92
    class OpTest(opcodes.OpCode):
93 93
      OP_DSC_FIELD = "data"
94 94
      OP_PARAMS = [
95 95
        ("data", ht.NoDefault, ht.TString),
96 96
        ]
97 97

  
98
    self.assertEqual(_TestOp(data="").Summary(), "TEST()")
99
    self.assertEqual(_TestOp(data="Hello World").Summary(),
98
    self.assertEqual(OpTest(data="").Summary(), "TEST()")
99
    self.assertEqual(OpTest(data="Hello World").Summary(),
100 100
                     "TEST(Hello World)")
101
    self.assertEqual(_TestOp(data="node1.example.com").Summary(),
101
    self.assertEqual(OpTest(data="node1.example.com").Summary(),
102 102
                     "TEST(node1.example.com)")
103 103

  
104 104
  def testListSummary(self):
105
    class _TestOp(opcodes.OpCode):
106
      OP_ID = "OP_TEST"
105
    class OpTest(opcodes.OpCode):
107 106
      OP_DSC_FIELD = "data"
108 107
      OP_PARAMS = [
109 108
        ("data", ht.NoDefault, ht.TList),
110 109
        ]
111 110

  
112
    self.assertEqual(_TestOp(data=["a", "b", "c"]).Summary(),
111
    self.assertEqual(OpTest(data=["a", "b", "c"]).Summary(),
113 112
                     "TEST(a,b,c)")
114
    self.assertEqual(_TestOp(data=["a", None, "c"]).Summary(),
113
    self.assertEqual(OpTest(data=["a", None, "c"]).Summary(),
115 114
                     "TEST(a,None,c)")
116
    self.assertEqual(_TestOp(data=[1, 2, 3, 4]).Summary(), "TEST(1,2,3,4)")
115
    self.assertEqual(OpTest(data=[1, 2, 3, 4]).Summary(), "TEST(1,2,3,4)")
117 116

  
118 117
  def testOpId(self):
119 118
    self.assertFalse(utils.FindDuplicates(cls.OP_ID
......
162 161
                           msg="Default value returned by function is callable")
163 162

  
164 163
  def testValidateNoModification(self):
165
    class _TestOp(opcodes.OpCode):
166
      OP_ID = "OP_TEST"
164
    class OpTest(opcodes.OpCode):
167 165
      OP_PARAMS = [
168 166
        ("nodef", ht.NoDefault, ht.TMaybeString),
169 167
        ("wdef", "default", ht.TMaybeString),
......
172 170
        ]
173 171

  
174 172
    # Missing required parameter "nodef"
175
    op = _TestOp()
173
    op = OpTest()
176 174
    before = op.__getstate__()
177 175
    self.assertRaises(errors.OpPrereqError, op.Validate, False)
178 176
    self.assertFalse(hasattr(op, "nodef"))
......
182 180
    self.assertEqual(op.__getstate__(), before, msg="Opcode was modified")
183 181

  
184 182
    # Required parameter "nodef" is provided
185
    op = _TestOp(nodef="foo")
183
    op = OpTest(nodef="foo")
186 184
    before = op.__getstate__()
187 185
    op.Validate(False)
188 186
    self.assertEqual(op.__getstate__(), before, msg="Opcode was modified")
......
192 190
    self.assertFalse(hasattr(op, "notype"))
193 191

  
194 192
    # Missing required parameter "nodef"
195
    op = _TestOp(wdef="hello", number=999)
193
    op = OpTest(wdef="hello", number=999)
196 194
    before = op.__getstate__()
197 195
    self.assertRaises(errors.OpPrereqError, op.Validate, False)
198 196
    self.assertFalse(hasattr(op, "nodef"))
......
200 198
    self.assertEqual(op.__getstate__(), before, msg="Opcode was modified")
201 199

  
202 200
    # Wrong type for "nodef"
203
    op = _TestOp(nodef=987)
201
    op = OpTest(nodef=987)
204 202
    before = op.__getstate__()
205 203
    self.assertRaises(errors.OpPrereqError, op.Validate, False)
206 204
    self.assertEqual(op.nodef, 987)
......
208 206
    self.assertEqual(op.__getstate__(), before, msg="Opcode was modified")
209 207

  
210 208
    # Testing different types for "notype"
211
    op = _TestOp(nodef="foo", notype=[1, 2, 3])
209
    op = OpTest(nodef="foo", notype=[1, 2, 3])
212 210
    before = op.__getstate__()
213 211
    op.Validate(False)
214 212
    self.assertEqual(op.nodef, "foo")
215 213
    self.assertEqual(op.notype, [1, 2, 3])
216 214
    self.assertEqual(op.__getstate__(), before, msg="Opcode was modified")
217 215

  
218
    op = _TestOp(nodef="foo", notype="Hello World")
216
    op = OpTest(nodef="foo", notype="Hello World")
219 217
    before = op.__getstate__()
220 218
    op.Validate(False)
221 219
    self.assertEqual(op.nodef, "foo")
......
223 221
    self.assertEqual(op.__getstate__(), before, msg="Opcode was modified")
224 222

  
225 223
  def testValidateSetDefaults(self):
226
    class _TestOp(opcodes.OpCode):
227
      OP_ID = "OP_TEST"
224
    class OpTest(opcodes.OpCode):
228 225
      OP_PARAMS = [
229 226
        # Static default value
230 227
        ("value1", "default", ht.TMaybeString),
......
233 230
        ("value2", lambda: "result", ht.TMaybeString),
234 231
        ]
235 232

  
236
    op = _TestOp()
233
    op = OpTest()
237 234
    before = op.__getstate__()
238 235
    op.Validate(True)
239 236
    self.assertNotEqual(op.__getstate__(), before,
......
244 241
    self.assert_(op.debug_level is None)
245 242
    self.assertEqual(op.priority, constants.OP_PRIO_DEFAULT)
246 243

  
247
    op = _TestOp(value1="hello", value2="world", debug_level=123)
244
    op = OpTest(value1="hello", value2="world", debug_level=123)
248 245
    before = op.__getstate__()
249 246
    op.Validate(True)
250 247
    self.assertNotEqual(op.__getstate__(), before,

Also available in: Unified diff