This repository has been archived by the owner on Jul 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #467 from 01org/sameo/topic/storage
ciao-image: Separate all storage interfaces and implement posix fs data storage
- Loading branch information
Showing
10 changed files
with
437 additions
and
234 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
// Copyright (c) 2016 Intel Corporation | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package datastore | ||
|
||
import ( | ||
"errors" | ||
"io" | ||
"time" | ||
|
||
"github.com/01org/ciao/openstack/image" | ||
) | ||
|
||
// State represents the state of the image. | ||
type State string | ||
|
||
const ( | ||
// Created means that an empty image has been created | ||
Created State = "created" | ||
|
||
// Saving means the image is being saved | ||
Saving State = "saving" | ||
|
||
// Active means that the image is created, uploaded and ready to use. | ||
Active State = "active" | ||
) | ||
|
||
// Status translate an image state to an openstack image status. | ||
func (state State) Status() image.Status { | ||
switch state { | ||
case Created: | ||
return image.Queued | ||
case Saving: | ||
return image.Saving | ||
case Active: | ||
return image.Active | ||
} | ||
|
||
return image.Active | ||
} | ||
|
||
// Visibility returns the image visibility | ||
func (i Image) Visibility() image.Visibility { | ||
if i.TenantID == "" { | ||
return image.Public | ||
} | ||
return image.Private | ||
} | ||
|
||
// Type represents the valid image types. | ||
type Type string | ||
|
||
const ( | ||
// Raw is the raw image format. | ||
Raw Type = "raw" | ||
|
||
// QCow is the qcow2 format. | ||
QCow Type = "qcow2" | ||
|
||
// ISO is the iso format. | ||
ISO Type = "iso" | ||
) | ||
|
||
// Image contains the information that ciao will store about the image | ||
type Image struct { | ||
ID string | ||
State State | ||
TenantID string | ||
Name string | ||
CreateTime time.Time | ||
Type Type | ||
} | ||
|
||
var ( | ||
// ErrNoImage is returned when an image is not found. | ||
ErrNoImage = errors.New("Image not found") | ||
|
||
// ErrImageSaving is returned when an image is being uploaded. | ||
ErrImageSaving = errors.New("Image being uploaded") | ||
) | ||
|
||
// DataStore is the image data storage interface. | ||
type DataStore interface { | ||
Init(RawDataStore, MetaDataStore) error | ||
CreateImage(Image) error | ||
GetAllImages() ([]Image, error) | ||
GetImage(string) (Image, error) | ||
UpdateImage(Image) error | ||
DeleteImage(string) error | ||
UploadImage(string, io.Reader) error | ||
} | ||
|
||
// MetaDataStore is the metadata storing interface that's used by | ||
// image cache implementation. | ||
type MetaDataStore interface { | ||
Write(Image) error | ||
Delete(ID string) error | ||
GetAll() ([]Image, error) | ||
} | ||
|
||
// RawDataStore is the raw data storage interface that's used by the | ||
// image cache implementation. | ||
type RawDataStore interface { | ||
Write(ID string, body io.Reader) (int64, error) | ||
Delete(ID string) error | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
// Copyright (c) 2016 Intel Corporation | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package datastore | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func testCreateAndGet(t *testing.T, d RawDataStore, m MetaDataStore) { | ||
i := Image{ | ||
ID: "validID", | ||
State: Created, | ||
} | ||
|
||
cache := ImageCache{} | ||
cache.Init(d, m) | ||
|
||
// create the entry | ||
err := cache.CreateImage(i) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
// retrieve the entry | ||
image, err := cache.GetImage(i.ID) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if image.ID != i.ID { | ||
t.Fatal(err) | ||
} | ||
} | ||
|
||
func testGetAll(t *testing.T, d RawDataStore, m MetaDataStore) { | ||
i := Image{ | ||
ID: "validID", | ||
State: Created, | ||
} | ||
|
||
cache := ImageCache{} | ||
cache.Init(d, m) | ||
|
||
// create the entry | ||
err := cache.CreateImage(i) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
// retrieve the entry | ||
images, err := cache.GetAllImages() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if len(images) != 1 { | ||
t.Fatalf("len is actually %d\n", len(images)) | ||
} | ||
|
||
if images[0].ID != i.ID { | ||
t.Fatal(err) | ||
} | ||
} | ||
|
||
func testDelete(t *testing.T, d RawDataStore, m MetaDataStore) { | ||
i := Image{ | ||
ID: "validID", | ||
State: Created, | ||
} | ||
|
||
cache := ImageCache{} | ||
cache.Init(d, m) | ||
|
||
// create the entry | ||
err := cache.CreateImage(i) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
// delete the entry | ||
err = cache.DeleteImage(i.ID) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
// now attempt to retrive the entry | ||
_, err = cache.GetImage(i.ID) | ||
if err == nil { | ||
t.Fatal(err) | ||
} | ||
} | ||
|
||
func testUpload(t *testing.T, d RawDataStore, m MetaDataStore) { | ||
i := Image{ | ||
ID: "validID", | ||
State: Created, | ||
} | ||
|
||
cache := ImageCache{} | ||
cache.Init(d, m) | ||
|
||
// create the entry | ||
err := cache.CreateImage(i) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
// Upload a string | ||
err = cache.UploadImage(i.ID, strings.NewReader("Upload file")) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
} | ||
|
||
var mountPoint = "/var/lib/ciao/images" | ||
|
||
func TestPosixNoopCreateAndGet(t *testing.T) { | ||
testCreateAndGet(t, &Posix{MountPoint: mountPoint}, &Noop{}) | ||
} | ||
|
||
func TestPosixNoopGetAll(t *testing.T) { | ||
testGetAll(t, &Posix{MountPoint: mountPoint}, &Noop{}) | ||
} | ||
|
||
func TestPosixNoopDelete(t *testing.T) { | ||
testDelete(t, &Posix{MountPoint: mountPoint}, &Noop{}) | ||
} | ||
|
||
func TestPosixNoopUpload(t *testing.T) { | ||
testUpload(t, &Posix{MountPoint: mountPoint}, &Noop{}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.