Skip to content

Commit d74f849

Browse files
Merge pull request #151 from dimitarproynov/refactor_unit_tests
Introduce lightweight E2E test for SDDC resource, using the ZEROCLOUD…
2 parents 846446f + 66a2ac0 commit d74f849

14 files changed

+539
-292
lines changed

.github/workflows/golangci-lint.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@ jobs:
1010
- name: golangci-lint
1111
uses: golangci/golangci-lint-action@v2
1212
with:
13-
version: v1.45.2
13+
version: v1.49.0
1414
args: --issues-exit-code=1
1515
only-new-issues: true

.github/workflows/release.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ jobs:
1717
name: Set up Go
1818
uses: actions/setup-go@v2
1919
with:
20-
go-version: 1.16.4
20+
go-version: 1.19.1
2121
-
2222
name: golangci-lint
2323
uses: golangci/golangci-lint-action@v2
2424
with:
25-
version: v1.45.2
25+
version: v1.49.0
2626
args: --issues-exit-code=1
2727
-
2828
name: Import GPG key

.golangci.yml

-3
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,14 @@ issues:
55
linters:
66
disable-all: true
77
enable:
8-
- deadcode
98
- errcheck
109
- gofmt
1110
- gosimple
1211
- ineffassign
1312
- misspell
1413
- staticcheck
15-
- structcheck
1614
- unconvert
1715
- unused
18-
- varcheck
1916
- vet
2017

2118
linters-settings:

go.mod

+55-7
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,69 @@
11
module github.com/vmware/terraform-provider-vmc
22

3-
go 1.16
3+
go 1.19
44

55
require (
6-
github.com/agext/levenshtein v1.2.3 // indirect
7-
github.com/fatih/color v1.9.0 // indirect
86
github.com/hashicorp/terraform-plugin-sdk/v2 v2.11.0
9-
github.com/hashicorp/yamux v0.0.0-20200609203250-aecfd211c9ce // indirect
10-
github.com/mattn/go-colorable v0.1.7 // indirect
11-
github.com/oklog/run v1.1.0 // indirect
127
github.com/satori/go.uuid v1.2.0
13-
github.com/stretchr/testify v1.7.0
8+
github.com/stretchr/testify v1.7.1
149
github.com/vmware/vsphere-automation-sdk-go/lib v0.4.0
1510
github.com/vmware/vsphere-automation-sdk-go/runtime v0.4.0
1611
github.com/vmware/vsphere-automation-sdk-go/services/nsxt-vmc-aws-integration v0.5.0
1712
github.com/vmware/vsphere-automation-sdk-go/services/vmc v0.10.0
1813
github.com/vmware/vsphere-automation-sdk-go/services/vmc/autoscaler v0.4.0
1914
github.com/vmware/vsphere-automation-sdk-go/services/vmc/draas v0.4.0
15+
)
16+
17+
require (
18+
github.com/agext/levenshtein v1.2.3 // indirect
19+
github.com/apparentlymart/go-cidr v1.1.0 // indirect
20+
github.com/apparentlymart/go-textseg/v13 v13.0.0 // indirect
21+
github.com/beevik/etree v1.1.0 // indirect
22+
github.com/davecgh/go-spew v1.1.1 // indirect
23+
github.com/fatih/color v1.9.0 // indirect
24+
github.com/gibson042/canonicaljson-go v1.0.3 // indirect
25+
github.com/golang/protobuf v1.5.2 // indirect
26+
github.com/google/go-cmp v0.5.7 // indirect
27+
github.com/google/uuid v1.2.0 // indirect
28+
github.com/hashicorp/errwrap v1.0.0 // indirect
29+
github.com/hashicorp/go-checkpoint v0.5.0 // indirect
30+
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
31+
github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320 // indirect
32+
github.com/hashicorp/go-hclog v1.2.0 // indirect
33+
github.com/hashicorp/go-multierror v1.1.1 // indirect
34+
github.com/hashicorp/go-plugin v1.4.3 // indirect
35+
github.com/hashicorp/go-uuid v1.0.2 // indirect
36+
github.com/hashicorp/go-version v1.4.0 // indirect
37+
github.com/hashicorp/hc-install v0.3.1 // indirect
38+
github.com/hashicorp/hcl/v2 v2.11.1 // indirect
39+
github.com/hashicorp/logutils v1.0.0 // indirect
40+
github.com/hashicorp/terraform-exec v0.16.0 // indirect
41+
github.com/hashicorp/terraform-json v0.13.0 // indirect
42+
github.com/hashicorp/terraform-plugin-go v0.8.0 // indirect
43+
github.com/hashicorp/terraform-plugin-log v0.3.0 // indirect
44+
github.com/hashicorp/terraform-registry-address v0.0.0-20210412075316-9b2996cce896 // indirect
45+
github.com/hashicorp/terraform-svchost v0.0.0-20200729002733-f050f53b9734 // indirect
46+
github.com/hashicorp/yamux v0.0.0-20200609203250-aecfd211c9ce // indirect
47+
github.com/mattn/go-colorable v0.1.7 // indirect
48+
github.com/mattn/go-isatty v0.0.12 // indirect
49+
github.com/mitchellh/copystructure v1.2.0 // indirect
50+
github.com/mitchellh/go-testing-interface v1.14.1 // indirect
51+
github.com/mitchellh/go-wordwrap v1.0.0 // indirect
52+
github.com/mitchellh/mapstructure v1.4.3 // indirect
53+
github.com/mitchellh/reflectwalk v1.0.2 // indirect
54+
github.com/oklog/run v1.1.0 // indirect
55+
github.com/pmezard/go-difflib v1.0.0 // indirect
56+
github.com/vmihailenco/msgpack v4.0.4+incompatible // indirect
57+
github.com/vmihailenco/msgpack/v4 v4.3.12 // indirect
58+
github.com/vmihailenco/tagparser v0.1.1 // indirect
59+
github.com/zclconf/go-cty v1.10.0 // indirect
60+
golang.org/x/crypto v0.0.0-20210616213533-5ff15b29337e // indirect
61+
golang.org/x/net v0.0.0-20210510120150-4163338589ed // indirect
62+
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a // indirect
63+
golang.org/x/text v0.3.6 // indirect
64+
google.golang.org/appengine v1.6.6 // indirect
2065
google.golang.org/genproto v0.0.0-20200904004341-0bd0a958aa1d // indirect
66+
google.golang.org/grpc v1.45.0 // indirect
67+
google.golang.org/protobuf v1.27.1 // indirect
68+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
2169
)

go.sum

+4-3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ github.com/apparentlymart/go-cidr v1.1.0/go.mod h1:EBcsNrHc3zQeuaeCeCtQruQm+n9/Y
2020
github.com/apparentlymart/go-dump v0.0.0-20180507223929-23540a00eaa3/go.mod h1:oL81AME2rN47vu18xqj1S1jPIPuN7afo62yKTNn3XMM=
2121
github.com/apparentlymart/go-dump v0.0.0-20190214190832-042adf3cf4a0 h1:MzVXffFUye+ZcSR6opIgz9Co7WcDx6ZcY+RjfFHoA0I=
2222
github.com/apparentlymart/go-dump v0.0.0-20190214190832-042adf3cf4a0/go.mod h1:oL81AME2rN47vu18xqj1S1jPIPuN7afo62yKTNn3XMM=
23-
github.com/apparentlymart/go-textseg v1.0.0 h1:rRmlIsPEEhUTIKQb7T++Nz/A5Q6C9IuX2wFoYVvnCs0=
2423
github.com/apparentlymart/go-textseg v1.0.0/go.mod h1:z96Txxhf3xSFMPmb5X/1W05FF/Nj9VFpLOpjS5yuumk=
2524
github.com/apparentlymart/go-textseg/v12 v12.0.0/go.mod h1:S/4uRK2UtaQttw1GenVJEynmyUenKwP++x/+DdGV/Ec=
2625
github.com/apparentlymart/go-textseg/v13 v13.0.0 h1:Y+KvPE1NYz0xl601PVImeQfFyEy6iT90AvPUL1NNfNw=
@@ -218,8 +217,9 @@ github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXf
218217
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
219218
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
220219
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
221-
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
222220
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
221+
github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY=
222+
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
223223
github.com/vmihailenco/msgpack v3.3.3+incompatible/go.mod h1:fy3FlTQTDXWkZ7Bh6AcGMlsjHatGryHQYUTf1ShIgkk=
224224
github.com/vmihailenco/msgpack v4.0.4+incompatible h1:dSLoQfGFAo3F6OoNhwUmLwVgaUXK79GlxNBwueZn0xI=
225225
github.com/vmihailenco/msgpack v4.0.4+incompatible/go.mod h1:fy3FlTQTDXWkZ7Bh6AcGMlsjHatGryHQYUTf1ShIgkk=
@@ -310,8 +310,9 @@ golang.org/x/sys v0.0.0-20210320140829-1e4c9ba3b0c4/go.mod h1:h1NjWce9XRLGQEsW7w
310310
golang.org/x/sys v0.0.0-20210324051608-47abb6519492/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
311311
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
312312
golang.org/x/sys v0.0.0-20210502180810-71e4cd670f79/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
313-
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1 h1:SrN+KX8Art/Sf4HNj6Zcz06G7VEz+7w9tdXTPOZ7+l4=
314313
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
314+
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a h1:dGzPydgVsqGcTRVwiLJ1jVbufYwmzD3LfVPLKsKg+0k=
315+
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
315316
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1 h1:v+OssWQX+hTHEmOBgwxdZxK4zHq3yOs8F9J7mk0PY8E=
316317
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
317318
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=

vmc/clientconnector.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"encoding/json"
99
"errors"
1010
"fmt"
11-
"io/ioutil"
11+
"io"
1212
"net/http"
1313
"reflect"
1414
"strings"
@@ -23,7 +23,7 @@ func NewClientConnectorByRefreshToken(refreshToken, serviceUrl, cspURL string,
2323
httpClient http.Client) (client.Connector, error) {
2424

2525
if len(serviceUrl) <= 0 {
26-
serviceUrl = DefaultVMCServer
26+
serviceUrl = DefaultVMCUrl
2727
}
2828

2929
if len(cspURL) <= 0 {
@@ -60,7 +60,7 @@ func SecurityContextByRefreshToken(refreshToken string, cspURL string) (core.Sec
6060
}
6161

6262
if res.StatusCode != 200 {
63-
b, _ := ioutil.ReadAll(res.Body)
63+
b, _ := io.ReadAll(res.Body)
6464
return nil, fmt.Errorf("response from Cloud Service Provider contains status code %d : %s", res.StatusCode, string(b))
6565
}
6666

vmc/constants.go

+10-6
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
package vmc
55

66
const (
7-
// DefaultVMCServer defines the default VMC server.
8-
DefaultVMCServer string = "https://vmc.vmware.com"
7+
// DefaultVMCUrl defines the default VMC server url.
8+
DefaultVMCUrl string = "https://vmc.vmware.com"
99

1010
// DefaultCSPUrl defines the default URL for CSP.
1111
DefaultCSPUrl string = "https://console.cloud.vmware.com"
@@ -65,10 +65,14 @@ const (
6565
MaxHosts = 16
6666

6767
// Env variables used in acceptance tests
68-
APIToken string = "API_TOKEN"
69-
OrgID string = "ORG_ID"
70-
OrgDisplayName string = "ORG_DISPLAY_NAME"
71-
TestSDDCId string = "TEST_SDDC_ID"
68+
VMCUrl string = "VMC_URL"
69+
CSPUrl string = "CSP_URL"
70+
APIToken string = "API_TOKEN"
71+
OrgID string = "ORG_ID"
72+
OrgDisplayName string = "ORG_DISPLAY_NAME"
73+
// ID of an existing SDDC used for sddc data source, site recovery and srm node tests
74+
TestSDDCId string = "TEST_SDDC_ID"
75+
// Name of an existing SDDC used for sddc data source tests
7276
TestSDDCName string = "TEST_SDDC_NAME"
7377
AWSAccountNumber string = "AWS_ACCOUNT_NUMBER"
7478
NSXTReverseProxyUrl string = "NSXT_REVERSE_PROXY_URL"

vmc/data_source_vmc_sddc.go

+47-3
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,48 @@ func dataSourceVmcSddc() *schema.Resource {
103103
Type: schema.TypeString,
104104
Computed: true,
105105
},
106+
// Below are added as part of the schema as they are set in the
107+
// dataSourceVmcSddcRead method
108+
"updated": {
109+
Type: schema.TypeString,
110+
Computed: true,
111+
},
112+
"user_id": {
113+
Type: schema.TypeString,
114+
Computed: true,
115+
},
116+
"updated_by_user_id": {
117+
Type: schema.TypeString,
118+
Computed: true,
119+
},
120+
"created": {
121+
Type: schema.TypeString,
122+
Computed: true,
123+
},
124+
"version": {
125+
Type: schema.TypeInt,
126+
Computed: true,
127+
},
128+
"updated_by_user_name": {
129+
Type: schema.TypeString,
130+
Computed: true,
131+
},
132+
"user_name": {
133+
Type: schema.TypeString,
134+
Computed: true,
135+
},
136+
"org_id": {
137+
Type: schema.TypeString,
138+
Computed: true,
139+
},
140+
"account_link_state": {
141+
Type: schema.TypeString,
142+
Computed: true,
143+
},
144+
"sddc_access_state": {
145+
Type: schema.TypeString,
146+
Computed: true,
147+
},
106148
},
107149
}
108150
}
@@ -131,16 +173,18 @@ func dataSourceVmcSddcRead(d *schema.ResourceData, m interface{}) error {
131173
d.SetId(sddc.Id)
132174

133175
d.Set("sddc_name", sddc.Name)
134-
d.Set("updated", sddc.Updated)
176+
d.Set("updated", sddc.Updated.String())
135177
d.Set("user_id", sddc.UserId)
136178
d.Set("updated_by_user_id", sddc.UpdatedByUserId)
137-
d.Set("created", sddc.Created)
179+
d.Set("created", sddc.Created.String())
138180
d.Set("version", sddc.Version)
139181
d.Set("updated_by_user_name", sddc.UpdatedByUserName)
140182
d.Set("user_name", sddc.UserName)
141183
d.Set("org_id", sddc.OrgId)
142184
d.Set("sddc_type", sddc.SddcType)
143-
d.Set("provider", sddc.Provider)
185+
// the key "provider" is reserved by the Terraform SDK, however the same information
186+
// is carried by the sddc.ResourceConfig.Provider variable
187+
//d.Set("provider", sddc.Provider)
144188
d.Set("account_link_state", sddc.AccountLinkState)
145189
d.Set("sddc_access_state", sddc.SddcAccessState)
146190
d.Set("sddc_type", sddc.SddcType)

vmc/data_source_vmc_sddc_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func TestAccDataSourceVmcSddc_basic(t *testing.T) {
1919
{
2020
Config: testAccDataSourceVmcSddcConfig(),
2121
Check: resource.ComposeTestCheckFunc(
22-
resource.TestCheckResourceAttr("data.vmc_sddc.my_sddc", "sddc_name", os.Getenv(TestSDDCName)),
22+
resource.TestCheckResourceAttr("data.vmc_sddc.sddc_imported", "sddc_name", os.Getenv(TestSDDCName)),
2323
),
2424
},
2525
},
@@ -28,7 +28,7 @@ func TestAccDataSourceVmcSddc_basic(t *testing.T) {
2828

2929
func testAccDataSourceVmcSddcConfig() string {
3030
return fmt.Sprintf(`
31-
data "vmc_sddc" "my_sddc" {
31+
data "vmc_sddc" "sddc_imported" {
3232
sddc_id = %q
3333
}
3434
`,

vmc/provider.go

+7-6
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,14 @@ func Provider() *schema.Provider {
4545
DefaultFunc: schema.EnvDefaultFunc("ORG_ID", nil),
4646
},
4747
"vmc_url": {
48-
Type: schema.TypeString,
49-
Optional: true,
50-
Default: "https://vmc.vmware.com",
48+
Type: schema.TypeString,
49+
Optional: true,
50+
DefaultFunc: schema.EnvDefaultFunc(VMCUrl, DefaultVMCUrl),
5151
},
5252
"csp_url": {
53-
Type: schema.TypeString,
54-
Optional: true,
55-
Default: "https://console.cloud.vmware.com",
53+
Type: schema.TypeString,
54+
Optional: true,
55+
DefaultFunc: schema.EnvDefaultFunc(CSPUrl, DefaultCSPUrl),
5656
},
5757
},
5858

@@ -84,6 +84,7 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {
8484
os.Setenv(APIToken, refreshToken)
8585
vmcURL := d.Get("vmc_url").(string)
8686
cspURL := d.Get("csp_url").(string)
87+
os.Setenv(CSPUrl, cspURL)
8788
orgID := d.Get("org_id").(string)
8889
httpClient := http.Client{}
8990
connector, err := NewClientConnectorByRefreshToken(refreshToken, vmcURL, cspURL, httpClient)

vmc/provider_test.go

+15
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,18 @@ func testAccPreCheck(t *testing.T) {
5555
t.Fatal(NSXTReverseProxyUrl + " must be set for acceptance tests")
5656
}
5757
}
58+
59+
// testAccPreCheckZerocloud this function validates a smaller set ot
60+
// environment variables needed for lightweight E2E testing using
61+
// the Zerocloud SDDC cloud provider option
62+
func testAccPreCheckZerocloud(t *testing.T) {
63+
if v := os.Getenv(APIToken); v == "" {
64+
t.Fatal(APIToken + " must be set for acceptance tests")
65+
}
66+
if v := os.Getenv(OrgID); v == "" {
67+
t.Fatal(OrgID + " must be set for acceptance tests")
68+
}
69+
if v := os.Getenv(AWSAccountNumber); v == "" {
70+
t.Fatal(AWSAccountNumber + " must be set for acceptance tests")
71+
}
72+
}

0 commit comments

Comments
 (0)