def manifests_equiv(man1, man2): try: if man1['kind'] != man2['kind']: return False meta1, meta2 = man1['metadata'], man2['metadata'] if meta1.get('namespace', None) != meta2.get('namespace', None): return False if meta1['name'] != meta2['name']: return False if man1['kind'] in workload_kinds: return containers_equal(kubeyaml.containers(man1), kubeyaml.containers(man2)) except KeyError: return False return True
def test_find_container(man, data): cs = kubeyaml.containers(man) assume(len(cs) > 0) spec = Spec.from_resource(man) ind = data.draw(strats.integers(min_value=0, max_value=len(cs) - 1)) spec.container = cs[ind]['name'] assert kubeyaml.find_container(spec, man) is not None
def test_update_image_apply(docs, data): originals = [man for doc in docs for man in kubeyaml.manifests(doc)] workloads = [wl for wl in originals if wl['kind'] in workload_kinds] assume(len(workloads) > 0) # Make sure we got workloads with different IDs assume(len(workloads) == len(set(map(resource_id, workloads)))) ind = data.draw(strats.integers(min_value=0, max_value=len(workloads) - 1)) workload = workloads[ind] containers = kubeyaml.containers(workload) assume(len(containers) > 0) yaml = kubeyaml.yaml() original = StringIO() for d in docs: yaml.dump(d, original) originalstr = comment_yaml(data.draw, original.getvalue()) note('Original:\n%s\n' % originalstr) indc = data.draw( strats.integers(min_value=0, max_value=len(containers) - 1)) spec = Spec.from_resource(workload) spec.container = containers[indc]['name'] spec.image = data.draw(images_with_tag) note('Spec: %r' % spec) infile, outfile = StringIO(originalstr), StringIO() kubeyaml.apply_to_yaml(lambda ds: kubeyaml.update_image(spec, ds), infile, outfile) # A rough check that the docs are in the same grouping into Lists, # since we'll look at individual manifests, ignoring whether they # are in Lists, after this. updateddocs = list(yaml.load_all(outfile.getvalue())) assert (len(docs) == len(updateddocs)) for i in range(len(docs)): assert (updateddocs[i]['kind'] == docs[i]['kind']) # check that the selected manifest->container has the updated # image; and, the rest are unchanged. updateds = [man for doc in updateddocs for man in kubeyaml.manifests(doc)] assert (len(originals) == len(updateds)) found = False for i in range(len(originals)): if kubeyaml.match_manifest(spec, updateds[i]): assert not found, "spec matched more than one manifest" c = kubeyaml.find_container(spec, updateds[i]) assert c is not None assert c['image'] == spec.image found = True else: assert manifests_equiv(originals[i], updateds[i]) assert found
def test_image_update(man, image, data): cs = kubeyaml.containers(man) assume(len(cs) > 0) args = Spec.from_resource(man) args.image = image ind = data.draw(strats.integers(min_value=0, max_value=len(cs) - 1)) args.container = cs[ind]['name'] man1 = copy.deepcopy(man) man2 = None for out in kubeyaml.update_image(args, [man]): man2 = out assert man2 is not None assert kubeyaml.match_manifest(args, man2) outcs = kubeyaml.containers(man2) assert len(outcs) == len(cs) assert outcs[ind]['image'] == image check_structure(man1, man2)
def test_extract_custom_containers(image_values, noise): assume(len(set(image_values) & set(noise)) == 0) containers = image_values['_containers'] custom_values = destructive_merge(image_values, noise) kind, ns, name = 'FluxHelmRelease', 'default', 'release' chart_name = 'chart' res = resource(kind, ns, name) res['spec'] = { 'chartGitPath': chart_name, 'values': custom_values, } original = {container: image for c in containers for container, image in c.items()} extracted = {c['name']: c['image'] for c in kubeyaml.containers(res)} assert original == extracted