Skip to content

Commit

Permalink
updating bios for 17G change
Browse files Browse the repository at this point in the history
updationg bios test file

fix lint issue

updating common file
  • Loading branch information
sapana05 committed Mar 4, 2025
1 parent 85993b6 commit 094b206
Show file tree
Hide file tree
Showing 3 changed files with 248 additions and 2 deletions.
1 change: 1 addition & 0 deletions redfish/provider/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,7 @@ func (p powerOperator) PowerOperation(resetType string, maximumWaitTime int64, c
tflog.Error(p.ctx, fmt.Sprintf("Failed to identify system: %s", err))
return "", fmt.Errorf("failed to identify system: %w", err)
}
system.Entity.SetETag("")

var targetPowerState redfish.PowerState

Expand Down
20 changes: 18 additions & 2 deletions redfish/provider/resource_redfish_bios.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,13 @@ func (r *BiosResource) updateRedfishDellBiosAttributes(ctx context.Context, serv
return nil, diags
}

attrsPayload, diagsAttr := getBiosAttrsToPatch(ctx, plan, attributes)
// check device is 17G or not
isGenerationSeventeenAndAbove, err := isServerGenerationSeventeenAndAbove(service)
if err != nil {
diags.AddError("Error retrieving the server generation", err.Error())
return nil, diags
}
attrsPayload, diagsAttr := getBiosAttrsToPatch(ctx, plan, attributes, isGenerationSeventeenAndAbove)
diags.Append(diagsAttr...)
if diags.HasError() {
return nil, diags
Expand Down Expand Up @@ -376,6 +382,11 @@ func (r *BiosResource) updateRedfishDellBiosAttributes(ctx context.Context, serv

tflog.Info(ctx, "rebooting the server completed successfully")
tflog.Info(ctx, "Waiting for the bios config job to finish")

// Below 17G device returns location as /redfish/v1/TaskService/Taks/JOB_ID for same GET call return status as 200 with all the job status.
// where as 17G device returns location as /redfish/v1/TaskService/TaskMonitors/JOB_ID for same GET call return no content hence
// we are replacing TaskMonitors to Taks.
biosTaskURI = strings.Replace(biosTaskURI, "TaskMonitors", "Tasks", 1)
// wait for the bios config job to finish
err = common.WaitForTaskToFinish(service, biosTaskURI, intervalBiosConfigJobCheckTime, biosConfigJobTimeout)
if err != nil {
Expand Down Expand Up @@ -455,13 +466,18 @@ func copyBiosAttributes(bios *redfish.Bios, attributes map[string]string) error
return nil
}

func getBiosAttrsToPatch(ctx context.Context, d *models.Bios, attributes map[string]string) (map[string]interface{}, diag.Diagnostics) {
// nolint: revive
func getBiosAttrsToPatch(ctx context.Context, d *models.Bios, attributes map[string]string, isSeventeenGen bool) (map[string]interface{}, diag.Diagnostics) {
var diags diag.Diagnostics
attrs := make(map[string]string)
attrsToPatch := make(map[string]interface{})
diags.Append(d.Attributes.ElementsAs(ctx, &attrs, true)...)

for key, newVal := range attrs {
if isSeventeenGen && strings.Contains(key, "AcPwrRcvry") {
diags.AddError(fmt.Sprintf("%s Configuration is not supported by 17G device", key), fmt.Sprintf("BIOS attribute %s not found", key))
continue
}
oldVal, ok := attributes[key]
if !ok {
diags.AddError("There was an issue while creating/updating bios attriutes", fmt.Sprintf("BIOS attribute %s not found", key))
Expand Down
229 changes: 229 additions & 0 deletions redfish/provider/resource_redfish_bios_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package provider

import (
"fmt"
"os"
"regexp"
"testing"

Expand All @@ -28,11 +29,18 @@ import (

// test redfish bios settings
func TestAccRedfishBios_basic(t *testing.T) {
version := os.Getenv("TF_TESTING_REDFISH_VERSION")
if version == "17" {
t.Skip("Skipping Bios Tests for 17G")
}
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
PreConfig: func() {
FunctionMocker = mockey.Mock(isServerGenerationSeventeenAndAbove).Return(false, nil).Build()
},
Config: testAccRedfishResourceBiosConfigOn(
creds),
Check: resource.ComposeAggregateTestCheckFunc(
Expand All @@ -48,14 +56,63 @@ func TestAccRedfishBios_basic(t *testing.T) {
},
},
})

if FunctionMocker != nil {
FunctionMocker.Release()
}
}

func TestAccRedfishBios_17Gbasic(t *testing.T) {
version := os.Getenv("TF_TESTING_REDFISH_VERSION")
if version != "17" {
t.Skip("Skipping Bios Tests for below 17G")
}
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
PreConfig: func() {
FunctionMocker = mockey.Mock(isServerGenerationSeventeenAndAbove).Return(true, nil).Build()
},
Config: testAccRedfishResourceBiosConfigOn(
creds),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("redfish_bios.bios", "attributes.NumLock", "On"),
),
},
{
Config: testAccRedfishResourceBiosConfigOff17G(
creds),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("redfish_bios.bios", "attributes.NumLock", "Off"),
),
},
{
Config: testAccRedfishResourceBiosConfigOff(
creds),
ExpectError: regexp.MustCompile("AcPwrRcvryUserDelay Configuration is not supported by 17G device"),
},
},
})
if FunctionMocker != nil {
FunctionMocker.Release()
}
}

func TestAccRedfishBios_InvalidSettings(t *testing.T) {
version := os.Getenv("TF_TESTING_REDFISH_VERSION")
if version == "17" {
t.Skip("Skipping Bios Tests for 17G")
}
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
PreConfig: func() {
FunctionMocker = mockey.Mock(isServerGenerationSeventeenAndAbove).Return(false, nil).Build()
},
Config: testAccRedfishResourceBiosConfigInvalidSettingsApplyTime(
creds),
ExpectError: regexp.MustCompile("Attribute settings_apply_time value must be one of"),
Expand All @@ -65,20 +122,35 @@ func TestAccRedfishBios_InvalidSettings(t *testing.T) {
}

func TestAccRedfishBios_InvalidAttributes(t *testing.T) {
version := os.Getenv("TF_TESTING_REDFISH_VERSION")
if version == "17" {
t.Skip("Skipping Bios Tests for 17G")
}
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
PreConfig: func() {
FunctionMocker = mockey.Mock(isServerGenerationSeventeenAndAbove).Return(false, nil).Build()
},
Config: testAccRedfishResourceBiosConfigInvalidAttributes(
creds),
ExpectError: regexp.MustCompile("Attribute settings_apply_time value must be one of"),
},
},
})
if FunctionMocker != nil {
FunctionMocker.Release()
}
}

func TestAccRedfishBios_Mock(t *testing.T) {
var funcMocker1 *mockey.Mocker
version := os.Getenv("TF_TESTING_REDFISH_VERSION")
if version == "17" {
t.Skip("Skipping Bios Tests for 17G")
}
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Expand All @@ -87,6 +159,7 @@ func TestAccRedfishBios_Mock(t *testing.T) {
{
PreConfig: func() {
FunctionMocker = mockey.Mock(NewConfig).Return(nil, fmt.Errorf("mock error")).Build()

},
Config: testAccRedfishResourceBiosConfigOn(creds),
ExpectError: regexp.MustCompile(`.*mock error*.`),
Expand All @@ -97,6 +170,8 @@ func TestAccRedfishBios_Mock(t *testing.T) {
if FunctionMocker != nil {
FunctionMocker.Release()
}
funcMocker1 = mockey.Mock(isServerGenerationSeventeenAndAbove).Return(false, nil).Build()

},
Config: testAccRedfishResourceBiosConfigOn(creds),
Check: resource.ComposeAggregateTestCheckFunc(
Expand Down Expand Up @@ -126,15 +201,26 @@ func TestAccRedfishBios_Mock(t *testing.T) {
},
},
})

if funcMocker1 != nil {
funcMocker1.Release()
}
}

// Test to import bios - positive
func TestAccRedfishBios_Import(t *testing.T) {
version := os.Getenv("TF_TESTING_REDFISH_VERSION")
if version == "17" {
t.Skip("Skipping Bios Tests for 17G")
}
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
PreConfig: func() {
FunctionMocker = mockey.Mock(isServerGenerationSeventeenAndAbove).Return(false, nil).Build()
},
Config: testAccRedfishResourceBiosConfigOn(
creds),
ResourceName: "redfish_bios.bios",
Expand All @@ -144,14 +230,24 @@ func TestAccRedfishBios_Import(t *testing.T) {
},
},
})
if FunctionMocker != nil {
FunctionMocker.Release()
}
}

func TestAccRedfishBios_ImportSystemID(t *testing.T) {
version := os.Getenv("TF_TESTING_REDFISH_VERSION")
if version == "17" {
t.Skip("Skipping Bios Tests for 17G")
}
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
PreConfig: func() {
FunctionMocker = mockey.Mock(isServerGenerationSeventeenAndAbove).Return(false, nil).Build()
},
Config: testAccRedfishResourceBiosConfigOn(
creds),
ResourceName: "redfish_bios.bios",
Expand All @@ -161,6 +257,112 @@ func TestAccRedfishBios_ImportSystemID(t *testing.T) {
},
},
})
if FunctionMocker != nil {
FunctionMocker.Release()
}
}

func TestAccRedfishBios_17GInvalidSettings(t *testing.T) {
version := os.Getenv("TF_TESTING_REDFISH_VERSION")
if version != "17" {
t.Skip("Skipping Bios Tests for below 17G")
}
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
PreConfig: func() {
FunctionMocker = mockey.Mock(isServerGenerationSeventeenAndAbove).Return(true, nil).Build()
},
Config: testAccRedfishResourceBiosConfigInvalidSettingsApplyTime(
creds),
ExpectError: regexp.MustCompile("Attribute settings_apply_time value must be one of"),
},
},
})
if FunctionMocker != nil {
FunctionMocker.Release()
}
}

func TestAccRedfishBios_17GInvalidAttributes(t *testing.T) {
version := os.Getenv("TF_TESTING_REDFISH_VERSION")
if version != "17" {
t.Skip("Skipping Bios Tests for below 17G")
}
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
PreConfig: func() {
FunctionMocker = mockey.Mock(isServerGenerationSeventeenAndAbove).Return(true, nil).Build()
},
Config: testAccRedfishResourceBiosConfigInvalidAttributes(
creds),
ExpectError: regexp.MustCompile("Attribute settings_apply_time value must be one of"),
},
},
})
if FunctionMocker != nil {
FunctionMocker.Release()
}
}

// Test to import bios - positive
func TestAccRedfishBios_17GImport(t *testing.T) {
version := os.Getenv("TF_TESTING_REDFISH_VERSION")
if version != "17" {
t.Skip("Skipping Bios Tests for below 17G")
}
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
PreConfig: func() {
FunctionMocker = mockey.Mock(isServerGenerationSeventeenAndAbove).Return(true, nil).Build()
},
Config: testAccRedfishResourceBiosConfigOn(
creds),
ResourceName: "redfish_bios.bios",
ImportState: true,
ImportStateId: "{\"username\":\"" + creds.Username + "\",\"password\":\"" + creds.Password + "\",\"endpoint\":\"" + creds.Endpoint + "\",\"ssl_insecure\":true}",
ExpectError: nil,
},
},
})
if FunctionMocker != nil {
FunctionMocker.Release()
}
}

func TestAccRedfishBios_17GImportSystemID(t *testing.T) {
version := os.Getenv("TF_TESTING_REDFISH_VERSION")
if version != "17" {
t.Skip("Skipping Bios Tests for below 17G")
}
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
PreConfig: func() {
FunctionMocker = mockey.Mock(isServerGenerationSeventeenAndAbove).Return(true, nil).Build()
},
Config: testAccRedfishResourceBiosConfigOn(
creds),
ResourceName: "redfish_bios.bios",
ImportState: true,
ImportStateId: "{\"username\":\"" + creds.Username + "\",\"password\":\"" + creds.Password + "\",\"endpoint\":\"" + creds.Endpoint + "\",\"ssl_insecure\":true,\"system_id\":\"System.Embedded.1\"}",
ExpectError: nil,
},
},
})
if FunctionMocker != nil {
FunctionMocker.Release()
}
}

func testAccRedfishResourceBiosConfigOn(testingInfo TestingServerCredentials) string {
Expand Down Expand Up @@ -215,6 +417,33 @@ func testAccRedfishResourceBiosConfigOff(testingInfo TestingServerCredentials) s
)
}

func testAccRedfishResourceBiosConfigOff17G(testingInfo TestingServerCredentials) string {
return fmt.Sprintf(`
resource "redfish_bios" "bios" {
redfish_server {
user = "%s"
password = "%s"
endpoint = "%s"
ssl_insecure = true
}
attributes = {
"NumLock" = "Off"
#"AcPwrRcvryUserDelay" = 70
}
reset_type = "ForceRestart"
bios_job_timeout = 1200
reset_timeout = 120
}
`,
testingInfo.Username,
testingInfo.Password,
testingInfo.Endpoint,
)
}

func testAccRedfishResourceBiosConfigInvalidSettingsApplyTime(testingInfo TestingServerCredentials) string {
return fmt.Sprintf(`
Expand Down

0 comments on commit 094b206

Please sign in to comment.