Skip to content

Commit b640d8e

Browse files
alexng-canuckrcohenma
authored andcommitted
Add test hooks and test cases for maintenance reboot time field.
To support this we, separate out test client configuration logic to allow intercepting SDK requests.
1 parent 6849ab4 commit b640d8e

File tree

3 files changed

+68
-10
lines changed

3 files changed

+68
-10
lines changed

oci/core_instance_resource.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -714,7 +714,7 @@ func (s *InstanceResourceCrud) SetData() error {
714714
if s.Res.TimeMaintenanceRebootDue != nil {
715715
s.D.Set("time_maintenance_reboot_due", s.Res.TimeMaintenanceRebootDue.String())
716716
} else {
717-
// If the maintenance time is cleared after reboot, the service will return a nil.
717+
// If the maintenance time is cleared after reboot, the service will return a nil.
718718
// We should explicitly zero it out to avoid returning the previously cached reboot time.
719719
s.D.Set("time_maintenance_reboot_due", "")
720720
}

oci/core_instance_test.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,11 @@ variable "InstanceImageOCID" {
9494

9595
func TestCoreInstanceResource_basic(t *testing.T) {
9696
provider := testAccProvider
97-
config := testProviderConfig()
97+
config := `
98+
provider oci {
99+
test_time_maintenance_reboot_due = "2030-01-01 00:00:00"
100+
}
101+
` + commonTestVariables()
98102

99103
compartmentId := getEnvSettingWithBlankDefault("compartment_ocid")
100104
compartmentIdVariableStr := fmt.Sprintf("variable \"compartment_id\" { default = \"%s\" }\n", compartmentId)
@@ -114,13 +118,14 @@ func TestCoreInstanceResource_basic(t *testing.T) {
114118
Steps: []resource.TestStep{
115119
// verify create
116120
{
117-
Config: config + compartmentIdVariableStr + InstanceResourceDependencies +
121+
Config: testProviderConfig() + compartmentIdVariableStr + InstanceResourceDependencies +
118122
generateResourceFromRepresentationMap("oci_core_instance", "test_instance", Required, Create, instanceRepresentation),
119123
Check: resource.ComposeAggregateTestCheckFunc(
120124
resource.TestCheckResourceAttrSet(resourceName, "availability_domain"),
121125
resource.TestCheckResourceAttr(resourceName, "compartment_id", compartmentId),
122126
resource.TestCheckResourceAttr(resourceName, "shape", "VM.Standard1.2"),
123127
resource.TestCheckResourceAttrSet(resourceName, "subnet_id"),
128+
resource.TestCheckResourceAttr(resourceName, "time_maintenance_reboot_due", ""),
124129

125130
func(s *terraform.State) (err error) {
126131
resId, err = fromInstanceState(s, resourceName, "id")
@@ -167,6 +172,7 @@ func TestCoreInstanceResource_basic(t *testing.T) {
167172
resource.TestCheckResourceAttrSet(resourceName, "state"),
168173
resource.TestCheckResourceAttrSet(resourceName, "subnet_id"),
169174
resource.TestCheckResourceAttrSet(resourceName, "time_created"),
175+
resource.TestCheckResourceAttrSet(resourceName, "time_maintenance_reboot_due"),
170176

171177
func(s *terraform.State) (err error) {
172178
resId, err = fromInstanceState(s, resourceName, "id")
@@ -250,6 +256,7 @@ func TestCoreInstanceResource_basic(t *testing.T) {
250256
resource.TestCheckResourceAttr(datasourceName, "instances.0.source_details.0.source_type", "image"),
251257
resource.TestCheckResourceAttrSet(datasourceName, "instances.0.state"),
252258
resource.TestCheckResourceAttrSet(datasourceName, "instances.0.time_created"),
259+
resource.TestCheckResourceAttrSet(datasourceName, "instances.0.time_maintenance_reboot_due"),
253260
),
254261
},
255262
// verify singular datasource
@@ -277,6 +284,7 @@ func TestCoreInstanceResource_basic(t *testing.T) {
277284
resource.TestCheckResourceAttr(singularDatasourceName, "source_details.0.source_type", "image"),
278285
resource.TestCheckResourceAttrSet(singularDatasourceName, "state"),
279286
resource.TestCheckResourceAttrSet(singularDatasourceName, "time_created"),
287+
resource.TestCheckResourceAttrSet(singularDatasourceName, "time_maintenance_reboot_due"),
280288

281289
resource.TestCheckResourceAttrSet(singularDatasourceName, "public_ip"),
282290
resource.TestCheckResourceAttrSet(singularDatasourceName, "private_ip"),

oci/provider_test.go

Lines changed: 57 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package provider
44

55
import (
66
"fmt"
7+
"net/http"
78
"runtime"
89
"sort"
910
"strings"
@@ -23,21 +24,32 @@ var requiredKeyAuthEnvVars = []string{"tenancy_ocid", "user_ocid", "fingerprint"
2324
var requiredOboTokenAuthEnvVars = []string{"tenancy_ocid", "obo_token"}
2425

2526
func init() {
26-
testAccProvider = Provider(func(d *schema.ResourceData) (interface{}, error) {
27-
return GetTestProvider(), nil
27+
testAccProvider = testProvider(func(d *schema.ResourceData) (interface{}, error) {
28+
return GetTestClients(d), nil
2829
}).(*schema.Provider)
2930

3031
testAccProviders = map[string]terraform.ResourceProvider{
3132
"oci": testAccProvider,
3233
}
3334
}
3435

35-
func testProviderConfig() string {
36-
return `
37-
# Need to have this block even though it's empty; for import testing
38-
provider "oci" {
36+
// Provider is the adapter for terraform, that gives access to all the resources
37+
func testProvider(configfn schema.ConfigureFunc) terraform.ResourceProvider {
38+
result := &schema.Provider{
39+
DataSourcesMap: dataSourcesMap(),
40+
Schema: schemaMap(),
41+
ResourcesMap: resourcesMap(),
42+
ConfigureFunc: configfn,
3943
}
4044

45+
// Additions for test parameters
46+
result.Schema["test_time_maintenance_reboot_due"] = &schema.Schema{Type: schema.TypeString, Optional: true}
47+
48+
return result
49+
}
50+
51+
func commonTestVariables() string {
52+
return `
4153
variable "tenancy_ocid" {
4254
default = "` + getEnvSettingWithBlankDefault("tenancy_ocid") + `"
4355
}
@@ -53,6 +65,14 @@ func testProviderConfig() string {
5365
`
5466
}
5567

68+
func testProviderConfig() string {
69+
return `
70+
# Need to have this block even though it's empty; for import testing
71+
provider "oci" {
72+
}
73+
` + commonTestVariables()
74+
}
75+
5676
func testAccPreCheck(t *testing.T) {
5777
envVarChecklist := []string{}
5878
copy(envVarChecklist, requiredTestEnvVars)
@@ -216,7 +236,11 @@ resource "oci_core_instance" "t" {
216236
` + DefinedTagsDependencies
217237
)
218238

219-
func GetTestProvider() *OracleClients {
239+
const (
240+
requestQueryOpcTimeMaintenanceRebootDue = "opc-time-maintenance-reboot-due"
241+
)
242+
243+
func GetTestClients(data *schema.ResourceData) *OracleClients {
220244
r := &schema.Resource{
221245
Schema: schemaMap(),
222246
}
@@ -240,6 +264,32 @@ func GetTestProvider() *OracleClients {
240264
if err != nil {
241265
panic(err)
242266
}
267+
268+
// This is a test hook to support creating instances that have a maintenance reboot time set
269+
// The test hook allows 'time_maintenance_reboot_due' field to be tested for instance datasources/resources
270+
// This is controlled by a provider option rather than environment variable: so that the tests can run in parallel
271+
// without affecting one another and also allow individual test steps to alter this
272+
//
273+
// If we have additional test hooks that need to be supported in this manner, then the following logic should be
274+
// compartmentalized and registered with the test provider in a scalable manner.
275+
maintenanceRebootTime, ok := data.GetOkExists("test_time_maintenance_reboot_due")
276+
if ok {
277+
computeClient := client.(*OracleClients).computeClient
278+
baseInterceptor := computeClient.Interceptor
279+
computeClient.Interceptor = func(r *http.Request) error {
280+
if err := baseInterceptor(r); err != nil {
281+
return err
282+
}
283+
284+
if r.Method == http.MethodPost && (strings.Contains(r.URL.Path, "/instances")) {
285+
query := r.URL.Query()
286+
query.Set(requestQueryOpcTimeMaintenanceRebootDue, maintenanceRebootTime.(string))
287+
r.URL.RawQuery = query.Encode()
288+
}
289+
return nil
290+
}
291+
}
292+
243293
return client.(*OracleClients)
244294
}
245295

0 commit comments

Comments
 (0)