MCPcopy Index your code
hub / github.com/getsops/sops / TestMasterKey_loadIdentities

Function TestMasterKey_loadIdentities

age/keysource_test.go:365–578  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

363}
364
365func TestMasterKey_loadIdentities(t *testing.T) {
366 t.Run(SopsAgeKeyEnv, func(t *testing.T) {
367 tmpDir := t.TempDir()
368 // Overwrite to ensure local config is not picked up by tests
369 overwriteUserConfigDir(t, tmpDir)
370
371 t.Setenv(SopsAgeKeyEnv, mockIdentity)
372
373 key := &MasterKey{}
374 got, unusedLocations, errs := key.loadIdentities()
375 assert.Len(t, errs, 0)
376 assert.Len(t, got, 1)
377 assert.Len(t, unusedLocations, 6)
378 })
379
380 t.Run(SopsAgeKeyEnv+" multiple", func(t *testing.T) {
381 tmpDir := t.TempDir()
382 // Overwrite to ensure local config is not picked up by tests
383 overwriteUserConfigDir(t, tmpDir)
384
385 t.Setenv(SopsAgeKeyEnv, mockIdentity+"\n"+mockOtherIdentity)
386
387 key := &MasterKey{}
388 got, unusedLocations, errs := key.loadIdentities()
389 assert.Len(t, errs, 0)
390 assert.Len(t, got, 2)
391 assert.Len(t, unusedLocations, 6)
392 })
393
394 t.Run(SopsAgeKeyFileEnv, func(t *testing.T) {
395 tmpDir := t.TempDir()
396 // Overwrite to ensure local config is not picked up by tests
397 overwriteUserConfigDir(t, tmpDir)
398
399 keyPath := filepath.Join(tmpDir, "keys.txt")
400 assert.NoError(t, os.WriteFile(keyPath, []byte(mockIdentity), 0o644))
401
402 t.Setenv(SopsAgeKeyFileEnv, keyPath)
403
404 key := &MasterKey{}
405 got, unusedLocations, errs := key.loadIdentities()
406 assert.Len(t, errs, 0)
407 assert.Len(t, got, 1)
408 assert.Len(t, unusedLocations, 6)
409 })
410
411 t.Run(SopsAgeKeyUserConfigPath, func(t *testing.T) {
412 tmpDir := t.TempDir()
413 overwriteUserConfigDir(t, tmpDir)
414
415 // We need to use getUserConfigDir and not tmpDir as it may add a suffix
416 cfgDir, err := getUserConfigDir()
417 assert.NoError(t, err)
418 keyPath := filepath.Join(cfgDir, SopsAgeKeyUserConfigPath)
419 assert.True(t, strings.HasPrefix(keyPath, cfgDir))
420
421 assert.NoError(t, os.MkdirAll(filepath.Dir(keyPath), 0o700))
422 assert.NoError(t, os.WriteFile(keyPath, []byte(mockIdentity), 0o644))

Callers

nothing calls this directly

Calls 5

loadIdentitiesMethod · 0.95
overwriteUserConfigDirFunction · 0.85
getUserConfigDirFunction · 0.85
LenMethod · 0.80
ErrorMethod · 0.45

Tested by

no test coverage detected