|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "time" |
| 5 | + "github.com/docker/docker/pkg/integration/checker" |
| 6 | + "github.com/go-check/check" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "gopkg.in/mgo.v2" |
| 10 | + "gopkg.in/mgo.v2/bson" |
| 11 | + "os" |
| 12 | + //"fmt" |
| 13 | +) |
| 14 | + |
| 15 | +func (s *DockerSuite) TestLoadFromLegacyImageArchiveFile(c *check.C) { |
| 16 | + printTestCaseName(); defer printTestDuration(time.Now()) |
| 17 | + testRequires(c, DaemonIsLinux) |
| 18 | + |
| 19 | + imageName := "ubuntu"; |
| 20 | + legacyImageUrl := "http://image-tarball.s3.amazonaws.com/test/public/ubuntu_1.8.tar.gz" |
| 21 | + imageUrl := "http://image-tarball.s3.amazonaws.com/test/public/ubuntu_1.10.tar.gz" |
| 22 | + |
| 23 | + |
| 24 | + ///////////////////////////////////////////////////////////////////// |
| 25 | + checkImageQuota(c, 2) |
| 26 | + //load legacy image(saved by docker 1.8) |
| 27 | + output, exitCode, err := dockerCmdWithError("load", "-i", legacyImageUrl) |
| 28 | + c.Assert(output, checker.Contains, "Start to download and load the image archive, please wait...\n") |
| 29 | + c.Assert(output, checker.Contains, "has been loaded.\n") |
| 30 | + c.Assert(exitCode, checker.Equals, 0) |
| 31 | + c.Assert(err, checker.IsNil) |
| 32 | + |
| 33 | + output, _ = dockerCmd(c, "images") |
| 34 | + c.Assert(output, checker.Contains, imageName) |
| 35 | + c.Assert(len(strings.Split(output, "\n")), checker.Equals, 3) |
| 36 | + |
| 37 | + |
| 38 | + ///////////////////////////////////////////////////////////////////// |
| 39 | + checkImageQuota(c, 1) |
| 40 | + //load new format image(saved by docker 1.10) |
| 41 | + output, exitCode, err = dockerCmdWithError("load", "-i", imageUrl) |
| 42 | + c.Assert(output, checker.Contains, "Start to download and load the image archive, please wait...\n") |
| 43 | + c.Assert(output, checker.Contains, "has been loaded.\n") |
| 44 | + c.Assert(exitCode, checker.Equals, 0) |
| 45 | + c.Assert(err, checker.IsNil) |
| 46 | + |
| 47 | + output, _ = dockerCmd(c, "images") |
| 48 | + c.Assert(output, checker.Contains, imageName) |
| 49 | + c.Assert(len(strings.Split(output, "\n")), checker.Equals, 3) |
| 50 | + |
| 51 | + |
| 52 | + ///////////////////////////////////////////////////////////////////// |
| 53 | + checkImageQuota(c, 1) |
| 54 | + //delete single layer |
| 55 | + output, _ = dockerCmd(c, "images", "-q", imageName) |
| 56 | + imageId := strings.Split(output, "\n")[0] |
| 57 | + c.Assert(imageId, checker.Not(checker.Equals), "") |
| 58 | + |
| 59 | + output, _ = dockerCmd(c, "rmi", "--no-prune", imageId) |
| 60 | + c.Assert(output, checker.Contains, "Untagged:") |
| 61 | + c.Assert(output, checker.Contains, "Deleted:") |
| 62 | + |
| 63 | + checkImageQuota(c, 1) |
| 64 | + |
| 65 | + output, _ = dockerCmd(c, "images") |
| 66 | + c.Assert(output, checker.Contains, "<none>") |
| 67 | + c.Assert(len(strings.Split(output, "\n")), checker.Equals, 3) |
| 68 | + imageId = strings.Split(output, "\n")[0] |
| 69 | + |
| 70 | + output, _ = dockerCmd(c, "images", "-a") |
| 71 | + c.Assert(output, checker.Contains, "<none>") |
| 72 | + c.Assert(len(strings.Split(output, "\n")), checker.Equals, 6) |
| 73 | + |
| 74 | + |
| 75 | + ///////////////////////////////////////////////////////////////////// |
| 76 | + checkImageQuota(c, 1) |
| 77 | + //delete all rest layer |
| 78 | + output, _ = dockerCmd(c, "images", "-q") |
| 79 | + imageId = strings.Split(output, "\n")[0] |
| 80 | + c.Assert(imageId, checker.Not(checker.Equals), "") |
| 81 | + |
| 82 | + output, _ = dockerCmd(c, "rmi", imageId) |
| 83 | + c.Assert(output, checker.Contains, "Deleted:") |
| 84 | + |
| 85 | + checkImageQuota(c, 2) |
| 86 | + |
| 87 | + output, _ = dockerCmd(c, "images") |
| 88 | + c.Assert(len(strings.Split(output, "\n")), checker.Equals, 2) |
| 89 | + |
| 90 | + output, _ = dockerCmd(c, "images", "-a") |
| 91 | + c.Assert(len(strings.Split(output, "\n")), checker.Equals, 2) |
| 92 | +} |
| 93 | + |
| 94 | + |
| 95 | +//func (s *DockerSuite) TestCheckImageQuota(c *check.C) { |
| 96 | +// printTestCaseName(); defer printTestDuration(time.Now()) |
| 97 | +// testRequires(c, DaemonIsLinux) |
| 98 | +// checkImageQuota(c, 2) |
| 99 | +//} |
| 100 | + |
| 101 | +func checkImageQuota(c *check.C, expected int) { |
| 102 | + |
| 103 | + //collection struct: credential |
| 104 | + type Credential struct { |
| 105 | + TenantId string `bson:"tenantId"` |
| 106 | + } |
| 107 | + |
| 108 | + //collection struct: tenant |
| 109 | + type Total struct { |
| 110 | + Images int `bson:"images"` |
| 111 | + } |
| 112 | + type Balance struct { |
| 113 | + Images int `bson:"images"` |
| 114 | + } |
| 115 | + type Resourceinfo struct { |
| 116 | + Total Total `bson:"total"` |
| 117 | + Balance Balance `bson:"balance"` |
| 118 | + } |
| 119 | + type Tenant struct { |
| 120 | + Resourceinfo Resourceinfo `bson:"resourceinfo"` |
| 121 | + } |
| 122 | + |
| 123 | + |
| 124 | + /////////////////////////////////////////// |
| 125 | + //init connection to mongodb |
| 126 | + session, err := mgo.Dial(os.Getenv("MONGODB_URL")) |
| 127 | + if err != nil { |
| 128 | + panic(err) |
| 129 | + } |
| 130 | + defer session.Close() |
| 131 | + // Optional. Switch the session to a monotonic behavior. |
| 132 | + session.SetMode(mgo.Monotonic, true) |
| 133 | + db := session.DB("hypernetes") |
| 134 | + |
| 135 | + /////////////////////////////////////////// |
| 136 | + // query tenantId by accessKey |
| 137 | + collection := db.C("credentials") |
| 138 | + resultCred := Credential{} |
| 139 | + |
| 140 | + //countNum, _ := collection.Find(condition).Count() |
| 141 | + //fmt.Println("\ncount:\n", countNum) |
| 142 | + |
| 143 | + collection.Find(bson.M{"accessKey": os.Getenv("ACCESS_KEY")}).Select(bson.M{"tenantId": 1}).One(&resultCred) |
| 144 | + c.Assert(resultCred.TenantId, checker.NotNil) |
| 145 | + tenantId := resultCred.TenantId |
| 146 | + |
| 147 | + |
| 148 | + /////////////////////////////////////////// |
| 149 | + // query image quota by tenant |
| 150 | + collection = db.C("tenant") |
| 151 | + resultTenant := Tenant{} |
| 152 | + |
| 153 | + //countNum, _ := collection.Find(condition).Count() |
| 154 | + //fmt.Println("\ncount:\n", countNum) |
| 155 | + |
| 156 | + collection.Find(bson.M{"tenantid": tenantId}).Select(bson.M{"resourceinfo": 1}).One(&resultTenant) |
| 157 | + //fmt.Printf("total images: %v\n", resultTenant.Resourceinfo.Total.Images) |
| 158 | + //fmt.Printf("balance images: %v\n", resultTenant.Resourceinfo.Balance.Images) |
| 159 | + totalImages := resultTenant.Resourceinfo.Total.Images |
| 160 | + balanceImages := resultTenant.Resourceinfo.Balance.Images |
| 161 | + |
| 162 | + c.Assert(totalImages, checker.GreaterThan, 0) |
| 163 | + c.Assert(balanceImages, checker.LessOrEqualThan, totalImages) |
| 164 | + c.Assert(balanceImages, checker.Equals, expected) |
| 165 | +} |
0 commit comments