Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

avoid string slice panic in version check #23039

Merged
merged 5 commits into from
Feb 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
})
}
}