Skip to content

Commit 2fb8a43

Browse files
ksamorayKobi Samorayclaude
authored
Expand unit test coverage for low-coverage resources and data sources (#2313)
Raises overall unit test coverage on ./nsxt/... from 68.8% to 73.8%, building on the prior "near-zero-coverage data sources" pass: - 8 resources whose existing mock tests only exercised error/guard paths now cover Create/Read/Update/Delete success paths too: cluster_security_config (20% -> ~97%), ldap_identity_source (20% -> ~97%), l7_access_profile (24% -> ~89%), idps_cluster_config (25% -> ~97%), segment_port_profile_bindings (33% -> ~86%), host_transport_node (36% -> ~88%), ip_pool_block_subnet (37% -> ~90%), ipsec_vpn_session (41% -> ~74%). Adds minimal package-level client-constructor seams (mirroring the existing cli*Client convention used throughout the provider) to cluster_security_config and idps_cluster_config so their SDK clients can be mocked; reuses existing seams elsewhere. - 9 near-identical LB monitor/application profile resources gain success-path tests via their already-shared mock helpers, raising each from ~46-50% to 84-100%. - 13 data sources that previously had no mock test at all now have one, nearly all reaching 100%: 8 trivial generic-search wrappers, plus provider_info, host_upgrade_group, edge_cluster, gateway_interface, and proxy_config (the last reusing an existing seam from its sibling resource file instead of constructing its SDK client inline). Co-authored-by: Kobi Samoray <kobis@Kobis-MacBook-Air.local> Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>
1 parent 990c0d5 commit 2fb8a43

36 files changed

Lines changed: 4095 additions & 31 deletions

File tree

mocks/aaa/LdapIdentitySourcesClient.go

Lines changed: 161 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

nsxt/data_source_nsxt_proxy_config.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"log"
99

1010
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
11-
"github.com/vmware/vsphere-automation-sdk-go/services/nsxt-mp/nsx/proxy"
1211
)
1312

1413
func dataSourceNsxtProxyConfig() *schema.Resource {
@@ -68,7 +67,7 @@ func dataSourceNsxtProxyConfigRead(d *schema.ResourceData, m interface{}) error
6867
log.Printf("[INFO] Reading Proxy Config data source with ID %s", id)
6968

7069
// Create proxy config client
71-
client := proxy.NewConfigClient(connector)
70+
client := cliProxyConfigClient(connector)
7271

7372
// Get proxy configuration
7473
proxyConfig, err := client.Get()

nsxt/resource_nsxt_policy_cluster_security_config.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,17 @@ import (
1010

1111
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1212
"github.com/vmware/terraform-provider-nsxt/nsxt/util"
13+
"github.com/vmware/vsphere-automation-sdk-go/runtime/protocol/client"
1314
"github.com/vmware/vsphere-automation-sdk-go/services/nsxt/infra/settings/security"
1415
"github.com/vmware/vsphere-automation-sdk-go/services/nsxt/model"
1516
)
1617

18+
// cliClusterSecurityConfigsClient is wrapped in a package-level var (returning the
19+
// exported ClusterConfigsClient interface) so it can be substituted with a mock in tests.
20+
var cliClusterSecurityConfigsClient = func(connector client.Connector) security.ClusterConfigsClient {
21+
return security.NewClusterConfigsClient(connector)
22+
}
23+
1724
func resourceNsxtPolicyClusterSecurityConfig() *schema.Resource {
1825
return &schema.Resource{
1926
Create: resourceNsxtPolicyClusterSecurityConfigCreate,
@@ -137,7 +144,7 @@ func resourceNsxtPolicyClusterSecurityConfigRead(d *schema.ResourceData, m inter
137144
}
138145

139146
connector := getPolicyConnector(m)
140-
client := security.NewClusterConfigsClient(connector)
147+
client := cliClusterSecurityConfigsClient(connector)
141148

142149
clusterID := d.Id()
143150
if clusterID == "" {
@@ -160,7 +167,7 @@ func resourceNsxtPolicyClusterSecurityConfigUpdate(d *schema.ResourceData, m int
160167
}
161168

162169
connector := getPolicyConnector(m)
163-
client := security.NewClusterConfigsClient(connector)
170+
client := cliClusterSecurityConfigsClient(connector)
164171

165172
clusterID := d.Id()
166173
if clusterID == "" {
@@ -186,7 +193,7 @@ func resourceNsxtPolicyClusterSecurityConfigDelete(d *schema.ResourceData, m int
186193
}
187194

188195
connector := getPolicyConnector(m)
189-
client := security.NewClusterConfigsClient(connector)
196+
client := cliClusterSecurityConfigsClient(connector)
190197

191198
clusterID := d.Id()
192199
if clusterID == "" {

nsxt/resource_nsxt_policy_host_transport_node.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@ import (
2323
var cliHostTransportNodesClient = enforcement_points.NewHostTransportNodesClient
2424
var cliHostTransportNodeStateClient = host_transport_nodes.NewStateClient
2525

26+
// hostTransportNodeStatePoll{Delay,Interval,Timeout} control the busy-wait performed
27+
// after Delete when remove_nsx_on_destroy is set. They are package-level vars (rather
28+
// than inline constants) so unit tests can shrink them to avoid real sleeps.
29+
var hostTransportNodeStatePollDelay = 5
30+
var hostTransportNodeStatePollInterval = 5
31+
var hostTransportNodeStatePollTimeout = 1200
32+
2633
func resourceNsxtPolicyHostTransportNode() *schema.Resource {
2734
return &schema.Resource{
2835
Create: resourceNsxtPolicyHostTransportNodeCreate,
@@ -237,9 +244,9 @@ func getHostTransportNodeStateConf(connector client.Connector, d *schema.Resourc
237244

238245
return "notyet", "notyet", nil
239246
},
240-
Delay: time.Duration(5) * time.Second,
241-
Timeout: time.Duration(1200) * time.Second,
242-
PollInterval: time.Duration(5) * time.Second,
247+
Delay: time.Duration(hostTransportNodeStatePollDelay) * time.Second,
248+
Timeout: time.Duration(hostTransportNodeStatePollTimeout) * time.Second,
249+
PollInterval: time.Duration(hostTransportNodeStatePollInterval) * time.Second,
243250
}
244251
}
245252

nsxt/resource_nsxt_policy_idps_cluster_config.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,17 @@ import (
99
"log"
1010

1111
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
12+
"github.com/vmware/vsphere-automation-sdk-go/runtime/protocol/client"
1213
"github.com/vmware/vsphere-automation-sdk-go/services/nsxt/infra/settings/firewall/security/intrusion_services"
1314
"github.com/vmware/vsphere-automation-sdk-go/services/nsxt/model"
1415
)
1516

17+
// cliIdsClusterConfigsClient is wrapped in a package-level var (returning the exported
18+
// ClusterConfigsClient interface) so it can be substituted with a mock in tests.
19+
var cliIdsClusterConfigsClient = func(connector client.Connector) intrusion_services.ClusterConfigsClient {
20+
return intrusion_services.NewClusterConfigsClient(connector)
21+
}
22+
1623
func resourceNsxtPolicyIdpsClusterConfig() *schema.Resource {
1724
return &schema.Resource{
1825
Create: resourceNsxtPolicyIdpsClusterConfigCreate,
@@ -94,7 +101,7 @@ func buildIdsClusterConfig(d *schema.ResourceData, id string) model.IdsClusterCo
94101

95102
func resourceNsxtPolicyIdpsClusterConfigCreate(d *schema.ResourceData, m interface{}) error {
96103
connector := getPolicyConnector(m)
97-
client := intrusion_services.NewClusterConfigsClient(connector)
104+
client := cliIdsClusterConfigsClient(connector)
98105
if client == nil {
99106
return policyResourceNotSupportedError()
100107
}
@@ -128,7 +135,7 @@ func resourceNsxtPolicyIdpsClusterConfigRead(d *schema.ResourceData, m interface
128135
return fmt.Errorf("Error obtaining IDPS Cluster Config ID")
129136
}
130137

131-
client := intrusion_services.NewClusterConfigsClient(connector)
138+
client := cliIdsClusterConfigsClient(connector)
132139
if client == nil {
133140
return policyResourceNotSupportedError()
134141
}
@@ -166,7 +173,7 @@ func resourceNsxtPolicyIdpsClusterConfigUpdate(d *schema.ResourceData, m interfa
166173
return fmt.Errorf("Error obtaining IDPS Cluster Config ID")
167174
}
168175

169-
client := intrusion_services.NewClusterConfigsClient(connector)
176+
client := cliIdsClusterConfigsClient(connector)
170177
if client == nil {
171178
return policyResourceNotSupportedError()
172179
}
@@ -189,7 +196,7 @@ func resourceNsxtPolicyIdpsClusterConfigDelete(d *schema.ResourceData, m interfa
189196
return fmt.Errorf("Error obtaining IDPS Cluster Config ID")
190197
}
191198

192-
client := intrusion_services.NewClusterConfigsClient(connector)
199+
client := cliIdsClusterConfigsClient(connector)
193200
if client == nil {
194201
return policyResourceNotSupportedError()
195202
}

nsxt/resource_nsxt_policy_segment_port_profile_bindings.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"strings"
66

77
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
8-
"github.com/vmware/terraform-provider-nsxt/api/infra/segments/ports"
98
"github.com/vmware/vsphere-automation-sdk-go/runtime/data"
109
"github.com/vmware/vsphere-automation-sdk-go/services/nsxt/model"
1110
)
@@ -154,23 +153,23 @@ func resourceNsxtPolicySegmentPortProfileBindingsDelete(d *schema.ResourceData,
154153
segmentId := getSegmentIdFromSegPath(segmentPath)
155154
securityProfileBindingMapID := getBindingMapID(d, "security_profile")
156155
if securityProfileBindingMapID != "" {
157-
securityProfileClient := ports.NewPortSecurityProfileBindingMapsClient(context, connector)
156+
securityProfileClient := cliPortSecurityProfileBindingMapsClient(context, connector)
158157
err = securityProfileClient.Delete(segmentId, segmentPortID, securityProfileBindingMapID)
159158
if err != nil {
160159
return fmt.Errorf("Error deleting the security profile: %v", err)
161160
}
162161
}
163162
discoveryProfileBindingMapID := getBindingMapID(d, "discovery_profile")
164163
if discoveryProfileBindingMapID != "" {
165-
discoveryProfileClient := ports.NewPortDiscoveryProfileBindingMapsClient(context, connector)
164+
discoveryProfileClient := cliPortDiscoveryProfileBindingMapsClient(context, connector)
166165
err = discoveryProfileClient.Delete(segmentId, segmentPortID, discoveryProfileBindingMapID)
167166
if err != nil {
168167
return fmt.Errorf("Error deleting the discovery profile: %v", err)
169168
}
170169
}
171170
qosProfileBindingMapID := getBindingMapID(d, "qos_profile")
172171
if qosProfileBindingMapID != "" {
173-
qosProfileClient := ports.NewPortQosProfileBindingMapsClient(context, connector)
172+
qosProfileClient := cliPortQosProfileBindingMapsClient(context, connector)
174173
err = qosProfileClient.Delete(segmentId, segmentPortID, qosProfileBindingMapID)
175174
if err != nil {
176175
return fmt.Errorf("Error deleting the qos profile: %v", err)

0 commit comments

Comments
 (0)