Debian Codegraph @

case_studies/kubernetes/test_same.py

raw
1 # Copyright 2015 Google Inc. All rights reserved.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 import yaml
16 import json
17 import sys
18 import os
19
20 os.chdir(os.path.dirname(os.path.abspath(__file__)))
21 os.system("jsonnet -m ./ example.jsonnet")
22
23 files = [
24 'bigquery-controller',
25 'redis-master',
26 'redis-master-service',
27 'twitter-stream',
28 ]
29
30 def jsonstr(v):
31 return json.dumps(v, sort_keys=True, indent=4, separators=(',', ': '))
32
33 def canonicalize(doc):
34 """De-duplicate environment vars and sort them alphabetically."""
35 spec = doc.get('spec')
36 if not spec: return doc
37 template = spec.get('template')
38 if not template: return doc
39 spec2 = template.get('spec')
40 if not spec2: return doc
41 containers = spec2.get('containers')
42 if not containers: return doc
43 for container in containers:
44 env = container.get('env')
45 if not env: continue
46 tab = { }
47 for pair in env:
48 tab[pair['name']] = pair['value']
49 new_env = []
50 for key in sorted(tab):
51 new_env.append({'name': key, 'value': tab[key]})
52 container['env'] = new_env
53 return doc
54
55 for filename in files:
56 with open(filename + '.old.yaml', 'r') as f:
57 yaml_doc = canonicalize(yaml.load(f, Loader=yaml.SafeLoader))
58
59 with open(filename + '.new.yaml', 'r') as f:
60 jsonnet_doc = yaml.load(f, Loader=yaml.SafeLoader)
61
62 if jsonstr(yaml_doc) == jsonstr(jsonnet_doc):
63 print('Identical: %s' % filename)
64 else:
65 print('Not identical, run: diff %s.old.yaml.out %s.new.yaml.out' % (filename, filename))
66 with open(filename + '.old.yaml.out', 'w') as f:
67 f.write(jsonstr(yaml_doc))
68
69 with open(filename + '.new.yaml.out', 'w') as f:
70 f.write(jsonstr(jsonnet_doc))
71
72 os.remove(filename + '.new.yaml')
73
74