From a9369983f4ab34f104475ffc5c7550bc5d25c8d1 Mon Sep 17 00:00:00 2001 From: Antonia Avramova Date: Mon, 27 Mar 2023 17:06:21 +0300 Subject: [PATCH 1/2] Provide registry resource method Signed-off-by: Antonia Avramova --- integration/util/common.go | 21 ++++++--------------- integration/util/registry.go | 14 ++++++++++++++ 2 files changed, 20 insertions(+), 15 deletions(-) diff --git a/integration/util/common.go b/integration/util/common.go index 2d93fb9d..a0b351c1 100644 --- a/integration/util/common.go +++ b/integration/util/common.go @@ -45,22 +45,19 @@ func WriteConfigFile(path string, cfg interface{}) error { } // Preserve the file mode if the file already exists - mode, err := getFileModeOrDefault(path, configDefaultMode) - if err != nil { - return fmt.Errorf("unable to get file mode %s: %v", path, err) - } + mode := getFileModeOrDefault(path, configDefaultMode) if err = os.WriteFile(path, jsonContents, mode); err != nil { return fmt.Errorf("unable to save file %s: %v", path, err) } return nil } -func getFileModeOrDefault(path string, defaultMode os.FileMode) (os.FileMode, error) { +func getFileModeOrDefault(path string, defaultMode os.FileMode) os.FileMode { fileInfo, err := os.Stat(path) if err != nil { - return defaultMode, err + return defaultMode } - return fileInfo.Mode(), nil + return fileInfo.Mode() } // CopyFile copies source file to the destination. @@ -71,13 +68,7 @@ func CopyFile(src, dst string) error { } // If the destination file exists, preserve its file mode. // If the destination file doesn't exist, use the file mode of the source file. - srcMode, err := getFileModeOrDefault(src, configDefaultMode) - if err != nil { - return err - } - dstMode, err := getFileModeOrDefault(dst, srcMode) - if err != nil { - return err - } + srcMode := getFileModeOrDefault(src, configDefaultMode) + dstMode := getFileModeOrDefault(dst, srcMode) return os.WriteFile(dst, data, dstMode) } diff --git a/integration/util/registry.go b/integration/util/registry.go index 75cda490..38403bdb 100644 --- a/integration/util/registry.go +++ b/integration/util/registry.go @@ -102,6 +102,20 @@ func getCredentialsBody(authID, pass string) string { return string(data) } +// RegisterDeviceResources registers all given resources. If some error ocurred delete resources. +func RegisterDeviceResources(cfg *TestConfiguration, + resources []*Resource, deviceId, url, user, pass string) error { + for i, r := range resources { + if _, err := SendDeviceRegistryRequest(([]byte)(r.Body), r.Method, r.URL, r.User, r.Pass); err != nil { + if i > 0 { + DeleteResources(cfg, resources[:i], deviceId, url, user, pass) + } + return err + } + } + return nil +} + // DeleteResources deletes all given resources and all related devices. func DeleteResources(cfg *TestConfiguration, resources []*Resource, deviceID, url, user, pass string) error { var errors []error From 262b0200b2a22722522f2f2e1196e39482dbcb38 Mon Sep 17 00:00:00 2001 From: Antonia Avramova Date: Tue, 28 Mar 2023 12:11:02 +0300 Subject: [PATCH 2/2] Provide registry resource method Signed-off-by: Antonia Avramova --- integration/util/registry.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/integration/util/registry.go b/integration/util/registry.go index 38403bdb..5a4f2432 100644 --- a/integration/util/registry.go +++ b/integration/util/registry.go @@ -102,13 +102,13 @@ func getCredentialsBody(authID, pass string) string { return string(data) } -// RegisterDeviceResources registers all given resources. If some error ocurred delete resources. +// RegisterDeviceResources registers all given resources. In case of error all resources registered by this function will be deleted. func RegisterDeviceResources(cfg *TestConfiguration, - resources []*Resource, deviceId, url, user, pass string) error { + resources []*Resource, deviceID, url, user, pass string) error { for i, r := range resources { if _, err := SendDeviceRegistryRequest(([]byte)(r.Body), r.Method, r.URL, r.User, r.Pass); err != nil { if i > 0 { - DeleteResources(cfg, resources[:i], deviceId, url, user, pass) + DeleteResources(cfg, resources[:i], deviceID, url, user, pass) } return err }