Skip to content

Commit

Permalink
Merge pull request #23039 from brenthc/brenthc-rds-version-slice-panic
Browse files Browse the repository at this point in the history
avoid string slice panic in version check
  • Loading branch information
ewbankkit authored Feb 11, 2022
2 parents fdc1c5d + 6db1e6f commit 9b116b1
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .changelog/23039.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
resource/aws_rds_cluster: Fix crash when configured `engine_version` string is shorter than the `EngineVersion` string returned from the AWS API
```
9 changes: 8 additions & 1 deletion internal/service/rds/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -1541,8 +1541,15 @@ func rdsClusterSetResourceDataEngineVersionFromCluster(d *schema.ResourceData, c
}

func compareActualEngineVersion(d *schema.ResourceData, oldVersion string, newVersion string) {
if oldVersion != newVersion && string(append([]byte(oldVersion), []byte(".")...)) != string([]byte(newVersion)[0:len(oldVersion)+1]) {
newVersionSubstr := newVersion

if len(newVersion) > len(oldVersion) {
newVersionSubstr = string([]byte(newVersion)[0 : len(oldVersion)+1])
}

if oldVersion != newVersion && string(append([]byte(oldVersion), []byte(".")...)) != newVersionSubstr {
d.Set("engine_version", newVersion)
}

d.Set("engine_version_actual", newVersion)
}
76 changes: 76 additions & 0 deletions internal/service/rds/engine_version_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package rds

import (
"testing"
)

func TestCompareActualEngineVersion(t *testing.T) {
t.Parallel()

type testCase struct {
configuredVersion string
actualVersion string
expectedEngineVersion string
expectedEngineVersionActual string
}
tests := map[string]testCase{
"point version upgrade": {
configuredVersion: "8.0",
actualVersion: "8.0.27",
expectedEngineVersion: "",
expectedEngineVersionActual: "8.0.27",
},
"minor version upgrade": {
configuredVersion: "8.0",
actualVersion: "8.1.1",
expectedEngineVersion: "8.1.1",
expectedEngineVersionActual: "8.1.1",
},
"major version upgrade": {
configuredVersion: "8.1",
actualVersion: "9.0.0",
expectedEngineVersion: "9.0.0",
expectedEngineVersionActual: "9.0.0",
},
"aurora upgrade": {
configuredVersion: "5.7.mysql_aurora.2.07",
actualVersion: "5.7.serverless_mysql_aurora.2.08.3",
expectedEngineVersion: "5.7.serverless_mysql_aurora.2.08.3",
expectedEngineVersionActual: "5.7.serverless_mysql_aurora.2.08.3",
},
"aurora upgrade - used to crash": {
configuredVersion: "5.7.serverless_mysql_aurora.2.07",
actualVersion: "5.7.mysql_aurora.2.08.3",
expectedEngineVersion: "5.7.mysql_aurora.2.08.3",
expectedEngineVersionActual: "5.7.mysql_aurora.2.08.3",
},
"oracle": {
configuredVersion: "12.1.0.2.v20",
actualVersion: "19.0.0.0.ru-2021-04.rur-2021-04.r1",
expectedEngineVersion: "19.0.0.0.ru-2021-04.rur-2021-04.r1",
expectedEngineVersionActual: "19.0.0.0.ru-2021-04.rur-2021-04.r1",
},
"oracle - used to crash": {
configuredVersion: "19.0.0.0.ru-2021-04.rur-2021-04.r1",
actualVersion: "12.1.0.2.v20",
expectedEngineVersion: "12.1.0.2.v20",
expectedEngineVersionActual: "12.1.0.2.v20",
},
}

for name, test := range tests {
name, test := name, test
t.Run(name, func(t *testing.T) {
r := ResourceCluster()
d := r.Data(nil)
compareActualEngineVersion(d, test.configuredVersion, test.actualVersion)

if want, got := test.expectedEngineVersion, d.Get("engine_version"); got != want {
t.Errorf("unexpected engine_version; want: %q, got: %q", want, got)
}
if want, got := test.expectedEngineVersionActual, d.Get("engine_version_actual"); got != want {
t.Errorf("unexpected engine_version_actual; want: %q, got: %q", want, got)
}
})
}
}

0 comments on commit 9b116b1

Please sign in to comment.