def _runInputAndOutputs(self, manifest): job = Runner(manifest).run(JobOptions(add=True, startTime="time-to-test")) assert not job.unexpectedAbort, job.unexpectedAbort.getStackTrace() my_server = manifest.getRootResource().findResource("my_server") assert my_server self.assertEqual( "10 GB", my_server.query({"get_property": ["SELF", "host", "disk_size"]}) ) assert my_server.attributes["test"], "cpus: 2" # print(job.out.getvalue()) testSensitive = manifest.getRootResource().findResource("testSensitive") for name, toscaType in ( ("access_token", "tosca.datatypes.Credential"), ("TEST_VAR", "unfurl.datatypes.EnvVar"), ): assert ( testSensitive.template.attributeDefs[name].schema["type"] == toscaType ) def t(datatype): return datatype.type == "unfurl.datatypes.EnvVar" envvars = set(testSensitive.template.findProps(testSensitive.attributes, t)) self.assertEqual(envvars, set([("TEST_VAR", "foo"), ("VAR1", "more")])) outputIp = job.getOutputs()["server_ip"] self.assertEqual(outputIp, "10.0.0.1") assert isinstance(outputIp, sensitive_str), type(outputIp) assert job.status == Status.ok, job.summary() self.assertEqual("RHEL", testSensitive.attributes["distribution"]) return outputIp, job
def test_import(self): foreign = ( """ apiVersion: %s kind: Manifest spec: service_template: node_types: test.nodes.AbstractTest: derived_from: tosca.nodes.Root properties: private_address: type: string required: false metadata: sensitive: true interfaces: Install: operations: check: implementation: SetAttribute instances: anInstance: type: test.nodes.AbstractTest """ % API_VERSION ) localConfig = """ apiVersion: unfurl/v1alpha1 kind: Project contexts: defaults: secrets: attributes: vault_default_password: a_password external: foreign: manifest: file: foreignmanifest.yaml instance: "*" # this is the default """ # import a node from a external manifest and have an abstract node template select it # check will be run on it each time mainManifest = ( """ apiVersion: %s kind: Manifest spec: service_template: imports: - foreignmanifest.yaml#/spec/service_template topology_template: outputs: server_ip: value: {eval: "::foreign:anInstance::private_address"} node_templates: anInstance: type: test.nodes.AbstractTest directives: - select """ % API_VERSION ) runner = CliRunner() # delete UNFURL_HOME try: UNFURL_HOME = os.environ.get("UNFURL_HOME") with runner.isolated_filesystem(): os.environ["UNFURL_HOME"] = "" with open("foreignmanifest.yaml", "w") as f: f.write(foreign) with open("unfurl.yaml", "w") as f: f.write(localConfig) with open("manifest.yaml", "w") as f: f.write(mainManifest) manifest = LocalEnv("manifest.yaml").getManifest() assert manifest.manifest.vault and manifest.manifest.vault.secrets job = Runner(manifest).run( JobOptions(add=True, startTime="time-to-test") ) # print(job.out.getvalue()) # print(job.jsonSummary(True)) assert job.status == Status.ok, job.summary() self.assertEqual( [ { "operation": "check", "configurator": "test_tosca.SetAttributeConfigurator", "changed": True, "priority": "required", "reason": "check", "status": "ok", "target": "foreign:anInstance", "targetStatus": "ok", "template": "anInstance", "type": "test.nodes.AbstractTest", } ], job.jsonSummary()["tasks"], ) job.getOutputs() self.assertEqual(job.getOutputs()["server_ip"], "10.0.0.1") self.assertEqual( len(manifest.localEnv._manifests), 2, manifest.localEnv._manifests ) # print("output", job.out.getvalue()) assert "10.0.0.1" not in job.out.getvalue(), job.out.getvalue() vaultString1 = "server_ip: !vault |\n $ANSIBLE_VAULT;1.1;AES256" assert vaultString1 in job.out.getvalue() vaultString2 = ( "private_address: !vault |\n $ANSIBLE_VAULT;1.1;AES256" ) assert vaultString2 in job.out.getvalue() # reload: manifest2 = LocalEnv("manifest.yaml").getManifest() assert manifest2.lastJob # test that restored manifest create a shadow instance for the foreign instance imported = manifest2.imports["foreign"].resource assert imported imported2 = manifest2.imports.findImport("foreign:anInstance") assert imported2 assert imported2.shadow self.assertIs(imported2.root, manifest2.getRootResource()) self.assertEqual(imported2.attributes["private_address"], "10.0.0.1") self.assertIsNot(imported2.shadow.root, manifest2.getRootResource()) finally: if UNFURL_HOME is not None: os.environ["UNFURL_HOME"] = UNFURL_HOME