given this ```export const ymml = new k8s.yaml.Con...
# general
s
given this
Copy code
export const ymml = new k8s.yaml.ConfigFile('yamlfile', {
  file: 'foo.yaml',
});
and a test like this
Copy code
describe('yaml', function () {
    it('reads', function (done) {
      pulumi.all([infra.ymml.urn, infra.ymml]).apply(([urn, thefile]) => {
        console.log(urn);
        var val = thefile.getResource('v1/Service', 'my-service');
        console.log(val);
        done();
      });
    });
  });
in a unit test is there away to see the output of the file? At the moment I get this
Copy code
yaml
urn:pulumi:stack::project::kubernetes:yaml:ConfigFile::yamlfile
OutputImpl {
  __pulumiOutput: true,
  resources: [Function (anonymous)],
  allResources: [Function (anonymous)],
  isKnown: Promise { <pending> },
  isSecret: Promise { <pending> },
  promise: [Function (anonymous)],
  toString: [Function (anonymous)],
  toJSON: [Function (anonymous)]
}
      ✓ reads
(node:58505) PromiseRejectionHandledWarning: Promise rejection was handled asynchronously (rejection id: 3)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:58505) PromiseRejectionHandledWarning: Promise rejection was handled asynchronously (rejection id: 4)
I am trying to understand an error that looks like this when running that deploys a blackbox exporter. I can apply it using
kubectl apply
but it's failing as below in pulumi
Error: invocation of kubernetes:yaml:decode returned an error: error converting YAML to JSON: yaml control characters are not allowed
l
I convert outputs like this to promises, then use the ChaiAsExpected library to achieve it. The alternative is to have all your expects / shoulds inside
apply()
blocks, and that's a bit tricky to synchonize properly.
However if all you want to do is log the val to the console, use
val.apply((contents) => console.log(contents));
s
https://github.com/pulumi/pulumi/issues/5924 what's being talked about here w.r.t using promises on outputs
l
Can you rephrase the question?
There is an internal Pulumi function that immediately returns a Promise from an Output. This is described in the GitHub issue. It can be useful when unit testing, because there are unit testing libraries that support Promises, but none that support Outputs.
The alternative is to make your unit test fully async (which Jest and Mocha support, and probably all common libraries), and do your assertions inside
apply()
blocks.
However your original question seems only to want to know how to look at the contents of an output, rather than how to assert on them.
In which case, simply moving your log statement into the apply block is all you need to do.
s
Will you share a code sample of how you assert with ChaiAsExpected that you mentioned above?
l
Sure, onesec..
s
found my problem using this. Copy pasted content into a new file. The problem file was initialiy created using an io redirect
>
Copy code
const yaml = require('js-yaml');
const fs = require('fs');
describe('js-yaml', function () {
  it('read', function (done) {
    let fileContent = fs.readFileSync('./foo.yml', 'utf-8');
    let data = yaml.load(fileContent);

    console.log(data);
    done();
  });
});
I do not appear to be getting output to the console using this snippet
Copy code
describe('yaml', function () {
    it('reads', function (done) {
      pulumi.all([infra.ymml.urn, infra.ymml]).apply(([urn, thefile]) => {
        console.log(urn);
        var val = thefile.getResource('v1/Service', 'my-service');
        val.apply((content) => console.log(content.apiVersion));
        done();
      });
    });
  });
});
where
foo.yaml
looks like this
Copy code
apiVersion: v1
kind: Service
metadata:
  name: my-service
spec:
  selector:
    app: MyApp
  ports:
    - protocol: TCP
      port: 80
      targetPort: 9376
the output is
Copy code
yaml
urn:pulumi:stack::project::kubernetes:yaml:ConfigFile::yamlfile
      ✓ reads
(node:72056) PromiseRejectionHandledWarning: Promise rejection was handled asynchronously (rejection id: 3)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:72056) PromiseRejectionHandledWarning: Promise rejection was handled asynchronously (rejection id: 4)
l
Sorry my internet has died, on phone atm.
The problem with using apply in tests is that the tests may finish before the promise resolves. Or as in this case, the promise may be rejected, leaving you with very little information.
I think you need to add
async
to the function you pass to
it
It's not waiting for the apply to finish.