Tizen_4.0 base
[platform/upstream/docker-engine.git] / api / common_test.go
1 package api
2
3 import (
4         "io/ioutil"
5         "path/filepath"
6         "testing"
7
8         "os"
9 )
10
11 // LoadOrCreateTrustKey
12 func TestLoadOrCreateTrustKeyInvalidKeyFile(t *testing.T) {
13         tmpKeyFolderPath, err := ioutil.TempDir("", "api-trustkey-test")
14         if err != nil {
15                 t.Fatal(err)
16         }
17         defer os.RemoveAll(tmpKeyFolderPath)
18
19         tmpKeyFile, err := ioutil.TempFile(tmpKeyFolderPath, "keyfile")
20         if err != nil {
21                 t.Fatal(err)
22         }
23
24         if _, err := LoadOrCreateTrustKey(tmpKeyFile.Name()); err == nil {
25                 t.Fatal("expected an error, got nothing.")
26         }
27
28 }
29
30 func TestLoadOrCreateTrustKeyCreateKey(t *testing.T) {
31         tmpKeyFolderPath, err := ioutil.TempDir("", "api-trustkey-test")
32         if err != nil {
33                 t.Fatal(err)
34         }
35         defer os.RemoveAll(tmpKeyFolderPath)
36
37         // Without the need to create the folder hierarchy
38         tmpKeyFile := filepath.Join(tmpKeyFolderPath, "keyfile")
39
40         if key, err := LoadOrCreateTrustKey(tmpKeyFile); err != nil || key == nil {
41                 t.Fatalf("expected a new key file, got : %v and %v", err, key)
42         }
43
44         if _, err := os.Stat(tmpKeyFile); err != nil {
45                 t.Fatalf("Expected to find a file %s, got %v", tmpKeyFile, err)
46         }
47
48         // With the need to create the folder hierarchy as tmpKeyFie is in a path
49         // where some folders do not exist.
50         tmpKeyFile = filepath.Join(tmpKeyFolderPath, "folder/hierarchy/keyfile")
51
52         if key, err := LoadOrCreateTrustKey(tmpKeyFile); err != nil || key == nil {
53                 t.Fatalf("expected a new key file, got : %v and %v", err, key)
54         }
55
56         if _, err := os.Stat(tmpKeyFile); err != nil {
57                 t.Fatalf("Expected to find a file %s, got %v", tmpKeyFile, err)
58         }
59
60         // With no path at all
61         defer os.Remove("keyfile")
62         if key, err := LoadOrCreateTrustKey("keyfile"); err != nil || key == nil {
63                 t.Fatalf("expected a new key file, got : %v and %v", err, key)
64         }
65
66         if _, err := os.Stat("keyfile"); err != nil {
67                 t.Fatalf("Expected to find a file keyfile, got %v", err)
68         }
69 }
70
71 func TestLoadOrCreateTrustKeyLoadValidKey(t *testing.T) {
72         tmpKeyFile := filepath.Join("fixtures", "keyfile")
73
74         if key, err := LoadOrCreateTrustKey(tmpKeyFile); err != nil || key == nil {
75                 t.Fatalf("expected a key file, got : %v and %v", err, key)
76         }
77 }