-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add unit testcase for addon file patterns
afero introduced for mocking a filesystem.
- Loading branch information
Showing
8 changed files
with
156 additions
and
15 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
/* | ||
Copyright 2016 The Kubernetes Authors 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 assets | ||
|
||
import ( | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/spf13/afero" | ||
"k8s.io/minikube/pkg/minikube/constants" | ||
) | ||
|
||
func TestAddMinikubeDirAssets(t *testing.T) { | ||
|
||
tests := []struct { | ||
description string | ||
baseDir string | ||
files []struct { | ||
relativePath string | ||
expectedPath string | ||
} | ||
vmPath string | ||
expectedCfg string | ||
}{ | ||
{ | ||
description: "relative path assets", | ||
baseDir: "/files", | ||
files: []struct { | ||
relativePath string | ||
expectedPath string | ||
}{ | ||
{ | ||
relativePath: "/dir1/file1.txt", | ||
expectedPath: constants.AddonsPath, | ||
}, | ||
{ | ||
relativePath: "/dir1/file2.txt", | ||
expectedPath: constants.AddonsPath, | ||
}, | ||
{ | ||
relativePath: "/dir2/file1.txt", | ||
expectedPath: constants.AddonsPath, | ||
}, | ||
}, | ||
vmPath: constants.AddonsPath, | ||
}, | ||
{ | ||
description: "absolute path assets", | ||
baseDir: "/files", | ||
files: []struct { | ||
relativePath string | ||
expectedPath string | ||
}{ | ||
{ | ||
relativePath: "/dir1/file1.txt", | ||
expectedPath: "/dir1", | ||
}, | ||
{ | ||
relativePath: "/dir1/file2.txt", | ||
expectedPath: "/dir1", | ||
}, | ||
{ | ||
relativePath: "/dir2/file1.txt", | ||
expectedPath: "/dir2", | ||
}, | ||
}, | ||
vmPath: "", | ||
}, | ||
} | ||
for _, test := range tests { | ||
t.Run(test.description, func(t *testing.T) { | ||
fs := afero.NewMemMapFs() | ||
assetNames := make([]string, 0) | ||
for _, fileDef := range test.files { | ||
err := func() error { | ||
path := filepath.Join(test.baseDir, fileDef.relativePath) | ||
assetNames = append(assetNames, path) | ||
err := fs.MkdirAll(filepath.Dir(path), 0755) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
file, err := fs.Create(path) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
defer file.Close() | ||
|
||
_, err = file.WriteString("test") | ||
return err | ||
}() | ||
if err != nil { | ||
t.Errorf("unable to create file on fs: %v", err) | ||
return | ||
} | ||
} | ||
|
||
var actualFiles []CopyableFile | ||
err := addMinikubeDirToAssets(fs, test.baseDir, test.vmPath, &actualFiles) | ||
if err != nil { | ||
t.Errorf("got unexpected error add minikube dir assets: %v", err) | ||
return | ||
} | ||
for idx, actualFile := range actualFiles { | ||
if actualFile.GetAssetName() != assetNames[idx] { | ||
t.Errorf("actual file assets does not match expected. actual:\n%s\nexpected:\n%s", actualFile.GetAssetName(), assetNames[idx]) | ||
return | ||
} | ||
if actualFile.GetTargetDir() != test.files[idx].expectedPath { | ||
t.Errorf("actual file target dir does not match expected. actual:\n%s\nexpected:\n%s", actualFile.GetTargetDir(), test.files[idx].expectedPath) | ||
return | ||
} | ||
} | ||
}) | ||
} | ||
|
||
} |
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
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