|
| 1 | +import { expect } from "chai"; |
| 2 | +import * as tf from "./terraform"; |
| 3 | + |
| 4 | +describe("terraform iac", () => { |
| 5 | + describe("expr", () => { |
| 6 | + it("should return an HCLExpression object", () => { |
| 7 | + expect(tf.expr("var.foo")).to.deep.equal({ |
| 8 | + "@type": "HCLExpression", |
| 9 | + value: "var.foo", |
| 10 | + }); |
| 11 | + }); |
| 12 | + }); |
| 13 | + |
| 14 | + describe("copyField", () => { |
| 15 | + it("should copy a field to attributes, converting its name to lower snake case", () => { |
| 16 | + const attrs: any = {}; |
| 17 | + tf.copyField(attrs, { camelCaseField: "value" }, "camelCaseField"); |
| 18 | + expect(attrs).to.deep.equal({ camel_case_field: "value" }); |
| 19 | + }); |
| 20 | + |
| 21 | + it("should optionally transform the field value", () => { |
| 22 | + const attrs: any = {}; |
| 23 | + tf.copyField(attrs, { field: 42 }, "field", (v) => (v as number) * 2); |
| 24 | + expect(attrs).to.deep.equal({ field: 84 }); |
| 25 | + }); |
| 26 | + |
| 27 | + it("should not set anything if the source field is missing", () => { |
| 28 | + const attrs: any = {}; |
| 29 | + tf.copyField(attrs, { other: 123 } as any, "field"); |
| 30 | + expect(attrs).to.deep.equal({}); |
| 31 | + }); |
| 32 | + }); |
| 33 | + |
| 34 | + describe("renameField", () => { |
| 35 | + it("should copy a field to a different attribute name", () => { |
| 36 | + const attrs: any = {}; |
| 37 | + tf.renameField(attrs, { field: "value" }, "new_field", "field"); |
| 38 | + expect(attrs).to.deep.equal({ new_field: "value" }); |
| 39 | + }); |
| 40 | + }); |
| 41 | + |
| 42 | + describe("serviceAccount", () => { |
| 43 | + it("should append the IAM domain for short service accounts", () => { |
| 44 | + expect(tf.serviceAccount("my-sa@")).to.equal("my-sa@${var.project}.iam.gserviceaccount.com"); |
| 45 | + }); |
| 46 | + |
| 47 | + it("should return identical string if not ending in @", () => { |
| 48 | + expect(tf.serviceAccount("foo@bar.com")).to.equal("foo@bar.com"); |
| 49 | + }); |
| 50 | + }); |
| 51 | + |
| 52 | + describe("serializeValue", () => { |
| 53 | + it("should serialize strings correctly and inject variable", () => { |
| 54 | + expect(tf.serializeValue("foo")).to.equal('"foo"'); |
| 55 | + expect(tf.serializeValue("proj: {{ params.PROJECT_ID }}")).to.equal('"proj: ${var.project}"'); |
| 56 | + }); |
| 57 | + |
| 58 | + it("should throw for other parameterized fields", () => { |
| 59 | + expect(() => tf.serializeValue("param: {{ params.OTHER }}")).to.throw( |
| 60 | + "Generalized parameterized fields are not supported in terraform yet", |
| 61 | + ); |
| 62 | + }); |
| 63 | + |
| 64 | + it("should serialize numbers and booleans", () => { |
| 65 | + expect(tf.serializeValue(42)).to.equal("42"); |
| 66 | + expect(tf.serializeValue(true)).to.equal("true"); |
| 67 | + }); |
| 68 | + |
| 69 | + it("should serialize null properly", () => { |
| 70 | + expect(tf.serializeValue(null)).to.equal("null"); |
| 71 | + }); |
| 72 | + |
| 73 | + it("should serialize an expression without quotes", () => { |
| 74 | + expect(tf.serializeValue(tf.expr("var.abc"))).to.equal("var.abc"); |
| 75 | + }); |
| 76 | + |
| 77 | + it("should serialize simple arrays inline", () => { |
| 78 | + expect(tf.serializeValue([1, 2, 3])).to.equal("[1, 2, 3]"); |
| 79 | + }); |
| 80 | + |
| 81 | + it("should serialize arrays of objects with line breaks", () => { |
| 82 | + expect(tf.serializeValue([{ a: 1 }])).to.equal("[\n {\n a = 1\n }\n]"); |
| 83 | + }); |
| 84 | + |
| 85 | + it("should serialize nested objects with proper indentation", () => { |
| 86 | + expect(tf.serializeValue({ a: { b: 2 } })).to.equal("{\n a = {\n b = 2\n }\n}"); |
| 87 | + }); |
| 88 | + }); |
| 89 | + |
| 90 | + describe("blockToString", () => { |
| 91 | + it("should stringify a block without labels", () => { |
| 92 | + expect( |
| 93 | + tf.blockToString({ |
| 94 | + type: "locals", |
| 95 | + attributes: { foo: "bar" }, |
| 96 | + }), |
| 97 | + ).to.equal('locals {\n foo = "bar"\n}'); |
| 98 | + }); |
| 99 | + |
| 100 | + it("should stringify a block with labels", () => { |
| 101 | + expect( |
| 102 | + tf.blockToString({ |
| 103 | + type: "resource", |
| 104 | + labels: ["google_function", "my_func"], |
| 105 | + attributes: { name: "test" }, |
| 106 | + }), |
| 107 | + ).to.equal('resource "google_function" "my_func" {\n name = "test"\n}'); |
| 108 | + }); |
| 109 | + }); |
| 110 | +}); |
0 commit comments