-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into update-sample-config
- Loading branch information
Showing
4 changed files
with
128 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
Copyright © 2019-2023 Dell Inc. or its subsidiaries. All Rights Reserved. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package service | ||
|
||
import ( | ||
"context" | ||
"os" | ||
"testing" | ||
|
||
"github.com/dell/csi-unity/service/utils" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
// Split TestMkdir into separate tests to test each case | ||
func TestMkdirCreateDir(t *testing.T) { | ||
log := utils.GetLogger() | ||
ctx := context.Background() | ||
entry := log.WithField(utils.RUNID, "1111") | ||
ctx = context.WithValue(ctx, utils.UnityLogger, entry) | ||
|
||
// Make temp directory to use for testing | ||
basepath, err := os.MkdirTemp("/tmp", "*") | ||
defer os.RemoveAll(basepath) | ||
path := basepath + "/test" | ||
|
||
// Test creating a directory | ||
created, err := mkdir(ctx, path) | ||
assert.NoError(t, err) | ||
assert.True(t, created) | ||
} | ||
|
||
func TestMkdirExistingDir(t *testing.T) { | ||
log := utils.GetLogger() | ||
ctx := context.Background() | ||
entry := log.WithField(utils.RUNID, "1111") | ||
ctx = context.WithValue(ctx, utils.UnityLogger, entry) | ||
|
||
// Make temp directory to use for testing | ||
basepath, err := os.MkdirTemp("/tmp", "*") | ||
defer os.RemoveAll(basepath) | ||
|
||
path := basepath + "/test" | ||
|
||
// Pre-create the directory | ||
err = os.Mkdir(path, 0o755) | ||
assert.NoError(t, err) | ||
|
||
// Test creating an existing directory | ||
created, err := mkdir(ctx, path) | ||
assert.NoError(t, err) | ||
assert.False(t, created) | ||
} | ||
|
||
func TestMkdirExistingFile(t *testing.T) { | ||
log := utils.GetLogger() | ||
ctx := context.Background() | ||
entry := log.WithField(utils.RUNID, "1111") | ||
ctx = context.WithValue(ctx, utils.UnityLogger, entry) | ||
|
||
// Make temp directory to use for testing | ||
basepath, err := os.MkdirTemp("/tmp", "*") | ||
defer os.RemoveAll(basepath) | ||
|
||
path := basepath + "/file" | ||
file, err := os.Create(path) | ||
assert.NoError(t, err) | ||
file.Close() | ||
|
||
// Test creating a directory with an existing file path | ||
created, err := mkdir(ctx, path) | ||
assert.Error(t, err) | ||
assert.False(t, created) | ||
} |