Skip to content

Commit

Permalink
Provide registry resource method (#215)
Browse files Browse the repository at this point in the history
[#212] Provide registry resource method

Signed-off-by: Antonia Avramova <[email protected]>
  • Loading branch information
antonia-avramova authored Mar 28, 2023
1 parent 9d6570b commit f7cf7c4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 15 deletions.
21 changes: 6 additions & 15 deletions integration/util/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)
}
14 changes: 14 additions & 0 deletions integration/util/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit f7cf7c4

Please sign in to comment.