mirror of
https://github.com/bpg/terraform-provider-proxmox.git
synced 2025-06-30 18:42:58 +00:00
* feat(file): Add support to set the file mode GH-733 [1] implemented basic support for hook scripts, but the authors "did not manage to find time to work on" [2] also including support to set the file mode. This small but important feature makes the use of the `proxmox_virtual_environment_container.hook_script_file_id` [3] and `virtual_environment_vm.hook_script_file_id` [34] attributes basically useless when not combined with the manual step of making the uploaded file executable (manually running `chmod +x /path/to/script` or using other methods, based on the storage backend). Using the `hook_script_file_id` on its own also causes all planned and applies changes in the same execution to not be saved in the state because the Proxmox VE API responses with a HTTP `500` because the uploaded and assigned file is not executable. This pull request implements the missing feature to set the file mode by adding a new `file_mode` attribute of type `string` where an octal-formatted value can be passed, e.g. `0700` or only `600`. Note that the support for the octal prefixes `0o` and `0x` are not supported to reduced the complexity, even though Go of course support it, including the used `os.FileMode` type [5]. Changing the file mode also causes the file to be replaced, which is true for almost any attribute in the `proxmox_virtual_environment_file` resource, to ensure that the file mode can also be changed after the initial creation. [1]: https://github.com/bpg/terraform-provider-proxmox/pull/733 [2]: https://github.com/bpg/terraform-provider-proxmox/pull/733#issuecomment-2096716738 [3]: https://registry.terraform.io/providers/bpg/proxmox/latest/docs/resources/virtual_environment_container#hook_script_file_id [4]: https://registry.terraform.io/providers/bpg/proxmox/latest/docs/resources/virtual_environment_vm#hook_script_file_id [5]: https://pkg.go.dev/os#FileMode Related to GH-570 Related to GH-733 Signed-off-by: Sven Greb <development@svengreb.de> --------- Signed-off-by: Sven Greb <development@svengreb.de>
190 lines
5.7 KiB
Go
190 lines
5.7 KiB
Go
/*
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
*/
|
|
|
|
package resource
|
|
|
|
import (
|
|
"reflect"
|
|
"testing"
|
|
|
|
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
|
|
|
|
"github.com/bpg/terraform-provider-proxmox/proxmoxtf/test"
|
|
)
|
|
|
|
// TestFileInstantiation tests whether the File instance can be instantiated.
|
|
func TestFileInstantiation(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
s := File()
|
|
if s == nil {
|
|
t.Fatalf("Cannot instantiate File")
|
|
}
|
|
}
|
|
|
|
// TestFileSchema tests the File schema.
|
|
func TestFileSchema(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
s := File().Schema
|
|
|
|
test.AssertRequiredArguments(t, s, []string{
|
|
mkResourceVirtualEnvironmentFileDatastoreID,
|
|
mkResourceVirtualEnvironmentFileNodeName,
|
|
})
|
|
|
|
test.AssertOptionalArguments(t, s, []string{
|
|
mkResourceVirtualEnvironmentFileContentType,
|
|
mkResourceVirtualEnvironmentFileSourceFile,
|
|
mkResourceVirtualEnvironmentFileFileMode,
|
|
mkResourceVirtualEnvironmentFileSourceRaw,
|
|
mkResourceVirtualEnvironmentFileTimeoutUpload,
|
|
})
|
|
|
|
test.AssertComputedAttributes(t, s, []string{
|
|
mkResourceVirtualEnvironmentFileFileModificationDate,
|
|
mkResourceVirtualEnvironmentFileFileName,
|
|
mkResourceVirtualEnvironmentFileFileSize,
|
|
mkResourceVirtualEnvironmentFileFileTag,
|
|
})
|
|
|
|
test.AssertValueTypes(t, s, map[string]schema.ValueType{
|
|
mkResourceVirtualEnvironmentFileContentType: schema.TypeString,
|
|
mkResourceVirtualEnvironmentFileDatastoreID: schema.TypeString,
|
|
mkResourceVirtualEnvironmentFileFileModificationDate: schema.TypeString,
|
|
mkResourceVirtualEnvironmentFileFileName: schema.TypeString,
|
|
mkResourceVirtualEnvironmentFileFileMode: schema.TypeString,
|
|
mkResourceVirtualEnvironmentFileFileSize: schema.TypeInt,
|
|
mkResourceVirtualEnvironmentFileFileTag: schema.TypeString,
|
|
mkResourceVirtualEnvironmentFileNodeName: schema.TypeString,
|
|
mkResourceVirtualEnvironmentFileSourceFile: schema.TypeList,
|
|
mkResourceVirtualEnvironmentFileSourceRaw: schema.TypeList,
|
|
mkResourceVirtualEnvironmentFileTimeoutUpload: schema.TypeInt,
|
|
})
|
|
|
|
sourceFileSchema := test.AssertNestedSchemaExistence(t, s, mkResourceVirtualEnvironmentFileSourceFile)
|
|
|
|
test.AssertRequiredArguments(t, sourceFileSchema, []string{
|
|
mkResourceVirtualEnvironmentFileSourceFilePath,
|
|
})
|
|
|
|
test.AssertOptionalArguments(t, sourceFileSchema, []string{
|
|
mkResourceVirtualEnvironmentFileSourceFileChanged,
|
|
mkResourceVirtualEnvironmentFileSourceFileChecksum,
|
|
mkResourceVirtualEnvironmentFileSourceFileFileName,
|
|
mkResourceVirtualEnvironmentFileSourceFileInsecure,
|
|
})
|
|
|
|
test.AssertValueTypes(t, sourceFileSchema, map[string]schema.ValueType{
|
|
mkResourceVirtualEnvironmentFileSourceFileChanged: schema.TypeBool,
|
|
mkResourceVirtualEnvironmentFileSourceFileChecksum: schema.TypeString,
|
|
mkResourceVirtualEnvironmentFileSourceFileFileName: schema.TypeString,
|
|
mkResourceVirtualEnvironmentFileSourceFileInsecure: schema.TypeBool,
|
|
mkResourceVirtualEnvironmentFileSourceFilePath: schema.TypeString,
|
|
})
|
|
|
|
sourceRawSchema := test.AssertNestedSchemaExistence(t, s, mkResourceVirtualEnvironmentFileSourceRaw)
|
|
|
|
test.AssertRequiredArguments(t, sourceRawSchema, []string{
|
|
mkResourceVirtualEnvironmentFileSourceRawData,
|
|
mkResourceVirtualEnvironmentFileSourceRawFileName,
|
|
})
|
|
|
|
test.AssertOptionalArguments(t, sourceRawSchema, []string{
|
|
mkResourceVirtualEnvironmentFileSourceRawResize,
|
|
})
|
|
|
|
test.AssertValueTypes(t, sourceRawSchema, map[string]schema.ValueType{
|
|
mkResourceVirtualEnvironmentFileSourceRawData: schema.TypeString,
|
|
mkResourceVirtualEnvironmentFileSourceRawFileName: schema.TypeString,
|
|
mkResourceVirtualEnvironmentFileSourceRawResize: schema.TypeInt,
|
|
})
|
|
}
|
|
|
|
func Test_fileParseVolumeID(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
tests := []struct {
|
|
name string
|
|
id string
|
|
want fileVolumeID
|
|
wantErr bool
|
|
}{
|
|
{"empty", "", fileVolumeID{}, true},
|
|
{"missing datastore", "iso/file.ido", fileVolumeID{}, true},
|
|
{"missing type", "local:/file.ido", fileVolumeID{}, true},
|
|
{"missing file", "local:iso", fileVolumeID{}, true},
|
|
{"missing file", "local:iso/", fileVolumeID{}, true},
|
|
{"valid", "local:iso/file.iso", fileVolumeID{
|
|
datastoreID: "local",
|
|
contentType: "iso",
|
|
fileName: "file.iso",
|
|
}, false},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
got, err := fileParseVolumeID(tt.id)
|
|
if (err != nil) != tt.wantErr {
|
|
t.Errorf("fileParseVolumeID() error = %v, wantErr %v", err, tt.wantErr)
|
|
return
|
|
}
|
|
|
|
if !reflect.DeepEqual(got, tt.want) {
|
|
t.Errorf("fileParseVolumeID() got = %v, want %v", got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func Test_fileParseImportID(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
tests := []struct {
|
|
name string
|
|
id string
|
|
node string
|
|
volID fileVolumeID
|
|
wantErr bool
|
|
}{
|
|
{"empty", "", "", fileVolumeID{}, true},
|
|
{"missing node", "local:iso/file.iso", "", fileVolumeID{}, true},
|
|
{"missing node 2", "/local:iso/file.iso", "", fileVolumeID{}, true},
|
|
{
|
|
"valid", "pve/local:iso/file.iso",
|
|
"pve",
|
|
fileVolumeID{
|
|
datastoreID: "local",
|
|
contentType: "iso",
|
|
fileName: "file.iso",
|
|
},
|
|
false,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
node, volID, err := fileParseImportID(tt.id)
|
|
if (err != nil) != tt.wantErr {
|
|
t.Errorf("fileParseImportID() error = %v, wantErr %v", err, tt.wantErr)
|
|
return
|
|
}
|
|
|
|
if node != tt.node {
|
|
t.Errorf("fileParseImportID() got node = %v, want %v", node, tt.node)
|
|
}
|
|
|
|
if !reflect.DeepEqual(volID, tt.volID) {
|
|
t.Errorf("fileParseImportID() got volID = %v, want %v", volID, tt.volID)
|
|
}
|
|
})
|
|
}
|
|
}
|