-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathComponentAnalyzerTests.cs
927 lines (775 loc) · 34.5 KB
/
ComponentAnalyzerTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
// Copyright 2025, Pulumi Corporation
namespace Pulumi.Tests.Provider;
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Text.Json;
using System.Text.Json.Serialization;
using Xunit;
using Pulumi.Experimental.Provider;
public class ComponentAnalyzerTests
{
[Fact]
public void TestGenerateSchemaWithMetadata()
{
var metadata = new Metadata("test");
var schema = ComponentAnalyzer.GenerateSchema(metadata, typeof(SelfSignedCertificate));
// Only verify the package name and resource type prefix
Assert.Equal("test", schema.Name);
Assert.Equal("test", schema.DisplayName);
Assert.Equal("", schema.Version);
Assert.Single(schema.Resources);
Assert.Contains("test:index:SelfSignedCertificate", schema.Resources.Keys);
}
[Fact]
public void TestGenerateSchemaWithNoClasses()
{
var metadata = new Metadata("test");
var exception = Assert.Throws<ArgumentException>(() =>
ComponentAnalyzer.GenerateSchema(metadata, Array.Empty<Type>()));
Assert.Equal("At least one component type must be provided", exception.Message);
}
class SelfSignedCertificateArgs : ResourceArgs
{
[Input("algorithm", required: true)]
public Input<string> Algorithm { get; set; } = null!;
[Input("ecdsaCurve")]
public Input<string>? EcdsaCurve { get; set; }
[Input("bits")]
public Input<int>? Bits { get; set; }
}
class SelfSignedCertificate : ComponentResource
{
[Output("pem")]
public Output<string> Pem { get; private set; } = null!;
[Output("privateKey")]
public Output<string> PrivateKey { get; private set; } = null!;
[Output("caCert")]
public Output<string?> CaCert { get; private set; } = null!;
public SelfSignedCertificate(string name, SelfSignedCertificateArgs args, ComponentResourceOptions? options = null)
: base("pkg:index:SelfSignedCertificate", name, args, options)
{
// Implementation not needed for schema testing
}
}
[Fact]
public void TestAnalyzeComponent()
{
var schema = ComponentAnalyzer.GenerateSchema(_metadata, typeof(SelfSignedCertificate));
var resources = new Dictionary<string, ResourceSpec>();
resources.Add("my-component:index:SelfSignedCertificate", new ResourceSpec(
new Dictionary<string, PropertySpec>
{
["algorithm"] = PropertySpec.String,
["ecdsaCurve"] = PropertySpec.String,
["bits"] = PropertySpec.Integer
},
new HashSet<string> { "algorithm" },
new Dictionary<string, PropertySpec>
{
["pem"] = PropertySpec.String,
["privateKey"] = PropertySpec.String,
["caCert"] = PropertySpec.String
},
new HashSet<string> { "pem", "privateKey" }));
var expected = CreateBasePackageSpec(resources);
AssertSchemaEqual(expected, schema);
}
class NoArgsComponent : ComponentResource
{
public NoArgsComponent()
: base("my-component:index:NoArgsComponent", "test")
{
}
public NoArgsComponent(string name)
: base("my-component:index:NoArgsComponent", name)
{
}
public NoArgsComponent(string name, EmptyArgs args)
: base("my-component:index:NoArgsComponent", name, args)
{
}
public NoArgsComponent(string name, ComponentResourceOptions? options = null)
: base("my-component:index:NoArgsComponent", name, ResourceArgs.Empty, options)
{
}
}
[Fact]
public void TestAnalyzeComponentNoArgs()
{
var exception = Assert.Throws<ArgumentException>(() =>
ComponentAnalyzer.GenerateSchema(_metadata, typeof(NoArgsComponent)));
Assert.Equal(
$"Component {nameof(NoArgsComponent)} must have a constructor with exactly three parameters: " +
"a string name, a parameter that extends ResourceArgs, and ComponentResourceOptions",
exception.Message);
}
class EmptyArgs : ResourceArgs
{
// Empty args class
}
class EmptyComponent : ComponentResource
{
public EmptyComponent(string name, EmptyArgs args, ComponentResourceOptions? options = null)
: base("my-component:index:EmptyComponent", name, args, options)
{
}
}
[Fact]
public void TestAnalyzeComponentEmpty()
{
var schema = ComponentAnalyzer.GenerateSchema(_metadata, typeof(EmptyComponent));
var resources = new Dictionary<string, ResourceSpec>();
resources.Add("my-component:index:EmptyComponent",
new ResourceSpec(
new Dictionary<string, PropertySpec>(),
new HashSet<string>(),
new Dictionary<string, PropertySpec>(),
new HashSet<string>()));
var expected = CreateBasePackageSpec(resources);
AssertSchemaEqual(expected, schema);
}
class ComplexTypeArgs : ResourceArgs
{
[Input("aStr", required: true)]
public string AStr { get; set; } = null!;
[Input("aListStr", required: false)]
public List<string>? AListStr { get; set; }
[Input("aDictStr", required: false)]
public Dictionary<string, string>? ADictStr { get; set; }
}
class PlainTypesArgs : ResourceArgs
{
[Input("aInt", required: true)]
public int AInt { get; set; }
[Input("aStr", required: true)]
public string AStr { get; set; } = null!;
[Input("aFloat", required: true)]
public double AFloat { get; set; }
[Input("aBool", required: true)]
public bool ABool { get; set; }
[Input("aOptional", required: false)]
public string? AOptional { get; set; }
[Input("aList", required: true)]
public List<string> AList { get; set; } = null!;
[Input("aInputList", required: true)]
public Input<List<string>> AInputList { get; set; } = null!;
[Input("aListInput", required: true)]
public List<Input<string>> AListInput { get; set; } = null!;
[Input("aDict", required: true)]
public Dictionary<string, int> ADict { get; set; } = null!;
[Input("aDictInput", required: true)]
public Dictionary<string, Input<int>> ADictInput { get; set; } = null!;
[Input("aInputDict", required: true)]
public Input<Dictionary<string, int>> AInputDict { get; set; } = null!;
[Input("aInputDictInput", required: true)]
public Input<Dictionary<string, Input<int>>> AInputDictInput { get; set; } = null!;
[Input("aComplexType", required: true)]
public ComplexTypeArgs AComplexType { get; set; } = null!;
[Input("aInputComplexType", required: true)]
public Input<ComplexTypeArgs> AInputComplexType { get; set; } = null!;
}
class PlainTypesComponent : ComponentResource
{
// Outputs are never plain.
public PlainTypesComponent(string name, PlainTypesArgs args, ComponentResourceOptions? options = null)
: base("my-component:index:PlainTypesComponent", name, args, options)
{
}
}
[Fact]
public void TestAnalyzeComponentPlainTypes()
{
var schema = ComponentAnalyzer.GenerateSchema(_metadata, typeof(PlainTypesComponent));
var inputs = new Dictionary<string, PropertySpec>();
// Basic types
inputs["aInt"] = PropertySpec.CreateBuiltin(BuiltinTypeSpec.Integer, true);
inputs["aStr"] = PropertySpec.CreateBuiltin(BuiltinTypeSpec.String, true);
inputs["aFloat"] = PropertySpec.CreateBuiltin(BuiltinTypeSpec.Number, true);
inputs["aBool"] = PropertySpec.CreateBuiltin(BuiltinTypeSpec.Boolean, true);
inputs["aOptional"] = PropertySpec.CreateBuiltin(BuiltinTypeSpec.String, true);
// Lists
inputs["aList"] = PropertySpec.CreateArray(TypeSpec.CreateBuiltin(BuiltinTypeSpec.String, true));
inputs["aInputList"] = PropertySpec.CreateArray(TypeSpec.CreateBuiltin(BuiltinTypeSpec.String, true));
inputs["aListInput"] = PropertySpec.CreateArray(TypeSpec.CreateBuiltin(BuiltinTypeSpec.String));
// Maps
inputs["aDict"] = PropertySpec.CreateDictionary(TypeSpec.CreateBuiltin(BuiltinTypeSpec.Integer, true));
inputs["aDictInput"] = PropertySpec.CreateDictionary(TypeSpec.CreateBuiltin(BuiltinTypeSpec.Integer));
inputs["aInputDict"] = PropertySpec.CreateDictionary(TypeSpec.CreateBuiltin(BuiltinTypeSpec.Integer, true));
inputs["aInputDictInput"] = PropertySpec.CreateDictionary(TypeSpec.CreateBuiltin(BuiltinTypeSpec.Integer));
// Complex types
inputs["aComplexType"] = PropertySpec.CreateReference("#/types/my-component:index:ComplexType", true);
inputs["aInputComplexType"] = PropertySpec.CreateReference("#/types/my-component:index:ComplexType");
var requiredInputs = new HashSet<string> {
"aInt", "aStr", "aFloat", "aBool", "aList", "aInputList", "aListInput",
"aDict", "aDictInput", "aInputDict", "aInputDictInput",
"aComplexType", "aInputComplexType"
};
var resources = new Dictionary<string, ResourceSpec>();
resources.Add("my-component:index:PlainTypesComponent",
new ResourceSpec(inputs, requiredInputs, new Dictionary<string, PropertySpec>(), new HashSet<string>()));
var types = new Dictionary<string, ComplexTypeSpec>
{
["my-component:index:ComplexType"] = ComplexTypeSpec.CreateObject(
new Dictionary<string, PropertySpec>
{
["aStr"] = PropertySpec.CreateBuiltin(BuiltinTypeSpec.String, true),
["aListStr"] = PropertySpec.CreateArray(TypeSpec.CreateBuiltin(BuiltinTypeSpec.String, true)),
["aDictStr"] = PropertySpec.CreateDictionary(TypeSpec.CreateBuiltin(BuiltinTypeSpec.String, true))
},
new HashSet<string> { "aStr" }
)
};
var expected = CreateBasePackageSpec(resources, types);
AssertSchemaEqual(expected, schema);
}
class ListTypesArgs : ResourceArgs
{
[Input("requiredList", required: true)]
public Input<List<string>> RequiredList { get; set; } = null!;
[Input("optionalList", required: false)]
public Input<List<string>>? OptionalList { get; set; }
[Input("array")]
public Input<string[]>? Array { get; set; }
[Input("complexList", required: true)]
public Input<List<ComplexListTypeArgs>> ComplexList { get; set; } = null!;
[Input("inputList", required: true)]
public InputList<string> InputList { get; set; } = null!;
[Input("optionalInputList", required: false)]
public InputList<string>? OptionalInputList { get; set; }
[Input("complexInputList", required: true)]
public InputList<ComplexListTypeArgs> ComplexInputList { get; set; } = null!;
}
class ComplexListTypeArgs : ResourceArgs
{
[Input("name", required: false)]
public Input<List<string>>? Name { get; set; }
}
class ComplexOutputListType
{
[Output("name")]
public Output<List<string>> Name { get; set; } = null!;
}
class ListTypesComponent : ComponentResource
{
[Output("simpleList")]
public Output<List<string>> SimpleList { get; private set; } = null!;
[Output("complexList")]
public Output<ComplexOutputListType[]> ComplexList { get; private set; } = null!;
public ListTypesComponent(string name, ListTypesArgs args, ComponentResourceOptions? options = null)
: base("my-component:index:ListTypesComponent", name, args, options)
{
}
}
[Fact]
public void TestAnalyzeList()
{
var schema = ComponentAnalyzer.GenerateSchema(_metadata, typeof(ListTypesComponent));
var inputs = new Dictionary<string, PropertySpec>
{
["requiredList"] = PropertySpec.CreateArray(TypeSpec.CreateBuiltin(BuiltinTypeSpec.String, true)),
["optionalList"] = PropertySpec.CreateArray(TypeSpec.CreateBuiltin(BuiltinTypeSpec.String, true)),
["array"] = PropertySpec.CreateArray(TypeSpec.CreateBuiltin(BuiltinTypeSpec.String, true)),
["complexList"] = PropertySpec.CreateArray(
TypeSpec.CreateReference("#/types/my-component:index:ComplexListType", true)
),
["inputList"] = PropertySpec.CreateArray(TypeSpec.CreateBuiltin(BuiltinTypeSpec.String)),
["optionalInputList"] = PropertySpec.CreateArray(TypeSpec.CreateBuiltin(BuiltinTypeSpec.String)),
["complexInputList"] = PropertySpec.CreateArray(
TypeSpec.CreateReference("#/types/my-component:index:ComplexListType")
)
};
var requiredInputs = new HashSet<string> {
"requiredList", "complexList", "inputList", "complexInputList"
};
var outputs = new Dictionary<string, PropertySpec>
{
["simpleList"] = PropertySpec.CreateArray(TypeSpec.CreateBuiltin(BuiltinTypeSpec.String)),
["complexList"] = PropertySpec.CreateArray(
TypeSpec.CreateReference("#/types/my-component:index:ComplexOutputListType")
)
};
var resources = new Dictionary<string, ResourceSpec>();
resources.Add("my-component:index:ListTypesComponent",
new ResourceSpec(
inputs,
requiredInputs,
outputs,
new HashSet<string> { "simpleList", "complexList" }
));
var types = new Dictionary<string, ComplexTypeSpec>
{
["my-component:index:ComplexListType"] = ComplexTypeSpec.CreateObject(
new Dictionary<string, PropertySpec>
{
["name"] = PropertySpec.CreateArray(TypeSpec.CreateBuiltin(BuiltinTypeSpec.String, true))
},
new HashSet<string>()
),
["my-component:index:ComplexOutputListType"] = ComplexTypeSpec.CreateObject(
new Dictionary<string, PropertySpec>
{
["name"] = PropertySpec.CreateArray(TypeSpec.CreateBuiltin(BuiltinTypeSpec.String))
},
new HashSet<string> { "name" }
)
};
var expected = CreateBasePackageSpec(resources, types);
AssertSchemaEqual(expected, schema);
}
class NonStringMapKeyArgs : ResourceArgs
{
[Input("badDict", required: true)]
public Dictionary<int, string> BadDict { get; set; } = null!;
}
class NonStringMapKeyComponent : ComponentResource
{
public NonStringMapKeyComponent(string name, NonStringMapKeyArgs args, ComponentResourceOptions? options = null)
: base("my-component:index:NonStringMapKeyComponent", name, args, options)
{
}
}
[Fact]
public void TestAnalyzeMapNonStringKey()
{
var exception = Assert.Throws<ArgumentException>(() =>
ComponentAnalyzer.GenerateSchema(_metadata, typeof(NonStringMapKeyComponent)));
Assert.Equal(
"Dictionary keys must be strings, got 'Int32' for 'NonStringMapKeyArgs.BadDict'",
exception.Message);
}
class ComplexDictOutputType
{
[Output("name")]
public Output<Dictionary<string, int>> Name { get; set; } = null!;
}
class ComplexDictTypeArgs : ResourceArgs
{
[Input("name", required: false)]
public Input<Dictionary<string, int>>? Name { get; set; }
}
class DictTypesArgs : ResourceArgs
{
[Input("dictInput", required: true)]
public Input<Dictionary<string, int>> DictInput { get; set; } = null!;
[Input("complexDictInput", required: true)]
public Input<Dictionary<string, ComplexDictTypeArgs>> ComplexDictInput { get; set; } = null!;
[Input("inputMap", required: true)]
public InputMap<string> InputMap { get; set; } = null!;
[Input("complexInputMap", required: true)]
public InputMap<ComplexDictTypeArgs> ComplexInputMap { get; set; } = null!;
}
class DictTypesComponent : ComponentResource
{
[Output("dictOutput")]
public Output<Dictionary<string, int>> DictOutput { get; private set; } = null!;
[Output("complexDictOutput")]
public Output<Dictionary<string, ComplexDictOutputType>> ComplexDictOutput { get; private set; } = null!;
public DictTypesComponent(string name, DictTypesArgs args, ComponentResourceOptions? options = null)
: base("my-component:index:DictTypesComponent", name, args, options)
{
}
}
[Fact]
public void TestAnalyzeDict()
{
var schema = ComponentAnalyzer.GenerateSchema(_metadata, typeof(DictTypesComponent));
var resources = new Dictionary<string, ResourceSpec>();
resources.Add("my-component:index:DictTypesComponent",
new ResourceSpec(
new Dictionary<string, PropertySpec>
{
["dictInput"] = PropertySpec.CreateDictionary(TypeSpec.CreateBuiltin(BuiltinTypeSpec.Integer, true)),
["complexDictInput"] = PropertySpec.CreateDictionary(
TypeSpec.CreateReference("#/types/my-component:index:ComplexDictType", true)),
["inputMap"] = PropertySpec.CreateDictionary(TypeSpec.CreateBuiltin(BuiltinTypeSpec.String)),
["complexInputMap"] = PropertySpec.CreateDictionary(
TypeSpec.CreateReference("#/types/my-component:index:ComplexDictType"))
},
new HashSet<string> { "dictInput", "complexDictInput", "inputMap", "complexInputMap" },
new Dictionary<string, PropertySpec>
{
["dictOutput"] = PropertySpec.CreateDictionary(TypeSpec.CreateBuiltin(BuiltinTypeSpec.Integer)),
["complexDictOutput"] = PropertySpec.CreateDictionary(
TypeSpec.CreateReference("#/types/my-component:index:ComplexDictOutputType"))
},
new HashSet<string> { "dictOutput", "complexDictOutput" }));
var types = new Dictionary<string, ComplexTypeSpec>
{
["my-component:index:ComplexDictType"] = ComplexTypeSpec.CreateObject(
new Dictionary<string, PropertySpec>
{
["name"] = PropertySpec.CreateDictionary(TypeSpec.CreateBuiltin(BuiltinTypeSpec.Integer, true))
},
new HashSet<string>()),
["my-component:index:ComplexDictOutputType"] = ComplexTypeSpec.CreateObject(
new Dictionary<string, PropertySpec>
{
["name"] = PropertySpec.CreateDictionary(TypeSpec.CreateBuiltin(BuiltinTypeSpec.Integer))
},
new HashSet<string> { "name" })
};
var expected = CreateBasePackageSpec(resources, types);
AssertSchemaEqual(expected, schema);
}
class ArchiveTypesArgs : ResourceArgs
{
[Input("inputArchive", required: true)]
public Input<Archive> InputArchive { get; set; } = null!;
}
class ArchiveTypesComponent : ComponentResource
{
[Output("outputArchive")]
public Output<Archive> OutputArchive { get; private set; } = null!;
public ArchiveTypesComponent(string name, ArchiveTypesArgs args, ComponentResourceOptions? options = null)
: base("my-component:index:ArchiveTypesComponent", name, args, options)
{
}
}
[Fact]
public void TestAnalyzeArchive()
{
var schema = ComponentAnalyzer.GenerateSchema(_metadata, typeof(ArchiveTypesComponent));
var resources = new Dictionary<string, ResourceSpec>();
resources.Add("my-component:index:ArchiveTypesComponent",
new ResourceSpec(
new Dictionary<string, PropertySpec>
{
["inputArchive"] = PropertySpec.CreateReference("pulumi.json#/Archive")
},
new HashSet<string> { "inputArchive" },
new Dictionary<string, PropertySpec>
{
["outputArchive"] = PropertySpec.CreateReference("pulumi.json#/Archive")
},
new HashSet<string> { "outputArchive" }));
var expected = CreateBasePackageSpec(resources);
AssertSchemaEqual(expected, schema);
}
class AssetTypesArgs : ResourceArgs
{
[Input("inputAsset", required: true)]
public Input<Asset> InputAsset { get; set; } = null!;
}
class AssetTypesComponent : ComponentResource
{
[Output("outputAsset")]
public Output<Asset> OutputAsset { get; private set; } = null!;
public AssetTypesComponent(string name, AssetTypesArgs args, ComponentResourceOptions? options = null)
: base("my-component:index:AssetTypesComponent", name, args, options)
{
}
}
[Fact]
public void TestAnalyzeAsset()
{
var schema = ComponentAnalyzer.GenerateSchema(_metadata, typeof(AssetTypesComponent));
var resources = new Dictionary<string, ResourceSpec>();
resources.Add("my-component:index:AssetTypesComponent",
new ResourceSpec(
new Dictionary<string, PropertySpec>
{
["inputAsset"] = PropertySpec.CreateReference("pulumi.json#/Asset")
},
new HashSet<string> { "inputAsset" },
new Dictionary<string, PropertySpec>
{
["outputAsset"] = PropertySpec.CreateReference("pulumi.json#/Asset")
},
new HashSet<string> { "outputAsset" }));
var expected = CreateBasePackageSpec(resources);
AssertSchemaEqual(expected, schema);
}
class MyResource : CustomResource
{
public MyResource(string name)
: base("my-component:index:MyResource", name, ResourceArgs.Empty, null)
{
}
}
class ResourceRefComponentArgs : ResourceArgs
{
[Input("resource", required: true)]
public Input<MyResource> Resource { get; set; } = null!;
}
class ResourceRefComponent : ComponentResource
{
public ResourceRefComponent(string name, ResourceRefComponentArgs args, ComponentResourceOptions? options = null)
: base("my-component:index:ResourceRefComponent", name, args, options)
{
}
}
[Fact]
public void TestAnalyzeResourceRef()
{
var exception = Assert.Throws<ArgumentException>(() =>
ComponentAnalyzer.GenerateSchema(_metadata, typeof(ResourceRefComponent)));
Assert.Equal(
"Resource references are not supported yet: found type 'MyResource' for 'ResourceRefComponentArgs.Resource'",
exception.Message);
}
class RecursiveTypeArgs : ResourceArgs
{
[Input("rec", required: false)]
public Input<RecursiveTypeArgs>? Rec { get; set; }
}
class RecursiveOutputType
{
[Output("rec")]
public Output<RecursiveOutputType> Rec { get; set; } = null!;
}
class RecursiveComponentArgs : ResourceArgs
{
[Input("rec", required: true)]
public Input<RecursiveTypeArgs> Rec { get; set; } = null!;
}
class RecursiveComponent : ComponentResource
{
[Output("rec")]
public Output<RecursiveOutputType> Rec { get; private set; } = null!;
public RecursiveComponent(string name, RecursiveComponentArgs args, ComponentResourceOptions? options = null)
: base("my-component:index:RecursiveComponent", name, args, options)
{
}
}
[Fact]
public void TestAnalyzeComponentSelfRecursiveComplexType()
{
var schema = ComponentAnalyzer.GenerateSchema(_metadata, typeof(RecursiveComponent));
var resources = new Dictionary<string, ResourceSpec>();
resources.Add("my-component:index:RecursiveComponent",
new ResourceSpec(
new Dictionary<string, PropertySpec>
{
["rec"] = PropertySpec.CreateReference("#/types/my-component:index:RecursiveType")
},
new HashSet<string> { "rec" },
new Dictionary<string, PropertySpec>
{
["rec"] = PropertySpec.CreateReference("#/types/my-component:index:RecursiveOutputType")
},
new HashSet<string> { "rec" }));
var types = new Dictionary<string, ComplexTypeSpec>
{
["my-component:index:RecursiveType"] = ComplexTypeSpec.CreateObject(
new Dictionary<string, PropertySpec>
{
["rec"] = PropertySpec.CreateReference("#/types/my-component:index:RecursiveType")
},
new HashSet<string>()),
["my-component:index:RecursiveOutputType"] = ComplexTypeSpec.CreateObject(
new Dictionary<string, PropertySpec>
{
["rec"] = PropertySpec.CreateReference("#/types/my-component:index:RecursiveOutputType")
},
new HashSet<string> { "rec" })
};
var expected = CreateBasePackageSpec(resources, types);
AssertSchemaEqual(expected, schema);
}
class RecursiveTypeAArgs : ResourceArgs
{
[Input("b", required: false)]
public Input<RecursiveTypeBArgs>? B { get; set; }
}
class RecursiveTypeBArgs : ResourceArgs
{
[Input("a", required: false)]
public Input<RecursiveTypeAArgs>? A { get; set; }
}
class RecursiveTypeAOutput
{
[Output("b")]
public Output<RecursiveTypeBOutput> B { get; set; } = null!;
}
class RecursiveTypeBOutput
{
[Output("a")]
public Output<RecursiveTypeAOutput> A { get; set; } = null!;
}
class MutuallyRecursiveComponentArgs : ResourceArgs
{
[Input("rec", required: true)]
public Input<RecursiveTypeAArgs> Rec { get; set; } = null!;
}
class MutuallyRecursiveComponent : ComponentResource
{
[Output("rec")]
public Output<RecursiveTypeBOutput> Rec { get; private set; } = null!;
public MutuallyRecursiveComponent(string name, MutuallyRecursiveComponentArgs args, ComponentResourceOptions? options = null)
: base("my-component:index:MutuallyRecursiveComponent", name, args, options)
{
}
}
[Fact]
public void TestAnalyzeComponentMutuallyRecursiveComplexTypes()
{
var schema = ComponentAnalyzer.GenerateSchema(_metadata, typeof(MutuallyRecursiveComponent));
var resources = new Dictionary<string, ResourceSpec>();
resources.Add("my-component:index:MutuallyRecursiveComponent",
new ResourceSpec(
new Dictionary<string, PropertySpec>
{
["rec"] = PropertySpec.CreateReference("#/types/my-component:index:RecursiveTypeA")
},
new HashSet<string> { "rec" },
new Dictionary<string, PropertySpec>
{
["rec"] = PropertySpec.CreateReference("#/types/my-component:index:RecursiveTypeBOutput")
},
new HashSet<string> { "rec" }));
var types = new Dictionary<string, ComplexTypeSpec>
{
["my-component:index:RecursiveTypeA"] = ComplexTypeSpec.CreateObject(
new Dictionary<string, PropertySpec>
{
["b"] = PropertySpec.CreateReference("#/types/my-component:index:RecursiveTypeB")
},
new HashSet<string>()),
["my-component:index:RecursiveTypeB"] = ComplexTypeSpec.CreateObject(
new Dictionary<string, PropertySpec>
{
["a"] = PropertySpec.CreateReference("#/types/my-component:index:RecursiveTypeA")
},
new HashSet<string>()),
["my-component:index:RecursiveTypeAOutput"] = ComplexTypeSpec.CreateObject(
new Dictionary<string, PropertySpec>
{
["b"] = PropertySpec.CreateReference("#/types/my-component:index:RecursiveTypeBOutput")
},
new HashSet<string> { "b" }),
["my-component:index:RecursiveTypeBOutput"] = ComplexTypeSpec.CreateObject(
new Dictionary<string, PropertySpec>
{
["a"] = PropertySpec.CreateReference("#/types/my-component:index:RecursiveTypeAOutput")
},
new HashSet<string> { "a" })
};
var expected = CreateBasePackageSpec(resources, types);
AssertSchemaEqual(expected, schema);
}
class RequiredOptionalTypeArgs : ResourceArgs
{
[Input("requiredString", required: true)]
public Input<string> RequiredString { get; set; } = null!;
[Input("optionalString")]
public Input<string>? OptionalString { get; set; }
[Input("requiredInt", required: true)]
public Input<int> RequiredInt { get; set; } = null!;
[Input("optionalInt")]
public Input<int>? OptionalInt { get; set; }
[Input("requiredBool", required: true)]
public Input<bool> RequiredBool { get; set; } = null!;
[Input("optionalBool")]
public Input<bool>? OptionalBool { get; set; }
}
class RequiredOptionalComponent : ComponentResource
{
[Output("requiredOutput")]
public Output<string> RequiredOutput { get; private set; } = null!;
[Output("optionalOutput")]
public Output<string?> OptionalOutput { get; private set; } = null!;
[Output("requiredIntOutput")]
public Output<int> RequiredIntOutput { get; private set; } = null!;
[Output("optionalIntOutput")]
public Output<int?> OptionalIntOutput { get; private set; } = null!;
public RequiredOptionalComponent(string name, RequiredOptionalTypeArgs args, ComponentResourceOptions? options = null)
: base("my-component:index:RequiredOptionalComponent", name, args, options)
{
}
}
[Fact]
public void TestAnalyzeRequiredOptionalProperties()
{
var schema = ComponentAnalyzer.GenerateSchema(_metadata, typeof(RequiredOptionalComponent));
var inputs = new Dictionary<string, PropertySpec>
{
["requiredString"] = PropertySpec.String,
["optionalString"] = PropertySpec.String,
["requiredInt"] = PropertySpec.Integer,
["optionalInt"] = PropertySpec.Integer,
["requiredBool"] = PropertySpec.Boolean,
["optionalBool"] = PropertySpec.Boolean
};
var outputs = new Dictionary<string, PropertySpec>
{
["requiredOutput"] = PropertySpec.String,
["optionalOutput"] = PropertySpec.String,
["requiredIntOutput"] = PropertySpec.Integer,
["optionalIntOutput"] = PropertySpec.Integer
};
var resources = new Dictionary<string, ResourceSpec>();
resources.Add("my-component:index:RequiredOptionalComponent",
new ResourceSpec(
inputs,
new HashSet<string> { "requiredString", "requiredInt", "requiredBool" },
outputs,
new HashSet<string> { "requiredOutput", "requiredIntOutput" }));
var expected = CreateBasePackageSpec(resources);
AssertSchemaEqual(expected, schema);
}
[Theory]
[InlineData("123test", "Package name must start with a letter and contain only letters, numbers, hyphens, and underscores")]
[InlineData("test!", "Package name must start with a letter and contain only letters, numbers, hyphens, and underscores")]
[InlineData("test space", "Package name must start with a letter and contain only letters, numbers, hyphens, and underscores")]
[InlineData("", "Package name cannot be empty or whitespace")]
[InlineData(" ", "Package name cannot be empty or whitespace")]
[InlineData(null, "Package name cannot be empty or whitespace")]
public void TestInvalidPackageNames(string? name, string expectedError)
{
var metadata = new Metadata(name!);
var exception = Assert.Throws<ArgumentException>(() =>
ComponentAnalyzer.GenerateSchema(metadata, typeof(SelfSignedCertificate)));
Assert.Equal($"{expectedError} (Parameter 'metadata')", exception.Message);
}
[Theory]
[InlineData("test")]
[InlineData("test-package")]
[InlineData("test_package")]
[InlineData("test123")]
[InlineData("myPackage")]
public void TestValidPackageNames(string name)
{
var metadata = new Metadata(name);
var schema = ComponentAnalyzer.GenerateSchema(metadata, typeof(SelfSignedCertificate));
Assert.Equal(name, schema.Name);
}
private readonly Metadata _metadata = new Metadata("my-component", "0.0.1", "Test package");
private static PackageSpec CreateBasePackageSpec(
Dictionary<string, ResourceSpec>? resources = null,
Dictionary<string, ComplexTypeSpec>? types = null)
{
var pkg = new PackageSpec
{
Name = "my-component",
Version = "0.0.1",
DisplayName = "Test package"
};
var languageSettings = new Dictionary<string, object>
{
{ "respectSchemaVersion", true }
}.ToImmutableSortedDictionary();
var languages = new Dictionary<string, ImmutableSortedDictionary<string, object>>();
foreach (var lang in new[] { "nodejs", "python", "csharp", "java", "go" })
{
languages[lang] = languageSettings;
}
return pkg with
{
Language = ImmutableSortedDictionary.CreateRange(languages),
Resources = resources?.ToImmutableSortedDictionary() ?? ImmutableSortedDictionary<string, ResourceSpec>.Empty,
Types = types?.ToImmutableSortedDictionary() ?? ImmutableSortedDictionary<string, ComplexTypeSpec>.Empty
};
}
private static void AssertSchemaEqual(PackageSpec expected, PackageSpec actual)
{
var options = new JsonSerializerOptions
{
WriteIndented = true,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
};
var expectedJson = JsonSerializer.Serialize(expected, options);
var actualJson = JsonSerializer.Serialize(actual, options);
Assert.Equal(expectedJson, actualJson);
}
}