|
8 | 8 | "github.com/kubernetes-sigs/alibaba-cloud-csi-driver/pkg/mounter/oss" |
9 | 9 | "github.com/kubernetes-sigs/alibaba-cloud-csi-driver/pkg/mounter/utils" |
10 | 10 | "github.com/stretchr/testify/assert" |
| 11 | + "github.com/stretchr/testify/require" |
11 | 12 | "k8s.io/klog/v2" |
12 | 13 | ) |
13 | 14 |
|
@@ -52,60 +53,73 @@ func TestPrepareCredentialFiles(t *testing.T) { |
52 | 53 | } |
53 | 54 | for _, tt := range tests { |
54 | 55 | t.Run(tt.name, func(t *testing.T) { |
55 | | - |
56 | | - file, dir, options, err := prepareCredentialFiles("/tmp/token-files", tt.secrets) |
| 56 | + hash := utils.ComputeMountPathHash("/mnt/target1") |
| 57 | + file, dir, options, err := prepareCredentialFiles("/mnt/target1", tt.secrets) |
57 | 58 | assert.Equal(t, tt.wantErr, err != nil) |
58 | 59 | assert.Equal(t, tt.wantFile, file != "") |
59 | 60 | assert.Equal(t, tt.wantDir, dir != "") |
60 | 61 | assert.Equal(t, tt.wantOpts, len(options) != 0) |
61 | | - err = os.RemoveAll("/tmp/token-files") |
62 | | - klog.ErrorS(err, "Remove token directory", "dir", "/tmp/token-files") |
| 62 | + err = os.RemoveAll("/tmp/" + hash) |
| 63 | + if err != nil { |
| 64 | + klog.ErrorS(err, "Remove token directory", "dir", "/tmp/"+hash) |
| 65 | + } |
63 | 66 | }) |
64 | 67 | } |
65 | 68 |
|
66 | 69 | } |
67 | 70 |
|
68 | 71 | func TestRotateTokenFiles(t *testing.T) { |
| 72 | + mountPath := "/mnt/target2" |
| 73 | + hash := utils.ComputeMountPathHash(mountPath) |
| 74 | + hashDir := filepath.Join("/tmp", hash) |
| 75 | + err := os.MkdirAll(hashDir, 0o644) |
| 76 | + require.NoError(t, err) |
| 77 | + |
69 | 78 | // case 1: initialize fiexd AKSK |
70 | 79 | secrets := map[string]string{OssfsPasswdFile: "testPasswd"} |
71 | | - rotated, err := rotateTokenFiles("/tmp/token-files", secrets) |
| 80 | + rotated, err := rotateTokenFiles(hashDir, secrets) |
| 81 | + assert.Error(t, err) |
72 | 82 | assert.Equal(t, false, rotated) |
73 | | - assert.NoError(t, err) |
| 83 | + |
74 | 84 | // case 2: initialize token |
75 | 85 | secrets = map[string]string{ |
76 | 86 | oss.KeyAccessKeyId: "testAKID", |
77 | 87 | oss.KeyAccessKeySecret: "testAKSecret", |
78 | 88 | oss.KeyExpiration: "testExpiration", |
79 | 89 | oss.KeySecurityToken: "testSecurityToken", |
80 | 90 | } |
81 | | - rotated, err = rotateTokenFiles("/tmp/token-files", secrets) |
82 | | - assert.Equal(t, true, rotated) |
| 91 | + rotated, err = rotateTokenFiles(hashDir, secrets) |
83 | 92 | assert.NoError(t, err) |
84 | | - ak, _ := os.ReadFile(filepath.Join("/tmp/token-files", oss.KeyAccessKeyId)) |
| 93 | + assert.Equal(t, true, rotated) |
| 94 | + ak, _ := os.ReadFile(filepath.Join(hashDir, oss.KeyAccessKeyId)) |
85 | 95 | assert.Equal(t, "testAKID", string(ak)) |
86 | | - sk, _ := os.ReadFile(filepath.Join("/tmp/token-files", oss.KeyAccessKeySecret)) |
| 96 | + sk, _ := os.ReadFile(filepath.Join(hashDir, oss.KeyAccessKeySecret)) |
87 | 97 | assert.Equal(t, "testAKSecret", string(sk)) |
88 | | - exp, _ := os.ReadFile(filepath.Join("/tmp/token-files", oss.KeyExpiration)) |
| 98 | + exp, _ := os.ReadFile(filepath.Join(hashDir, oss.KeyExpiration)) |
89 | 99 | assert.Equal(t, "testExpiration", string(exp)) |
90 | | - st, _ := os.ReadFile(filepath.Join("/tmp/token-files", oss.KeySecurityToken)) |
| 100 | + st, _ := os.ReadFile(filepath.Join(hashDir, oss.KeySecurityToken)) |
91 | 101 | assert.Equal(t, "testSecurityToken", string(st)) |
| 102 | + |
92 | 103 | // case 3: rotate token |
93 | 104 | secrets = map[string]string{ |
94 | 105 | oss.KeyAccessKeyId: "newAKID", |
95 | 106 | oss.KeyAccessKeySecret: "newAKSecret", |
96 | 107 | oss.KeyExpiration: "newExpiration", |
97 | 108 | oss.KeySecurityToken: "newSecurityToken", |
98 | 109 | } |
99 | | - rotated, err = rotateTokenFiles("/tmp/token-files", secrets) |
100 | | - assert.Equal(t, true, rotated) |
| 110 | + rotated, err = rotateTokenFiles(hashDir, secrets) |
101 | 111 | assert.NoError(t, err) |
102 | | - ak, _ = os.ReadFile(filepath.Join("/tmp/token-files", oss.KeyAccessKeyId)) |
| 112 | + assert.Equal(t, true, rotated) |
| 113 | + ak, _ = os.ReadFile(filepath.Join(hashDir, oss.KeyAccessKeyId)) |
103 | 114 | assert.Equal(t, "newAKID", string(ak)) |
104 | | - sk, _ = os.ReadFile(filepath.Join("/tmp/token-files", oss.KeyAccessKeySecret)) |
| 115 | + sk, _ = os.ReadFile(filepath.Join(hashDir, oss.KeyAccessKeySecret)) |
105 | 116 | assert.Equal(t, "newAKSecret", string(sk)) |
106 | | - exp, _ = os.ReadFile(filepath.Join("/tmp/token-files", oss.KeyExpiration)) |
| 117 | + exp, _ = os.ReadFile(filepath.Join(hashDir, oss.KeyExpiration)) |
107 | 118 | assert.Equal(t, "newExpiration", string(exp)) |
108 | | - st, _ = os.ReadFile(filepath.Join("/tmp/token-files", oss.KeySecurityToken)) |
| 119 | + st, _ = os.ReadFile(filepath.Join(hashDir, oss.KeySecurityToken)) |
109 | 120 | assert.Equal(t, "newSecurityToken", string(st)) |
110 | | - os.RemoveAll("/tmp/token-files") |
| 121 | + err = os.RemoveAll(hashDir) |
| 122 | + if err != nil { |
| 123 | + t.Errorf("removeall hashdir failed %v", err) |
| 124 | + } |
111 | 125 | } |
0 commit comments