-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PR For bucket API test Work Done 1. CRUD for basic bucket 2. Test Bucket Directory 3. Test Bucket File --------- Signed-off-by: Yash Khare <yash2010118@akgec.ac.in>
- Loading branch information
1 parent
2d50a53
commit d3ba9c1
Showing
6 changed files
with
170 additions
and
2 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
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,132 @@ | ||
package bucket | ||
|
||
import ( | ||
"io/ioutil" | ||
"os" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/konveyor/tackle2-hub/test/assert" | ||
) | ||
|
||
func TestBucketCRUD(t *testing.T) { | ||
for _, bucket := range Buckets { | ||
bucketPath := bucket.Path | ||
t.Run("Create Bucket "+bucketPath+" and Compare", func(t *testing.T) { | ||
|
||
// Create a new bucket. | ||
assert.Must(t, Bucket.Create(&bucket)) | ||
|
||
// Get all the buckets. | ||
gotBuckets, err := Bucket.List() | ||
if err != nil { | ||
t.Errorf(err.Error()) | ||
} | ||
|
||
// Find for the specific bucket and compare Paths as it is a unique value. | ||
for _, gotBucket := range gotBuckets { | ||
if gotBucket.ID == bucket.ID { | ||
if gotBucket.Path != bucket.Path { | ||
t.Errorf("Difference in Path between the buckets %v and %v", gotBucket.Path, bucket.Path) | ||
} | ||
} | ||
} | ||
|
||
// Get specific bucket. | ||
gotBucket, err := Bucket.Get(uint(bucket.ID)) | ||
if err != nil { | ||
t.Errorf(err.Error()) | ||
} | ||
|
||
// Compare bucket Paths. | ||
if gotBucket.Path != bucket.Path { | ||
t.Errorf("Difference in Path between the buckets %v and %v", gotBucket.Path, bucket.Path) | ||
} | ||
}) | ||
|
||
t.Run("File and Directory Tests "+bucketPath, func(t *testing.T) { | ||
expectedBucket := Bucket.Content(bucket.ID) | ||
|
||
expectedFile, err := ioutil.TempFile("", "a") | ||
if err != nil { | ||
t.Errorf(err.Error()) | ||
} | ||
defer os.Remove(expectedFile.Name()) | ||
|
||
data := []byte("Hello World") | ||
_, err = expectedFile.Write(data) | ||
if err != nil { | ||
t.Errorf(err.Error()) | ||
} | ||
|
||
assert.Should(t, expectedBucket.Put(expectedFile.Name(), expectedFile.Name())) | ||
|
||
gotFile, err := ioutil.TempFile("", "b") | ||
if err != nil { | ||
t.Errorf(err.Error()) | ||
} | ||
defer os.Remove(gotFile.Name()) | ||
|
||
assert.Should(t, expectedBucket.Get(expectedFile.Name(), gotFile.Name())) | ||
|
||
expected, err := ioutil.ReadFile(expectedFile.Name()) | ||
if err != nil { | ||
t.Errorf(err.Error()) | ||
} | ||
|
||
got, err := ioutil.ReadFile(gotFile.Name()) | ||
if err != nil { | ||
t.Errorf(err.Error()) | ||
} | ||
|
||
if len(expected) != len(got) { | ||
t.Errorf("Mismatch in outputs") | ||
} | ||
|
||
/*----------------------Directory Tests----------------------*/ | ||
|
||
assert.Should(t, expectedBucket.Put("sample", "sample")) | ||
|
||
gotDir, err := ioutil.TempDir("", "b") | ||
if err != nil { | ||
t.Errorf(err.Error()) | ||
} | ||
defer os.RemoveAll(gotDir) | ||
|
||
assert.Should(t, expectedBucket.Get("sample", gotDir)) | ||
|
||
expectedDirContent, err := ioutil.ReadDir("sample") | ||
if err != nil { | ||
t.Errorf(err.Error()) | ||
} | ||
|
||
gotDirContent, err := ioutil.ReadDir(gotDir) | ||
if err != nil { | ||
t.Errorf(err.Error()) | ||
} | ||
|
||
// Compare length of expected and got Directory content | ||
if len(expectedDirContent) != len(gotDirContent) { | ||
t.Errorf("Mismatch in outputs") | ||
} | ||
|
||
// Compare elementwise content of expected and got Directory | ||
for i := 0; i < len(expectedDirContent); i++ { | ||
expectedDirInfo := expectedDirContent[i] | ||
gotDirInfo := gotDirContent[i] | ||
|
||
if strings.Split(expectedDirInfo.Name(), ".")[0] != strings.Split(gotDirInfo.Name(), ".")[0] { | ||
t.Errorf("Mismatch in names expected: %v, got: %v", strings.Split(expectedDirInfo.Name(), ".")[0], strings.Split(gotDirInfo.Name(), ".")[0]) | ||
} | ||
|
||
if expectedDirInfo.Mode() != gotDirInfo.Mode() { | ||
t.Errorf("Mismatch in modes expected: %v, got %v", expectedDirInfo.Mode(), gotDirInfo.Mode()) | ||
} | ||
|
||
} | ||
}) | ||
|
||
// Delete the bucket | ||
assert.Must(t, Bucket.Delete(bucket.ID)) | ||
} | ||
} |
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,23 @@ | ||
package bucket | ||
|
||
import ( | ||
"github.com/konveyor/tackle2-hub/binding" | ||
"github.com/konveyor/tackle2-hub/test/api/client" | ||
) | ||
|
||
var ( | ||
RichClient *binding.RichClient | ||
Client *binding.Client | ||
Bucket binding.Bucket | ||
) | ||
|
||
func init() { | ||
// Prepare RichClient and login to Hub API (configured from env variables). | ||
RichClient = client.PrepareRichClient() | ||
|
||
// Access REST client directly | ||
Client = RichClient.Client | ||
|
||
// Shortcut for Bucket related RichClient methods. | ||
Bucket = RichClient.Bucket | ||
} |
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 @@ | ||
Hello World! |
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,11 @@ | ||
package bucket | ||
|
||
import ( | ||
"github.com/konveyor/tackle2-hub/api" | ||
) | ||
|
||
var Buckets = []api.Bucket{ | ||
{ | ||
Path: "sample/sample.txt", | ||
}, | ||
} |