Run Ginkgo automation tests on LambdaTest cloud grid
Perform automation testing on 3000+ real desktop and mobile devices online.
package datastore
import (
"database/sql"
"encoding/json"
"log"
"strconv"
"strings"
)
// WatchInsert type used to insert records into the db
type WatchInsert struct {
SubscriptionRecordID string `json:"subscription_record_id,omitempty" col:"subscription_id"`
Kind string `json:"kind,omitempty" col:"kind"`
Path string `json:"path,omitempty" col:"path"`
CRNFull []string `json:"crnMasks,omitempty" col:"crn_full"`
Wildcards string `json:"wildcards,omitempty" col:"wildcards"`
RecordIDToWatch []string `json:"recordIDToWatch,omitempty" col:"record_id_to_watch"`
SubscriptionEmail string `json:"subscriptionEmail,omitempty" col:"subscription_email"`
}
// WatchGet type used to get records from the db
type WatchGet struct {
RecordID string `json:"record_id,omitempty"`
SubscriptionRecordID string `json:"subscription_record_id,omitempty"`
Kind sql.NullString `json:"kind,omitempty"`
Path sql.NullString `json:"path,omitempty"`
CRNFull sql.NullString `json:"crnMasks,omitempty"`
Wildcards sql.NullBool `json:"wildcards,omitempty"`
RecordIDToWatch sql.NullString `json:"recordIDToWatch,omitempty"`
SubscriptionEmail sql.NullString `json:"subscriptionEmail,omitempty"`
}
// This is for the case when offset is beyond the result set, and ends up with all Null in all columns in the returned row, except total_count
type WatchGetNull struct {
RecordID sql.NullString `json:"record_id,omitempty"`
SubscriptionRecordID sql.NullString `json:"subscription_record_id,omitempty"`
Kind sql.NullString `json:"kind,omitempty"`
Path sql.NullString `json:"path,omitempty"`
CRNFull sql.NullString `json:"crnMasks,omitempty"`
Wildcards sql.NullBool `json:"wildcards,omitempty"`
RecordIDToWatch sql.NullString `json:"recordIDToWatch,omitempty"`
SubscriptionEmail sql.NullString `json:"subscriptionEmail,omitempty"`
}
// WatchReturn type used to pass records back to the API request
type WatchReturn struct {
Href string `json:"href,omitempty"`
RecordID string `json:"record_id,omitempty"`
SubscriptionURL Href `json:"subscription,omitempty"`
Kind string `json:"kind,omitempty"`
Path string `json:"path,omitempty"`
CRNFull []string `json:"crnMasks,omitempty"`
Wildcards string `json:"wildcards,omitempty"`
RecordIDToWatch []string `json:"recordIDToWatch,omitempty"`
SubscriptionEmail string `json:"subscriptionEmail,omitempty"`
}
// WatchResponse type used to multiple records back to an API request
type WatchResponse struct {
WatchURL string `json:"href,omitempty"`
Resources []WatchReturn `json:"resources"`
}
// ConvertWatchReturnToWatchIns converts from a return type to an insert type
func ConvertWatchReturnToWatchIns(watchReturn WatchReturn) WatchInsert {
//FCT := "ConvertWatchReturnToWatchIns: "
tmpWatchInsert := WatchInsert{}
tmpWatchInsert.SubscriptionRecordID = strings.Split(strings.Split(watchReturn.SubscriptionURL.URL, "/subscriptions/")[0], "/")[0]
tmpWatchInsert.Kind = watchReturn.Kind
tmpWatchInsert.Path = watchReturn.Path
tmpWatchInsert.CRNFull = watchReturn.CRNFull
tmpWatchInsert.RecordIDToWatch = watchReturn.RecordIDToWatch
tmpWatchInsert.SubscriptionEmail = watchReturn.SubscriptionEmail
tmpWatchInsert.Wildcards = watchReturn.Wildcards
return tmpWatchInsert
}
// ConvertWatchGetToWatchIns from the Get type to an Insert type
func ConvertWatchGetToWatchIns(watchGet WatchGet) WatchInsert {
FCT := "ConvertWatchGetToWatchIns: "
tmpWatchInsert := WatchInsert{}
tmpWatchInsert.SubscriptionRecordID = watchGet.SubscriptionRecordID
if watchGet.Kind.Valid {
tmpWatchInsert.Kind = watchGet.Kind.String
}
if watchGet.Path.Valid {
tmpWatchInsert.Path = watchGet.Path.String
}
if watchGet.CRNFull.Valid {
var tmpArray []string
err := json.Unmarshal([]byte(watchGet.CRNFull.String), &tmpArray)
if err != nil {
log.Println(FCT+" Error on unmarshal :", err.Error())
tmpWatchInsert.CRNFull = []string{}
}
tmpWatchInsert.CRNFull = tmpArray
} else {
tmpWatchInsert.CRNFull = []string{}
}
if watchGet.RecordIDToWatch.Valid {
var tmpArr []string
err := json.Unmarshal([]byte(watchGet.RecordIDToWatch.String), &tmpArr)
if err != nil {
log.Print(FCT+": Error : ", err.Error())
return WatchInsert{}
}
tmpWatchInsert.RecordIDToWatch = tmpArr
} else {
tmpWatchInsert.RecordIDToWatch = []string{}
}
if watchGet.SubscriptionEmail.Valid {
tmpWatchInsert.SubscriptionEmail = watchGet.SubscriptionEmail.String
}
if watchGet.Wildcards.Valid {
tmpWatchInsert.Wildcards = strconv.FormatBool(watchGet.Wildcards.Bool)
} else {
tmpWatchInsert.Wildcards = "false"
}
return tmpWatchInsert
}
// ConvertWatchGetToWatchReturn converts from a Get type to a Return type
func ConvertWatchGetToWatchReturn(watchGet WatchGet, url string) WatchReturn {
FCT := "ConvertWatchGetToWatchReturn: "
url = strings.TrimRight(url, "/") + "/"
tmpWatchReturn := WatchReturn{}
tmpWatchReturn.Href = url + watchGet.SubscriptionRecordID + "/watches/" + watchGet.RecordID
tmpWatchReturn.RecordID = watchGet.RecordID
tmpWatchReturn.SubscriptionURL = Href{URL: url + watchGet.SubscriptionRecordID}
if watchGet.Kind.Valid {
tmpWatchReturn.Kind = strings.TrimRight(watchGet.Kind.String, "s") + "Watch"
}
if watchGet.Path.Valid {
tmpWatchReturn.Path = watchGet.Path.String
}
if watchGet.CRNFull.Valid {
watchGet.CRNFull.String = strings.Trim(watchGet.CRNFull.String, "{}")
tmp := strings.Split(watchGet.CRNFull.String, ",")
crnsString := ""
if watchGet.CRNFull.String != "" {
for i, v := range tmp {
crnsString += "\"" + v + "\""
if i < len(tmp)-1 {
crnsString += ","
}
}
}
watchGet.CRNFull.String = "[" + crnsString + "]"
err := json.Unmarshal([]byte(watchGet.CRNFull.String), &tmpWatchReturn.CRNFull)
if err != nil {
log.Print(FCT+": Error : ", err.Error())
return WatchReturn{}
}
} else {
tmpWatchReturn.CRNFull = []string{}
}
if watchGet.RecordIDToWatch.Valid {
watchGet.RecordIDToWatch.String = strings.Trim(watchGet.RecordIDToWatch.String, "{}")
tmp := strings.Split(watchGet.RecordIDToWatch.String, ",")
recordString := ""
for i, v := range tmp {
if v != "" {
recordString += "\"" + v + "\""
if i < len(tmp)-1 {
recordString += ","
}
}
}
watchGet.RecordIDToWatch.String = "[" + recordString + "]"
err := json.Unmarshal([]byte(watchGet.RecordIDToWatch.String), &tmpWatchReturn.RecordIDToWatch)
if err != nil {
log.Print(FCT+": Error : ", err.Error())
return WatchReturn{}
}
} else {
tmpWatchReturn.RecordIDToWatch = []string{}
}
if watchGet.SubscriptionEmail.Valid {
tmpWatchReturn.SubscriptionEmail = watchGet.SubscriptionEmail.String
}
if watchGet.Wildcards.Valid {
tmpWatchReturn.Wildcards = strconv.FormatBool(watchGet.Wildcards.Bool)
} else {
tmpWatchReturn.Wildcards = "false"
}
return tmpWatchReturn
}
package logfetcher
import (
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"reflect"
"strings"
"sync"
"testing"
"time"
"koding/klient/testutil"
"github.com/koding/kite"
"github.com/koding/kite/dnode"
)
var (
lf *kite.Kite
remote *kite.Client
remote2 *kite.Client
)
func init() {
lf := kite.New("logfetcher", "0.0.1")
lf.Config.DisableAuthentication = true
lf.Config.Port = 3639
lf.HandleFunc("tail", Tail)
go lf.Run()
<-lf.ServerReadyNotify()
remoteKite := kite.New("remote", "0.0.1")
remoteKite.Config.Username = "remote"
remote = remoteKite.NewClient("http://127.0.0.1:3639/kite")
err := remote.Dial()
if err != nil {
log.Fatalf("err")
}
remoteKite2 := kite.New("remote2", "0.0.1")
remoteKite2.Config.Username = "remote2"
remote2 = remoteKite2.NewClient("http://127.0.0.1:3639/kite")
err = remote2.Dial()
if err != nil {
log.Fatalf("err")
}
}
func makeTempAndCopy(copyFrom string) (dir, path string, err error) {
tmpDir, err := ioutil.TempDir("", "logfetcher")
if err != nil {
return "", "", err
}
tmpFile := filepath.Join(tmpDir, "file")
// Create a new file for testing, so we can test offsetting and watching.
if err := testutil.FileCopy(copyFrom, tmpFile); err != nil {
return "", "", err
}
return tmpDir, tmpFile, nil
}
func TestTail(t *testing.T) {
tmpDir, tmpFile, err := makeTempAndCopy("testdata/testfile1.txt")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmpDir)
var watchCount int
var watchResult []string
var watchMu sync.Mutex
watchFunc := dnode.Callback(func(r *dnode.Partial) {
watchCount++
line := r.One().MustString()
watchMu.Lock()
watchResult = append(watchResult, line)
watchMu.Unlock()
})
_, err = remote.Tell("tail", &Request{
Path: tmpFile,
Watch: watchFunc,
})
if err != nil {
t.Fatal(err)
}
fmt.Println("Waiting for the results..")
time.Sleep(time.Second * 1)
// Should return empty by default, since no new lines were given.
watchMu.Lock()
n := len(watchResult)
watchMu.Unlock()
if n != 0 {
t.Errorf("WatchFunc should not be called for pre-existing lines.\nWant: 0\nGot : %d\n", n)
}
file, err := os.OpenFile(tmpFile, os.O_APPEND|os.O_WRONLY, 0600)
if err != nil {
t.Fatal(err)
}
file.WriteString("Tail2\n")
file.WriteString("Tail3\n")
file.WriteString("Tail4\n")
file.Close()
// wait so the watch function picked up the tail changes
time.Sleep(time.Second * 5)
var modifiedLines = []string{"Tail2", "Tail3", "Tail4"}
watchMu.Lock()
if !reflect.DeepEqual(modifiedLines, watchResult) {
err = fmt.Errorf("\nWatchFunc should not be called for pre-existing lines.\n"+
"Want: %#v\nGot : %#v\n", modifiedLines, watchResult)
}
watchMu.Unlock()
if err != nil {
t.Error(err)
}
}
// TestMultipleTail compares two log.tail calls on a single file, and ensures that
// they both receive the same input.
func TestMultipleTail(t *testing.T) {
tmpDir, tmpFile, err := makeTempAndCopy("testdata/testfile2.txt")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmpDir)
watchResult := []string{}
watchFunc := dnode.Callback(func(r *dnode.Partial) {
line := r.One().MustString()
watchResult = append(watchResult, line)
})
_, err = remote.Tell("tail", &Request{
Path: tmpFile,
Watch: watchFunc,
})
if err != nil {
t.Fatal(err)
}
watchResult2 := []string{}
watchFunc2 := dnode.Callback(func(r *dnode.Partial) {
line := r.One().MustString()
watchResult2 = append(watchResult2, line)
})
_, err = remote2.Tell("tail", &Request{
Path: tmpFile,
Watch: watchFunc2,
})
if err != nil {
t.Fatal(err)
}
time.Sleep(time.Second * 2)
file, err := os.OpenFile(tmpFile, os.O_APPEND|os.O_WRONLY, 0600)
if err != nil {
t.Fatal(err)
}
defer file.Close()
file.WriteString("Tail2\n")
file.WriteString("Tail3\n")
// wait so the watch function picked up the tail changes
time.Sleep(time.Second)
t.Logf("watchResult = %+v\n", watchResult)
t.Logf("watchResult2 = %+v\n", watchResult2)
// Now check the new two results
if !reflect.DeepEqual(
watchResult[len(watchResult)-2:],
watchResult2[len(watchResult2)-2:],
) {
t.Errorf("\nWant: %v\nGot : %v\n",
watchResult[len(watchResult)-2:],
watchResult2[len(watchResult2)-2:],
)
}
// Now let us disconnect the second connection, we should receive any new
// changes for watchResult2 (From watchFunc2) anymore
currentWatchLen := len(watchResult)
currentWatch2Len := len(watchResult2)
remote2.Close()
// wait so onDisconnect get recognized on Kite
time.Sleep(time.Second)
file.WriteString("Tail4\n")
file.WriteString("Tail5\n")
// wait so the watch function picked up the tail changes
time.Sleep(time.Second)
if currentWatch2Len != len(watchResult2) {
t.Errorf("WatchFunc2 is still triggered, got %d should have %d", len(watchResult2), currentWatch2Len)
}
if currentWatchLen+2 != len(watchResult) {
t.Errorf("WatchFunc2 is not triggered, got %d should have %d", len(watchResult), currentWatchLen+2)
}
}
func TestTailOffset(t *testing.T) {
t.Skip("this test is correct but prefetcher implementation is not: #10840")
tmpDir, tmpFile, err := makeTempAndCopy("testdata/testfile1.txt")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmpDir)
var (
offset = 3 // Read the last 3 lines of the file.
linesC = make(chan string, 10)
watchFunc = dnode.Callback(func(r *dnode.Partial) {
linesC <- r.One().MustString()
})
)
if _, err = remote.Tell("tail", &Request{
Path: tmpFile,
Watch: watchFunc,
LineOffset: offset,
}); err != nil {
t.Fatal(err)
}
// Write some data to file
file, err := os.OpenFile(tmpFile, os.O_APPEND|os.O_WRONLY, 0600)
if err != nil {
t.Fatal(err)
}
for _, data := range []string{"DataA\n", "DataB\n"} {
if _, err = file.WriteString(data); err != nil {
t.Fatal(err)
}
}
if err = file.Close(); err != nil {
t.Fatal(err)
}
t.Log("....Waiting for the results..")
var offsetAll = offset + 2 // We wrote two more lines.
lines := make([]string, 0, offsetAll)
for i := 0; i < offsetAll; i++ {
select {
case line := <-linesC:
t.Log(line)
lines = append(lines, line)
case <-time.After(2 * time.Second): // Wait each time.
t.Fatalf("test timed out after %v", 2*time.Second)
}
}
// Read the file, and get the offset lines to compare against.
sourceText, err := ioutil.ReadFile(tmpFile)
if err != nil {
t.Fatal(err)
}
// wait so the watch function picked up the tail changes
offsetLines := strings.Split(strings.TrimSpace(string(sourceText)), "\n")
offsetLines = offsetLines[len(offsetLines)-offsetAll:]
if !reflect.DeepEqual(offsetLines, lines) {
t.Errorf("want offset lines = %#v\n; got %#v\n", offsetLines, lines)
}
}
func TestGetOffsetLines(t *testing.T) {
tmpDir, tmpFile, err := makeTempAndCopy("testdata/testfile1.txt")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmpDir)
// Read the file, and get the offset lines to compare against.
sourceText, err := ioutil.ReadFile(tmpFile)
if err != nil {
t.Fatal(err)
}
sourceLines := strings.Split(strings.TrimSpace(string(sourceText)), "\n")
// Open our file, to pass to the func
file1, err := os.Open(tmpFile)
if err != nil {
t.Fatal(err)
}
defer file1.Close()
offset := 3
result, err := GetOffsetLines(file1, 4, offset)
if err != nil {
t.Error(err)
}
expected := sourceLines[len(sourceLines)-offset:]
if !reflect.DeepEqual(expected, result) {
t.Errorf(
"\nIt should return offset lines.\nWant: %#v\nGot : %#v\n",
expected, result,
)
}
// Set the offset to the entire file.
offset = len(sourceLines) + 1
result, err = GetOffsetLines(file1, 4, offset)
if err != nil {
t.Error(err)
}
expected = sourceLines
if !reflect.DeepEqual(expected, result) {
t.Errorf(
"\nIt should return all the lines, if offset is larger than total.\nWant: %#v\nGot : %#v\n",
expected, result,
)
}
// Create the 2nd test file to be empty (Create truncates by default)
file2, err := os.Create(tmpFile)
if err != nil {
t.Fatal(err)
}
fmt.Println("Requesting empty lines")
offset = 3
result, err = GetOffsetLines(file2, 4, offset)
if err != nil {
t.Error(err)
}
expected = nil
if !reflect.DeepEqual(expected, result) {
t.Errorf(
"\nIt should callback with no lines.\nWant: %#v\nGot : %#v\n",
expected, result,
)
}
tmpDir2, tmpFile, err := makeTempAndCopy("testdata/testfile1.txt")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmpDir2)
// Open our file, to pass to the func
file1, err = os.Open(tmpFile)
if err != nil {
t.Fatal(err)
}
defer file1.Close()
offset = 3
result, err = GetOffsetLines(file1, defaultOffsetChunkSize, offset)
if err != nil {
t.Error(err)
}
expected = sourceLines[len(sourceLines)-offset:]
if !reflect.DeepEqual(expected, result) {
t.Errorf(
"\nIt should return offset lines with a chunkSize larger than the file.\nWant: %#v\nGot : %#v\n",
expected, result,
)
}
}
package test
import (
"database/sql"
"log"
"net/http"
"strings"
"github.ibm.com/cloud-sre/pnp-abstraction/db"
"github.ibm.com/cloud-sre/pnp-abstraction/datastore"
)
const (
API_URL = "apiUrl"
)
func TestSubscriptionWatch(database *sql.DB) int {
FCT := "TestSubscriptionWatch "
failed := 0
log.Println("======== "+FCT+" Test 1 ========")
subscription := datastore.SubscriptionInsert {
Name: "test_name1",
TargetAddress: "target1",
TargetToken: "token1",
Expiration: "2018-08-01T10:00:00Z",
}
subscription_id, err, status := db.InsertSubscriptionStatement(database, &subscription)
if status != http.StatusOK {
log.Println("Insert Subscription failed: ", err.Error())
failed++
} else {
log.Println("record_id: ", subscription_id)
log.Println("Insert Subscription passed")
}
log.Println("Test get Subscription")
subscriptionReturn, err1,_ := db.GetSubscriptionByRecordIDStatement(database, subscription_id)
if err1 != nil {
log.Println("Get Subscription failed: ", err1.Error())
failed++
} else {
log.Println("Get result: ", subscriptionReturn)
if subscriptionReturn.Name == subscription.Name &&
subscriptionReturn.TargetAddress == subscription.TargetAddress &&
subscriptionReturn.TargetToken == subscription.TargetToken { //&&
// subscriptionReturn.Expiration == subscription.Expiration {
log.Println("Test 1: Get Subscription PASSED")
} else {
log.Println("Test 1: Get Subscription FAILED")
failed++
}
}
log.Println("======== "+FCT+" Test 2 ========")
crnFull2 := []string{"crn:v1:BLUEMIX:PUBLIC:service-NAME2:location2::::",
"crn:v1:aa:dedicated:service-name2:LOCATION3::::",
}
recordIDToWatch2 := []string {"INC00001"}
watch2 := datastore.WatchInsert {
SubscriptionRecordID: subscription_id,
Kind: "incident",
Path: "new",
CRNFull: crnFull2,
Wildcards: "true",
RecordIDToWatch: recordIDToWatch2,
SubscriptionEmail: "[email protected]",
}
watchReturn2, err2,_ := db.InsertWatchByTypeForSubscriptionStatement(database, &watch2, API_URL)
if err2 != nil {
log.Println("Insert Watch failed: ", err2.Error())
failed++
} else {
log.Println("record_id: ", watchReturn2.RecordID)
log.Println("Insert Watch passed")
}
log.Println("Test get Watch")
watchReturn2b, err2b,_ := db.GetWatchByRecordIDStatement(database, watchReturn2.RecordID, API_URL)
if err2b != nil {
log.Println("Get Watch failed: ", err2b.Error())
failed++
} else {
log.Println("Get result: ", watchReturn2b)
if strings.Contains(watchReturn2b.SubscriptionURL.URL, watch2.SubscriptionRecordID) &&
watchReturn2b.Kind == watch2.Kind+"Watch" &&
watchReturn2b.Path == watch2.Path &&
len(watchReturn2b.CRNFull) == len(watch2.CRNFull) &&
watchReturn2b.CRNFull[0] == strings.ToLower(watch2.CRNFull[0]) &&
watchReturn2b.CRNFull[1] == strings.ToLower(watch2.CRNFull[1]) &&
watchReturn2b.Wildcards == watch2.Wildcards &&
len(watchReturn2b.RecordIDToWatch) == len(watch2.RecordIDToWatch) &&
watchReturn2b.RecordIDToWatch[0] == watch2.RecordIDToWatch[0] &&
watchReturn2b.SubscriptionEmail == watch2.SubscriptionEmail {
log.Println("Test 2: Get Watch PASSED")
} else {
log.Println("Test 2: Get Watch FAILED")
failed++
}
}
log.Println("======== "+FCT+" Test 3 ========")
crnFull3 := []string{"crn:v1:bluemix:public:service-name1:location1::service-instance1:resource-type1:",
}
recordIDToWatch3 := []string {db.CreateRecordIDFromSourceSourceID("source1", "INC00011")}
watch3 := datastore.WatchInsert {
SubscriptionRecordID: subscription_id,
Kind: "maintenance",
Path: "new",
CRNFull: crnFull3,
Wildcards: "false",
RecordIDToWatch: recordIDToWatch3,
SubscriptionEmail: "[email protected]",
}
watchReturn3, err3,_ := db.InsertWatchByTypeForSubscriptionStatement(database, &watch3, API_URL)
if err3 != nil {
log.Println("Insert Watch failed: ", err3.Error())
failed++
} else {
log.Println("record_id: ", watchReturn3.RecordID)
log.Println("Insert Watch passed")
}
log.Println("Test get Watch")
watchReturn3b, err3b,_ := db.GetWatchByRecordIDStatement(database, watchReturn3.RecordID, API_URL)
if err3b != nil {
log.Println("Get Watch failed: ", err3b.Error())
failed++
} else {
log.Println("Get result: ", watchReturn3b)
if strings.Contains(watchReturn3b.SubscriptionURL.URL, watch3.SubscriptionRecordID) &&
watchReturn3b.Kind == watch3.Kind+"Watch" &&
watchReturn3b.Path == watch3.Path &&
len(watchReturn3b.CRNFull) == len(watch3.CRNFull) &&
watchReturn3b.CRNFull[0] == strings.ToLower(watch3.CRNFull[0]) &&
watchReturn3b.Wildcards == watch3.Wildcards &&
len(watchReturn3b.RecordIDToWatch) == len(watch3.RecordIDToWatch) &&
watchReturn3b.RecordIDToWatch[0] == watch3.RecordIDToWatch[0] &&
watchReturn3b.SubscriptionEmail == watch3.SubscriptionEmail {
log.Println("Test 3: Get Watch PASSED")
} else {
log.Println("Test 3: Get Watch FAILED")
failed++
}
}
log.Println("======== "+FCT+" Test 4 ========")
queryStr4 := db.RESOURCE_QUERY_CRN + "=crn:v1:aa:dedicated:service-name2:LOCation3::::"
log.Println("query: ", queryStr4)
watchReturn4, total_count4, err4,_ := db.GetWatchesByQuery(database, queryStr4, 0, 0, API_URL)
if err4 != nil {
log.Println("Get Watches failed: ", err4.Error())
log.Println("Test 4: Get Watches FAILED")
failed++
} else if total_count4 == 1 {
log.Println("total_count4: ", total_count4)
log.Println("watchReturn4: ", watchReturn4)
if len(*watchReturn4) != 1 {
log.Printf("Error: Number of returned: %d, expecting: 1\n", len(*watchReturn4))
log.Println("Test 4: Get Watches FAILED")
failed++
} else {
match := 0
for _,r := range *watchReturn4 {
if strings.Contains(r.SubscriptionURL.URL, watch2.SubscriptionRecordID) &&
r.Kind == watch2.Kind+"Watch" &&
r.Path == watch2.Path &&
len(r.CRNFull) == len(watch2.CRNFull) &&
r.CRNFull[0] == strings.ToLower(watch2.CRNFull[0]) &&
r.CRNFull[1] == strings.ToLower(watch2.CRNFull[1]) &&
r.Wildcards == watch2.Wildcards &&
len(r.RecordIDToWatch) == len(watch2.RecordIDToWatch) &&
r.RecordIDToWatch[0] == watch2.RecordIDToWatch[0] &&
r.SubscriptionEmail == watch2.SubscriptionEmail {
match++
}
}
if (match <= 0){
log.Println("There are rows unmatched, failed")
log.Println("Test 4: Get Watches FAILED")
failed++
} else {
log.Println("Test 4: Get Watches PASSED")
}
}
} else {
log.Printf("total_count is %d, expecting 1, failed", total_count4)
log.Println("Test 4: Get Watches FAILED")
failed++
}
log.Println("======== "+FCT+" Test 5 ========")
queryStr5, err5a := db.CreateNonWildcardCRNQueryString(crnFull3[0])
if err5a != nil {
log.Println("Error: ", err5a)
}
log.Println("query: ", queryStr5)
watchReturn5, total_count5, err5,_ := db.GetWatchesByQuery(database, queryStr5, 0, 0, API_URL)
if err5 != nil {
log.Println("Get Watches failed: ", err5.Error())
log.Println("Test 5: Get Watches FAILED")
failed++
} else if total_count5 == 1 {
log.Println("total_count5: ", total_count5)
log.Println("watchReturn5: ", watchReturn5)
if len(*watchReturn5) != 1 {
log.Printf("Error: Number of returned: %d, expecting: 1\n", len(*watchReturn5))
log.Println("Test 5: Get Watches FAILED")
failed++
} else {
match := 0
for _,r := range *watchReturn5 {
if strings.Contains(r.SubscriptionURL.URL, watch3.SubscriptionRecordID) &&
r.Kind == watch3.Kind+"Watch" &&
r.Path == watch3.Path &&
len(r.CRNFull) == len(watch3.CRNFull) &&
r.CRNFull[0] == strings.ToLower(watch3.CRNFull[0]) &&
r.Wildcards == watch3.Wildcards &&
len(r.RecordIDToWatch) == len(watch3.RecordIDToWatch) &&
r.RecordIDToWatch[0] == watch3.RecordIDToWatch[0] &&
r.SubscriptionEmail == watch3.SubscriptionEmail {
match++
}
}
if (match <= 0){
log.Println("There are rows unmatched, failed")
log.Println("Test 5: Get Watches FAILED")
failed++
} else {
log.Println("Test 5: Get Watches PASSED")
}
}
} else {
log.Printf("total_count is %d, expecting 1, failed", total_count5)
log.Println("Test 5: Get Watches FAILED")
failed++
}
log.Println("======== "+FCT+" Test 6 ========")
subscription6 := datastore.SubscriptionInsert {
Name: "test_name6",
TargetAddress: "target6",
TargetToken: "token6",
Expiration: "2018-12-01T06:06:00Z",
}
subscription_id6, err6,_ := db.InsertSubscriptionStatement(database, &subscription6)
if err6 != nil {
log.Println("Insert Subscription failed: ", err6.Error())
failed++
} else {
log.Println("record_id: ", subscription_id6)
log.Println("Insert Subscription passed")
}
log.Println("Test get Subscription")
subscriptionReturn6, err61,_ := db.GetSubscriptionByRecordIDStatement(database, subscription_id6)
if err61 != nil {
log.Println("Get Subscription failed: ", err61.Error())
failed++
} else {
log.Println("Get result: ", subscriptionReturn6)
if subscriptionReturn6.Name == subscription6.Name &&
subscriptionReturn6.TargetAddress == subscription6.TargetAddress &&
subscriptionReturn6.TargetToken == subscription6.TargetToken { //&&
//subscriptionReturn6.Expiration == subscription6.Expiration {
log.Println("Get Subscription passed")
} else {
log.Println("Get Subscription failed")
failed++
}
}
crnFull6 := []string{"crn:v1:blueMIX:public:service-naME1:location1::::",
}
recordIDToWatch6 := []string {db.CreateRecordIDFromSourceSourceID("source1", "INC00006")}
watch6 := datastore.WatchInsert {
SubscriptionRecordID: subscription_id6,
Kind: "incident",
Path: "new",
CRNFull: crnFull6,
Wildcards: "true",
RecordIDToWatch: recordIDToWatch6,
SubscriptionEmail: "[email protected]",
}
watchReturn6, err62,_ := db.InsertWatchByTypeForSubscriptionStatement(database, &watch6, API_URL)
if err62 != nil {
log.Println("Insert Watch failed: ", err62.Error())
failed++
} else {
log.Println("record_id: ", watchReturn6.RecordID)
log.Println("Insert Watch passed")
}
log.Println("Test get Watch")
queryStr6 := db.RESOURCE_QUERY_CRN + "=crn:v1:bluemix:public:service-name1:::::&" + db.WATCH_QUERY_KIND + "=incident,maintenance"
log.Println("query: ", queryStr6)
watchReturn6b, total_count6, err6b,_ := db.GetWatchesByQuery(database, queryStr6, 0, 0, API_URL)
if err6b != nil {
log.Println("Get Watches failed: ", err6b.Error())
log.Println("Test 6: Get Watches FAILED")
failed++
} else if total_count6 == 2 {
log.Println("total_count6: ", total_count6)
log.Println("watchReturn6: ", watchReturn6b)
if len(*watchReturn6b) != 2 {
log.Printf("Error: Number of returned: %d, expecting: 2\n", len(*watchReturn6b))
log.Println("Test 6: Get Watches FAILED")
failed++
} else {
match := 0
for _,r := range *watchReturn6b {
if strings.Contains(r.SubscriptionURL.URL, watch3.SubscriptionRecordID) &&
r.Kind == watch3.Kind+"Watch" &&
r.Path == watch3.Path &&
len(r.CRNFull) == len(watch3.CRNFull) &&
r.CRNFull[0] == strings.ToLower(watch3.CRNFull[0]) &&
r.Wildcards == watch3.Wildcards &&
len(r.RecordIDToWatch) == len(watch3.RecordIDToWatch) &&
r.RecordIDToWatch[0] == watch3.RecordIDToWatch[0] &&
r.SubscriptionEmail == watch3.SubscriptionEmail {
match++
} else if strings.Contains(r.SubscriptionURL.URL, watch6.SubscriptionRecordID) &&
r.Kind == watch6.Kind+"Watch" &&
r.Path == watch6.Path &&
len(r.CRNFull) == len(watch6.CRNFull) &&
r.CRNFull[0] == strings.ToLower(watch6.CRNFull[0]) &&
r.Wildcards == watch6.Wildcards &&
len(r.RecordIDToWatch) == len(watch6.RecordIDToWatch) &&
r.RecordIDToWatch[0] == watch6.RecordIDToWatch[0] &&
r.SubscriptionEmail == watch6.SubscriptionEmail {
match++
}
}
if (match <= 1){
log.Println("There are rows unmatched, failed")
log.Println("Test 6: Get Watches FAILED")
failed++
} else {
log.Println("Test 6: Get Watches PASSED")
}
}
} else {
log.Printf("total_count is %d, expecting 2, failed", total_count6)
log.Println("Test 6: Get Watches FAILED")
failed++
}
log.Println("======== "+FCT+" Test 7 ========")
log.Println("Test get Watch")
queryStr7 := db.WATCH_QUERY_KIND + "=incident,maintenance&" + db.WATCH_QUERY_SUBSCRIPTION_ID + "=" + subscription_id
log.Println("query: ", queryStr7)
watchReturn7, total_count7, err7,_ := db.GetWatchesByQuery(database, queryStr7, 0, 0, API_URL)
if err7 != nil {
log.Println("Get Watches failed: ", err7.Error())
log.Println("Test 7: Get Watches FAILED")
failed++
} else if total_count7 == 2 {
log.Println("total_count7: ", total_count7)
log.Println("watchReturn7: ", watchReturn7)
if len(*watchReturn7) != 2 {
log.Printf("Error: Number of returned: %d, expecting: 2\n", len(*watchReturn7))
log.Println("Test 7: Get Watches FAILED")
failed++
} else {
match := 0
for _,r := range *watchReturn7 {
if strings.Contains(r.SubscriptionURL.URL, watch2.SubscriptionRecordID) &&
r.Kind == watch2.Kind+"Watch" &&
r.Path == watch2.Path &&
len(r.CRNFull) == len(watch2.CRNFull) &&
r.CRNFull[0] == strings.ToLower(watch2.CRNFull[0]) &&
r.Wildcards == watch2.Wildcards &&
len(r.RecordIDToWatch) == len(watch2.RecordIDToWatch) &&
r.RecordIDToWatch[0] == watch2.RecordIDToWatch[0] &&
r.SubscriptionEmail == watch2.SubscriptionEmail {
match++
} else if strings.Contains(r.SubscriptionURL.URL, watch3.SubscriptionRecordID) &&
r.Kind == watch3.Kind+"Watch" &&
r.Path == watch3.Path &&
len(r.CRNFull) == len(watch3.CRNFull) &&
r.CRNFull[0] == strings.ToLower(watch3.CRNFull[0]) &&
r.Wildcards == watch3.Wildcards &&
len(r.RecordIDToWatch) == len(watch3.RecordIDToWatch) &&
r.RecordIDToWatch[0] == watch3.RecordIDToWatch[0] &&
r.SubscriptionEmail == watch3.SubscriptionEmail {
match++
}
}
if (match <= 1){
log.Println("There are rows unmatched, failed")
log.Println("Test 7: Get Watches FAILED")
failed++
} else {
log.Println("Test 7: Get Watches PASSED")
}
}
} else {
log.Printf("total_count is %d, expecting 2, failed", total_count7)
log.Println("Test 7: Get Watches FAILED")
failed++
}
log.Println("======== "+FCT+" Test 8 ========")
log.Println("Test delete expired subscription")
err8,_ := db.DeleteExpiredSubscriptions(database)
if err8 != nil {
log.Println("Delete expired subscription failed: ", err8.Error())
failed++
}
subscriptionReturn8, err8c,_ := db.GetSubscriptionByRecordIDStatement(database, subscription_id)
if subscriptionReturn8 != nil {
log.Println("Delete Maintenance failed, Maintenance still exists: ", err8c.Error())
log.Println("Test 8: Delete expired subscription FAILED")
failed++
} else {
log.Println("Test 8: Delete expired subscription PASSED")
}
// Summary
log.Println("**** SUMMARY ****")
if failed > 0 {
log.Println("**** "+FCT+"FAILED ****")
} else {
log.Println("**** "+FCT+"PASSED ****")
}
return failed
}
Accelerate Your Automation Test Cycles With LambdaTest
Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.