github.com/verrazzano/verrazzano-monitoring-operator@v0.0.30/verrazzano-backup-hook/utilities/basicUtils_test.go (about) 1 // Copyright (c) 2022, Oracle and/or its affiliates. 2 // Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl. 3 4 package utilities_test 5 6 import ( 7 "encoding/json" 8 "fmt" 9 "github.com/stretchr/testify/assert" 10 "github.com/verrazzano/verrazzano-monitoring-operator/verrazzano-backup-hook/log" 11 utils "github.com/verrazzano/verrazzano-monitoring-operator/verrazzano-backup-hook/utilities" 12 "go.uber.org/zap" 13 "os" 14 "strings" 15 "testing" 16 ) 17 18 func logHelper() (*zap.SugaredLogger, string) { 19 file, err := os.CreateTemp(os.TempDir(), fmt.Sprintf("verrazzano-%s-hook-*.log", strings.ToLower("TEST"))) 20 if err != nil { 21 fmt.Printf("Unable to create temp file") 22 os.Exit(1) 23 } 24 defer file.Close() 25 log, _ := log.Logger(file.Name()) 26 return log, file.Name() 27 } 28 29 // TestCreateTempFileWithData tests the CreateTempFileWithData method create a temp file for snapshot registration 30 // GIVEN input data as a []byte 31 // WHEN file needs to be created as a temp file 32 // THEN creates a files under temp and returns the filepath 33 func TestCreateTempFileWithData(t *testing.T) { 34 t.Parallel() 35 nullBody := make(map[string]interface{}) 36 data, _ := json.Marshal(nullBody) 37 file, err := utils.CreateTempFileWithData(data) 38 defer os.Remove(file) 39 assert.Nil(t, err) 40 assert.NotNil(t, file) 41 } 42 43 // TestWaitRandom tests the WaitRandom method 44 // GIVEN min and max limits 45 // WHEN invoked from another method 46 // THEN generates a crypto safe random number in a predefined range and waits for that duration 47 func TestWaitRandom(t *testing.T) { 48 t.Parallel() 49 log, fname := logHelper() 50 defer os.Remove(fname) 51 message := "Waiting for Verrazzano Monitoring Operator to come up" 52 _, err := utils.WaitRandom(message, "1s", log) 53 assert.Nil(t, err) 54 } 55 56 // TestReadTempCredsFile tests the ReadTempCredsFile method for the following use case. 57 // GIVEN an existing file to read 58 // WHEN the file exists 59 // THEN read the keys from the file 60 func TestReadTempCredsFile(t *testing.T) { 61 t.Parallel() 62 file, err := os.CreateTemp(os.TempDir(), fmt.Sprintf("test-%s-hook-*.log", strings.ToLower("TEST"))) 63 if err != nil { 64 fmt.Printf("Unable to create temp file") 65 os.Exit(1) 66 } 67 file.Close() 68 data1, data2, err := utils.ReadTempCredsFile(file.Name(), "default") 69 assert.Equal(t, "", data1) 70 assert.Equal(t, "", data2) 71 assert.Nil(t, err) 72 os.Remove(file.Name()) 73 74 fileNotExist := "/tmp/foo.txt" 75 data1, data2, err = utils.ReadTempCredsFile(fileNotExist, "default") 76 assert.Equal(t, "", data1) 77 assert.Equal(t, "", data2) 78 assert.Nil(t, err) 79 } 80 81 // TestGetComponent tests the GetComponent method for the following use case. 82 // GIVEN a file with a single line 83 // WHEN the file exists 84 // THEN read and return the single line 85 func TestGetComponent(t *testing.T) { 86 t.Parallel() 87 file, err := os.CreateTemp(os.TempDir(), fmt.Sprintf("component-%s-hook-*.log", strings.ToLower("TEST"))) 88 if err != nil { 89 fmt.Printf("Unable to create temp file") 90 os.Exit(1) 91 } 92 d1 := []byte("opensearch") 93 file.Write(d1) 94 file.Close() 95 96 value, err := utils.GetComponent(file.Name()) 97 assert.Nil(t, err) 98 assert.Equal(t, value, "opensearch") 99 os.Remove(file.Name()) 100 101 _, err = utils.GetComponent("/tmp/foo") 102 assert.NotNil(t, err) 103 104 }