Skip to content

Commit

Permalink
Fix and add tests for v3 and karbon client
Browse files Browse the repository at this point in the history
Add image nodes service func unit test

Image nodes test fixes

1. Add networking and file management foundation service unit tests.
2. Add progress of image nodes unit test.
3. Fix v3 image upload test.

Resolve lint erros

Fix upload unit test after rebase

Minor fix

Resolve lint errors

resolve lint errors

fix lint errors

Fix image node unit test

Fix tests

1. Add integration tests for foundation. 2. Update docs for imageing of nodes. 3. Update Existing tests. 4. Add new computed fields for printing cluster urls

Add foundation config json structure

Resolve lint errors

Acceptance tests workflow changes. Update karbon tests names

Minor fix in image upload test in foundation

Fix image upload tests for foundation

test_files is no longer need

Add build tags to upload image tests to remove it from compile tests

unit testcase for fc

lint fixes

adding testcases

lint fix

changing comments

test config

Change test names

Change some test names

Minor test filx

Conifg changes for regression
  • Loading branch information
bhatipradeep authored and abhimutant committed May 6, 2022
1 parent 7aebc8a commit 6da9785
Show file tree
Hide file tree
Showing 45 changed files with 2,778 additions and 163 deletions.
4 changes: 2 additions & 2 deletions GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ build: fmtcheck
go install

test: fmtcheck
go test $(TEST) -timeout=30s -parallel=4
go test --tags=unit $(TEST) -timeout=30s -parallel=4

testacc: fmtcheck
TF_ACC=1 go test $(TEST) -v $(TESTARGS) -timeout 200m -coverprofile c.out -covermode=count
TF_ACC=1 go test $(TEST) -v $(TESTARGS) -timeout 500m -coverprofile c.out -covermode=count

fmt:
@echo "==> Fixing source code with gofmt..."
Expand Down
137 changes: 74 additions & 63 deletions client/client_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package client

import (
"bytes"
"context"
"fmt"
"io"
Expand All @@ -19,6 +20,7 @@ const (
testLibraryVersion = "v3"
testAbsolutePath = "api/nutanix/" + testLibraryVersion
testUserAgent = "nutanix/" + testLibraryVersion
fileName = "v3/v3.go"
)

func setup() (*http.ServeMux, *Client, *httptest.Server) {
Expand Down Expand Up @@ -90,6 +92,50 @@ func TestNewRequest(t *testing.T) {
}
}

func TestNewUploadRequest(t *testing.T) {
c, err := NewClient(&Credentials{"foo.com", "username", "password", "", "", true, false, "", "", "", nil}, testUserAgent, testAbsolutePath, true)

if err != nil {
t.Errorf("Unexpected Error: %v", err)
}

inURL, outURL := "/foo", fmt.Sprintf(defaultBaseURL+testAbsolutePath+"/foo", httpPrefix, "foo.com")
inBody, _ := os.Open(fileName)
if err != nil {
t.Fatalf("Error opening file %v, error : %v", fileName, err)
}

// expected body
out, _ := os.Open(fileName)
outBody, _ := ioutil.ReadAll(out)

req, err := c.NewUploadRequest(context.TODO(), http.MethodPost, inURL, inBody)
if err != nil {
t.Fatalf("NewUploadRequest() errored out with error : %v", err.Error())
}
// test relative URL was expanded
if req.URL.String() != outURL {
t.Errorf("NewUploadRequest(%v) URL = %v, expected %v", inURL, req.URL, outURL)
}

//test body contents
got, _ := ioutil.ReadAll(req.Body)
if !bytes.Equal(got, outBody) {
t.Errorf("NewUploadRequest(%v) Body = %v, expected %v", inBody, string(got), string(outBody))
}

// test headers.
inHeaders := map[string]string{
"Content-Type": octetStreamType,
"Accept": mediaType,
}
for k, v := range inHeaders {
if v != req.Header[k][0] {
t.Errorf("NewUploadRequest() Header value for %v = %v, expected %v", k, req.Header[k][0], v)
}
}
}

func TestNewUnAuthRequest(t *testing.T) {
c, err := NewClient(&Credentials{"foo.com", "username", "password", "", "", true, false, "", "", "", nil}, testUserAgent, testAbsolutePath, true)

Expand Down Expand Up @@ -137,7 +183,8 @@ func TestNewUnAuthFormEncodedRequest(t *testing.T) {
}

inURL, outURL := "/foo", fmt.Sprintf(defaultBaseURL+testAbsolutePath+"/foo", httpPrefix, "foo.com")
inBody, outBody := map[string]string{"name": "bar", "fullname": "foobar"}, "name=bar&fullname=foobar"+"\n"
inBody := map[string]string{"name": "bar", "fullname": "foobar"}
outBody := map[string][]string{"name": {"bar"}, "fullname": {"foobar"}}

req, _ := c.NewUnAuthFormEncodedRequest(context.TODO(), http.MethodPost, inURL, inBody)

Expand All @@ -146,10 +193,13 @@ func TestNewUnAuthFormEncodedRequest(t *testing.T) {
t.Errorf("NewUnAuthFormEncodedRequest(%v) URL = %v, expected %v", inURL, req.URL, outURL)
}

// test body was JSON encoded
body, _ := ioutil.ReadAll(req.Body)
if string(body) != outBody {
t.Errorf("NewUnAuthFormEncodedRequest(%v) Body = %v, expected %v", inBody, string(body), outBody)
// test body
// Parse the body form data to a map structure which can be accessed by req.PostForm
req.ParseForm()

// check form encoded key-values as compared to input values
if !reflect.DeepEqual(outBody, (map[string][]string)(req.PostForm)) {
t.Errorf("NewUnAuthFormEncodedRequest(%v) Form encoded k-v, got = %v, expected %v", inBody, req.PostForm, outBody)
}

// test headers. Authorization header shouldn't exist
Expand All @@ -176,18 +226,28 @@ func TestNewUnAuthUploadRequest(t *testing.T) {
}

inURL, outURL := "/foo", fmt.Sprintf(defaultBaseURL+testAbsolutePath+"/foo", httpPrefix, "foo.com")
inBody, outBody := []byte("Yeah I am genius!"), "Yeah I am genius!"
req, _ := c.NewUnAuthUploadRequest(context.TODO(), http.MethodPost, inURL, inBody)
inBody, _ := os.Open(fileName)
if err != nil {
t.Fatalf("Error opening fiele %v, error : %v", fileName, err)
}

// expected body
out, _ := os.Open(fileName)
outBody, _ := ioutil.ReadAll(out)

req, err := c.NewUnAuthUploadRequest(context.TODO(), http.MethodPost, inURL, inBody)
if err != nil {
t.Fatalf("NewUnAuthUploadRequest() errored out with error : %v", err.Error())
}
// test relative URL was expanded
if req.URL.String() != outURL {
t.Errorf("NewUnAuthUploadRequest(%v) URL = %v, expected %v", inURL, req.URL, outURL)
}

//test body was JSON encoded
body, _ := ioutil.ReadAll(req.Body)
if string(body) != outBody {
t.Errorf("NewUnAuthUploadRequest(%v) Body = %v, expected %v", inBody, string(body), outBody)
//test body contents
got, _ := ioutil.ReadAll(req.Body)
if !bytes.Equal(got, outBody) {
t.Errorf("NewUnAuthUploadRequest(%v) Body = %v, expected %v", inBody, string(got), string(outBody))
}

// test headers. Authorization header shouldn't exist
Expand All @@ -197,11 +257,10 @@ func TestNewUnAuthUploadRequest(t *testing.T) {
inHeaders := map[string]string{
"Content-Type": octetStreamType,
"Accept": mediaType,
"User-Agent": testUserAgent,
}
for k, v := range req.Header {
if v[0] != inHeaders[k] {
t.Errorf("NewUnAuthUploadRequest() Header value for %v = %v, expected %v", k, v[0], inHeaders[k])
for k, v := range inHeaders {
if v != req.Header[k][0] {
t.Errorf("NewUploadRequest() Header value for %v = %v, expected %v", k, req.Header[k][0], v)
}
}
}
Expand Down Expand Up @@ -484,54 +543,6 @@ func TestClient_NewRequest(t *testing.T) {
}
}

func TestClient_NewUploadRequest(t *testing.T) {
type fields struct {
Credentials *Credentials
client *http.Client
BaseURL *url.URL
UserAgent string
onRequestCompleted RequestCompletionCallback
}
type args struct {
ctx context.Context
method string
urlStr string
file *os.File
}

tests := []struct {
name string
fields fields
args args
want *http.Request
wantErr bool
}{
// TODO: Add test cases.
}

for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
c := &Client{
Credentials: tt.fields.Credentials,
client: tt.fields.client,
BaseURL: tt.fields.BaseURL,
UserAgent: tt.fields.UserAgent,
onRequestCompleted: tt.fields.onRequestCompleted,
}
got, err := c.NewUploadRequest(tt.args.ctx, tt.args.method, tt.args.urlStr, tt.args.file)
if (err != nil) != tt.wantErr {
t.Errorf("Client.NewUploadRequest() error = %v, wantErr %v", err, tt.wantErr)

return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("Client.NewUploadRequest() = %v, want %v", got, tt.want)
}
})
}
}

func TestClient_OnRequestCompleted(t *testing.T) {
type fields struct {
Credentials *Credentials
Expand Down
Loading

0 comments on commit 6da9785

Please sign in to comment.