Licensed 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 common
import (
"context"
"encoding/json"
"fmt"
"syscall"
"testing"
"github.com/agiledragon/gomonkey/v2"
"github.com/smartystreets/goconvey/convey"
"ascend-common/common-utils/hwlog"
)
const ValidDiveType = 0x03
const WrongDiveType = 0xFF
var testFaultTypeCode = &FaultTypeCode{
NotHandleFaultCodes: []string{"00000001"},
PreSeparateFaultCodes: []string{"00000002"},
SeparateFaultCodes: []string{"00000003"},
}
func init() {
hwLogConfig := hwlog.LogConfig{
OnlyToStdout: true,
}
hwlog.InitRunLogger(&hwLogConfig, context.Background())
}
func TestGetDeviceType(t *testing.T) {
convey.Convey("test get device type", t, func() {
convey.Convey("right device byte", func() {
device := GetDeviceType(ValidDiveType)
convey.So(device, convey.ShouldEqual, "PSU")
})
convey.Convey("wrong device byte", func() {
device := GetDeviceType(WrongDiveType)
convey.So(device, convey.ShouldEqual, UnknownDevice)
})
})
}
func TestGetPattern(t *testing.T) {
convey.Convey("test get node pattern", t, func() {
convey.Convey("right node name str", func() {
pattern := GetPattern()[RegexNodeNameKey]
match := pattern.MatchString("node-1")
convey.So(match, convey.ShouldBeTrue)
})
convey.Convey("wrong node name str", func() {
pattern := GetPattern()[RegexNodeNameKey]
match := pattern.MatchString("!%!#das#%")
convey.So(match, convey.ShouldBeFalse)
})
})
}
func TestCopyStringSlice(t *testing.T) {
convey.Convey("test copy string slice", t, func() {
convey.Convey("copy string slice", func() {
oldStrSlice := []string{"test1", "test2"}
newStrSlice := CopyStringSlice(oldStrSlice)
convey.So(len(oldStrSlice), convey.ShouldEqual, len(newStrSlice))
for i, str := range oldStrSlice {
if i >= len(newStrSlice) {
return
}
convey.So(str, convey.ShouldEqual, newStrSlice[i])
}
})
})
}
func TestMakeDataHash(t *testing.T) {
convey.Convey("test make data hash", t, func() {
convey.Convey("hash success", func() {
NodeInfo := FaultDevInfo{FaultDevList: []*FaultDev{&FaultDev{
DeviceType: "PSU",
DeviceId: 0,
FaultCode: []string{"00000001"},
FaultLevel: NotHandleFault,
}}}
result := MakeDataHash(NodeInfo)
convey.So(result, convey.ShouldNotBeEmpty)
})
convey.Convey("Marshal failed", func() {
mockMarshal := gomonkey.ApplyFunc(json.Marshal, func(v interface{}) ([]byte, error) {
return nil, fmt.Errorf("marshal failed")
})
defer mockMarshal.Reset()
NodeInfo := FaultDevInfo{FaultDevList: []*FaultDev{&FaultDev{
DeviceType: "PSU",
DeviceId: 0,
FaultCode: []string{"00000001"},
FaultLevel: NotHandleFault,
}}}
result := MakeDataHash(NodeInfo)
convey.So(result, convey.ShouldBeEmpty)
})
})
}
func TestDeepCopyFaultConfig(t *testing.T) {
convey.Convey("test deep copy fault config", t, func() {
convey.Convey("deep copy fault config", func() {
oldFaultConfig := &FaultConfig{FaultTypeCode: testFaultTypeCode}
newFaultConfig := &FaultConfig{FaultTypeCode: &FaultTypeCode{}}
DeepCopyFaultConfig(oldFaultConfig, newFaultConfig)
FaultConfigEqual(oldFaultConfig, newFaultConfig)
})
})
}
func FaultConfigEqual(oldFaultConfig, newFaultConfig *FaultConfig) {
convey.So(oldFaultConfig.FaultTypeCode, convey.ShouldNotBeNil)
convey.So(newFaultConfig.FaultTypeCode, convey.ShouldNotBeNil)
FaultTypeCodesEqual(oldFaultConfig.FaultTypeCode, newFaultConfig.FaultTypeCode)
}
func TestRemoveDuplicateString(t *testing.T) {
convey.Convey("test remove duplicate string", t, func() {
convey.Convey("remove duplicate string", func() {
slice1 := []string{"00000001", "00000001", "00000002", "00000003"}
slice2 := []string{"00000001", "00000002", "00000003"}
newSlice := RemoveDuplicateString(slice1)
SliceStrEqual(newSlice, slice2)
})
})
}
func TestRevertByteSlice(t *testing.T) {
convey.Convey("test revert byte slice", t, func() {
convey.Convey("revert byte slice", func() {
slice1 := []byte{0x01, 0x02, 0x03}
slice2 := []byte{0x03, 0x02, 0x01}
RevertByteSlice(slice2)
SliceByteEqual(slice1, slice2)
})
})
}
func TestConvertIntToByteSlice(t *testing.T) {
convey.Convey("test covert positive int to byte slice", t, func() {
convey.Convey("covert int to byte slice", func() {
var num int64 = 257
targetByteSlice := []byte{0x01, 0x01}
resultByteSlice := ConvertIntToTwoByteSlice(num)
SliceByteEqual(resultByteSlice, targetByteSlice)
})
convey.Convey("convert negative int to byte slice", func() {
var num int64 = -1
targetByteSlice := []byte{0x00, 0x00}
resultByteSlice := ConvertIntToTwoByteSlice(num)
SliceByteEqual(resultByteSlice, targetByteSlice)
})
convey.Convey("convert normal int to slice to test slice length", func() {
var num int64 = 1
targetByteSlice := []byte{0x01, 0x00}
resultByteSlice := ConvertIntToTwoByteSlice(num)
SliceByteEqual(resultByteSlice, targetByteSlice)
})
convey.Convey("convert out of range int to slice to byte slice", func() {
var num int64 = 65536
targetByteSlice := []byte{0x00, 0x00}
resultByteSlice := ConvertIntToTwoByteSlice(num)
SliceByteEqual(resultByteSlice, targetByteSlice)
})
})
}
func TestNewSignalWatcher(t *testing.T) {
convey.Convey("create new signal watcher", t, func() {
signWatcher := NewSignalWatcher(syscall.SIGKILL)
convey.So(signWatcher, convey.ShouldNotBeNil)
})
}
func TestFilterDuplicateFaultCodes(t *testing.T) {
convey.Convey("test func FilterDuplicateFaultCodes", t, func() {
FilterDuplicateFaultCodes(testFaultTypeCode)
})
}
func TestToUpperFaultCodesStr(t *testing.T) {
convey.Convey("test func ToUpperFaultCodesStr", t, func() {
ToUpperFaultCodesStr(testFaultTypeCode)
})
}
func TestCheckFaultCodes(t *testing.T) {
convey.Convey("test func CheckFaultCodes", t, func() {
convey.Convey("check success", func() {
faultCodes := []string{"00000001", "00000001", "00000002", "00000003"}
convey.So(CheckFaultCodes(faultCodes), convey.ShouldBeNil)
})
convey.Convey("check failed", func() {
faultCodes := []string{"Wrong fault codes"}
err := CheckFaultCodes(faultCodes)
convey.So(err.Error(), convey.ShouldContainSubstring, "contains illegal character")
})
})
}
func FaultTypeCodesEqual(oldFaultTypeCode, newFaultTypeCode *FaultTypeCode) {
SliceStrEqual(oldFaultTypeCode.NotHandleFaultCodes, newFaultTypeCode.NotHandleFaultCodes)
SliceStrEqual(oldFaultTypeCode.PreSeparateFaultCodes, newFaultTypeCode.PreSeparateFaultCodes)
SliceStrEqual(oldFaultTypeCode.SeparateFaultCodes, newFaultTypeCode.SeparateFaultCodes)
}
func SliceStrEqual(slice1, slice2 []string) {
convey.So(len(slice1), convey.ShouldEqual, len(slice2))
if len(slice1) != len(slice2) {
return
}
for i := range slice1 {
convey.So(slice1[i], convey.ShouldEqual, slice2[i])
}
}
func SliceByteEqual(slice1, slice2 []byte) {
convey.So(len(slice1), convey.ShouldEqual, len(slice2))
if len(slice1) != len(slice2) {
return
}
for i := range slice1 {
convey.So(slice1[i], convey.ShouldEqual, slice2[i])
}
}
func TestTriggerUpdate(t *testing.T) {
convey.Convey("test TriggerUpdate", t, func() {
convey.Convey("01-when send ipmi, should get ipmi", func() {
TriggerUpdate("ipmi")
processType := <-GetTrigger()
convey.So(processType, convey.ShouldEqual, "ipmi")
})
})
}