From f7cf7c41b5dcfef7e23bc71123f30bf126f49b84 Mon Sep 17 00:00:00 2001 From: Antonia Avramova Date: Tue, 28 Mar 2023 16:52:58 +0300 Subject: [PATCH] Provide registry resource method (#215) [#212] 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..5a4f2432 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. 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 { + 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