github.com/myhau/pulumi/pkg/v3@v3.70.2-0.20221116134521-f2775972e587/codegen/testing/test/testdata/output-funcs-tfbridge20/nodejs-extras/tests/codegen.spec.ts (about)

     1  // Copyright 2016-2021, Pulumi Corporation.
     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 "mocha";
    16  import * as assert from "assert";
    17  
    18  import * as pulumi from "@pulumi/pulumi";
    19  
    20  import { listStorageAccountKeysOutput, ListStorageAccountKeysResult } from "../listStorageAccountKeys";
    21  import * as amiIds from "../getAmiIds";
    22  import { GetAmiIdsFilterArgs } from "../types/input";
    23  
    24  pulumi.runtime.setMocks({
    25      newResource: function(_: pulumi.runtime.MockResourceArgs): {id: string, state: any} {
    26          throw new Error("newResource not implemented");
    27      },
    28      call: function(args: pulumi.runtime.MockCallArgs) {
    29          if (args.token == "mypkg::listStorageAccountKeys") {
    30              return {
    31                  "keys": [{
    32                      "creationTime": "my-creation-time",
    33                      "keyName": "my-key-name",
    34                      "permissions": "my-permissions",
    35                      "value": JSON.stringify(args.inputs),
    36                  }]
    37              };
    38          }
    39  
    40          if (args.token == "mypkg::getAmiIds") {
    41              return {
    42                  sortAscending: args.inputs.sortAscending,
    43                  nameRegex: args.inputs.nameRegex,
    44                  owners: args.inputs.owners,
    45                  id: JSON.stringify({filters: args.inputs.filters})
    46              }
    47          }
    48  
    49          throw new Error("call not implemented for " + args.token);
    50      },
    51  });
    52  
    53  function checkTable(done: any, transform: (res: any) => any, table: {given: pulumi.Output<any>, expect: any}[]) {
    54      checkOutput(done, pulumi.all(table.map(x => x.given)), res => {
    55          res.map((actual, i) => {
    56              assert.deepStrictEqual(transform(actual), table[i].expect);
    57          });
    58      });
    59  }
    60  
    61  describe("output-funcs", () => {
    62  
    63      it("getAmiIdsOutput", (done) => {
    64  
    65          function filter(n: number): GetAmiIdsFilterArgs {
    66              return {
    67                  name: pulumi.output(`filter-${n}-name`),
    68                  values: [
    69                      pulumi.output(`filter-${n}-value-1`),
    70                      pulumi.output(`filter-${n}-value-2`)
    71                  ],
    72              }
    73          }
    74  
    75          const output = amiIds.getAmiIdsOutput({
    76              owners: [pulumi.output("owner-1"),
    77                       pulumi.output("owner-2")],
    78              nameRegex: pulumi.output("[a-z]"),
    79              sortAscending: pulumi.output(true),
    80              filters: [filter(1), filter(2)],
    81          });
    82  
    83          checkOutput(done, output, (res: amiIds.GetAmiIdsResult) => {
    84              assert.equal(res.sortAscending, true);
    85              assert.equal(res.nameRegex, "[a-z]");
    86              assert.deepStrictEqual(res.owners, ["owner-1", "owner-2"]);
    87  
    88              assert.deepStrictEqual(JSON.parse(res.id), {
    89                  filters: [
    90                      {
    91                          name: 'filter-1-name',
    92                          values: [
    93                              'filter-1-value-1',
    94                              'filter-1-value-2'
    95                          ]
    96                      },
    97                      {
    98                          name: 'filter-2-name',
    99                          values: [
   100                              'filter-2-value-1',
   101                              'filter-2-value-2'
   102                          ]
   103                      },
   104                  ]
   105              });
   106          });
   107      });
   108  
   109      it("listStorageAccountKeysOutput", (done) => {
   110          const output = listStorageAccountKeysOutput({
   111              accountName: pulumi.output("my-account-name"),
   112              resourceGroupName: pulumi.output("my-resource-group-name"),
   113          });
   114          checkOutput(done, output, (res: ListStorageAccountKeysResult) => {
   115              assert.equal(res.keys.length, 1);
   116              const k = res.keys[0];
   117              assert.equal(k.creationTime, "my-creation-time");
   118              assert.equal(k.keyName, "my-key-name");
   119              assert.equal(k.permissions, "my-permissions");
   120              assert.deepStrictEqual(JSON.parse(k.value), {
   121                  "accountName": "my-account-name",
   122                  "resourceGroupName": "my-resource-group-name"
   123              });
   124          });
   125      });
   126  
   127      it("listStorageAccountKeysOutput with optional arg set", (done) => {
   128          const output = listStorageAccountKeysOutput({
   129              accountName: pulumi.output("my-account-name"),
   130              resourceGroupName: pulumi.output("my-resource-group-name"),
   131              expand: pulumi.output("my-expand"),
   132          });
   133          checkOutput(done, output, (res: ListStorageAccountKeysResult) => {
   134              assert.equal(res.keys.length, 1);
   135              const k = res.keys[0];
   136              assert.equal(k.creationTime, "my-creation-time");
   137              assert.equal(k.keyName, "my-key-name");
   138              assert.equal(k.permissions, "my-permissions");
   139              assert.deepStrictEqual(JSON.parse(k.value), {
   140                  "accountName": "my-account-name",
   141                  "resourceGroupName": "my-resource-group-name",
   142                  "expand": "my-expand"
   143              });
   144          });
   145      });
   146  
   147   });
   148  
   149  
   150  function checkOutput<T>(done: any, output: pulumi.Output<T>, check: (value: T) => void) {
   151      output.apply(value => {
   152          try {
   153              check(value);
   154              done();
   155          } catch (error) {
   156              done(error);
   157          }
   158      });
   159  }