github.com/afbjorklund/moby@v20.10.5+incompatible/daemon/trustkey_test.go (about) 1 package daemon // import "github.com/docker/docker/daemon" 2 3 import ( 4 "io/ioutil" 5 "os" 6 "path/filepath" 7 "testing" 8 9 "gotest.tools/v3/assert" 10 is "gotest.tools/v3/assert/cmp" 11 "gotest.tools/v3/fs" 12 ) 13 14 // LoadOrCreateTrustKey 15 func TestLoadOrCreateTrustKeyInvalidKeyFile(t *testing.T) { 16 tmpKeyFolderPath, err := ioutil.TempDir("", "api-trustkey-test") 17 assert.NilError(t, err) 18 defer os.RemoveAll(tmpKeyFolderPath) 19 20 tmpKeyFile, err := ioutil.TempFile(tmpKeyFolderPath, "keyfile") 21 assert.NilError(t, err) 22 23 _, err = loadOrCreateTrustKey(tmpKeyFile.Name()) 24 assert.Check(t, is.ErrorContains(err, "Error loading key file")) 25 } 26 27 func TestLoadOrCreateTrustKeyCreateKeyWhenFileDoesNotExist(t *testing.T) { 28 tmpKeyFolderPath := fs.NewDir(t, "api-trustkey-test") 29 defer tmpKeyFolderPath.Remove() 30 31 // Without the need to create the folder hierarchy 32 tmpKeyFile := tmpKeyFolderPath.Join("keyfile") 33 34 key, err := loadOrCreateTrustKey(tmpKeyFile) 35 assert.NilError(t, err) 36 assert.Check(t, key != nil) 37 38 _, err = os.Stat(tmpKeyFile) 39 assert.NilError(t, err, "key file doesn't exist") 40 } 41 42 func TestLoadOrCreateTrustKeyCreateKeyWhenDirectoryDoesNotExist(t *testing.T) { 43 tmpKeyFolderPath := fs.NewDir(t, "api-trustkey-test") 44 defer tmpKeyFolderPath.Remove() 45 tmpKeyFile := tmpKeyFolderPath.Join("folder/hierarchy/keyfile") 46 47 key, err := loadOrCreateTrustKey(tmpKeyFile) 48 assert.NilError(t, err) 49 assert.Check(t, key != nil) 50 51 _, err = os.Stat(tmpKeyFile) 52 assert.NilError(t, err, "key file doesn't exist") 53 } 54 55 func TestLoadOrCreateTrustKeyCreateKeyNoPath(t *testing.T) { 56 defer os.Remove("keyfile") 57 key, err := loadOrCreateTrustKey("keyfile") 58 assert.NilError(t, err) 59 assert.Check(t, key != nil) 60 61 _, err = os.Stat("keyfile") 62 assert.NilError(t, err, "key file doesn't exist") 63 } 64 65 func TestLoadOrCreateTrustKeyLoadValidKey(t *testing.T) { 66 tmpKeyFile := filepath.Join("testdata", "keyfile") 67 key, err := loadOrCreateTrustKey(tmpKeyFile) 68 assert.NilError(t, err) 69 expected := "AWX2:I27X:WQFX:IOMK:CNAK:O7PW:VYNB:ZLKC:CVAE:YJP2:SI4A:XXAY" 70 assert.Check(t, is.Contains(key.String(), expected)) 71 }