Skip to content

Commit

Permalink
🌱 add bucket API test (#432)
Browse files Browse the repository at this point in the history
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
khareyash05 authored Sep 19, 2023
1 parent 2d50a53 commit d3ba9c1
Show file tree
Hide file tree
Showing 6 changed files with 170 additions and 2 deletions.
3 changes: 2 additions & 1 deletion binding/bucket.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package binding

import (
"github.com/konveyor/tackle2-hub/api"
pathlib "path"

"github.com/konveyor/tackle2-hub/api"
)

//
Expand Down
2 changes: 1 addition & 1 deletion docs/test-api-matrix.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Endpoint | binding functions | basic CRUD test | deeper test| notes/component/st
-- | -- | -- | -- | --
**Application Inventory**||||
application|:white_check_mark: partially|:heavy_check_mark:||
bucket||||partially within application
bucket|:heavy_check_mark:|:heavy_check_mark:||partially within application
dependency|:heavy_check_mark:|:heavy_check_mark:|:heavy_check_mark:|
file|:heavy_check_mark:|:heavy_check_mark:||
import||:heavy_check_mark:||
Expand Down
132 changes: 132 additions & 0 deletions test/api/bucket/api_test.go
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))
}
}
23 changes: 23 additions & 0 deletions test/api/bucket/pkg.go
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
}
1 change: 1 addition & 0 deletions test/api/bucket/sample/sample.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello World!
11 changes: 11 additions & 0 deletions test/api/bucket/samples.go
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",
},
}

0 comments on commit d3ba9c1

Please sign in to comment.