1
- using System . Reflection ;
2
-
3
- using FluentAssertions ;
4
-
5
- using k8s ;
6
- using k8s . Models ;
7
-
8
- using KubeOps . Operator . Builder ;
9
- using KubeOps . Operator . Entities ;
10
- using KubeOps . Test . TestEntities ;
11
-
12
- using Xunit ;
13
-
14
- namespace KubeOps . Test . Operator . Generators ;
15
-
16
- public class CrdGeneratorTest
17
- {
18
- private readonly IEnumerable < V1CustomResourceDefinition > _crds ;
19
- private readonly ComponentRegistrar _componentRegistrar = new ( ) ;
20
-
21
- public CrdGeneratorTest ( )
22
- {
23
- _componentRegistrar . RegisterEntity < TestIgnoredEntity > ( ) ;
24
- _componentRegistrar . RegisterEntity < TestInvalidEntity > ( ) ;
25
- _componentRegistrar . RegisterEntity < TestSpecEntity > ( ) ;
26
- _componentRegistrar . RegisterEntity < TestClusterSpecEntity > ( ) ;
27
- _componentRegistrar . RegisterEntity < TestStatusEntity > ( ) ;
28
- _componentRegistrar . RegisterEntity < V1Alpha1VersionedEntity > ( ) ;
29
- _componentRegistrar . RegisterEntity < V1AttributeVersionedEntity > ( ) ;
30
- _componentRegistrar . RegisterEntity < V1Beta1VersionedEntity > ( ) ;
31
- _componentRegistrar . RegisterEntity < V1VersionedEntity > ( ) ;
32
- _componentRegistrar . RegisterEntity < V2AttributeVersionedEntity > ( ) ;
33
- _componentRegistrar . RegisterEntity < V2Beta2VersionedEntity > ( ) ;
34
- _componentRegistrar . RegisterEntity < V2VersionedEntity > ( ) ;
35
- _componentRegistrar . RegisterEntity < TestCustomCrdTypeOverrides > ( ) ;
36
-
37
- // Should be ignored since V1Pod is from the k8s assembly.
38
- _componentRegistrar . RegisterEntity < V1Pod > ( ) ;
39
-
40
- _crds = new CrdBuilder ( _componentRegistrar ) . BuildCrds ( ) ;
41
- }
42
-
43
- [ Fact ]
44
- public void Should_Generate_Correct_Number_Of_Crds ( )
45
- {
46
- _crds . Count ( ) . Should ( ) . Be ( 6 ) ;
47
- }
48
-
49
- [ Fact ]
50
- public void Should_Not_Contain_Ignored_Entities ( )
51
- {
52
- _crds . Should ( )
53
- . NotContain ( crd => crd . Name ( ) . Contains ( "ignored" , StringComparison . InvariantCultureIgnoreCase ) ) ;
54
- }
55
-
56
- [ Fact ]
57
- public void Should_Not_Contain_K8s_Entities ( )
58
- {
59
- _crds . Should ( )
60
- . NotContain ( crd => crd . Spec . Names . Kind == "Pod" ) ;
61
- }
62
-
63
- [ Fact ]
64
- public void Should_Set_Highest_Version_As_Storage ( )
65
- {
66
- var crd = _crds . First ( c => c . Spec . Names . Kind == "VersionedEntity" ) ;
67
- crd . Spec . Versions . Count ( v => v . Storage ) . Should ( ) . Be ( 1 ) ;
68
- crd . Spec . Versions . First ( v => v . Storage ) . Name . Should ( ) . Be ( "v2" ) ;
69
- }
70
-
71
- [ Fact ]
72
- public void Should_Set_Storage_When_Attribute_Is_Set ( )
73
- {
74
- var crd = _crds . First ( c => c . Spec . Names . Kind == "AttributeVersionedEntity" ) ;
75
- crd . Spec . Versions . Count ( v => v . Storage ) . Should ( ) . Be ( 1 ) ;
76
- crd . Spec . Versions . First ( v => v . Storage ) . Name . Should ( ) . Be ( "v1" ) ;
77
- }
78
-
79
- [ Fact ]
80
- public void Should_Add_Multiple_Versions_To_Crd ( )
81
- {
82
- _crds
83
- . First ( c => c . Spec . Names . Kind . Contains ( "testspecentity" , StringComparison . InvariantCultureIgnoreCase ) )
84
- . Spec . Versions . Should ( )
85
- . HaveCount ( 1 ) ;
86
- _crds
87
- . First ( c => c . Spec . Names . Kind . Contains ( "teststatusentity" , StringComparison . InvariantCultureIgnoreCase ) )
88
- . Spec . Versions . Should ( )
89
- . HaveCount ( 1 ) ;
90
- _crds
91
- . First ( c => c . Spec . Names . Kind == "VersionedEntity" )
92
- . Spec . Versions . Should ( )
93
- . HaveCount ( 5 ) ;
94
- _crds
95
- . First ( c => c . Spec . Names . Kind == "AttributeVersionedEntity" )
96
- . Spec . Versions . Should ( )
97
- . HaveCount ( 2 ) ;
98
- }
99
-
100
- [ Fact ]
101
- public void Should_Add_ShortNames_To_Crd ( )
102
- {
103
- _crds
104
- . First ( c => c . Spec . Names . Kind . Contains ( "teststatusentity" , StringComparison . InvariantCultureIgnoreCase ) )
105
- . Spec . Names . ShortNames . Should ( )
106
- . NotBeNull ( )
107
- . And
108
- . Contain ( new [ ] { "foo" , "bar" , "baz" } ) ;
109
- }
110
-
111
- [ Fact ]
112
- public void Should_Create_Crd_As_Default_Without_Crd_Type_Overrides ( )
113
- {
114
- var crdWithoutOverrides = new CrdBuilder ( _componentRegistrar )
115
- . BuildCrds ( )
116
- . First (
117
-
118
- c => c . Spec . Names . Kind . Contains ( "testcustomtypeoverrides" , StringComparison . InvariantCultureIgnoreCase ) ) ;
119
-
120
-
121
- var serializedWithoutOverrides = TestTypeOverridesValues . SerializeWithoutDescriptions ( crdWithoutOverrides ) ;
122
-
123
- serializedWithoutOverrides . Should ( ) . Contain ( TestTypeOverridesValues . ExpectedDefaultYamlResources ) ;
124
- serializedWithoutOverrides . Should ( ) . NotContain ( TestTypeOverridesValues . ExpectedOverriddenResourcesYaml ) ;
125
- }
126
-
127
- [ Fact ]
128
- public void Should_Convert_Desired_Crd_Type_Everywhere_To_Desired_Crd_Format ( )
129
- {
130
- var customOverrides = new List < ICrdBuilderTypeOverride > { new CrdBuilderResourceQuantityOverride ( ) } ;
131
- var crdWithTypeOverrides = new CrdBuilder ( _componentRegistrar , customOverrides )
132
- . BuildCrds ( )
133
- . First (
134
- c => c . Spec . Names . Kind . Contains ( "testcustomtypeoverrides" , StringComparison . InvariantCultureIgnoreCase ) ) ;
135
- var serializedWithOverrides = TestTypeOverridesValues . SerializeWithoutDescriptions ( crdWithTypeOverrides ) ;
136
-
137
- serializedWithOverrides . Should ( ) . Contain ( TestTypeOverridesValues . ExpectedOverriddenResourcesYaml ) ;
138
- serializedWithOverrides . Should ( ) . NotContain ( TestTypeOverridesValues . ExpectedDefaultYamlResources ) ;
139
-
140
- }
141
- }
1
+ using System . Reflection ;
2
+
3
+ using FluentAssertions ;
4
+
5
+ using k8s ;
6
+ using k8s . Models ;
7
+
8
+ using KubeOps . Operator . Builder ;
9
+ using KubeOps . Operator . Entities ;
10
+ using KubeOps . Test . TestEntities ;
11
+
12
+ using Xunit ;
13
+
14
+ namespace KubeOps . Test . Operator . Generators ;
15
+
16
+ public class CrdGeneratorTest
17
+ {
18
+ private readonly IEnumerable < V1CustomResourceDefinition > _crds ;
19
+ private readonly ComponentRegistrar _componentRegistrar = new ( ) ;
20
+
21
+ public CrdGeneratorTest ( )
22
+ {
23
+ _componentRegistrar . RegisterEntity < TestIgnoredEntity > ( ) ;
24
+ _componentRegistrar . RegisterEntity < TestInvalidEntity > ( ) ;
25
+ _componentRegistrar . RegisterEntity < TestSpecEntity > ( ) ;
26
+ _componentRegistrar . RegisterEntity < TestClusterSpecEntity > ( ) ;
27
+ _componentRegistrar . RegisterEntity < TestStatusEntity > ( ) ;
28
+ _componentRegistrar . RegisterEntity < V1Alpha1VersionedEntity > ( ) ;
29
+ _componentRegistrar . RegisterEntity < V1AttributeVersionedEntity > ( ) ;
30
+ _componentRegistrar . RegisterEntity < V1Beta1VersionedEntity > ( ) ;
31
+ _componentRegistrar . RegisterEntity < V1VersionedEntity > ( ) ;
32
+ _componentRegistrar . RegisterEntity < V2AttributeVersionedEntity > ( ) ;
33
+ _componentRegistrar . RegisterEntity < V2Beta2VersionedEntity > ( ) ;
34
+ _componentRegistrar . RegisterEntity < V2VersionedEntity > ( ) ;
35
+ _componentRegistrar . RegisterEntity < TestCustomCrdTypeOverrides > ( ) ;
36
+
37
+ // Should be ignored since V1Pod is from the k8s assembly.
38
+ _componentRegistrar . RegisterEntity < V1Pod > ( ) ;
39
+
40
+ _crds = new CrdBuilder ( _componentRegistrar ) . BuildCrds ( ) ;
41
+ }
42
+
43
+ [ Fact ]
44
+ public void Should_Set_Highest_Version_As_Storage ( )
45
+ {
46
+ var crd = _crds . First ( c => c . Spec . Names . Kind == "VersionedEntity" ) ;
47
+ crd . Spec . Versions . Count ( v => v . Storage ) . Should ( ) . Be ( 1 ) ;
48
+ crd . Spec . Versions . First ( v => v . Storage ) . Name . Should ( ) . Be ( "v2" ) ;
49
+ }
50
+
51
+ [ Fact ]
52
+ public void Should_Set_Storage_When_Attribute_Is_Set ( )
53
+ {
54
+ var crd = _crds . First ( c => c . Spec . Names . Kind == "AttributeVersionedEntity" ) ;
55
+ crd . Spec . Versions . Count ( v => v . Storage ) . Should ( ) . Be ( 1 ) ;
56
+ crd . Spec . Versions . First ( v => v . Storage ) . Name . Should ( ) . Be ( "v1" ) ;
57
+ }
58
+
59
+ [ Fact ]
60
+ public void Should_Add_Multiple_Versions_To_Crd ( )
61
+ {
62
+ _crds
63
+ . First ( c => c . Spec . Names . Kind . Contains ( "testspecentity" , StringComparison . InvariantCultureIgnoreCase ) )
64
+ . Spec . Versions . Should ( )
65
+ . HaveCount ( 1 ) ;
66
+ _crds
67
+ . First ( c => c . Spec . Names . Kind . Contains ( "teststatusentity" , StringComparison . InvariantCultureIgnoreCase ) )
68
+ . Spec . Versions . Should ( )
69
+ . HaveCount ( 1 ) ;
70
+ _crds
71
+ . First ( c => c . Spec . Names . Kind == "VersionedEntity" )
72
+ . Spec . Versions . Should ( )
73
+ . HaveCount ( 5 ) ;
74
+ _crds
75
+ . First ( c => c . Spec . Names . Kind == "AttributeVersionedEntity" )
76
+ . Spec . Versions . Should ( )
77
+ . HaveCount ( 2 ) ;
78
+ }
79
+
80
+ [ Fact ]
81
+ public void Should_Add_ShortNames_To_Crd ( )
82
+ {
83
+ _crds
84
+ . First ( c => c . Spec . Names . Kind . Contains ( "teststatusentity" , StringComparison . InvariantCultureIgnoreCase ) )
85
+ . Spec . Names . ShortNames . Should ( )
86
+ . NotBeNull ( )
87
+ . And
88
+ . Contain ( new [ ] { "foo" , "bar" , "baz" } ) ;
89
+ }
90
+
91
+ [ Fact ]
92
+ public void Should_Create_Crd_As_Default_Without_Crd_Type_Overrides ( )
93
+ {
94
+ var crdWithoutOverrides = new CrdBuilder ( _componentRegistrar )
95
+ . BuildCrds ( )
96
+ . First (
97
+
98
+ c => c . Spec . Names . Kind . Contains ( "testcustomtypeoverrides" , StringComparison . InvariantCultureIgnoreCase ) ) ;
99
+
100
+
101
+ var serializedWithoutOverrides = TestTypeOverridesValues . SerializeWithoutDescriptions ( crdWithoutOverrides ) ;
102
+
103
+ serializedWithoutOverrides . Should ( ) . Contain ( TestTypeOverridesValues . ExpectedDefaultYamlResources ) ;
104
+ serializedWithoutOverrides . Should ( ) . NotContain ( TestTypeOverridesValues . ExpectedOverriddenResourcesYaml ) ;
105
+ }
106
+
107
+ [ Fact ]
108
+ public void Should_Convert_Desired_Crd_Type_Everywhere_To_Desired_Crd_Format ( )
109
+ {
110
+ var customOverrides = new List < ICrdBuilderTypeOverride > { new CrdBuilderResourceQuantityOverride ( ) } ;
111
+ var crdWithTypeOverrides = new CrdBuilder ( _componentRegistrar , customOverrides )
112
+ . BuildCrds ( )
113
+ . First (
114
+ c => c . Spec . Names . Kind . Contains ( "testcustomtypeoverrides" , StringComparison . InvariantCultureIgnoreCase ) ) ;
115
+ var serializedWithOverrides = TestTypeOverridesValues . SerializeWithoutDescriptions ( crdWithTypeOverrides ) ;
116
+
117
+ serializedWithOverrides . Should ( ) . Contain ( TestTypeOverridesValues . ExpectedOverriddenResourcesYaml ) ;
118
+ serializedWithOverrides . Should ( ) . NotContain ( TestTypeOverridesValues . ExpectedDefaultYamlResources ) ;
119
+
120
+ }
121
+ }
0 commit comments