-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy pathprovider_test.go
More file actions
236 lines (200 loc) · 6.66 KB
/
provider_test.go
File metadata and controls
236 lines (200 loc) · 6.66 KB
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
//
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
//
package cloudstack
import (
"context"
"fmt"
"os"
"regexp"
"strconv"
"strings"
"testing"
"github.com/apache/cloudstack-go/v2/cloudstack"
"github.com/hashicorp/terraform-plugin-framework/providerserver"
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
"github.com/hashicorp/terraform-plugin-mux/tf5to6server"
"github.com/hashicorp/terraform-plugin-mux/tf6muxserver"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
)
var testAccProviders map[string]*schema.Provider
var testAccProvider *schema.Provider
var testAccMuxProvider map[string]func() (tfprotov6.ProviderServer, error)
var cloudStackTemplateURL = os.Getenv("CLOUDSTACK_TEMPLATE_URL")
func init() {
testAccProvider = Provider()
testAccProviders = map[string]*schema.Provider{
"cloudstack": testAccProvider,
}
testAccMuxProvider = map[string]func() (tfprotov6.ProviderServer, error){
"cloudstack": func() (tfprotov6.ProviderServer, error) {
ctx := context.Background()
upgradedSdkServer, err := tf5to6server.UpgradeServer(
ctx,
Provider().GRPCProvider,
)
if err != nil {
return nil, err
}
providers := []func() tfprotov6.ProviderServer{
providerserver.NewProtocol6(New()),
func() tfprotov6.ProviderServer {
return upgradedSdkServer
},
}
muxServer, err := tf6muxserver.NewMuxServer(ctx, providers...)
if err != nil {
return nil, err
}
return muxServer.ProviderServer(), nil
},
}
}
func TestProvider(t *testing.T) {
if err := Provider().InternalValidate(); err != nil {
t.Fatalf("err: %s", err)
}
}
func TestProvider_impl(t *testing.T) {
var _ *schema.Provider = Provider()
}
func TestMuxServer(t *testing.T) {
resource.Test(t, resource.TestCase{
ProtoV6ProviderFactories: testAccMuxProvider,
Steps: []resource.TestStep{
{
Config: testMuxServerConfig_conflict,
ExpectError: regexp.MustCompile("Invalid Attribute Combination"),
},
{
Config: testMuxServerConfig_basic,
},
},
})
}
const testMuxServerConfig_basic = `
resource "cloudstack_zone" "zone_resource"{
name = "TestZone"
dns1 = "8.8.8.8"
internal_dns1 = "172.20.0.1"
network_type = "Advanced"
}
data "cloudstack_zone" "zone_data_source"{
filter{
name = "name"
value = cloudstack_zone.zone_resource.name
}
}
`
const testMuxServerConfig_conflict = `
provider "cloudstack" {
api_url = "http://localhost:8080/client/api"
api_key = "xxxxx"
secret_key = "xxxxx"
config = "cloudstack.ini"
}
data "cloudstack_zone" "zone_data_source"{
filter{
name = "name"
value = "test"
}
}
`
func testAccPreCheck(t *testing.T) {
if v := os.Getenv("CLOUDSTACK_API_URL"); v == "" {
t.Fatal("CLOUDSTACK_API_URL must be set for acceptance tests")
}
if v := os.Getenv("CLOUDSTACK_API_KEY"); v == "" {
t.Fatal("CLOUDSTACK_API_KEY must be set for acceptance tests")
}
if v := os.Getenv("CLOUDSTACK_SECRET_KEY"); v == "" {
t.Fatal("CLOUDSTACK_SECRET_KEY must be set for acceptance tests")
}
}
// parseCloudStackVersion parses a CloudStack version string (e.g., "4.22.0.0")
// and returns a numeric value for comparison (e.g., 4.22 -> 4022).
// The numeric value is calculated as: major * 1000 + minor.
// Returns 0 if the version string cannot be parsed.
func parseCloudStackVersion(version string) int {
parts := strings.Split(version, ".")
if len(parts) < 2 {
return 0
}
major := 0
minor := 0
// Parse major version - extract first numeric part
majorStr := regexp.MustCompile(`^\d+`).FindString(parts[0])
if majorStr != "" {
major, _ = strconv.Atoi(majorStr)
}
// Parse minor version - extract first numeric part
minorStr := regexp.MustCompile(`^\d+`).FindString(parts[1])
if minorStr != "" {
minor, _ = strconv.Atoi(minorStr)
}
return major*1000 + minor
}
// getCloudStackVersion retrieves the CloudStack version from the API.
// Returns the version string and any error encountered.
func getCloudStackVersion(cs *cloudstack.CloudStackClient) (string, error) {
p := cs.Configuration.NewListCapabilitiesParams()
caps, err := cs.Configuration.ListCapabilities(p)
if err != nil {
return "", err
}
if caps != nil && caps.Capabilities != nil && caps.Capabilities.Cloudstackversion != "" {
return caps.Capabilities.Cloudstackversion, nil
}
return "", fmt.Errorf("unable to determine CloudStack version")
}
// requireMinimumCloudStackVersion checks if the CloudStack version meets the minimum requirement.
// If the version is below the minimum, it skips the test with an appropriate message.
// The minVersion parameter should be in the format returned by parseCloudStackVersion (e.g., 4022 for 4.22.0).
func requireMinimumCloudStackVersion(t *testing.T, cs *cloudstack.CloudStackClient, minVersion int, featureName string) {
version, err := getCloudStackVersion(cs)
if err != nil {
t.Skipf("Unable to check CloudStack version: %v", err)
return
}
versionNum := parseCloudStackVersion(version)
if versionNum < minVersion {
// Convert minVersion back to readable format (e.g., 4022 -> "4.22")
major := minVersion / 1000
minor := minVersion % 1000
t.Skipf("%s not supported in CloudStack version %s (requires %d.%d+)", featureName, version, major, minor)
}
}
// testAccPreCheckStaticRouteNexthop checks if the CloudStack version supports
// the nexthop parameter for static routes (requires 4.22.0+)
func testAccPreCheckStaticRouteNexthop(t *testing.T) {
testAccPreCheck(t)
// Create a CloudStack client to check version
config := Config{
APIURL: os.Getenv("CLOUDSTACK_API_URL"),
APIKey: os.Getenv("CLOUDSTACK_API_KEY"),
SecretKey: os.Getenv("CLOUDSTACK_SECRET_KEY"),
Timeout: 900,
}
cs, err := config.NewClient()
if err != nil {
t.Fatalf("Failed to create CloudStack client: %v", err)
}
const minVersionNum = 4022 // 4.22.0
requireMinimumCloudStackVersion(t, cs, minVersionNum, "Static route nexthop parameter")
}