def testGetWithIgnore(self): """ return config without certain lines """ c = Configuration() c.set(self.config_one) conf = c.get(ignore_lines=[0, 2]) #logging.error( conf ) assert conf == ['line 2'], 'get with ignore lines failed'
def testStr(self): """ test string output """ config = Configuration() config.set(self.config_one) this = str(config) # we force that we have newlines after each line # map that to have newlines - not forgetting last newline that = "\n".join(self.config_one) + "\n" assert this == that, 'str() is different; this: ' + this + ", that: " + that
def testConfigSet(self): """ setting config """ config = Configuration() this = [] for i in self.config_one: this.append(i) config.set(self.config_one) total = _compareArrays(this, self.config_one) assert total == 0, "different configs on set"
def testDiffSame(self): """ diff of same configs """ one = Configuration() one.set(self.config_one) two = Configuration() two.set(self.config_one) assert len(one.diff(two)) == 0, 'diff() with idential failed'
def testCmpDifferent(self): """ cmp of different configs """ one = Configuration() one.set(self.config_one) two = Configuration() two.set(self.config_two) assert not cmp(one, two) == 0, 'cmp() failed'
def testCmpSame(self): """ cmp of same configs """ one = Configuration() one.set(self.config_one) two = Configuration() two.set(self.config_one) assert cmp(one, two) == 0, 'cmp() failed'
def testConfigFromFile(self): # create file # logging.error("CONF: " + str(self.config_one)) temp, path = tempfile.mkstemp() f = open(path, 'w') for l in self.config_one: f.write(l + "\n") f.close() # assign to object config = Configuration() config.set(path) # logging.error("FILE: " + str(config.get())) res = _compareArrays(config.get(), self.config_one) os.remove(path) assert res == 0, 'different configs for file'
def testInit(self): """ object creation """ config = None try: config = Configuration() except Exception, e: pass
def testConvertDate(self): """ test time convesion """ string = '11:27:21 PST Tue Dec 1 2009' one = Configuration() dt = one._convertDate(string) #logging.error( 'IN: ' + str(dt) ) class pst(tzinfo): def tzname(self, dt): return 'PST' def utcoffset(self, dt): return datetime.timedelta(hours=-8) def dst(self, dt): return datetime.timedelta(0) correct = datetime.datetime(2009, 12, 1, 11, 27, 21, tzinfo=pst()) #logging.error( "OUT: " + str(correct) ) assert dt == correct, 'convert timestamp failed'
def testDiffDifferent(self): """ diff of different configs""" one = Configuration() one.set(self.config_one) two = Configuration() two.set(self.config_one) diff = one.diff(two) # assert len(diff) == 0, 'no differences ('+str(pprint(diff))+')' manual_diff = [ '--- \n', '+++ \n', '@@ -1,3 +1,3 @@\n', '-line 1\n', '-line 2\n', '-line 3\n', '+line 4\n', '+line 5\n', '+line 6\n' ] check = 0 for i in diff: #logging.error( "'" + i + "'") j = manual_diff.pop(0) if not i == j: logging.error("-->\ni='" + i + '\'\nj=\n' + j + "\'") check = check + 1 assert check == 0, 'diff() with different failed'
def testConfigInstantiate(self): """ create new config """ config = Configuration() config.set(self.config_one) res = _compareArrays(config.get(), self.config_one) assert res == 0, "different configs on instantiation " + str(res)
def testGet(self): """ return all config """ c = Configuration() c.set(self.config_one) conf = c.get() assert conf == self.config_one, 'get raw config'
usage() sys.exit( ) # inheritors of backup configuration logging.error( '%s' % inheritors( 'netconfig.backup.configuration' ) sys.exit(1) for f in files: # logging.info("FILE %s" % (f,) ) fn = open( f, 'r' ) this = [] for l in fn.readlines(): this.append( l.rstrip() ) conf = Configuration( this ) conf = append_scrubs( conf, CiscoIOSConfiguration() ) conf = append_scrubs( conf, CiscoIOSWirelessConfiguration() ) conf = append_scrubs( conf, CiscoASAConfiguration() ) conf = append_scrubs( conf, CiscoCATOSConfiguration() ) conf = append_scrubs( conf, CiscoNexusConfiguration() ) conf = append_scrubs( conf, CiscoTopSpinConfiguration() ) conf = append_scrubs( conf, CiscoASAConfiguration() ) conf = append_scrubs( conf, AristaConfiguration() ) conf = append_scrubs( conf, BigIPLTMConfiguration() ) if 'replace' in options and options['replace']: with open( f, 'w' ) as fh: for i in conf.scrub( this ): fh.write( i + "\n")