forked from aws/amazon-ecs-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfs_cache_test.go
More file actions
122 lines (103 loc) · 3.5 KB
/
fs_cache_test.go
File metadata and controls
122 lines (103 loc) · 3.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// Copyright 2015-2017 Amazon.com, Inc. or its affiliates. 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. A copy of the
// License is located at
//
// http://aws.amazon.com/apache2.0/
//
// or in the "license" file accompanying this file. This file 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 cache
import (
"io/ioutil"
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
)
func TestCacheCreatesDir(t *testing.T) {
tempDirName := tempDir(t)
defer os.Remove(tempDirName)
os.Setenv("HOME", tempDirName)
defer os.Unsetenv("HOME")
created := make(chan string)
osMkdirAll = func(path string, perms os.FileMode) error {
if perms != 0700 {
t.Errorf("directory created with more open perms than expected; %v", perms)
}
go func() { created <- path }()
return nil
}
NewFSCache("foo")
dir := <-created
expected, _ := cacheDir("foo")
if dir != expected {
t.Errorf("expected %v, got %v", expected, dir)
}
}
func TestCacheDirLinux(t *testing.T) {
// Mock GetOSName in the test, then reset it after the test
oldGetOSName := getOSName
getOSName = func() string {
return "linux"
}
defer func() { getOSName = oldGetOSName }()
// Create a temporary directory for the dummy ecs config
tempDirName, err := ioutil.TempDir("", "test")
if err != nil {
t.Fatal("Error while creating the dummy ecs config directory")
}
defer os.Remove(tempDirName)
os.Setenv("HOME", tempDirName)
defer os.Unsetenv("HOME")
path, err := cacheDir("name")
assert.NoError(t, err, "Unexpected error creating new config path")
assert.Equal(t, filepath.Join(tempDirName, ".cache", cachePrefix, "name"), path)
}
func TestCacheDirDarwin(t *testing.T) {
// Mock GetOSName in the test, then reset it after the test
oldGetOSName := getOSName
getOSName = func() string {
return "darwin"
}
defer func() { getOSName = oldGetOSName }()
// Create a temporary directory for the dummy ecs config
tempDirName, err := ioutil.TempDir("", "test")
if err != nil {
t.Fatal("Error while creating the dummy ecs config directory")
}
defer os.Remove(tempDirName)
os.Setenv("HOME", tempDirName)
defer os.Unsetenv("HOME")
path, err := cacheDir("name")
assert.NoError(t, err, "Unexpected error creating new config path")
assert.Equal(t, filepath.Join(tempDirName, ".cache", cachePrefix, "name"), path)
}
func TestCacheDirWindows(t *testing.T) {
// Mock GetOSName in the test, then reset it after the test
oldGetOSName := getOSName
getOSName = func() string {
return "windows"
}
defer func() { getOSName = oldGetOSName }()
// Create a temporary directory for the dummy ecs config
tempDirName, err := ioutil.TempDir("", "test")
if err != nil {
t.Fatal("Error while creating the dummy ecs config directory")
}
defer os.Remove(tempDirName)
os.Setenv("HOME", tempDirName)
defer os.Unsetenv("HOME")
path, err := cacheDir("name")
assert.NoError(t, err, "Unexpected error creating new config path")
assert.Equal(t, filepath.Join(tempDirName, "AppData", "local", "ecs", "cache", "name"), path)
}
func tempDir(t *testing.T) string {
// Create a temporary directory for the dummy ecs config
tempDirName, err := ioutil.TempDir("", "test")
assert.NoError(t, err, "Unexpected error while creating the dummy ecs config directory")
return tempDirName
}